Skip to content

Commit

Permalink
build: enable suppression of cargo:rustc-link-* lines
Browse files Browse the repository at this point in the history
PyOxidizer requires advanced control over the settings used to link
libpython. We recently implemented support for configuration files
defining explicit lines to emit from build scripts to give callers
control over what lines to emit from build scripts so use cases
like PyOxidizer's are feasible without hacks in PyO3's code base.

However, the default logic in `emit_link_config()` may not be
appropriate in scenarios where link settings are provided via this
"extra lines" mechanism. The default logic may prohibit use of or
interfere with desired settings provided externally.

This commit defines a new field on the interpreter config that
suppresses the emission of the default link control logic from the
`pyo3` build script. It effectively gives advanced consumers like
PyOxidizer full control over link logic while minimally polluting
PyO3's build logic.

I thought about implementing this control as a crate feature. But
given the expected target audience size of ~1, I thought a crate
feature was too visible for a power user feature and decided to
implement it via the configuration file.
  • Loading branch information
indygreg committed Aug 30, 2021
1 parent 04c77e3 commit c9c606f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
7 changes: 5 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 Down
1 change: 1 addition & 0 deletions pyo3-build-config/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ 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
27 changes: 27 additions & 0 deletions pyo3-build-config/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ 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
Expand Down Expand Up @@ -244,6 +256,7 @@ 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 @@ -284,6 +297,7 @@ 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() {
Expand All @@ -307,6 +321,9 @@ 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());
}
Expand Down Expand Up @@ -335,6 +352,7 @@ 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 @@ -374,6 +392,7 @@ 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")?;
Expand Down Expand Up @@ -950,6 +969,7 @@ 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 @@ -970,6 +990,7 @@ 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 @@ -1174,6 +1195,7 @@ 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();
Expand Down Expand Up @@ -1204,6 +1226,7 @@ mod tests {
major: 3,
minor: 10,
},
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
};
let mut buf: Vec<u8> = Vec::new();
Expand All @@ -1230,6 +1253,7 @@ 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 @@ -1341,6 +1365,7 @@ mod tests {
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 @@ -1407,6 +1432,7 @@ mod tests {
lib_name: None,
shared: true,
version: PythonVersion { major: 3, minor: 7 },
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
};

Expand All @@ -1426,6 +1452,7 @@ mod tests {
lib_name: None,
shared: true,
version: PythonVersion { major: 3, minor: 6 },
suppress_build_script_link_lines: false,
extra_build_script_lines: vec![],
};

Expand Down

0 comments on commit c9c606f

Please sign in to comment.