Skip to content

Commit

Permalink
Merge pull request #604 from althonos/master
Browse files Browse the repository at this point in the history
Fix build script always using `python3` in OSX builds
  • Loading branch information
kngwyu authored Sep 15, 2019
2 parents a700e3d + b1ff620 commit ce4b3a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

* Make sure the right Python interpreter is used in OSX builds. [#604](https://github.com/PyO3/pyo3/pull/604)

## [0.8.0] - 2018-09-05

### Added
Expand Down
53 changes: 18 additions & 35 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct InterpreterConfig {
ld_version: String,
/// Prefix used for determining the directory of libpython
base_prefix: String,
executable: String,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -195,6 +196,7 @@ fn load_cross_compile_info() -> Result<(InterpreterConfig, HashMap<String, Strin
shared,
ld_version: "".to_string(),
base_prefix: "".to_string(),
executable: "".to_string(),
};

Ok((intepreter_config, fix_config_map(config_map)))
Expand Down Expand Up @@ -342,26 +344,22 @@ fn get_library_link_name(version: &PythonVersion, ld_version: &str) -> String {

#[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "windows"))]
fn get_rustc_link_lib(
version: &PythonVersion,
ld_version: &str,
enable_shared: bool,
) -> Result<String, String> {
if enable_shared {
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String, String> {
if config.shared {
Ok(format!(
"cargo:rustc-link-lib={}",
get_library_link_name(&version, ld_version)
get_library_link_name(&config.version, &config.ld_version)
))
} else {
Ok(format!(
"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")]
fn get_macos_linkmodel() -> Result<String, String> {
fn get_macos_linkmodel(config: &InterpreterConfig) -> Result<String, String> {
let script = r#"
import sysconfig
Expand All @@ -372,45 +370,37 @@ elif sysconfig.get_config_var("Py_ENABLE_SHARED"):
else:
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())
}

#[cfg(target_os = "macos")]
fn get_rustc_link_lib(
version: &PythonVersion,
ld_version: &str,
_: bool,
) -> Result<String, String> {
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String, String> {
// os x can be linked to a framework or static or dynamic, and
// 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!(
"cargo:rustc-link-lib=static={}",
get_library_link_name(&version, ld_version)
get_library_link_name(&config.version, &config.ld_version)
)),
"shared" => Ok(format!(
"cargo:rustc-link-lib={}",
get_library_link_name(&version, ld_version)
get_library_link_name(&config.version, &config.ld_version)
)),
"framework" => Ok(format!(
"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)),
}
}

#[cfg(target_os = "windows")]
fn get_rustc_link_lib(
version: &PythonVersion,
ld_version: &str,
_: bool,
) -> Result<String, String> {
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String, String> {
// Py_ENABLE_SHARED doesn't seem to be present on windows.
Ok(format!(
"cargo:rustc-link-lib=pythonXY:{}",
get_library_link_name(&version, ld_version)
get_library_link_name(&config.version, &config.ld_version)
))
}

Expand Down Expand Up @@ -483,7 +473,8 @@ print(json.dumps({
"libdir": sysconfig.get_config_var('LIBDIR'),
"ld_version": sysconfig.get_config_var('LDVERSION') or sysconfig.get_config_var('py_version_short'),
"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)?;
Expand All @@ -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();
if !is_extension_module || cfg!(target_os = "windows") {
println!(
"{}",
get_rustc_link_lib(
&interpreter_config.version,
&interpreter_config.ld_version,
interpreter_config.shared
)
.unwrap()
);
println!("{}", get_rustc_link_lib(&interpreter_config).unwrap());
if let Some(libdir) = &interpreter_config.libdir {
println!("cargo:rustc-link-search=native={}", libdir);
} else if cfg!(target_os = "windows") {
Expand Down

0 comments on commit ce4b3a5

Please sign in to comment.