Skip to content

Commit 5e62a7a

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 6fc6d29 commit 5e62a7a

File tree

8 files changed

+58
-1
lines changed

8 files changed

+58
-1
lines changed

cmake/Corrosion.cmake

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ function(_add_cargo_build)
220220
endif()
221221

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

224225
set(features_target_property "$<GENEX_EVAL:$<TARGET_PROPERTY:${target_name},${_CORR_PROP_FEATURES}>>")
225226
set(features_genex "$<$<BOOL:${features_target_property}>:--features=$<JOIN:${features_target_property},$<COMMA>>>")
@@ -316,6 +317,9 @@ function(_add_cargo_build)
316317
# However single quotes don't work on windows, so we can only add double quotes here. Any double
317318
# quotes _in_ the rustflags must be escaped like `\\\"`.
318319
set(global_rustflags_genex "$<$<BOOL:${global_rustflags_target_property}>:RUSTFLAGS=\"${global_rustflags_target_property}\">")
320+
set(local_rustflags_delimiter "$<$<BOOL:${local_rustflags_target_property}>:-->")
321+
set(local_rustflags_genex "$<$<BOOL:${local_rustflags_target_property}>:${local_rustflags_target_property}>")
322+
319323

320324
if(CORROSION_LINKER_PREFERENCE)
321325
if(CMAKE_CROSSCOMPILING)
@@ -351,7 +355,7 @@ function(_add_cargo_build)
351355
CORROSION_BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}
352356
CARGO_BUILD_RUSTC="${_CORROSION_RUSTC}"
353357
"${_CORROSION_CARGO}"
354-
build
358+
rustc
355359
${cargo_target_option}
356360
${_CORROSION_VERBOSE_OUTPUT_FLAG}
357361
# Global --features arguments added via corrosion_import_crate()
@@ -363,6 +367,8 @@ function(_add_cargo_build)
363367
--package ${package_name}
364368
--manifest-path "${path_to_toml}"
365369
${cargo_profile}
370+
${local_rustflags_delimiter}
371+
${local_rustflags_genex}
366372

367373
# Copy crate artifacts to the binary dir
368374
COMMAND
@@ -539,6 +545,16 @@ function(corrosion_add_target_rustflags target_name rustflag)
539545
)
540546
endfunction()
541547

548+
function(corrosion_add_target_local_rustflags target_name rustc_flag)
549+
# Set Rustflags via `cargo rustc` which only affect the current crate, but not dependencies.
550+
# Additional rustflags may be passed as optional parameters after rustflag.
551+
set_property(
552+
TARGET ${target_name}
553+
APPEND
554+
PROPERTY INTERFACE_CORROSION_LOCAL_RUSTFLAGS ${rustc_flag} ${ARGN}
555+
)
556+
endfunction()
557+
542558
function(corrosion_set_env_vars target_name env_var)
543559
# Additional environment variables may be passed as optional parameters after env_var.
544560
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:$<CONFIG:Release>,release,debug>\\\""
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)