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

Fixup lib_name when using PYO3_CONFIG_FILE #2404

Merged
merged 4 commits into from May 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
70 changes: 42 additions & 28 deletions pyo3-build-config/src/impl_.rs
Expand Up @@ -440,6 +440,14 @@ print("mingw", get_platform().startswith("mingw"))
let version = version.ok_or("missing value for version")?;
let implementation = implementation.unwrap_or(PythonImplementation::CPython);
let abi3 = abi3.unwrap_or(false);
// Fixup lib_name if it's not set
let lib_name = lib_name.or_else(|| {
if let Ok(Ok(target)) = env::var("TARGET").map(|target| target.parse::<Triple>()) {
default_lib_name_for_target(version, implementation, abi3, &target)
} else {
None
}
});

Ok(InterpreterConfig {
implementation,
Expand All @@ -456,28 +464,24 @@ print("mingw", get_platform().startswith("mingw"))
})
}

#[cfg(feature = "python3-dll-a")]
#[allow(clippy::unnecessary_wraps)]
pub fn fixup_import_libs(&mut self) -> Result<()> {
let target = target_triple_from_env();
if self.lib_name.is_none() && target.operating_system == OperatingSystem::Windows {
self.lib_name = Some(default_lib_name_windows(
self.version,
self.implementation,
self.abi3,
false,
));
}
pub fn generate_import_libs(&mut self) -> Result<()> {
// Auto generate python3.dll import libraries for Windows targets.
#[cfg(feature = "python3-dll-a")]
{
if self.lib_dir.is_none() {
let py_version = if self.abi3 { None } else { Some(self.version) };
self.lib_dir = self::import_lib::generate_import_lib(&target, py_version)?;
}
if self.lib_dir.is_none() {
let target = target_triple_from_env();
let py_version = if self.abi3 { None } else { Some(self.version) };
self.lib_dir = import_lib::generate_import_lib(&target, py_version)?;
}
Ok(())
}

#[cfg(not(feature = "python3-dll-a"))]
#[allow(clippy::unnecessary_wraps)]
pub fn generate_import_libs(&mut self) -> Result<()> {
Ok(())
}

#[doc(hidden)]
/// Serialize the `InterpreterConfig` and print it to the environment for Cargo to pass along
/// to dependent packages during build time.
Expand Down Expand Up @@ -1412,18 +1416,8 @@ fn default_cross_compile(cross_compile_config: &CrossCompileConfig) -> Result<In
.implementation
.unwrap_or(PythonImplementation::CPython);

let lib_name = if cross_compile_config.target.operating_system == OperatingSystem::Windows {
Some(default_lib_name_windows(
version,
implementation,
abi3,
false,
))
} else if is_linking_libpython_for_target(&cross_compile_config.target) {
Some(default_lib_name_unix(version, implementation, None))
} else {
None
};
let lib_name =
default_lib_name_for_target(version, implementation, abi3, &cross_compile_config.target);

let mut lib_dir = cross_compile_config.lib_dir_string();

Expand Down Expand Up @@ -1532,6 +1526,26 @@ fn load_cross_compile_config(
// This contains only the limited ABI symbols.
const WINDOWS_ABI3_LIB_NAME: &str = "python3";

fn default_lib_name_for_target(
version: PythonVersion,
implementation: PythonImplementation,
abi3: bool,
target: &Triple,
) -> Option<String> {
if target.operating_system == OperatingSystem::Windows {
Some(default_lib_name_windows(
version,
implementation,
abi3,
false,
))
} else if is_linking_libpython_for_target(target) {
Some(default_lib_name_unix(version, implementation, None))
} else {
None
}
}

fn default_lib_name_windows(
version: PythonVersion,
implementation: PythonImplementation,
Expand Down
2 changes: 1 addition & 1 deletion pyo3-build-config/src/lib.rs
Expand Up @@ -178,7 +178,7 @@ pub mod pyo3_build_script_impl {
pub fn resolve_interpreter_config() -> Result<InterpreterConfig> {
if !CONFIG_FILE.is_empty() {
let mut interperter_config = InterpreterConfig::from_reader(Cursor::new(CONFIG_FILE))?;
interperter_config.fixup_import_libs()?;
interperter_config.generate_import_libs()?;
Ok(interperter_config)
} else if let Some(interpreter_config) = make_cross_compile_config()? {
// This is a cross compile and need to write the config file.
Expand Down