Skip to content

Commit

Permalink
Breaking: Replace dashes with underscores in lib names
Browse files Browse the repository at this point in the history
Rust 1.79 and newer replace dashes with underscores in lib names.
This can only happen when the lib name is inherited from the
package name, since explicit lib names have never been allowed
to use dashes.
Since we import our CMake target names by using cargo metadata,
we make the same change as cargo 1.79 does, but also for
older Rust versions, so that Corrosion users can use the
same CMake target name independent of the Rust version.

Fixes corrosion-rs#501
(cherry-picked from f85b242)
  • Loading branch information
jschwe committed May 11, 2024
1 parent 9943de7 commit fd400a1
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 24 deletions.
7 changes: 7 additions & 0 deletions cmake/CorrosionGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ function(_generator_add_package_targets)
endif()

if("staticlib" IN_LIST kinds OR "cdylib" IN_LIST kinds)
# Explicitly set library names have always been forbidden from using dashes (by cargo).
# Starting with Rust 1.79, names inherited from the package name will have dashes replaced
# by underscores too. Corrosion will thus replace dashes with underscores, to make the target
# name consistent independent of the Rust version. `bin` target names are not affected.
# See https://github.com/corrosion-rs/corrosion/issues/501 for more details.
string(REPLACE "\-" "_" target_name "${target_name}")

set(archive_byproducts "")
set(shared_lib_byproduct "")
set(pdb_byproduct "")
Expand Down
9 changes: 7 additions & 2 deletions generator/src/subcommands/gen_cmake/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ impl CargoTarget {
})
}

pub(crate) fn target_name(&self) -> &str {
&self.cargo_target.name
/// Cargo / Rust 1.78 and newer replace dashes with underscores in libraries
/// To make the names consistent across versions we also do the replacement here.
pub(crate) fn target_name(&self) -> String {
match self.target_type {
CargoTargetType::Library { .. } => self.cargo_target.name.replace("-", "_"),
_ => self.cargo_target.name.to_string(),
}
}

pub fn emit_cmake_target(
Expand Down
8 changes: 4 additions & 4 deletions test/cargo_flags/cargo_flags/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ include(../../test_header.cmake)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml FLAGS --features one)

add_executable(flags-exe main.cpp)
target_link_libraries(flags-exe PUBLIC flags-lib)
corrosion_set_cargo_flags(flags-lib --features two)
corrosion_set_cargo_flags(flags-lib $<TARGET_PROPERTY:flags-lib,more_flags>)
target_link_libraries(flags-exe PUBLIC flags_lib)
corrosion_set_cargo_flags(flags_lib --features two)
corrosion_set_cargo_flags(flags_lib $<TARGET_PROPERTY:flags_lib,more_flags>)

set_property(
TARGET flags-lib
TARGET flags_lib
APPEND
PROPERTY more_flags --features three
)
4 changes: 2 additions & 2 deletions test/cbindgen/rust2cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ project(test_project VERSION 0.1.0)
include(../../test_header.cmake)

corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml)
corrosion_experimental_cbindgen(TARGET rust-lib HEADER_NAME "rust-lib.h")
corrosion_experimental_cbindgen(TARGET rust_lib HEADER_NAME "rust-lib.h")

add_executable(cpp-exe main.cpp)
set_property(TARGET cpp-exe PROPERTY CXX_STANDARD 11)
target_link_libraries(cpp-exe PUBLIC rust-lib)
target_link_libraries(cpp-exe PUBLIC rust_lib)
2 changes: 1 addition & 1 deletion test/custom_profiles/basic_profiles/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ endif()
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml PROFILE ${CARGO_PROFILE})

add_executable(${CARGO_PROFILE}_bin main.cpp)
target_link_libraries(${CARGO_PROFILE}_bin PUBLIC cargo-profiles-lib)
target_link_libraries(${CARGO_PROFILE}_bin PUBLIC cargo_profiles_lib)
4 changes: 2 additions & 2 deletions test/custom_profiles/custom_profiles/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(custom_profile $<IF:$<CONFIG:Debug>,dev-without-dbg,${_release_profile}>)
if(CORROSION_TEST_USE_TARGET_SPECIFIC_OVERRIDE)
# Select "wrong" profile here on purpose.
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml PROFILE dev)
set_target_properties(custom-profiles-lib
set_target_properties(custom_profiles_lib
PROPERTIES
INTERFACE_CORROSION_CARGO_PROFILE "${custom_profile}"
)
Expand All @@ -17,4 +17,4 @@ else()
endif()

add_executable(custom-profile-exe main.cpp)
target_link_libraries(custom-profile-exe PUBLIC custom-profiles-lib)
target_link_libraries(custom-profile-exe PUBLIC custom_profiles_lib)
4 changes: 2 additions & 2 deletions test/cxxbridge/cxxbridge_rust2cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED 1)

corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml)
corrosion_add_cxxbridge(cxxbridge-cpp CRATE cxxbridge-crate MANIFEST_PATH rust FILES lib.rs foo/mod.rs)
corrosion_add_cxxbridge(cxxbridge-cpp CRATE cxxbridge_crate MANIFEST_PATH rust FILES lib.rs foo/mod.rs)

add_executable(cxxbridge-exe main.cpp)
target_link_libraries(cxxbridge-exe PUBLIC cxxbridge-cpp)

if(MSVC)
# Note: This is required because we use `cxx` which uses `cc` to compile and link C++ code.
corrosion_set_env_vars(cxxbridge-crate "CFLAGS=-MDd" "CXXFLAGS=-MDd")
corrosion_set_env_vars(cxxbridge_crate "CFLAGS=-MDd" "CXXFLAGS=-MDd")
endif()
4 changes: 2 additions & 2 deletions test/envvar/envvar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(../../test_header.cmake)

corrosion_import_crate(MANIFEST_PATH Cargo.toml)

corrosion_set_env_vars(rust-lib-requiring-envvar
corrosion_set_env_vars(rust_lib_requiring_envvar
"ANOTHER_VARIABLE=ANOTHER_VALUE"
"$<TARGET_PROPERTY:program_requiring_rust_lib_with_envvar,INDIRECT_VAR_TEST>"
"COR_CARGO_VERSION_MAJOR=${Rust_CARGO_VERSION_MAJOR}"
Expand All @@ -20,4 +20,4 @@ set_property(
"REQUIRED_VARIABLE=EXPECTED_VALUE"
)

target_link_libraries(program_requiring_rust_lib_with_envvar PUBLIC rust-lib-requiring-envvar)
target_link_libraries(program_requiring_rust_lib_with_envvar PUBLIC rust_lib_requiring_envvar)
4 changes: 2 additions & 2 deletions test/features/features/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ include(../../test_header.cmake)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml FEATURES thirdfeature ALL_FEATURES)

add_executable(features-cpp-exe main.cpp)
target_link_libraries(features-cpp-exe PUBLIC rust-feature-lib)
target_link_libraries(features-cpp-exe PUBLIC rust_feature_lib)

corrosion_set_features(rust-feature-lib
corrosion_set_features(rust_feature_lib
ALL_FEATURES OFF
NO_DEFAULT_FEATURES
FEATURES
Expand Down
4 changes: 2 additions & 2 deletions test/rust2cpp/rust2cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ include(../../test_header.cmake)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml)

add_executable(cpp-exe main.cpp)
target_link_libraries(cpp-exe PUBLIC rust-lib)
target_link_libraries(cpp-exe PUBLIC rust_lib)

add_executable(cpp-exe-shared main.cpp)
target_link_libraries(cpp-exe-shared
PUBLIC rust-lib-shared)
PUBLIC rust_lib-shared)
10 changes: 5 additions & 5 deletions test/rustflags/rustflags/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ include(../../test_header.cmake)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml)

add_executable(rustflags-cpp-exe main.cpp)
target_link_libraries(rustflags-cpp-exe PUBLIC rustflag-test-lib)
target_link_libraries(rustflags-cpp-exe PUBLIC rustflag_test_lib)

# Test --cfg=key="value" rustflag.
corrosion_add_target_rustflags(rustflag-test-lib --cfg=test_rustflag_cfg1="test_rustflag_cfg1_value")
corrosion_add_target_rustflags(rustflag_test_lib --cfg=test_rustflag_cfg1="test_rustflag_cfg1_value")

# Test using a generator expression to produce a rustflag and passing multiple rustflags.
corrosion_add_target_rustflags(rustflag-test-lib
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")
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")

0 comments on commit fd400a1

Please sign in to comment.