/
import_lib.rs
59 lines (47 loc) · 1.96 KB
/
import_lib.rs
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
//! Optional `python3.dll` import library generator for Windows
use std::env;
use std::path::PathBuf;
use python3_dll_a::ImportLibraryGenerator;
use crate::errors::{Context, Result};
use super::{Architecture, OperatingSystem, PythonImplementation, PythonVersion, Triple};
/// Generates the `python3.dll` or `pythonXY.dll` import library for Windows targets.
///
/// Places the generated import library into the build script output directory
/// and returns the full library directory path.
///
/// Does nothing if the target OS is not Windows.
pub(super) fn generate_import_lib(
target: &Triple,
py_impl: PythonImplementation,
py_version: Option<PythonVersion>,
) -> Result<Option<String>> {
if target.operating_system != OperatingSystem::Windows {
return Ok(None);
}
let out_dir =
env::var_os("OUT_DIR").expect("generate_import_lib() must be called from a build script");
// Put the newly created import library into the build script output directory.
let mut out_lib_dir = PathBuf::from(out_dir);
out_lib_dir.push("lib");
// Convert `Architecture` enum to rustc `target_arch` option format.
let arch = match target.architecture {
// i686, i586, etc.
Architecture::X86_32(_) => "x86".to_string(),
other => other.to_string(),
};
let env = target.environment.to_string();
let implementation = match py_impl {
PythonImplementation::CPython => python3_dll_a::PythonImplementation::CPython,
PythonImplementation::PyPy => python3_dll_a::PythonImplementation::PyPy,
};
ImportLibraryGenerator::new(&arch, &env)
.version(py_version.map(|v| (v.major, v.minor)))
.implementation(implementation)
.generate(&out_lib_dir)
.context("failed to generate python3.dll import library")?;
let out_lib_dir_string = out_lib_dir
.to_str()
.ok_or("build directory is not a valid UTF-8 string")?
.to_owned();
Ok(Some(out_lib_dir_string))
}