Skip to content

Commit

Permalink
Add support for local Rustflags
Browse files Browse the repository at this point in the history
For some scenarios it is better to not set Rustflags for all crates
in the dependency graph and instead only set it for the top-level
crate.
For example rust-lang/cargo#8716
can be avoided in some scenarios by setting the rustflags via
rustc, which allows for faster rebuilds in such cases.
  • Loading branch information
jschwe committed Sep 6, 2022
1 parent 9e926bf commit 6277ccb
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ function(_add_cargo_build)
endif()

set(global_rustflags_target_property "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_RUSTFLAGS>>")
set(local_rustflags_target_property "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_LOCAL_RUSTFLAGS>>")

set(features_target_property "$<GENEX_EVAL:$<TARGET_PROPERTY:${target_name},${_CORR_PROP_FEATURES}>>")
set(features_genex "$<$<BOOL:${features_target_property}>:--features=$<JOIN:${features_target_property},$<COMMA>>>")
Expand Down Expand Up @@ -357,6 +358,9 @@ function(_add_cargo_build)

set(global_joined_rustflags "$<JOIN:${global_rustflags_target_property}, >")
set(global_rustflags_genex "$<$<BOOL:${global_rustflags_target_property}>:RUSTFLAGS=${global_joined_rustflags}>")
set(local_rustflags_delimiter "$<$<BOOL:${local_rustflags_target_property}>:-->")
set(local_rustflags_genex "$<$<BOOL:${local_rustflags_target_property}>:${local_rustflags_target_property}>")


# Used to set a linker for a specific target-triple.
set(cargo_target_linker_var "CARGO_TARGET_${_CORROSION_RUST_CARGO_TARGET_UPPER}_LINKER")
Expand Down Expand Up @@ -393,7 +397,7 @@ function(_add_cargo_build)
"CORROSION_BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}"
"CARGO_BUILD_RUSTC=${_CORROSION_RUSTC}"
"${_CORROSION_CARGO}"
build
rustc
${cargo_target_option}
${_CORROSION_VERBOSE_OUTPUT_FLAG}
# Global --features arguments added via corrosion_import_crate()
Expand All @@ -408,6 +412,9 @@ function(_add_cargo_build)
${cargo_profile}
${flag_args}
${flags_genex}
# Any arguments to cargo must be placed before this line
${local_rustflags_delimiter}
${local_rustflags_genex}

# Copy crate artifacts to the binary dir
COMMAND
Expand Down Expand Up @@ -602,6 +609,16 @@ function(corrosion_add_target_rustflags target_name rustflag)
)
endfunction()

function(corrosion_add_target_local_rustflags target_name rustc_flag)
# Set Rustflags via `cargo rustc` which only affect the current crate, but not dependencies.
# Additional rustflags may be passed as optional parameters after rustflag.
set_property(
TARGET ${target_name}
APPEND
PROPERTY INTERFACE_CORROSION_LOCAL_RUSTFLAGS ${rustc_flag} ${ARGN}
)
endfunction()

function(corrosion_set_env_vars target_name env_var)
# Additional environment variables may be passed as optional parameters after env_var.
set_property(
Expand Down
7 changes: 7 additions & 0 deletions test/custom_profiles/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/rustflags/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ corrosion_add_target_rustflags(rustflag-test-lib
--cfg=test_rustflag_cfg2="$<IF:$<OR:$<CONFIG:Debug>,$<CONFIG:>>,debug,release>"
"--cfg=test_rustflag_cfg3"
)

corrosion_add_target_local_rustflags(rustflag-test-lib "--cfg=test_local_rustflag1")
corrosion_add_target_local_rustflags(rustflag-test-lib --cfg=test_local_rustflag2="value")
7 changes: 7 additions & 0 deletions test/rustflags/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/rustflags/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ license = "MIT"
edition = "2018"

[dependencies]
some_dependency = { path = "some_dependency" }

[lib]
crate-type=["staticlib"]
6 changes: 6 additions & 0 deletions test/rustflags/rust/some_dependency/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "some_dependency"
version = "0.1.0"
license = "MIT"
edition = "2018"

10 changes: 10 additions & 0 deletions test/rustflags/rust/some_dependency/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Test that the local rustflags are only passed to the main crate and not to dependencies.
#[cfg(test_local_rustflag1)]
const _: [(); 1] = [(); 2];

#[cfg(test_local_rustflag2 = "value")]
const _: [(); 1] = [(); 2];

pub fn some_function() -> u32 {
42
}
7 changes: 7 additions & 0 deletions test/rustflags/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ pub extern "C" fn rust_second_function(name: *const c_char) {
pub extern "C" fn rust_third_function(name: *const c_char) {
let name = unsafe { std::ffi::CStr::from_ptr(name).to_str().unwrap() };
println!("Hello, {}! I'm Rust again, third time the charm!", name);
assert_eq!(some_dependency::some_function(), 42);
}

#[cfg(not(test_rustflag_cfg3))]
const _: [(); 1] = [(); 2];

#[cfg(not(test_local_rustflag1))]
const _: [(); 1] = [(); 2];

#[cfg(not(test_local_rustflag2 = "value"))]
const _: [(); 1] = [(); 2];

0 comments on commit 6277ccb

Please sign in to comment.