Skip to content

Commit

Permalink
Support CRATE_TYPES argument for corrosion_import_crate
Browse files Browse the repository at this point in the history
  • Loading branch information
vdimir authored and jschwe committed Dec 13, 2022
1 parent 90ed7e0 commit 85fde50
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ corrosion_import_crate(MANIFEST_PATH <path/to/cargo.toml>
[NO_STD]
# Specify cargo build profile (e.g. release or a custom profile)
[PROFILE <cargo-profile>]
# Build only the specified crate types (Ignored with CMake < 3.19)
[CRATE_TYPES <crate_type1> ... <crate_typeN>]
# Only import the specified crates from a workspace
[CRATES <crate1> ... <crateN>]
# Enable the specified features
Expand Down
4 changes: 3 additions & 1 deletion cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ endfunction()
function(corrosion_import_crate)
set(OPTIONS ALL_FEATURES NO_DEFAULT_FEATURES NO_STD)
set(ONE_VALUE_KEYWORDS MANIFEST_PATH PROFILE)
set(MULTI_VALUE_KEYWORDS CRATES FEATURES FLAGS)
set(MULTI_VALUE_KEYWORDS CRATE_TYPES CRATES FEATURES FLAGS)
cmake_parse_arguments(COR "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}" ${ARGN})

if (NOT DEFINED COR_MANIFEST_PATH)
Expand Down Expand Up @@ -1122,6 +1122,8 @@ function(corrosion_import_crate)
"${COR_MANIFEST_PATH}"
CRATES
"${COR_CRATES}"
CRATE_TYPES
"${COR_CRATE_TYPES}"
PROFILE
"${COR_PROFILE}"
)
Expand Down
10 changes: 6 additions & 4 deletions cmake/CorrosionGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function(_cargo_metadata out manifest)
endfunction()

# Add targets (crates) of one package
function(_generator_add_package_targets workspace_manifest_path package_manifest_path package_name targets profile out_created_targets)
function(_generator_add_package_targets workspace_manifest_path package_manifest_path package_name targets profile out_created_targets crate_types)
# target types
set(has_staticlib FALSE)
set(has_cdylib FALSE)
Expand Down Expand Up @@ -52,7 +52,9 @@ function(_generator_add_package_targets workspace_manifest_path package_manifest
set(kinds)
foreach(ix RANGE ${target_kind_len-1})
string(JSON kind GET "${target_kind}" ${ix})
list(APPEND kinds ${kind})
if(NOT crate_types OR ${kind} IN_LIST crate_types)
list(APPEND kinds ${kind})
endif()
endforeach()

if(TARGET "${target_name}"
Expand Down Expand Up @@ -163,7 +165,7 @@ endfunction()
function(_generator_add_cargo_targets)
set(options "")
set(one_value_args MANIFEST_PATH PROFILE)
set(multi_value_args CRATES)
set(multi_value_args CRATES CRATE_TYPES)
cmake_parse_arguments(
GGC
"${options}"
Expand Down Expand Up @@ -197,7 +199,7 @@ function(_generator_add_cargo_targets)
if(ws_mem STREQUAL pkg_id AND ((NOT GGC_CRATES) OR (pkg_name IN_LIST GGC_CRATES)))
message(DEBUG "Found ${targets_len} targets in package ${pkg_name}")

_generator_add_package_targets("${GGC_MANIFEST_PATH}" "${pkg_manifest_path}" "${pkg_name}" "${targets}" "${GGC_PROFILE}" curr_created_targets)
_generator_add_package_targets("${GGC_MANIFEST_PATH}" "${pkg_manifest_path}" "${pkg_name}" "${targets}" "${GGC_PROFILE}" curr_created_targets "${GGC_CRATE_TYPES}")
list(APPEND created_targets "${curr_created_targets}")
endif()
endforeach()
Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ endfunction()
# Please keep this in alphabetical order.
add_subdirectory(cargo_flags)
add_subdirectory(cpp2rust)
if(Rust_VERSION VERSION_GREATER_EQUAL "1.64.0")
# Flag `--crate-type` is only supported since Rust 1.64.0
add_subdirectory(crate_type)
endif()
add_subdirectory(custom_profiles)
add_subdirectory(cxxbridge)
add_subdirectory(envvar)
Expand Down
6 changes: 6 additions & 0 deletions test/crate_type/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
corrosion_tests_add_test(crate_type "cpp-exe")


set_tests_properties("crate_type_run_cpp-exe" PROPERTIES PASS_REGULAR_EXPRESSION
"Hello from lib 1!\r?\nHello from lib 2!"
)
11 changes: 11 additions & 0 deletions test/crate_type/crate_type/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.15)
project(test_project VERSION 0.1.0)
include(../../test_header.cmake)

# Add --crate-type to ensure that only the specified type of library is built and no error is thrown
corrosion_import_crate(MANIFEST_PATH proj1/Cargo.toml CRATE_TYPES staticlib FLAGS --crate-type=staticlib)
corrosion_import_crate(MANIFEST_PATH proj2/Cargo.toml CRATE_TYPES cdylib FLAGS --crate-type=cdylib)

add_executable(cpp-exe main.cpp)
target_link_libraries(cpp-exe proj1)
target_link_libraries(cpp-exe proj2)
8 changes: 8 additions & 0 deletions test/crate_type/crate_type/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern "C" void rust_function1();
extern "C" void rust_function2();

int main() {
rust_function1();
rust_function2();
return 0;
}
7 changes: 7 additions & 0 deletions test/crate_type/crate_type/proj1/Cargo.lock

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

10 changes: 10 additions & 0 deletions test/crate_type/crate_type/proj1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "proj1"
version = "0.1.0"
edition = "2018"

[dependencies]

[lib]
crate-type=["staticlib", "cdylib"]

4 changes: 4 additions & 0 deletions test/crate_type/crate_type/proj1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub extern "C" fn rust_function1() {
println!("Hello from lib 1!");
}
7 changes: 7 additions & 0 deletions test/crate_type/crate_type/proj2/Cargo.lock

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

9 changes: 9 additions & 0 deletions test/crate_type/crate_type/proj2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "proj2"
version = "0.1.0"
edition = "2018"

[dependencies]

[lib]
crate-type=["staticlib", "cdylib"]
4 changes: 4 additions & 0 deletions test/crate_type/crate_type/proj2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub extern "C" fn rust_function2() {
println!("Hello from lib 2!");
}

0 comments on commit 85fde50

Please sign in to comment.