Skip to content

Commit

Permalink
Don't set linker for MSVC abi
Browse files Browse the repository at this point in the history
MSVC abi may be used independently of the MSVC generator.
MSVC uses cl.exe, so we can't use
the compiler as the linker driver.
We continue to just ignore MSVC in this case and let cargo / rust decide
on the linker on its own.
  • Loading branch information
jschwe committed Dec 10, 2022
1 parent 7426f4a commit f03517a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
8 changes: 6 additions & 2 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ endif()
# A target may be either a specific bin
function(_add_cargo_build out_cargo_build_out_dir)
set(options "")
set(one_value_args PACKAGE TARGET MANIFEST_PATH PROFILE TARGET_KIND)
set(one_value_args PACKAGE TARGET MANIFEST_PATH PROFILE TARGET_KIND WORKSPACE_MANIFEST_PATH)
set(multi_value_args BYPRODUCTS)
cmake_parse_arguments(
ACB
Expand All @@ -744,6 +744,7 @@ function(_add_cargo_build out_cargo_build_out_dir)
set(path_to_toml "${ACB_MANIFEST_PATH}")
set(cargo_profile_name "${ACB_PROFILE}")
set(target_kind "${ACB_TARGET_KIND}")
set(workspace_manifest_path "${ACB_WORKSPACE_MANIFEST_PATH}")

if(NOT target_kind)
message(FATAL_ERROR "TARGET_KIND not specified")
Expand All @@ -768,9 +769,12 @@ function(_add_cargo_build out_cargo_build_out_dir)
set (build_dir .)
endif()

unset(is_windows_msvc)
get_source_file_property(is_windows_msvc "${workspace_manifest_path}" CORROSION_PLATFORM_IS_WINDOWS_MSVC)

# For MSVC targets, don't mess with linker preferences.
# TODO: We still should probably make sure that rustc is using the correct cl.exe to link programs.
if (NOT MSVC)
if (NOT is_windows_msvc)
set(languages C CXX Fortran)

set(has_compiler OFF)
Expand Down
2 changes: 2 additions & 0 deletions cmake/CorrosionGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function(_generator_add_package_targets workspace_manifest_path package_manifest
PACKAGE ${package_name}
TARGET ${target_name}
MANIFEST_PATH "${manifest_path}"
WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
PROFILE "${profile}"
TARGET_KIND "lib"
BYPRODUCTS "${byproducts}"
Expand Down Expand Up @@ -130,6 +131,7 @@ function(_generator_add_package_targets workspace_manifest_path package_manifest
PACKAGE "${package_name}"
TARGET "${target_name}"
MANIFEST_PATH "${manifest_path}"
WORKSPACE_MANIFEST_PATH "${workspace_manifest_path}"
PROFILE "${profile}"
TARGET_KIND "bin"
BYPRODUCTS "${byproducts}"
Expand Down
14 changes: 4 additions & 10 deletions generator/src/subcommands/gen_cmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ pub fn invoke(
args: &crate::GeneratorSharedArgs,
matches: &ArgMatches,
) -> Result<(), Box<dyn std::error::Error>> {

let mut out_file: Box<dyn Write> = if let Some(path) = matches.value_of(OUT_FILE) {
let path = Path::new(path);
if let Some(parent) = path.parent() {
Expand Down Expand Up @@ -95,22 +94,17 @@ cmake_minimum_required(VERSION 3.15)
.flat_map(|package| {
let package2 = package.clone();
let ws_manifest_path = workspace_manifest_path.clone();
package
.targets
.clone()
.into_iter()
.filter_map(move |t| target::CargoTarget::from_metadata(package2.clone(), t, ws_manifest_path.clone()))
package.targets.clone().into_iter().filter_map(move |t| {
target::CargoTarget::from_metadata(package2.clone(), t, ws_manifest_path.clone())
})
})
.collect();

let cargo_profile = matches.value_of(PROFILE);

for target in &targets {
target
.emit_cmake_target(
&mut out_file,
cargo_profile,
)
.emit_cmake_target(&mut out_file, cargo_profile)
.unwrap();
}

Expand Down
22 changes: 12 additions & 10 deletions generator/src/subcommands/gen_cmake/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,27 @@ impl CargoTarget {
out_file: &mut dyn std::io::Write,
cargo_profile: Option<&str>,
) -> Result<(), Box<dyn Error>> {

let cargo_build_profile_option = if let Some(profile) = cargo_profile {
format!("PROFILE \"{}\"", profile)
} else {
String::default()
};

writeln!(out_file, "set(byproducts \"\")
writeln!(
out_file,
"set(byproducts \"\")
set(cargo_build_out_dir \"\")
set(archive_byproducts \"\")
set(shared_lib_byproduct \"\")
set(pdb_byproduct \"\")
set(bin_byproduct \"\")
")?;
"
)?;
let ws_manifest = self
.workspace_manifest_path
.to_str()
.expect("Non-utf8 path encountered")
.replace("\\", "/");

let target_kind = match self.target_type {
CargoTargetType::Library {
Expand Down Expand Up @@ -97,7 +104,6 @@ impl CargoTarget {
\"${{pdb_byproduct}}\"
)
",
// todo: check if this should be the workspace manifest (probably yes)
workspace_manifest_path = ws_manifest,
target_name = self.cargo_target.name,
has_staticlib = has_staticlib,
Expand All @@ -106,11 +112,6 @@ impl CargoTarget {
"lib"
}
CargoTargetType::Executable => {
let ws_manifest = self
.workspace_manifest_path
.to_str()
.expect("Non-utf8 path encountered")
.replace("\\", "/");
writeln!(
out_file,
"
Expand All @@ -134,6 +135,7 @@ impl CargoTarget {
PACKAGE \"{package_name}\"
TARGET \"{target_name}\"
MANIFEST_PATH \"{package_manifest_path}\"
WORKSPACE_MANIFEST_PATH \"{workspace_manifest_path}\"
{profile_option}
TARGET_KIND \"{target_kind}\"
BYPRODUCTS \"${{byproducts}}\"
Expand Down Expand Up @@ -167,11 +169,11 @@ impl CargoTarget {
package_name = self.cargo_package.name,
target_name = self.cargo_target.name,
package_manifest_path = self.cargo_package.manifest_path.as_str().replace("\\", "/"),
workspace_manifest_path = ws_manifest,
profile_option = cargo_build_profile_option,
target_kind = target_kind,

)?;
Ok(())
}

}

0 comments on commit f03517a

Please sign in to comment.