Skip to content

Commit 54096b6

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 0bd27ca commit 54096b6

File tree

8 files changed

+67
-1
lines changed

8 files changed

+67
-1
lines changed

cmake/Corrosion.cmake

Lines changed: 26 additions & 1 deletion
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
@@ -601,6 +608,12 @@ function(corrosion_set_hostbuild target_name)
601608
)
602609
endfunction()
603610

611+
# Add flags for rustc (RUSTFLAGS) which affect the target and all of it's Rust dependencies
612+
#
613+
# Additional rustflags may be passed as optional parameters after rustflag.
614+
# Please note, that if you import multiple targets from a package or workspace, but set different
615+
# Rustflags via this function, the Rust dependencies will have to be rebuilt when changing targets.
616+
# Consider `corrosion_add_target_local_rustflags()` as an alternative to avoid this.
604617
function(corrosion_add_target_rustflags target_name rustflag)
605618
# Additional rustflags may be passed as optional parameters after rustflag.
606619
set_property(
@@ -610,6 +623,18 @@ function(corrosion_add_target_rustflags target_name rustflag)
610623
)
611624
endfunction()
612625

626+
# Add flags for rustc (RUSTFLAGS) which only affect the target, but none of it's (Rust) dependencies
627+
#
628+
# Additional rustflags may be passed as optional parameters after rustc_flag.
629+
function(corrosion_add_target_local_rustflags target_name rustc_flag)
630+
# Set Rustflags via `cargo rustc` which only affect the current crate, but not dependencies.
631+
set_property(
632+
TARGET ${target_name}
633+
APPEND
634+
PROPERTY INTERFACE_CORROSION_LOCAL_RUSTFLAGS ${rustc_flag} ${ARGN}
635+
)
636+
endfunction()
637+
613638
function(corrosion_set_env_vars target_name env_var)
614639
# Additional environment variables may be passed as optional parameters after env_var.
615640
set_property(

test/custom_profiles/rust/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/rustflags/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/rustflags/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
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"]
Lines changed: 6 additions & 0 deletions
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+
Lines changed: 10 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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)