Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: set the CARGO_BUILD_TARGET to reuse same cache #1191

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/github-cxx-qt-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ jobs:
clang_format_path: /home/runner/.local/bin/clang-format
cargo_dir: ~/.cargo
rustc_wrapper: sccache
cargo_target: x86_64-unknown-linux-gnu
- name: Ubuntu 24.04 (gcc) Qt6
os: ubuntu-24.04
qt_version: 6
Expand All @@ -289,6 +290,7 @@ jobs:
libgl1-mesa-dev
libvulkan-dev
libxkbcommon-dev
cargo_target: x86_64-unknown-linux-gnu

- name: macOS 13 (clang) Qt5
os: macos-13
Expand All @@ -311,6 +313,7 @@ jobs:
cc: clang
cxx: clang++
rustc_wrapper: sccache
cargo_target: x86_64-apple-darwin
- name: macOS 14 (clang) Qt6
os: macos-14
qt_version: 6
Expand All @@ -332,6 +335,7 @@ jobs:
cc: clang
cxx: clang++
rustc_wrapper: sccache
cargo_target: aarch64-apple-darwin

- name: Windows 2022 (MSVC) Qt5
os: windows-2022
Expand All @@ -350,6 +354,7 @@ jobs:
cc: cl
cxx: cl
rustc_wrapper: sccache
cargo_target: x86_64-pc-windows-msvc
- name: Windows 2022 (MSVC2019) Qt6
os: windows-2022
qt_version: 6
Expand All @@ -367,6 +372,7 @@ jobs:
cc: cl
cxx: cl
rustc_wrapper: sccache
cargo_target: x86_64-pc-windows-msvc
- name: Windows 2022 (MSVC2022) Qt6
os: windows-2022
qt_version: 6
Expand All @@ -384,6 +390,7 @@ jobs:
cc: cl
cxx: cl
rustc_wrapper: sccache
cargo_target: x86_64-pc-windows-msvc

runs-on: ${{ matrix.os }}
name: ${{ matrix.name }}
Expand All @@ -397,6 +404,8 @@ jobs:
SCCACHE_CACHE_SIZE: 600M
SCCACHE_LOG: debug
SCCACHE_ERROR_LOG: ${{ matrix.sccache_log_path }}
# Ensure that we share the same target sub folder
CARGO_BUILD_TARGET: ${{ matrix.cargo_target }}

steps:
- name: "Clone Git repository"
Expand Down
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,18 @@ set(CARGO_TARGET_DIR "${CMAKE_BINARY_DIR}/${BUILD_DIR}/cargo/build")

if(BUILD_TESTING)
# Add CMake tests for `cargo test/clippy/fmt/doc`.
add_test(NAME cargo_tests COMMAND cargo test --release --all-features --target-dir ${CARGO_TARGET_DIR})
add_test(NAME cargo_doc COMMAND cargo doc --release --all-features --target-dir ${CARGO_TARGET_DIR})
add_test(NAME cargo_tests COMMAND cargo test --locked --release --all-features --target-dir ${CARGO_TARGET_DIR})
add_test(NAME cargo_doc COMMAND cargo doc --locked --release --all-features --target-dir ${CARGO_TARGET_DIR})
# Minimum clippy version for the incompatible_msrv lint is 1.78.0
add_test(NAME cargo_clippy COMMAND cargo +1.78.0 clippy --release --all-features --target-dir ${CARGO_TARGET_DIR} -- -D warnings)
add_test(NAME cargo_clippy COMMAND cargo +1.78.0 clippy --locked --release --all-features --target-dir ${CARGO_TARGET_DIR} -- -D warnings)

set_tests_properties(cargo_tests cargo_clippy PROPERTIES
ENVIRONMENT_MODIFICATION "${CARGO_ENV}"
)
# Cargo doc cannot have a target set otherwise it fails
# https://github.com/rust-lang/cargo/issues/10368
set_tests_properties(cargo_doc PROPERTIES
ENVIRONMENT_MODIFICATION "${CARGO_ENV};RUSTDOCFLAGS=set:--deny=warnings"
ENVIRONMENT_MODIFICATION "${CARGO_ENV};RUSTDOCFLAGS=set:--deny=warnings;CARGO_BUILD_TARGET=unset:;"
)

# Ensure test inputs and outputs are formatted
Expand All @@ -168,7 +170,7 @@ if(BUILD_TESTING)
add_test(NAME cxx_qt_gen_test_outputs_gen COMMAND rustfmt --check ${CXX_QT_GEN_TEST_OUTPUTS})

# Add test which checks that a build rerun doesn't recompile and uses caches instead
add_test(NAME cargo_build_rerun COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_cargo_build_rerun.sh" "${CMAKE_CURRENT_SOURCE_DIR}")
add_test(NAME cargo_build_rerun COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_cargo_build_rerun.sh" "${CMAKE_CURRENT_SOURCE_DIR}" "${CARGO_TARGET_DIR}")

# Ensure that cargo_build_rerun doesn't run while we are already building
set_tests_properties(cargo_build_rerun PROPERTIES RUN_SERIAL TRUE)
Expand Down
17 changes: 9 additions & 8 deletions scripts/check_cargo_build_rerun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

set -ex

SOURCE_FOLDER=$1
BUILD_FOLDER=$2

# Ensure we are in the right directory
SCRIPT=$(realpath "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
cd "$SCRIPTPATH/../"
cd "$SOURCE_FOLDER"

# Ensure that we do see a "Compiling" in the output
# as if we do it means we have a cargo::rerun-if-changed incorrectly
function check_build_contains_compiling() {
BUILD=$(cargo build -p qml-minimal-no-cmake 2>&1)
BUILD=$(cargo build --locked --release --target-dir="$BUILD_FOLDER" -p qml-minimal-no-cmake 2>&1)

if ! echo "$BUILD" | grep -q Compiling; then
echo "cargo build is missing text 'Compiling', likely an incorrect cargo::rerun-if-changed in a build script."
Expand All @@ -26,7 +27,7 @@ function check_build_contains_compiling() {
# Ensure that we don't see any "Compiling" in the output
# as if we do it means we have a cargo::rerun-if-changed incorrectly
function check_build_no_compiling() {
BUILD=$(cargo build -p qml-minimal-no-cmake 2>&1)
BUILD=$(cargo build --locked --release --target-dir="$BUILD_FOLDER" -p qml-minimal-no-cmake 2>&1)

if echo "$BUILD" | grep -q Compiling; then
echo "cargo build contained text 'Compiling', likely an incorrect cargo::rerun-if-changed in a build script."
Expand All @@ -35,20 +36,20 @@ function check_build_no_compiling() {
}

# Build once
cargo build -p qml-minimal-no-cmake
cargo build --locked --release --target-dir="$BUILD_FOLDER" -p qml-minimal-no-cmake

# Build a second time
check_build_no_compiling

# Modify a qml file
touch "$SCRIPTPATH/../examples/cargo_without_cmake/qml/main.qml"
touch "$SOURCE_FOLDER/examples/cargo_without_cmake/qml/main.qml"

# Build a third and fourth time
check_build_contains_compiling
check_build_no_compiling

# Modify a Rust file
touch "$SCRIPTPATH/../examples/cargo_without_cmake/src/cxxqt_object.rs"
touch "$SOURCE_FOLDER/examples/cargo_without_cmake/src/cxxqt_object.rs"

# Build a fifth and sixth time
check_build_contains_compiling
Expand Down
Loading