Skip to content

Commit d1cf907

Browse files
committed
feat!: Use Path type for the output directory arg
This is a major public API change and requires a major crate SemVer bump. Remove the legacy `generate_implib()` function from the public crate API. The existing users should migrate to `generate_implib_for_target()`. Update the crate documentation and remove all mentions of `generate_implib()`. Bump the crate version to 0.2.0 (preliminary).
1 parent 7a191c4 commit d1cf907

File tree

3 files changed

+36
-75
lines changed

3 files changed

+36
-75
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "python3-dll-a"
3-
version = "0.1.2"
3+
version = "0.2.0"
44
edition = "2021"
55
description = "Standalone python3.dll import library generator"
66
repository = "https://github.com/ravenexp/python3-dll-a"

README.md

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,29 @@ to be present in `PATH` when targeting `*-pc-windows-msvc`.
1717
Example `build.rs` script
1818
-------------------------
1919

20-
The following script can be used to cross-compile Stable ABI
21-
PyO3 extension modules for Windows (64-bit MinGW-w64):
22-
23-
```rust
24-
fn main() {
25-
if std::env::var("TARGET").unwrap() == "x86_64-pc-windows-gnu" {
26-
let libdir = std::env::var("PYO3_CROSS_LIB_DIR")
27-
.expect("PYO3_CROSS_LIB_DIR is not set when cross-compiling");
28-
python3_dll_a::generate_implib(&libdir)
29-
.expect("python3.dll import library generator failed");
30-
}
31-
}
32-
```
33-
34-
A compatible `python3.dll` import library will be automatically created in
35-
the directory pointed by `PYO3_CROSS_LIB_DIR` environment variable.
36-
37-
If both 64-bit and 32-bit or GNU and MSVC ABI (cross-)compile target
38-
support is needed, the more generic `generate_implib_for_target()`
39-
function must be used:
40-
20+
The following Cargo build script can be used to cross-compile Stable ABI
21+
PyO3 extension modules for Windows (64/32-bit x86 or 64-bit ARM)
22+
using either MinGW-w64 or MSVC target environment ABI:
4123

4224
```rust
4325
fn main() {
4426
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
45-
let libdir = std::env::var("PYO3_CROSS_LIB_DIR")
27+
let cross_lib_dir = std::env::var_os("PYO3_CROSS_LIB_DIR")
4628
.expect("PYO3_CROSS_LIB_DIR is not set when cross-compiling");
4729
let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
4830
let env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap();
49-
python3_dll_a::generate_implib_for_target(&libdir, &arch, &env)
31+
32+
let libdir = std::path::Path::new(&cross_lib_dir);
33+
python3_dll_a::generate_implib_for_target(libdir, &arch, &env)
5034
.expect("python3.dll import library generator failed");
5135
}
5236
}
5337
```
5438

39+
A compatible `python3.dll` import library file named `python3.dll.a`
40+
or `python3.lib` will be automatically created in the directory
41+
pointed by the `PYO3_CROSS_LIB_DIR` environment variable.
42+
5543
Example `cargo build` invocation
5644
--------------------------------
5745

src/lib.rs

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,29 @@
1717
//! Example `build.rs` script
1818
//! -------------------------
1919
//!
20-
//! The following script can be used to cross-compile Stable ABI
21-
//! PyO3 extension modules for Windows (64-bit MinGW-w64):
22-
//!
23-
//! ```no_run
24-
//! fn main() {
25-
//! if std::env::var("TARGET").unwrap() == "x86_64-pc-windows-gnu" {
26-
//! let libdir = std::env::var("PYO3_CROSS_LIB_DIR")
27-
//! .expect("PYO3_CROSS_LIB_DIR is not set when cross-compiling");
28-
//! python3_dll_a::generate_implib(&libdir)
29-
//! .expect("python3.dll import library generator failed");
30-
//! }
31-
//! }
32-
//! ```
33-
//!
34-
//! A compatible `python3.dll` import library will be automatically created in
35-
//! the directory pointed by `PYO3_CROSS_LIB_DIR` environment variable.
36-
//!
37-
//! If both 64-bit and 32-bit or GNU and MSVC ABI (cross-)compile target
38-
//! support is needed, the more generic `generate_implib_for_target()`
39-
//! function must be used:
20+
//! The following Cargo build script can be used to cross-compile Stable ABI
21+
//! PyO3 extension modules for Windows (64/32-bit x86 or 64-bit ARM)
22+
//! using either MinGW-w64 or MSVC target environment ABI:
4023
//!
4124
//! ```no_run
4225
//! fn main() {
4326
//! if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
44-
//! let libdir = std::env::var("PYO3_CROSS_LIB_DIR")
27+
//! let cross_lib_dir = std::env::var_os("PYO3_CROSS_LIB_DIR")
4528
//! .expect("PYO3_CROSS_LIB_DIR is not set when cross-compiling");
4629
//! let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
4730
//! let env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap();
48-
//! python3_dll_a::generate_implib_for_target(&libdir, &arch, &env)
31+
//!
32+
//! let libdir = std::path::Path::new(&cross_lib_dir);
33+
//! python3_dll_a::generate_implib_for_target(libdir, &arch, &env)
4934
//! .expect("python3.dll import library generator failed");
5035
//! }
5136
//! }
5237
//! ```
5338
//!
39+
//! A compatible `python3.dll` import library file named `python3.dll.a`
40+
//! or `python3.lib` will be automatically created in the directory
41+
//! pointed by the `PYO3_CROSS_LIB_DIR` environment variable.
42+
//!
5443
//! Example `cargo build` invocation
5544
//! --------------------------------
5645
//!
@@ -64,7 +53,7 @@
6453
use std::fs::create_dir_all;
6554
use std::fs::File;
6655
use std::io::{BufWriter, Error, ErrorKind, Result, Write};
67-
use std::path::{Path, PathBuf};
56+
use std::path::Path;
6857
use std::process::Command;
6958

7059
/// Stable ABI Python DLL file name
@@ -96,18 +85,18 @@ const STABLE_ABI_DEFS: &str = include_str!("../Misc/stable_abi.txt");
9685
/// Generates `python3.dll` import library directly from the embedded
9786
/// Python Stable ABI definitions data for the specified compile target.
9887
///
99-
/// The import library file named `python3.dll.a` is created
88+
/// The import library file named `python3.dll.a` or `python3.lib` is created
10089
/// in directory `out_dir`.
10190
///
10291
/// The compile target architecture name (as in `CARGO_CFG_TARGET_ARCH`)
10392
/// is passed in `arch`.
10493
///
10594
/// The compile target environment ABI name (as in `CARGO_CFG_TARGET_ENV`)
10695
/// is passed in `env`.
107-
pub fn generate_implib_for_target(out_dir: &str, arch: &str, env: &str) -> Result<()> {
96+
pub fn generate_implib_for_target(out_dir: &Path, arch: &str, env: &str) -> Result<()> {
10897
create_dir_all(out_dir)?;
10998

110-
let mut defpath = PathBuf::from(out_dir);
99+
let mut defpath = out_dir.to_owned();
111100
defpath.push(DEF_FILE);
112101

113102
let stable_abi_exports = parse_stable_abi_defs(STABLE_ABI_DEFS);
@@ -144,8 +133,8 @@ pub fn generate_implib_for_target(out_dir: &str, arch: &str, env: &str) -> Resul
144133
/// Generates the complete `dlltool` executable invocation command.
145134
///
146135
/// Supports both LLVM and MinGW `dlltool` flavors.
147-
fn build_dlltool_command(dlltool: &str, arch: &str, defpath: &Path, out_dir: &str) -> Command {
148-
let mut libpath = PathBuf::from(out_dir);
136+
fn build_dlltool_command(dlltool: &str, arch: &str, defpath: &Path, out_dir: &Path) -> Command {
137+
let mut libpath = out_dir.to_owned();
149138
let mut command = Command::new(dlltool);
150139

151140
// Check whether we are using LLVM `dlltool` or MinGW `dlltool`.
@@ -180,19 +169,6 @@ fn build_dlltool_command(dlltool: &str, arch: &str, defpath: &Path, out_dir: &st
180169
command
181170
}
182171

183-
/// Generates `python3.dll` import library directly from the embedded
184-
/// Python Stable ABI definitions data for the default 64-bit MinGW-w64
185-
/// compile target.
186-
///
187-
/// The import library file named `python3.dll.a` is created
188-
/// in directory `out_dir`.
189-
///
190-
/// The import library is generated for the default `x86_64-pc-windows-gnu`
191-
/// cross-compile target.
192-
pub fn generate_implib(out_dir: &str) -> Result<()> {
193-
generate_implib_for_target(out_dir, "x86_64", "gnu")
194-
}
195-
196172
/// Exported DLL symbol definition
197173
struct DllExport {
198174
/// Export symbol name
@@ -249,6 +225,8 @@ fn write_export_defs(writer: &mut impl Write, dll_name: &str, exports: &[DllExpo
249225

250226
#[cfg(test)]
251227
mod tests {
228+
use std::path::PathBuf;
229+
252230
use super::*;
253231

254232
#[test]
@@ -259,8 +237,7 @@ mod tests {
259237
dir.push("x86_64-pc-windows-gnu");
260238
dir.push("python3-dll");
261239

262-
let out_dir = dir.to_str().unwrap();
263-
generate_implib(out_dir).unwrap();
240+
generate_implib_for_target(&dir, "x86_64", "gnu").unwrap();
264241
}
265242

266243
#[test]
@@ -270,8 +247,7 @@ mod tests {
270247
dir.push("i686-pc-windows-gnu");
271248
dir.push("python3-dll");
272249

273-
let out_dir = dir.to_str().unwrap();
274-
generate_implib_for_target(out_dir, "x86", "gnu").unwrap();
250+
generate_implib_for_target(&dir, "x86", "gnu").unwrap();
275251
}
276252

277253
#[test]
@@ -281,8 +257,7 @@ mod tests {
281257
dir.push("x86_64-pc-windows-msvc");
282258
dir.push("python3-dll");
283259

284-
let out_dir = dir.to_str().unwrap();
285-
generate_implib_for_target(out_dir, "x86_64", "msvc").unwrap();
260+
generate_implib_for_target(&dir, "x86_64", "msvc").unwrap();
286261
}
287262

288263
#[test]
@@ -292,8 +267,7 @@ mod tests {
292267
dir.push("i686-pc-windows-msvc");
293268
dir.push("python3-dll");
294269

295-
let out_dir = dir.to_str().unwrap();
296-
generate_implib_for_target(out_dir, "x86", "msvc").unwrap();
270+
generate_implib_for_target(&dir, "x86", "msvc").unwrap();
297271
}
298272

299273
#[test]
@@ -303,8 +277,7 @@ mod tests {
303277
dir.push("aarch64-pc-windows-msvc");
304278
dir.push("python3-dll");
305279

306-
let out_dir = dir.to_str().unwrap();
307-
generate_implib_for_target(out_dir, "aarch64", "msvc").unwrap();
280+
generate_implib_for_target(&dir, "aarch64", "msvc").unwrap();
308281
}
309282

310283
#[test]

0 commit comments

Comments
 (0)