diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 1af19949..aa92da58 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -253,6 +253,7 @@ function(_add_cargo_build) endif() set(global_rustflags_target_property "$>") + set(local_rustflags_target_property "$>") set(features_target_property "$>") set(features_genex "$<$:--features=$>>") @@ -357,6 +358,9 @@ function(_add_cargo_build) set(global_joined_rustflags "$") set(global_rustflags_genex "$<$:RUSTFLAGS=${global_joined_rustflags}>") + set(local_rustflags_delimiter "$<$:-->") + set(local_rustflags_genex "$<$:${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") @@ -401,7 +405,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() @@ -416,6 +420,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 @@ -610,6 +617,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( diff --git a/test/custom_profiles/rust/Cargo.lock b/test/custom_profiles/rust/Cargo.lock new file mode 100644 index 00000000..6c1abd7f --- /dev/null +++ b/test/custom_profiles/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "custom-profiles-lib" +version = "0.1.0" diff --git a/test/rustflags/CMakeLists.txt b/test/rustflags/CMakeLists.txt index c18f6d4b..016bf739 100644 --- a/test/rustflags/CMakeLists.txt +++ b/test/rustflags/CMakeLists.txt @@ -11,3 +11,6 @@ corrosion_add_target_rustflags(rustflag-test-lib --cfg=test_rustflag_cfg2="$,$>,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") diff --git a/test/rustflags/rust/Cargo.lock b/test/rustflags/rust/Cargo.lock index fcc72380..333ce249 100644 --- a/test/rustflags/rust/Cargo.lock +++ b/test/rustflags/rust/Cargo.lock @@ -5,3 +5,10 @@ version = 3 [[package]] name = "rustflag-test-lib" version = "0.1.0" +dependencies = [ + "some_dependency", +] + +[[package]] +name = "some_dependency" +version = "0.1.0" diff --git a/test/rustflags/rust/Cargo.toml b/test/rustflags/rust/Cargo.toml index e7f53152..2bdd26f7 100644 --- a/test/rustflags/rust/Cargo.toml +++ b/test/rustflags/rust/Cargo.toml @@ -5,6 +5,7 @@ license = "MIT" edition = "2018" [dependencies] +some_dependency = { path = "some_dependency" } [lib] crate-type=["staticlib"] diff --git a/test/rustflags/rust/some_dependency/Cargo.toml b/test/rustflags/rust/some_dependency/Cargo.toml new file mode 100644 index 00000000..94627d03 --- /dev/null +++ b/test/rustflags/rust/some_dependency/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "some_dependency" +version = "0.1.0" +license = "MIT" +edition = "2018" + diff --git a/test/rustflags/rust/some_dependency/src/lib.rs b/test/rustflags/rust/some_dependency/src/lib.rs new file mode 100644 index 00000000..d240a7ca --- /dev/null +++ b/test/rustflags/rust/some_dependency/src/lib.rs @@ -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 +} diff --git a/test/rustflags/rust/src/lib.rs b/test/rustflags/rust/src/lib.rs index 0d68a2fd..f6da1f6e 100644 --- a/test/rustflags/rust/src/lib.rs +++ b/test/rustflags/rust/src/lib.rs @@ -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];