Skip to content

Commit

Permalink
Simplify dylib install name link-args code
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Feb 20, 2021
1 parent 6f1cf13 commit cafb7c1
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ fn compile_target(
shared_args.push("--target");
shared_args.push(target);

let mut rustc_args: Vec<String> = context.rustc_extra_args.clone();
let mut rustc_args: Vec<&str> = context
.rustc_extra_args
.iter()
.map(String::as_str)
.collect();

let mut rust_flags = env::var_os("RUSTFLAGS").unwrap_or_default();

Expand All @@ -129,30 +133,33 @@ fn compile_target(
}
}

let module_name = &context.module_name;
let so_filename = match python_interpreter {
Some(python_interpreter) => python_interpreter.get_library_name(module_name),
// abi3
None => {
format!("{base}.abi3.so", base = module_name)
}
};
let macos_dylib_install_name = format!("link-args=-Wl,-install_name,@rpath/{}", so_filename);

// https://github.com/PyO3/pyo3/issues/88#issuecomment-337744403
if context.target.is_macos() {
if let BridgeModel::Bindings(_) | BridgeModel::BindingsAbi3(_, _) = bindings_crate {
let mac_args = &["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"];
rustc_args.extend(mac_args.iter().map(|x| x.to_string()));
let module_name = &context.module_name;
let so_filename = match python_interpreter {
Some(python_interpreter) => python_interpreter.get_library_name(module_name),
// abi3
None => {
format!("{base}.abi3.so", base = module_name)
}
};
rustc_args.push("-C".to_string());
rustc_args.push(format!(
"link-args=-Wl,-install_name,@rpath/{}",
so_filename
));
let mac_args = &[
"-C",
"link-arg=-undefined",
"-C",
"link-arg=dynamic_lookup",
"-C",
&macos_dylib_install_name,
];
rustc_args.extend(mac_args);
}
}

if context.strip {
rustc_args.push("-C".to_string());
rustc_args.push("link-arg=-s".to_string());
rustc_args.extend(&["-C", "link-arg=-s"]);
}

let pythonxy_lib_folder;
Expand All @@ -168,19 +175,17 @@ fn compile_target(
let python_interpreter = python_interpreter
.expect("Must have a python interpreter for building abi3 on windows");
pythonxy_lib_folder = format!("native={}", python_interpreter.libs_dir.display());
rustc_args.push("-L".to_string());
rustc_args.push(pythonxy_lib_folder);
rustc_args.extend(&["-L", &pythonxy_lib_folder]);
}
}

let cargo_args = vec!["rustc", "--message-format", "json"];

let build_args: Vec<_> = cargo_args
.iter()
.map(|x| x.to_string())
.chain(shared_args.into_iter().map(|x| x.to_string()))
.chain(vec!["--".to_string()])
.chain(rustc_args)
.chain(&shared_args)
.chain(&["--"])
.chain(&rustc_args)
.collect();
let command_str = build_args
.iter()
Expand Down

0 comments on commit cafb7c1

Please sign in to comment.