1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
//! Standalone `python3(y).dll` import library generator
//! ====================================================
//!
//! Generates import libraries for the Python DLL
//! (either `python3.dll` or `python3y.dll`)
//! for MinGW-w64 and MSVC (cross-)compile targets.
//!
//! See <https://docs.python.org/3/c-api/stable.html> for the Stable ABI details.
//!
//! This crate **does not require** Python 3 distribution files
//! to be present on the (cross-)compile host system.
//!
//! **Note:** MSVC cross-compile targets require LLVM binutils
//! to be available on the host system.
//! More specifically, `python3-dll-a` requires `llvm-dlltool` executable
//! to be present in `PATH` when targeting `*-pc-windows-msvc` from Linux.
//!
//! PyO3 integration
//! ----------------
//!
//! Since version **0.16.4**, the `pyo3` crate implements support
//! for the Stable ABI Python DLL import library generation via
//! its new `generate-abi3-import-lib` feature.
//!
//! In this configuration, `python3-dll-a` becomes a `pyo3` crate dependency
//! and is automatically invoked by its build script in both native
//! and cross compilation scenarios.
//!
//! ### Example `Cargo.toml` usage for a PyO3 extension module
//!
//! ```toml
//! [dependencies]
//! pyo3 = { version = "0.16.4", features = ["extension-module", "abi3-py37", "generate-abi3-import-lib"] }
//! ```
//!
//! Standalone build script usage
//! -----------------------------
//!
//! If an older `pyo3` crate version is used, or a different Python bindings
//! library is required, `python3-dll-a` can be used directly
//! from the crate build script.
//!
//! The examples below assume using an older version of PyO3.
//!
//! ### Example `build.rs` script
//!
//! The following cargo build script can be used to cross-compile Stable ABI
//! PyO3 extension modules for Windows (64/32-bit x86 or 64-bit ARM)
//! using either MinGW-w64 or MSVC target environment ABI:
//!
//! ```no_run
//! fn main() {
//! if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
//! let cross_lib_dir = std::env::var_os("PYO3_CROSS_LIB_DIR")
//! .expect("PYO3_CROSS_LIB_DIR is not set when cross-compiling");
//! let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
//! let env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap();
//!
//! let libdir = std::path::Path::new(&cross_lib_dir);
//! python3_dll_a::generate_implib_for_target(libdir, &arch, &env)
//! .expect("python3.dll import library generator failed");
//! }
//! }
//! ```
//!
//! A compatible `python3.dll` import library file named `python3.dll.a`
//! or `python3.lib` will be automatically created in the directory
//! pointed by the `PYO3_CROSS_LIB_DIR` environment variable.
//!
//! ### Example `cargo build` invocation
//!
//! ```sh
//! PYO3_CROSS_LIB_DIR=target/python3-dll cargo build --target x86_64-pc-windows-gnu
//! ```
#![deny(missing_docs)]
#![allow(clippy::needless_doctest_main)]
use std::ffi::OsStr;
use std::fs::{create_dir_all, write};
use std::io::{Error, ErrorKind, Result};
use std::path::Path;
use std::process::Command;
/// Import library file extension for the GNU environment ABI (MinGW-w64)
const IMPLIB_EXT_GNU: &str = ".dll.a";
/// Import library file extension for the MSVC environment ABI
const IMPLIB_EXT_MSVC: &str = ".lib";
/// Canonical MinGW-w64 `dlltool` program name
const DLLTOOL_GNU: &str = "x86_64-w64-mingw32-dlltool";
/// Canonical MinGW-w64 `dlltool` program name (32-bit version)
const DLLTOOL_GNU_32: &str = "i686-w64-mingw32-dlltool";
/// Canonical `dlltool` program name for the MSVC environment ABI (LLVM dlltool)
const DLLTOOL_MSVC: &str = "llvm-dlltool";
/// Canonical `lib` program name for the MSVC environment ABI (MSVC lib.exe)
const LIB_MSVC: &str = "lib.exe";
/// Windows import library generator for Python
///
/// Generates `python3.dll` or `pythonXY.dll` import library directly from the
/// embedded Python ABI definitions data for the specified compile target.
#[derive(Debug, Clone)]
pub struct ImportLibraryGenerator {
/// The compile target architecture name (as in `CARGO_CFG_TARGET_ARCH`)
arch: String,
// The compile target environment ABI name (as in `CARGO_CFG_TARGET_ENV`)
env: String,
/// Python major and minor version
version: Option<(u8, u8)>,
}
impl ImportLibraryGenerator {
/// Creates a new import library generator for the specified compile target
pub fn new(arch: &str, env: &str) -> Self {
Self {
arch: arch.to_string(),
env: env.to_string(),
version: None,
}
}
/// Set python major and minor version
pub fn version(&mut self, version: Option<(u8, u8)>) -> &mut Self {
self.version = version;
self
}
/// Generates the import library in `out_dir`
pub fn generate(&self, out_dir: &Path) -> Result<()> {
create_dir_all(out_dir)?;
let mut defpath = out_dir.to_owned();
let (def_file, def_file_content) = match self.version {
None => ("python3.def", include_str!("python3.def")),
Some((3, 7)) => ("python37.def", include_str!("python37.def")),
Some((3, 8)) => ("python38.def", include_str!("python38.def")),
Some((3, 9)) => ("python39.def", include_str!("python39.def")),
Some((3, 10)) => ("python310.def", include_str!("python310.def")),
Some((3, 11)) => ("python311.def", include_str!("python311.def")),
_ => return Err(Error::new(ErrorKind::Other, "Unsupported Python version")),
};
defpath.push(def_file);
write(&defpath, def_file_content)?;
// Try to guess the `dlltool` executable name from the target triple.
let mut command = match (self.arch.as_str(), self.env.as_str()) {
// 64-bit MinGW-w64 (aka x86_64-pc-windows-gnu)
("x86_64", "gnu") => self.build_dlltool_command(
DLLTOOL_GNU,
&def_file.replace(".def", IMPLIB_EXT_GNU),
&defpath,
out_dir,
),
// 32-bit MinGW-w64 (aka i686-pc-windows-gnu)
("x86", "gnu") => self.build_dlltool_command(
DLLTOOL_GNU_32,
&def_file.replace(".def", IMPLIB_EXT_GNU),
&defpath,
out_dir,
),
// MSVC ABI (multiarch)
(_, "msvc") => {
let implib_file = def_file.replace(".def", IMPLIB_EXT_MSVC);
if let Some(command) = find_lib_exe(&self.arch) {
self.build_dlltool_command(
command.get_program(),
&implib_file,
&defpath,
out_dir,
)
} else {
self.build_dlltool_command(DLLTOOL_MSVC, &implib_file, &defpath, out_dir)
}
}
_ => {
let msg = format!(
"Unsupported target arch '{}' or env ABI '{}'",
self.arch, self.env
);
return Err(Error::new(ErrorKind::Other, msg));
}
};
// Run the selected `dlltool` executable to generate the import library.
let status = command.status()?;
if status.success() {
Ok(())
} else {
let msg = format!("{:?} failed with {}", command, status);
Err(Error::new(ErrorKind::Other, msg))
}
}
/// Generates the complete `dlltool` executable invocation command.
///
/// Supports Visual Studio `lib.exe`, LLVM and MinGW `dlltool` flavors.
fn build_dlltool_command(
&self,
dlltool: impl AsRef<OsStr>,
implib_file: &str,
defpath: &Path,
out_dir: &Path,
) -> Command {
let dlltool = dlltool.as_ref();
let mut libpath = out_dir.to_owned();
let mut command = if self.env == "msvc" {
find_lib_exe(&self.arch).unwrap_or_else(|| Command::new(dlltool))
} else {
Command::new(dlltool)
};
// Check whether we are using LLVM `dlltool` or MinGW `dlltool`.
if dlltool == DLLTOOL_MSVC {
libpath.push(implib_file);
// LLVM tools use their own target architecture names...
let machine = match self.arch.as_str() {
"x86_64" => "i386:x86-64",
"x86" => "i386",
"aarch64" => "arm64",
arch => arch,
};
command
.arg("-m")
.arg(machine)
.arg("-d")
.arg(defpath)
.arg("-l")
.arg(libpath);
} else if Path::new(dlltool).file_name() == Some(LIB_MSVC.as_ref()) {
libpath.push(implib_file);
// lib.exe use their own target architecure names...
let machine = match self.arch.as_str() {
"x86_64" => "X64",
"x86" => "X86",
"aarch64" => "ARM64",
arch => arch,
};
command
.arg(format!("/MACHINE:{}", machine))
.arg(format!("/DEF:{}", defpath.display()))
.arg(format!("/OUT:{}", libpath.display()));
} else {
libpath.push(implib_file);
command
.arg("--input-def")
.arg(defpath)
.arg("--output-lib")
.arg(libpath);
}
command
}
}
/// Generates `python3.dll` import library directly from the embedded
/// Python Stable ABI definitions data for the specified compile target.
///
/// The import library file named `python3.dll.a` or `python3.lib` is created
/// in directory `out_dir`.
///
/// The compile target architecture name (as in `CARGO_CFG_TARGET_ARCH`)
/// is passed in `arch`.
///
/// The compile target environment ABI name (as in `CARGO_CFG_TARGET_ENV`)
/// is passed in `env`.
pub fn generate_implib_for_target(out_dir: &Path, arch: &str, env: &str) -> Result<()> {
ImportLibraryGenerator::new(arch, env).generate(out_dir)
}
/// Find Visual Studio lib.exe on Windows
#[cfg(windows)]
fn find_lib_exe(arch: &str) -> Option<Command> {
let target = match arch {
"x86_64" => "x86_64-pc-windows-msvc",
"x86" => "i686-pc-windows-msvc",
"aarch64" => "aarch64-pc-windows-msvc",
_ => return None,
};
cc::windows_registry::find(target, LIB_MSVC)
}
#[cfg(not(windows))]
fn find_lib_exe(_arch: &str) -> Option<Command> {
None
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
#[cfg(unix)]
#[test]
fn generate() {
// FIXME: Use "target/<arch>" dirs for temporary files.
let mut dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
dir.push("target");
dir.push("x86_64-pc-windows-gnu");
dir.push("python3-dll");
for minor in 7..=11 {
ImportLibraryGenerator::new("x86_64", "gnu")
.version(Some((3, minor)))
.generate(&dir)
.unwrap();
}
}
#[cfg(unix)]
#[test]
fn generate_gnu32() {
let mut dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
dir.push("target");
dir.push("i686-pc-windows-gnu");
dir.push("python3-dll");
generate_implib_for_target(&dir, "x86", "gnu").unwrap();
}
#[test]
fn generate_msvc() {
let mut dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
dir.push("target");
dir.push("x86_64-pc-windows-msvc");
dir.push("python3-dll");
generate_implib_for_target(&dir, "x86_64", "msvc").unwrap();
for minor in 7..=11 {
ImportLibraryGenerator::new("x86_64", "msvc")
.version(Some((3, minor)))
.generate(&dir)
.unwrap();
}
}
#[test]
fn generate_msvc32() {
let mut dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
dir.push("target");
dir.push("i686-pc-windows-msvc");
dir.push("python3-dll");
generate_implib_for_target(&dir, "x86", "msvc").unwrap();
}
#[test]
fn generate_msvc_arm64() {
let mut dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
dir.push("target");
dir.push("aarch64-pc-windows-msvc");
dir.push("python3-dll");
generate_implib_for_target(&dir, "aarch64", "msvc").unwrap();
}
}