Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build configuration settings to facilitate complete link control #1793

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn rustc_minor_version() -> Option<u32> {
pieces.next()?.parse().ok()
}

fn emit_cargo_configuration(interpreter_config: &InterpreterConfig) -> Result<()> {
fn emit_link_config(interpreter_config: &InterpreterConfig) -> Result<()> {
let target_os = cargo_env_var("CARGO_CFG_TARGET_OS").unwrap();
let is_extension_module = cargo_env_var("CARGO_FEATURE_EXTENSION_MODULE").is_some();
if target_os == "windows" || target_os == "android" || !is_extension_module {
Expand Down Expand Up @@ -132,7 +132,10 @@ fn configure_pyo3() -> Result<()> {
ensure_target_pointer_width(&interpreter_config)?;
ensure_auto_initialize_ok(&interpreter_config)?;

emit_cargo_configuration(&interpreter_config)?;
if !interpreter_config.suppress_build_script_link_lines {
emit_link_config(&interpreter_config)?;
}

interpreter_config.emit_pyo3_cfgs();

let rustc_minor_version = rustc_minor_version().unwrap_or(0);
Expand All @@ -152,6 +155,11 @@ fn configure_pyo3() -> Result<()> {
println!("cargo:rustc-cfg=addr_of");
}

// Extra lines come last, to support last write wins.
for line in &interpreter_config.extra_build_script_lines {
println!("{}", line);
}

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions pyo3-build-config/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub fn abi3_config() -> Option<InterpreterConfig> {
pointer_width: None,
executable: None,
shared: true,
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
});
}
}
Expand Down
59 changes: 58 additions & 1 deletion pyo3-build-config/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ pub struct InterpreterConfig {
///
/// Serialized to `build_flags`.
pub build_flags: BuildFlags,

/// Whether to suppress emitting of `cargo:rustc-link-*` lines from the build script.
///
/// Typically, `pyo3`'s build script will emit `cargo:rustc-link-lib=` and
/// `cargo:rustc-link-search=` lines derived from other fields in this struct. In
/// advanced building configurations, the default logic to derive these lines may not
/// be sufficient. This field can be set to `Some(true)` to suppress the emission
/// of these lines.
///
/// If suppression is enabled, `extra_build_script_lines` should contain equivalent
/// functionality or else a build failure is likely.
pub suppress_build_script_link_lines: bool,

/// Additional lines to `println!()` from Cargo build scripts.
///
/// This field can be populated to enable the `pyo3` crate to emit additional lines from its
/// its Cargo build script.
///
/// This crate doesn't populate this field itself. Rather, it is intended to be used with
/// externally provided config files to give them significant control over how the crate
/// is build/configured.
///
/// Serialized to multiple `extra_build_script_line` values.
pub extra_build_script_lines: Vec<String>,
}

impl InterpreterConfig {
Expand Down Expand Up @@ -232,6 +256,8 @@ print("mingw", get_platform() == "mingw")
executable: map.get("executable").cloned(),
pointer_width: Some(calcsize_pointer * 8),
build_flags: BuildFlags::from_interpreter(interpreter)?.fixup(version, implementation),
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
})
}

Expand Down Expand Up @@ -271,6 +297,8 @@ print("mingw", get_platform() == "mingw")
let mut executable = None;
let mut pointer_width = None;
let mut build_flags = None;
let mut suppress_build_script_link_lines = None;
let mut extra_build_script_lines = vec![];

for (i, line) in lines.enumerate() {
let line = line.context("failed to read line from config")?;
Expand All @@ -293,6 +321,12 @@ print("mingw", get_platform() == "mingw")
"executable" => parse_value!(executable, value),
"pointer_width" => parse_value!(pointer_width, value),
"build_flags" => parse_value!(build_flags, value),
"suppress_build_script_link_lines" => {
parse_value!(suppress_build_script_link_lines, value)
}
"extra_build_script_line" => {
extra_build_script_lines.push(value.to_string());
}
unknown => bail!("unknown config key `{}`", unknown),
}
}
Expand All @@ -318,6 +352,8 @@ print("mingw", get_platform() == "mingw")
}
.fixup(version, implementation)
}),
suppress_build_script_link_lines: suppress_build_script_link_lines.unwrap_or(false),
extra_build_script_lines,
})
}

Expand Down Expand Up @@ -356,6 +392,11 @@ print("mingw", get_platform() == "mingw")
write_option_line!(executable)?;
write_option_line!(pointer_width)?;
write_line!(build_flags)?;
write_line!(suppress_build_script_link_lines)?;
for line in &self.extra_build_script_lines {
writeln!(writer, "extra_build_script_line={}", line)
.context("failed to write extra_build_script_line")?;
}
Ok(())
}
}
Expand Down Expand Up @@ -928,6 +969,8 @@ fn load_cross_compile_from_sysconfigdata(
executable: None,
pointer_width,
build_flags: BuildFlags::from_config_map(&sysconfig_data).fixup(version, implementation),
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
})
}

Expand All @@ -947,6 +990,8 @@ fn windows_hardcoded_cross_compile(
executable: None,
pointer_width: None,
build_flags: BuildFlags::windows_hardcoded(),
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
})
}

Expand Down Expand Up @@ -1150,6 +1195,8 @@ mod tests {
lib_dir: Some("lib_dir".into()),
shared: true,
version: MINIMUM_SUPPORTED_VERSION,
suppress_build_script_link_lines: true,
extra_build_script_lines: vec!["cargo:test1".to_string(), "cargo:test2".to_string()],
};
let mut buf: Vec<u8> = Vec::new();
config.to_writer(&mut buf).unwrap();
Expand Down Expand Up @@ -1179,6 +1226,8 @@ mod tests {
major: 3,
minor: 10,
},
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
};
let mut buf: Vec<u8> = Vec::new();
config.to_writer(&mut buf).unwrap();
Expand All @@ -1204,6 +1253,8 @@ mod tests {
executable: None,
pointer_width: None,
build_flags: BuildFlags::default(),
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
}
)
}
Expand Down Expand Up @@ -1313,7 +1364,9 @@ mod tests {
lib_dir: Some("C:\\some\\path".into()),
executable: None,
pointer_width: None,
build_flags: BuildFlags::windows_hardcoded()
build_flags: BuildFlags::windows_hardcoded(),
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
}
);
}
Expand Down Expand Up @@ -1379,6 +1432,8 @@ mod tests {
lib_name: None,
shared: true,
version: PythonVersion { major: 3, minor: 7 },
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
};

fixup_config_for_abi3(&mut config, Some(PythonVersion { major: 3, minor: 6 })).unwrap();
Expand All @@ -1397,6 +1452,8 @@ mod tests {
lib_name: None,
shared: true,
version: PythonVersion { major: 3, minor: 6 },
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
};

assert!(
Expand Down