Fix build script always using python3 in OSX builds

This commit is contained in:
Martin Larralde 2019-09-15 14:39:22 +02:00
parent a700e3d7cf
commit 9facb20d89

View file

@ -43,6 +43,7 @@ struct InterpreterConfig {
ld_version: String, ld_version: String,
/// Prefix used for determining the directory of libpython /// Prefix used for determining the directory of libpython
base_prefix: String, base_prefix: String,
executable: String,
} }
#[derive(Deserialize, Debug, Clone, PartialEq)] #[derive(Deserialize, Debug, Clone, PartialEq)]
@ -195,6 +196,7 @@ fn load_cross_compile_info() -> Result<(InterpreterConfig, HashMap<String, Strin
shared, shared,
ld_version: "".to_string(), ld_version: "".to_string(),
base_prefix: "".to_string(), base_prefix: "".to_string(),
executable: "".to_string(),
}; };
Ok((intepreter_config, fix_config_map(config_map))) Ok((intepreter_config, fix_config_map(config_map)))
@ -342,26 +344,22 @@ fn get_library_link_name(version: &PythonVersion, ld_version: &str) -> String {
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
fn get_rustc_link_lib( fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String, String> {
version: &PythonVersion, if config.shared {
ld_version: &str,
enable_shared: bool,
) -> Result<String, String> {
if enable_shared {
Ok(format!( Ok(format!(
"cargo:rustc-link-lib={}", "cargo:rustc-link-lib={}",
get_library_link_name(&version, ld_version) get_library_link_name(&config.version, &config.ld_version)
)) ))
} else { } else {
Ok(format!( Ok(format!(
"cargo:rustc-link-lib=static={}", "cargo:rustc-link-lib=static={}",
get_library_link_name(&version, ld_version) get_library_link_name(&config.version, &config.ld_version)
)) ))
} }
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
fn get_macos_linkmodel() -> Result<String, String> { fn get_macos_linkmodel(config: &InterpreterConfig) -> Result<String, String> {
let script = r#" let script = r#"
import sysconfig import sysconfig
@ -372,45 +370,37 @@ elif sysconfig.get_config_var("Py_ENABLE_SHARED"):
else: else:
print("static") print("static")
"#; "#;
let out = run_python_script(&PYTHON_INTERPRETER, script).unwrap(); let out = run_python_script(&config.executable, script).unwrap();
Ok(out.trim_end().to_owned()) Ok(out.trim_end().to_owned())
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
fn get_rustc_link_lib( fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String, String> {
version: &PythonVersion,
ld_version: &str,
_: bool,
) -> Result<String, String> {
// os x can be linked to a framework or static or dynamic, and // os x can be linked to a framework or static or dynamic, and
// Py_ENABLE_SHARED is wrong; framework means shared library // Py_ENABLE_SHARED is wrong; framework means shared library
match get_macos_linkmodel().unwrap().as_ref() { match get_macos_linkmodel(config).unwrap().as_ref() {
"static" => Ok(format!( "static" => Ok(format!(
"cargo:rustc-link-lib=static={}", "cargo:rustc-link-lib=static={}",
get_library_link_name(&version, ld_version) get_library_link_name(&config.version, &config.ld_version)
)), )),
"shared" => Ok(format!( "shared" => Ok(format!(
"cargo:rustc-link-lib={}", "cargo:rustc-link-lib={}",
get_library_link_name(&version, ld_version) get_library_link_name(&config.version, &config.ld_version)
)), )),
"framework" => Ok(format!( "framework" => Ok(format!(
"cargo:rustc-link-lib={}", "cargo:rustc-link-lib={}",
get_library_link_name(&version, ld_version) get_library_link_name(&config.version, &config.ld_version)
)), )),
other => Err(format!("unknown linkmodel {}", other)), other => Err(format!("unknown linkmodel {}", other)),
} }
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn get_rustc_link_lib( fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String, String> {
version: &PythonVersion,
ld_version: &str,
_: bool,
) -> Result<String, String> {
// Py_ENABLE_SHARED doesn't seem to be present on windows. // Py_ENABLE_SHARED doesn't seem to be present on windows.
Ok(format!( Ok(format!(
"cargo:rustc-link-lib=pythonXY:{}", "cargo:rustc-link-lib=pythonXY:{}",
get_library_link_name(&version, ld_version) get_library_link_name(&config.version, &config.ld_version)
)) ))
} }
@ -483,7 +473,8 @@ print(json.dumps({
"libdir": sysconfig.get_config_var('LIBDIR'), "libdir": sysconfig.get_config_var('LIBDIR'),
"ld_version": sysconfig.get_config_var('LDVERSION') or sysconfig.get_config_var('py_version_short'), "ld_version": sysconfig.get_config_var('LDVERSION') or sysconfig.get_config_var('py_version_short'),
"base_prefix": base_prefix, "base_prefix": base_prefix,
"shared": PYPY or bool(sysconfig.get_config_var('Py_ENABLE_SHARED')) "shared": PYPY or bool(sysconfig.get_config_var('Py_ENABLE_SHARED')),
"executable": sys.executable,
})) }))
"#; "#;
let json = run_python_script(interpreter, script)?; let json = run_python_script(interpreter, script)?;
@ -502,15 +493,7 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<(String), String>
let is_extension_module = env::var_os("CARGO_FEATURE_EXTENSION_MODULE").is_some(); let is_extension_module = env::var_os("CARGO_FEATURE_EXTENSION_MODULE").is_some();
if !is_extension_module || cfg!(target_os = "windows") { if !is_extension_module || cfg!(target_os = "windows") {
println!( println!("{}", get_rustc_link_lib(&interpreter_config).unwrap());
"{}",
get_rustc_link_lib(
&interpreter_config.version,
&interpreter_config.ld_version,
interpreter_config.shared
)
.unwrap()
);
if let Some(libdir) = &interpreter_config.libdir { if let Some(libdir) = &interpreter_config.libdir {
println!("cargo:rustc-link-search=native={}", libdir); println!("cargo:rustc-link-search=native={}", libdir);
} else if cfg!(target_os = "windows") { } else if cfg!(target_os = "windows") {