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//!
6453use std:: fs:: create_dir_all;
6554use std:: fs:: File ;
6655use std:: io:: { BufWriter , Error , ErrorKind , Result , Write } ;
67- use std:: path:: { Path , PathBuf } ;
56+ use std:: path:: Path ;
6857use 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
197173struct 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) ]
251227mod 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