Skip to content

Commit 7d535ab

Browse files
committed
Add support for local Rustflags
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.
1 parent cae421c commit 7d535ab

File tree

8 files changed

+59
-1
lines changed

8 files changed

+59
-1
lines changed

cmake/Corrosion.cmake

+18-1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ function(_add_cargo_build)
253253
endif()
254254

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

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

358359
set(global_joined_rustflags "$<JOIN:${global_rustflags_target_property}, >")
359360
set(global_rustflags_genex "$<$<BOOL:${global_rustflags_target_property}>:RUSTFLAGS=${global_joined_rustflags}>")
361+
set(local_rustflags_delimiter "$<$<BOOL:${local_rustflags_target_property}>:-->")
362+
set(local_rustflags_genex "$<$<BOOL:${local_rustflags_target_property}>:${local_rustflags_target_property}>")
363+
360364

361365
# Used to set a linker for a specific target-triple.
362366
set(cargo_target_linker_var "CARGO_TARGET_${_CORROSION_RUST_CARGO_TARGET_UPPER}_LINKER")
@@ -401,7 +405,7 @@ function(_add_cargo_build)
401405
"CORROSION_BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}"
402406
"CARGO_BUILD_RUSTC=${_CORROSION_RUSTC}"
403407
"${_CORROSION_CARGO}"
404-
build
408+
rustc
405409
${cargo_target_option}
406410
${_CORROSION_VERBOSE_OUTPUT_FLAG}
407411
# Global --features arguments added via corrosion_import_crate()
@@ -416,6 +420,9 @@ function(_add_cargo_build)
416420
${cargo_profile}
417421
${flag_args}
418422
${flags_genex}
423+
# Any arguments to cargo must be placed before this line
424+
${local_rustflags_delimiter}
425+
${local_rustflags_genex}
419426

420427
# Copy crate artifacts to the binary dir
421428
COMMAND
@@ -610,6 +617,16 @@ function(corrosion_add_target_rustflags target_name rustflag)
610617
)
611618
endfunction()
612619

620+
function(corrosion_add_target_local_rustflags target_name rustc_flag)
621+
# Set Rustflags via `cargo rustc` which only affect the current crate, but not dependencies.
622+
# Additional rustflags may be passed as optional parameters after rustflag.
623+
set_property(
624+
TARGET ${target_name}
625+
APPEND
626+
PROPERTY INTERFACE_CORROSION_LOCAL_RUSTFLAGS ${rustc_flag} ${ARGN}
627+
)
628+
endfunction()
629+
613630
function(corrosion_set_env_vars target_name env_var)
614631
# Additional environment variables may be passed as optional parameters after env_var.
615632
set_property(

test/custom_profiles/rust/Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/rustflags/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ corrosion_add_target_rustflags(rustflag-test-lib
1111
--cfg=test_rustflag_cfg2="$<IF:$<OR:$<CONFIG:Debug>,$<CONFIG:>>,debug,release>"
1212
"--cfg=test_rustflag_cfg3"
1313
)
14+
15+
corrosion_add_target_local_rustflags(rustflag-test-lib "--cfg=test_local_rustflag1")
16+
corrosion_add_target_local_rustflags(rustflag-test-lib --cfg=test_local_rustflag2="value")

test/rustflags/rust/Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/rustflags/rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ license = "MIT"
55
edition = "2018"
66

77
[dependencies]
8+
some_dependency = { path = "some_dependency" }
89

910
[lib]
1011
crate-type=["staticlib"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "some_dependency"
3+
version = "0.1.0"
4+
license = "MIT"
5+
edition = "2018"
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Test that the local rustflags are only passed to the main crate and not to dependencies.
2+
#[cfg(test_local_rustflag1)]
3+
const _: [(); 1] = [(); 2];
4+
5+
#[cfg(test_local_rustflag2 = "value")]
6+
const _: [(); 1] = [(); 2];
7+
8+
pub fn some_function() -> u32 {
9+
42
10+
}

test/rustflags/rust/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ pub extern "C" fn rust_second_function(name: *const c_char) {
2727
pub extern "C" fn rust_third_function(name: *const c_char) {
2828
let name = unsafe { std::ffi::CStr::from_ptr(name).to_str().unwrap() };
2929
println!("Hello, {}! I'm Rust again, third time the charm!", name);
30+
assert_eq!(some_dependency::some_function(), 42);
3031
}
3132

3233
#[cfg(not(test_rustflag_cfg3))]
3334
const _: [(); 1] = [(); 2];
35+
36+
#[cfg(not(test_local_rustflag1))]
37+
const _: [(); 1] = [(); 2];
38+
39+
#[cfg(not(test_local_rustflag2 = "value"))]
40+
const _: [(); 1] = [(); 2];

0 commit comments

Comments
 (0)