-
-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added ability to import a crate containing lib and bin targets with the same name #394
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}") | ||
|
||
|
@@ -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." | ||
|
@@ -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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering: Is the original There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is used to form the lib file names. |
||
TARGET_NAME_CMAKE "${target_name_cmake}" | ||
LIB_KINDS ${kinds} | ||
OUT_ARCHIVE_OUTPUT_BYPRODUCTS archive_byproducts | ||
OUT_SHARED_LIB_BYPRODUCTS shared_lib_byproduct | ||
|
@@ -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}" | ||
|
@@ -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" | ||
) | ||
|
||
|
@@ -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" | ||
|
@@ -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() | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
||
|
@@ -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() | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,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" |
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(); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats the reason to make the interface library
IMPORTED
andGLOBAL
? It doesn't seem directly related to this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is what I get without this.