Skip to content

Commit f816786

Browse files
authored
Merge pull request #923 from davidhewitt/fix-cross-compile
Fix cross-compilation from unix to windows
2 parents 987e83b + 3fa1639 commit f816786

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

build.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ fn cross_compiling() -> Result<Option<CrossCompileConfig>> {
171171
///
172172
/// (hrm, this is sort of re-implementing what distutils does, except
173173
/// by passing command line args instead of referring to a python.h)
174-
#[cfg(not(target_os = "windows"))]
175174
static SYSCONFIG_FLAGS: [&str; 7] = [
176175
"Py_USING_UNICODE",
177176
"Py_UNICODE_WIDE",
@@ -443,8 +442,11 @@ fn load_cross_compile_info(
443442
/// Examine python's compile flags to pass to cfg by launching
444443
/// the interpreter and printing variables of interest from
445444
/// sysconfig.get_config_vars.
446-
#[cfg(not(target_os = "windows"))]
447445
fn get_config_vars(python_path: &Path) -> Result<HashMap<String, String>> {
446+
if env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
447+
return get_config_vars_windows(python_path);
448+
}
449+
448450
let mut script = "import sysconfig; \
449451
config = sysconfig.get_config_vars();"
450452
.to_owned();
@@ -478,8 +480,7 @@ fn get_config_vars(python_path: &Path) -> Result<HashMap<String, String>> {
478480
Ok(fix_config_map(all_vars))
479481
}
480482

481-
#[cfg(target_os = "windows")]
482-
fn get_config_vars(_: &Path) -> Result<HashMap<String, String>> {
483+
fn get_config_vars_windows(_: &Path) -> Result<HashMap<String, String>> {
483484
// sysconfig is missing all the flags on windows, so we can't actually
484485
// query the interpreter directly for its build flags.
485486
//
@@ -567,9 +568,15 @@ fn get_library_link_name(version: &PythonVersion, ld_version: &str) -> String {
567568
}
568569
}
569570

570-
#[cfg(not(target_os = "macos"))]
571-
#[cfg(not(target_os = "windows"))]
572571
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
572+
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
573+
"windows" => get_rustc_link_lib_windows(config),
574+
"macos" => get_rustc_link_lib_macos(config),
575+
_ => get_rustc_link_lib_unix(config),
576+
}
577+
}
578+
579+
fn get_rustc_link_lib_unix(config: &InterpreterConfig) -> Result<String> {
573580
if config.shared {
574581
Ok(format!(
575582
"cargo:rustc-link-lib={}",
@@ -583,7 +590,6 @@ fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
583590
}
584591
}
585592

586-
#[cfg(target_os = "macos")]
587593
fn get_macos_linkmodel(config: &InterpreterConfig) -> Result<String> {
588594
// PyPy 3.6 ships with a shared library, but doesn't have Py_ENABLE_SHARED.
589595
if config.version.implementation == PythonInterpreterKind::PyPy {
@@ -604,8 +610,7 @@ else:
604610
Ok(out.trim_end().to_owned())
605611
}
606612

607-
#[cfg(target_os = "macos")]
608-
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
613+
fn get_rustc_link_lib_macos(config: &InterpreterConfig) -> Result<String> {
609614
// os x can be linked to a framework or static or dynamic, and
610615
// Py_ENABLE_SHARED is wrong; framework means shared library
611616
let link_name = get_library_link_name(&config.version, &config.ld_version);
@@ -617,8 +622,7 @@ fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
617622
}
618623
}
619624

620-
#[cfg(target_os = "windows")]
621-
fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
625+
fn get_rustc_link_lib_windows(config: &InterpreterConfig) -> Result<String> {
622626
// Py_ENABLE_SHARED doesn't seem to be present on windows.
623627
Ok(format!(
624628
"cargo:rustc-link-lib=pythonXY:{}",
@@ -733,7 +737,7 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<String> {
733737
println!("{}", get_rustc_link_lib(&interpreter_config)?);
734738
if let Some(libdir) = &interpreter_config.libdir {
735739
println!("cargo:rustc-link-search=native={}", libdir);
736-
} else if cfg!(target_os = "windows") {
740+
} else if target_os == "windows" {
737741
println!(
738742
"cargo:rustc-link-search=native={}\\libs",
739743
interpreter_config.base_prefix
@@ -818,9 +822,6 @@ fn main() -> Result<()> {
818822
// Detecting if cross-compiling by checking if the target triple is different from the host
819823
// rustc's triple.
820824
let (interpreter_config, mut config_map) = if let Some(paths) = cross_compiling()? {
821-
// If cross compiling we need the path to the cross-compiled include dir and lib dir, else
822-
// fail quickly and loudly
823-
824825
load_cross_compile_info(paths)?
825826
} else {
826827
find_interpreter_and_get_config()?

0 commit comments

Comments
 (0)