Skip to content

Commit

Permalink
Added ability to import a crate containing lib and bin targets with t…
Browse files Browse the repository at this point in the history
…he same name.
  • Loading branch information
obsgolem committed Oct 8, 2024
1 parent 99304ec commit ade5ba9
Show file tree
Hide file tree
Showing 26 changed files with 160 additions and 103 deletions.
127 changes: 74 additions & 53 deletions cmake/Corrosion.cmake

Large diffs are not rendered by default.

55 changes: 37 additions & 18 deletions cmake/CorrosionGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ function(_generator_add_package_targets)
PACKAGE_NAME
PACKAGE_VERSION
TARGETS_JSON
OUT_CREATED_TARGETS)
OUT_CREATED_TARGETS
BIN_NAMESPACE
LIB_NAMESPACE)
set(MULTI_VALUE_KEYWORDS CRATE_TYPES OVERRIDE_CRATE_TYPE_ARGS)
cmake_parse_arguments(PARSE_ARGV 0 GAPT "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")

Expand Down Expand Up @@ -112,13 +114,22 @@ function(_generator_add_package_targets)
endif()
endforeach()
endif()
if(TARGET "${target_name}"

if(("staticlib" IN_LIST kinds OR "cdylib" IN_LIST kinds) AND GAPT_LIB_NAMESPACE)
set(target_name_cmake "${GAPT_LIB_NAMESPACE}::${target_name}")
elseif(("bin" IN_LIST kinds) AND GAPT_BIN_NAMESPACE)
set(target_name_cmake "${GAPT_BIN_NAMESPACE}::${target_name}")
else()
set(target_name_cmake "${target_name}")
endif()

if(TARGET "${target_name_cmake}"
AND ("staticlib" IN_LIST kinds OR "cdylib" IN_LIST kinds OR "bin" IN_LIST kinds)
)
message(WARNING "Failed to import Rust crate ${target_name} (kind: `${target_kind}`) because a target "
"with the same name already exists. Skipping this target.\n"
"Help: If you are importing a package which exposes both a `lib` and "
"a `bin` target, please consider explicitly naming the targets in your `Cargo.toml` manifest.\n"
"a `bin` target, use the LIB_NAMESPACE and BIN_NAMESPACE options to import them into separate namespaces.\n"
"Note: If you have multiple different packages which have targets with the same name, please note that "
"this is currently not supported by Corrosion. Feel free to open an issue on Github to request "
"supporting this scenario."
Expand All @@ -134,16 +145,18 @@ function(_generator_add_package_targets)
# 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}")
string(REPLACE "\-" "_" target_name_cmake "${target_name_cmake}")

set(archive_byproducts "")
set(shared_lib_byproduct "")
set(pdb_byproduct "")

add_library(${target_name} INTERFACE)
_corrosion_initialize_properties(${target_name})
add_library(${target_name_cmake} INTERFACE IMPORTED GLOBAL)
_corrosion_initialize_properties(${target_name_cmake})
_corrosion_add_library_target(
WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
TARGET_NAME "${target_name}"
TARGET_NAME_CMAKE "${target_name_cmake}"
LIB_KINDS ${kinds}
OUT_ARCHIVE_OUTPUT_BYPRODUCTS archive_byproducts
OUT_SHARED_LIB_BYPRODUCTS shared_lib_byproduct
Expand All @@ -158,6 +171,7 @@ function(_generator_add_package_targets)
cargo_build_out_dir
PACKAGE ${package_name}
TARGET ${target_name}
TARGET_NAME_CMAKE ${target_name_cmake}
MANIFEST_PATH "${manifest_path}"
WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
TARGET_KINDS "${kinds}"
Expand All @@ -167,28 +181,28 @@ function(_generator_add_package_targets)
)
if(archive_byproducts)
_corrosion_copy_byproducts(
${target_name} ARCHIVE_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${archive_byproducts}"
${target_name_cmake} ARCHIVE_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${archive_byproducts}"
)
endif()
if(shared_lib_byproduct)
_corrosion_copy_byproducts(
${target_name} LIBRARY_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${shared_lib_byproduct}"
${target_name_cmake} LIBRARY_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${shared_lib_byproduct}"
)
endif()
if(pdb_byproduct)
_corrosion_copy_byproducts(
${target_name} "PDB_OUTPUT_DIRECTORY;LIBRARY_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}"
${target_name_cmake} "PDB_OUTPUT_DIRECTORY;LIBRARY_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}"
)
endif()
list(APPEND corrosion_targets ${target_name})
set_property(TARGET "${target_name}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
list(APPEND corrosion_targets ${target_name_cmake})
set_property(TARGET "${target_name_cmake}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
# Note: "bin" is mutually exclusive with "staticlib/cdylib", since `bin`s are seperate crates from libraries.
elseif("bin" IN_LIST kinds)
set(bin_byproduct "")
set(pdb_byproduct "")
add_executable(${target_name} IMPORTED GLOBAL)
_corrosion_initialize_properties(${target_name})
_corrosion_add_bin_target("${workspace_manifest_path}" "${target_name}"
add_executable(${target_name_cmake} IMPORTED GLOBAL)
_corrosion_initialize_properties(${target_name_cmake})
_corrosion_add_bin_target("${workspace_manifest_path}" "${target_name}" "${target_name_cmake}"
"bin_byproduct" "pdb_byproduct"
)

Expand All @@ -200,6 +214,7 @@ function(_generator_add_package_targets)
cargo_build_out_dir
PACKAGE "${package_name}"
TARGET "${target_name}"
TARGET_NAME_CMAKE ${target_name_cmake}
MANIFEST_PATH "${manifest_path}"
WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
TARGET_KINDS "bin"
Expand All @@ -208,15 +223,15 @@ function(_generator_add_package_targets)
${no_linker_override}
)
_corrosion_copy_byproducts(
${target_name} RUNTIME_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${bin_byproduct}"
${target_name_cmake} RUNTIME_OUTPUT_DIRECTORY "${cargo_build_out_dir}" "${bin_byproduct}"
)
if(pdb_byproduct)
_corrosion_copy_byproducts(
${target_name} "PDB_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}"
${target_name_cmake} "PDB_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY" "${cargo_build_out_dir}" "${pdb_byproduct}"
)
endif()
list(APPEND corrosion_targets ${target_name})
set_property(TARGET "${target_name}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
list(APPEND corrosion_targets ${target_name_cmake})
set_property(TARGET "${target_name_cmake}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
else()
# ignore other kinds (like examples, tests, build scripts, ...)
endif()
Expand All @@ -235,7 +250,7 @@ endfunction()
# `MANIFEST_PATH`.
function(_generator_add_cargo_targets)
set(options NO_LINKER_OVERRIDE)
set(one_value_args MANIFEST_PATH IMPORTED_CRATES)
set(one_value_args MANIFEST_PATH IMPORTED_CRATES BIN_NAMESPACE LIB_NAMESPACE)
set(multi_value_args CRATES CRATE_TYPES OVERRIDE_CRATE_TYPE_ARGS)
cmake_parse_arguments(
GGC
Expand All @@ -247,6 +262,8 @@ function(_generator_add_cargo_targets)
list(APPEND CMAKE_MESSAGE_CONTEXT "_add_cargo_targets")

_corrosion_option_passthrough_helper(NO_LINKER_OVERRIDE GGC no_linker_override)
_corrosion_arg_passthrough_helper(BIN_NAMESPACE COR bin_namespace)
_corrosion_arg_passthrough_helper(LIB_NAMESPACE COR lib_namespace)
_corrosion_arg_passthrough_helper(CRATE_TYPES GGC crate_types)
_corrosion_arg_passthrough_helper(OVERRIDE_CRATE_TYPE_ARGS GGC override_crate_types)

Expand Down Expand Up @@ -309,6 +326,8 @@ function(_generator_add_cargo_targets)
${no_linker_override}
${crate_types}
${override_crate_types}
${bin_namespace}
${lib_namespace}
)
list(APPEND created_targets "${curr_created_targets}")
endforeach()
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ add_subdirectory(gensource)
add_subdirectory(hostbuild)
add_subdirectory(multitarget)
add_subdirectory(nostd)
add_subdirectory(one_package_bin_and_lib)
add_subdirectory("output directory")
add_subdirectory(parse_target_triple)
add_subdirectory(rust2cpp)
Expand Down
2 changes: 0 additions & 2 deletions test/cargo_flags/cargo_flags/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/cbindgen/rust2cpp/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/corrosion_install/install_rust_bin/rust_bin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/cpp2rust/cpp2rust/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/features/features/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/gensource/gensource/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/gensource/gensource/generator/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/hostbuild/hostbuild/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/multitarget/multitarget/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/nostd/nostd/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/one_package_bin_and_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
corrosion_tests_add_test(one_package_bin_and_lib "my_program")

set_tests_properties("one_package_bin_and_lib_run_my_program" PROPERTIES PASS_REGULAR_EXPRESSION
"^Ok\r?\n$"
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.15)
project(test_project VERSION 0.1.0)
include(../../test_header.cmake)

corrosion_import_crate(
MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml
BIN_NAMESPACE bin LIB_NAMESPACE lib
)

if(NOT TARGET bin::mycrate)
message(FATAL_ERROR "Unable to import bin::mycrate.")
endif()

add_executable(my_program main.cpp)
target_link_libraries(my_program PUBLIC lib::mycrate)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "mycrate"
version = "0.1.0"
edition = "2018"
description = "descr;\"hello\\"

[lib]
crate-type = ["staticlib", "rlib"]
name = "mycrate"
4 changes: 4 additions & 0 deletions test/one_package_bin_and_lib/one_package_bin_and_lib/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <iostream>
int main() {
std::cout << "Ok";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello() {
println!("Hello world");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use mycrate::hello;

fn main() {
hello();
}
2 changes: 0 additions & 2 deletions test/output directory/output directory/proj1/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/output directory/output directory/proj2/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/output directory/output directory/proj3/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/rustflags/cargo_config_rustflags/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/rustflags/rustflags/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/workspace/workspace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ade5ba9

Please sign in to comment.