Skip to content

Commit

Permalink
Updated native tooling to support namespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
obsgolem committed May 26, 2023
1 parent e5ffd29 commit 6956fb7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
5 changes: 5 additions & 0 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,9 @@ function(corrosion_import_crate)
set(passthrough_to_acb "--passthrough-acb=${joined_args}")
endif()

string(REPLACE "BIN_NAMESPACE" "--bin_namespace=" bin_namespace ${bin_namespace})
string(REPLACE "LIB_NAMESPACE" "--lib_namespace=" lib_namespace ${lib_namespace})

execute_process(
COMMAND
${_CORROSION_GENERATOR}
Expand All @@ -1076,6 +1079,8 @@ function(corrosion_import_crate)
${cargo_profile_native_generator}
--imported-crates=imported_crates
${passthrough_to_acb}
${bin_namespace}
${lib_namespace}
-o ${generated_cmake}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
RESULT_VARIABLE ret)
Expand Down
21 changes: 20 additions & 1 deletion generator/src/subcommands/gen_cmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const CRATES: &str = "crates";
const IMPORTED_CRATES: &str = "imported-crates";
const CRATE_TYPE: &str = "crate-type";
const PASSTHROUGH_ADD_CARGO_BUILD: &str = "passthrough-acb";
const BIN_NAMESPACE: &str = "bin_namespace";
const LIB_NAMESPACE: &str = "lib_namespace";

pub fn subcommand() -> App<'static, 'static> {
SubCommand::with_name(GEN_CMAKE)
Expand Down Expand Up @@ -72,6 +74,18 @@ pub fn subcommand() -> App<'static, 'static> {
.value_delimiter(std::char::from_u32(0x1f).unwrap().to_string().as_str())
.help("Passthrough arguments to the _add_cargo_build invocation(s) in CMake")
)
.arg(
Arg::with_name(BIN_NAMESPACE)
.long("bin_namespace")
.help("The namespace that bin targets should be placed in in generated CMake")
.takes_value(true),
)
.arg(
Arg::with_name(LIB_NAMESPACE)
.long("lib_namespace")
.help("The namespace that lib targets should be placed in in generated CMake")
.takes_value(true),
)
}

pub fn invoke(
Expand Down Expand Up @@ -120,6 +134,8 @@ cmake_minimum_required(VERSION 3.15)
package.clone(),
t.clone(),
workspace_manifest_path.clone(),
matches.value_of(BIN_NAMESPACE),
matches.value_of(LIB_NAMESPACE),
&crate_kinds,
)
})
Expand All @@ -145,7 +161,10 @@ cmake_minimum_required(VERSION 3.15)
.unwrap();
}
if let Some(imported_crate_list_name) = matches.value_of(IMPORTED_CRATES) {
let imported_targets: Vec<_> = targets.iter().map(|target| target.target_name()).collect();
let imported_targets: Vec<_> = targets
.iter()
.map(|target| target.target_name_cmake())
.collect();
let imported_targets_list = imported_targets.join(";");
writeln!(
out_file,
Expand Down
34 changes: 26 additions & 8 deletions generator/src/subcommands/gen_cmake/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum CargoTargetType {
pub struct CargoTarget {
cargo_package: Rc<cargo_metadata::Package>,
cargo_target: cargo_metadata::Target,
target_name_cmake: String,
target_type: CargoTargetType,
workspace_manifest_path: Rc<PathBuf>,
}
Expand Down Expand Up @@ -47,6 +48,8 @@ impl CargoTarget {
cargo_package: Rc<cargo_metadata::Package>,
cargo_target: cargo_metadata::Target,
workspace_manifest_path: Rc<PathBuf>,
bin_namespace: Option<&str>,
lib_namespace: Option<&str>,
// If Some, only import crates if the kind variant is given in crate_kinds.
crate_kinds: &Option<Vec<&str>>,
) -> Option<Self> {
Expand Down Expand Up @@ -74,16 +77,26 @@ impl CargoTarget {
return None;
};

let target_name_cmake = match (&target_type, bin_namespace, lib_namespace) {
(CargoTargetType::Executable, None, _) => cargo_target.name.clone(),
(CargoTargetType::Library { .. }, _, None) => cargo_target.name.clone(),
(CargoTargetType::Executable, Some(n), _) => format!("{}::{}", n, cargo_target.name),
(CargoTargetType::Library { .. }, _, Some(n)) => {
format!("{}::{}", n, cargo_target.name)
}
};

Some(Self {
cargo_package,
cargo_target,
target_name_cmake,
target_type,
workspace_manifest_path,
})
}

pub(crate) fn target_name(&self) -> &str {
&self.cargo_target.name
pub(crate) fn target_name_cmake(&self) -> &str {
&self.target_name_cmake
}

pub fn emit_cmake_target(
Expand Down Expand Up @@ -132,6 +145,7 @@ impl CargoTarget {
_corrosion_add_library_target(
WORKSPACE_MANIFEST_PATH \"{workspace_manifest_path}\"
TARGET_NAME \"{target_name}\"
TARGET_NAME_CMAKE \"{target_name_cmake}\"
LIB_KINDS {lib_kinds}
OUT_ARCHIVE_OUTPUT_BYPRODUCTS archive_byproducts
OUT_SHARED_LIB_BYPRODUCTS shared_lib_byproduct
Expand All @@ -145,21 +159,23 @@ impl CargoTarget {
",
workspace_manifest_path = ws_manifest,
target_name = self.cargo_target.name,
target_name_cmake = self.target_name_cmake,
lib_kinds = lib_kinds,
)?;
}
CargoTargetType::Executable => {
writeln!(
out_file,
"
_corrosion_add_bin_target(\"{workspace_manifest_path}\" \"{target_name}\"
_corrosion_add_bin_target(\"{workspace_manifest_path}\" \"{target_name}\" \"{target_name_cmake}\"
bin_byproduct pdb_byproduct
)
set(byproducts \"\")
list(APPEND byproducts \"${{bin_byproduct}}\" \"${{pdb_byproduct}}\")
",
workspace_manifest_path = ws_manifest,
target_name = self.cargo_target.name,
target_name_cmake = self.target_name_cmake,
)?;
}
};
Expand All @@ -171,35 +187,36 @@ impl CargoTarget {
cargo_build_out_dir
PACKAGE \"{package_name}\"
TARGET \"{target_name}\"
TARGET_NAME_CMAKE \"{target_name_cmake}\"
MANIFEST_PATH \"{package_manifest_path}\"
WORKSPACE_MANIFEST_PATH \"{workspace_manifest_path}\"
TARGET_KINDS {target_kinds}
BYPRODUCTS \"${{byproducts}}\"
{passthrough_add_cargo_build}
)
set_target_properties({target_name} PROPERTIES
set_target_properties({target_name_cmake} PROPERTIES
INTERFACE_COR_PACKAGE_MANIFEST_PATH \"{package_manifest_path}\"
)
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 \"${{cargo_build_out_dir}}\" \"${{pdb_byproduct}}\"
{target_name_cmake} PDB_OUTPUT_DIRECTORY \"${{cargo_build_out_dir}}\" \"${{pdb_byproduct}}\"
)
endif()
if(bin_byproduct)
_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}}\"
)
endif()
",
Expand All @@ -209,6 +226,7 @@ impl CargoTarget {
workspace_manifest_path = ws_manifest,
target_kinds = target_kinds,
passthrough_add_cargo_build = passthrough_add_cargo_build,
target_name_cmake = self.target_name_cmake,

)?;
Ok(())
Expand Down

0 comments on commit 6956fb7

Please sign in to comment.