Skip to content
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

Always set IMPORTED_LOCATION #213

Merged
merged 2 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ get_property(
TARGET Rust::Cargo PROPERTY IMPORTED_LOCATION
)

# Internal function used by both the Rust CMake Generator and CorrosionGenerator.cmake
#
# base_property: Name of the base property - i.e. `IMPORTED_LOCATION` or `IMPORTED_IMPLIB`.
# Optional parameters: Multi-Config configuration types.
function(corrosion_internal_set_imported_location target_name base_property filename)
foreach(config_type ${ARGN})
set(binary_root "${CMAKE_CURRENT_BINARY_DIR}/${config_type}")
string(TOUPPER "${config_type}" config_type_upper)
message(DEBUG "Setting ${base_property}_${config_type_upper} for target ${target_name}"
" to `${binary_root}/${filename}`.")
# For Multiconfig we want to specify the correct location for each configuration
set_property(
TARGET ${target_name}
PROPERTY "${base_property}_${config_type_upper}"
"${binary_root}/${filename}"
)
endforeach()
if(NOT ARGN)
set(binary_root "${CMAKE_CURRENT_BINARY_DIR}")
endif()

message(DEBUG "Setting ${base_property} for target ${target_name}"
" to `${binary_root}/${filename}`.")

# IMPORTED_LOCATION must be set regardless of possible overrides. In the multiconfig case,
# the last configuration "wins".
set_property(
TARGET ${target_name}
PROPERTY "${base_property}" "${binary_root}/${filename}"
)
endfunction()

if (NOT CORROSION_NATIVE_TOOLING)
include(CorrosionGenerator)
endif()
Expand Down
56 changes: 18 additions & 38 deletions cmake/CorrosionGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ function(_generator_add_target manifest ix cargo_version profile)
)
endfunction()

function(_generator_add_config_info manifest ix is_multi_config config_type)

function(_generator_add_config_info manifest ix is_multi_config config_types)
get_source_file_property(target_name ${manifest} CORROSION_TARGET${ix}_TARGET_NAME)

get_source_file_property(is_library ${manifest} CORROSION_TARGET${ix}_IS_LIBRARY)
Expand All @@ -347,47 +348,28 @@ function(_generator_add_config_info manifest ix is_multi_config config_type)

get_source_file_property(is_windows ${manifest} CORROSION_PLATFORM_IS_WINDOWS)

if(config_type)
string(TOUPPER "${config_type}" config_type_upper)
set(imported_location "IMPORTED_LOCATION_${config_type_upper}")
set(imported_implib "IMPORTED_IMPLIB_${config_type_upper}")
else()
set(imported_location "IMPORTED_LOCATION")
set(imported_implib "IMPORTED_IMPLIB")
endif()

if(is_multi_config)
set(binary_root "${CMAKE_CURRENT_BINARY_DIR}/${config_type}")
else()
set(binary_root "${CMAKE_CURRENT_BINARY_DIR}")
set(multi_config_config_types "${config_types}")
endif()

if(is_library)
if(has_staticlib)
set_property(
TARGET ${target_name}-static
PROPERTY ${imported_location} "${binary_root}/${static_lib_name}"
)
corrosion_internal_set_imported_location("${target_name}-static" "IMPORTED_LOCATION"
"${static_lib_name}" ${multi_config_config_types})
endif()

if(has_cdylib)
set_property(
TARGET ${target_name}-shared
PROPERTY ${imported_location} "${binary_root}/${dynamic_lib_name}"
)
corrosion_internal_set_imported_location("${target_name}-shared" "IMPORTED_LOCATION"
"${dynamic_lib_name}" ${multi_config_config_types})

if(is_windows)
set_property(
TARGET ${target_name}-shared
PROPERTY ${imported_implib} "${binary_root}/${implib_name}"
)
corrosion_internal_set_imported_location("${target_name}-shared" "IMPORTED_IMPLIB"
"${implib_name}" ${multi_config_config_types})
endif()
endif()
elseif(is_executable)
set_property(
TARGET ${target_name}
PROPERTY ${imported_location} "${binary_root}/${exe_name}"
)
corrosion_internal_set_imported_location("${target_name}" "IMPORTED_LOCATION"
"${exe_name}" ${multi_config_config_types})
else()
message(FATAL_ERROR "unknown target type")
endif()
Expand Down Expand Up @@ -481,14 +463,12 @@ function(_generator_add_cargo_targets)
)
endforeach()

foreach(config_type config_folder IN ZIP_LISTS config_types config_folders)
foreach(ix RANGE ${num_targets-1})
_generator_add_config_info(
${GGC_MANIFEST_PATH}
${ix}
${is_multi_config}
"${config_type}"
)
endforeach()
foreach(ix RANGE ${num_targets-1})
_generator_add_config_info(
${GGC_MANIFEST_PATH}
${ix}
${is_multi_config}
"${config_types}"
)
endforeach()
endfunction()
25 changes: 10 additions & 15 deletions generator/src/subcommands/gen_cmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use clap::{App, Arg, ArgMatches, SubCommand};
use platforms::Platform;
use semver::Version;

use self::target::ConfigType;

mod platform;
mod target;

Expand Down Expand Up @@ -146,18 +148,18 @@ cmake_minimum_required(VERSION 3.15)

let config_root = Path::new(matches.value_of(CONFIGURATION_ROOT).unwrap_or("."));

let mut config_folders = Vec::new();
if let Some(config_types) = matches.values_of(CONFIGURATION_TYPES) {
let config_type = if let Some(config_types) = matches.values_of(CONFIGURATION_TYPES) {
let mut configuration_types = Vec::new();
for config_type in config_types {
let config_folder = config_root.join(config_type);
std::fs::create_dir_all(&config_folder).expect("Could not create config folder");
config_folders.push((Some(config_type), config_folder));
configuration_types.push(config_type.into());
}
ConfigType::MultiConfig(configuration_types)
} else {
let config_type = matches.value_of(CONFIGURATION_TYPE);
let config_folder = config_root;
config_folders.push((config_type, config_folder.to_path_buf()));
}
ConfigType::SingleConfig(config_type.map(|s| s.into()))
};

let crates = matches
.values_of(CRATES)
Expand Down Expand Up @@ -198,15 +200,8 @@ cmake_minimum_required(VERSION 3.15)

writeln!(out_file)?;

for (config_type, _config_folder) in config_folders {
for target in &targets {
target.emit_cmake_config_info(
&mut out_file,
&cargo_platform,
matches.is_present(CONFIGURATION_TYPES),
&config_type,
)?;
}
for target in &targets {
target.emit_cmake_config_info(&mut out_file, &cargo_platform, &config_type)?;
}

std::process::exit(0);
Expand Down
54 changes: 24 additions & 30 deletions generator/src/subcommands/gen_cmake/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub struct CargoTarget {
target_type: CargoTargetType,
}

pub(crate) enum ConfigType {
SingleConfig(Option<String>),
MultiConfig(Vec<String>),
}

impl CargoTarget {
pub fn from_metadata(
cargo_package: Rc<cargo_metadata::Package>,
Expand Down Expand Up @@ -276,21 +281,15 @@ _add_cargo_build(
Ok(())
}

pub fn emit_cmake_config_info(
pub(crate) fn emit_cmake_config_info(
&self,
out_file: &mut dyn std::io::Write,
platform: &super::platform::Platform,
is_multi_config: bool,
config_type: &Option<&str>,
config_type: &ConfigType,
) -> Result<(), Box<dyn Error>> {
let imported_location = config_type.map_or("IMPORTED_LOCATION".to_owned(), |config_type| {
format!("IMPORTED_LOCATION_{}", config_type.to_uppercase())
});

let binary_root = if is_multi_config {
format!("${{CMAKE_CURRENT_BINARY_DIR}}/{}", config_type.unwrap())
} else {
"${CMAKE_CURRENT_BINARY_DIR}".to_string()
let multi_config_args = match config_type {
ConfigType::SingleConfig(_) => "".into(),
ConfigType::MultiConfig(configs) => configs.join(" "),
};

match self.target_type {
Expand All @@ -301,37 +300,32 @@ _add_cargo_build(
if has_staticlib {
writeln!(
out_file,
"set_property(TARGET {0}-static PROPERTY {1} \"{2}/{3}\")",
"corrosion_internal_set_imported_location({0}-static {1} {2} {3})",
self.cargo_target.name,
imported_location,
binary_root,
self.static_lib_name(platform)
"IMPORTED_LOCATION",
self.static_lib_name(platform),
multi_config_args
)?;
}

if has_cdylib {
writeln!(
out_file,
"set_property(TARGET {0}-shared PROPERTY {1} \"{2}/{3}\")",
"corrosion_internal_set_imported_location({0}-shared {1} {2} {3})",
self.cargo_target.name,
imported_location,
binary_root,
self.dynamic_lib_name(platform)
"IMPORTED_LOCATION",
self.dynamic_lib_name(platform),
multi_config_args
)?;

if platform.is_windows() {
let imported_implib = config_type
.map_or("IMPORTED_IMPLIB".to_owned(), |config_type| {
format!("IMPORTED_IMPLIB_{}", config_type.to_uppercase())
});

writeln!(
out_file,
"set_property(TARGET {0}-shared PROPERTY {1} \"{2}/{3}\")",
"corrosion_internal_set_imported_location({0}-shared {1} {2} {3})",
self.cargo_target.name,
imported_implib,
binary_root,
self.implib_name(platform)
"IMPORTED_IMPLIB",
self.implib_name(platform),
multi_config_args
)?;
}
}
Expand All @@ -345,8 +339,8 @@ _add_cargo_build(

writeln!(
out_file,
"set_property(TARGET {0} PROPERTY {1} \"{2}/{3}\")",
self.cargo_target.name, imported_location, binary_root, exe_file
"corrosion_internal_set_imported_location({0} {1} {2} {3})",
self.cargo_target.name, "IMPORTED_LOCATION", exe_file, multi_config_args
)?;
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/cpp2rust/lib2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <iostream>
#include <stdint.h>

extern "C" void cpp_function2(char const *name) {
std::cout << "Hello, " << name << "! I'm C++ library Number 2!\n";
}

extern "C" uint32_t get_42() {
uint32_t v = 42;
return v;
}
7 changes: 7 additions & 0 deletions test/cpp2rust/rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion test/cpp2rust/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ license = "MIT"
edition = "2018"

[dependencies]

rust-dependency = { path = "rust_dependency"}
8 changes: 8 additions & 0 deletions test/cpp2rust/rust/rust_dependency/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "rust-dependency"
version = "0.1.0"
license = "MIT"
edition = "2018"

[dependencies]

8 changes: 8 additions & 0 deletions test/cpp2rust/rust/rust_dependency/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

extern "C" {
fn get_42() -> u32;
}
pub fn calls_ffi() {
let res = unsafe { get_42()};
assert_eq!(res, 42);
}
1 change: 1 addition & 0 deletions test/cpp2rust/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ fn main() {
} else {
greeting("Rust");
}
rust_dependency::calls_ffi();
}