| 1 |
use anyhow::{Context, Result}; |
| 2 |
use clap::Parser; |
| 3 |
use expect_test::Expect; |
| 4 |
use flate2::read::GzDecoder; |
| 5 |
use fs_err::File; |
| 6 |
use maturin::pyproject_toml::{SdistGenerator, ToolMaturin}; |
| 7 |
use maturin::{BuildOptions, CargoOptions, PlatformTag}; |
| 8 |
use pretty_assertions::assert_eq; |
| 9 |
use std::collections::BTreeSet; |
| 10 |
use std::io::Read; |
| 11 |
use std::path::{Path, PathBuf}; |
| 12 |
use tar::Archive; |
| 13 |
use time::OffsetDateTime; |
| 14 |
use zip::ZipArchive; |
| 15 |
|
| 16 |
/// Tries to compile a sample crate (pyo3-pure) for musl, |
| 17 |
/// given that rustup and the the musl target are installed |
| 18 |
/// |
| 19 |
/// The bool in the Ok() response says whether the test was actually run |
| 20 |
pub fn test_musl() -> Result<bool> { |
| 21 |
use anyhow::bail; |
| 22 |
use fs_err as fs; |
| 23 |
use fs_err::File; |
| 24 |
use goblin::elf::Elf; |
| 25 |
use std::io::ErrorKind; |
| 26 |
use std::process::Command; |
| 27 |
|
| 28 |
let get_target_list = Command::new("rustup") |
| 29 |
.args(["target", "list", "--installed"]) |
| 30 |
.output(); |
| 31 |
|
| 32 |
match get_target_list { |
| 33 |
Ok(output) => { |
| 34 |
if output.status.success() { |
| 35 |
let has_musl = String::from_utf8_lossy(&output.stdout) |
| 36 |
.lines() |
| 37 |
.any(|line| line.trim() == "x86_64-unknown-linux-musl"); |
| 38 |
if !has_musl { |
| 39 |
return Ok(false); |
| 40 |
} |
| 41 |
} else { |
| 42 |
bail!( |
| 43 |
"`rustup target list --installed` failed with status {}", |
| 44 |
output.status |
| 45 |
) |
| 46 |
} |
| 47 |
} |
| 48 |
// Ignore installations without rustup |
| 49 |
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(false), |
| 50 |
Err(err) => return Err(err.into()), |
| 51 |
}; |
| 52 |
|
| 53 |
// The first arg gets ignored |
| 54 |
let options: BuildOptions = BuildOptions::try_parse_from([ |
| 55 |
"build", |
| 56 |
"--manifest-path", |
| 57 |
"test-crates/hello-world/Cargo.toml", |
| 58 |
"--interpreter", |
| 59 |
"python3", |
| 60 |
"--target", |
| 61 |
"x86_64-unknown-linux-musl", |
| 62 |
"--compatibility", |
| 63 |
"linux", |
| 64 |
"--quiet", |
| 65 |
"--target-dir", |
| 66 |
"test-crates/targets/test_musl", |
| 67 |
"--out", |
| 68 |
"test-crates/wheels/test_musl", |
| 69 |
])?; |
| 70 |
|
| 71 |
let build_context = options |
| 72 |
.into_build_context() |
| 73 |
.release(false) |
| 74 |
.strip(cfg!(feature = "faster-tests")) |
| 75 |
.editable(false) |
| 76 |
.build()?; |
| 77 |
let built_lib = |
| 78 |
PathBuf::from("test-crates/targets/test_musl/x86_64-unknown-linux-musl/debug/hello-world"); |
| 79 |
if built_lib.is_file() { |
| 80 |
fs::remove_file(&built_lib)?; |
| 81 |
} |
| 82 |
let wheels = build_context.build_wheels()?; |
| 83 |
assert_eq!(wheels.len(), 1); |
| 84 |
|
| 85 |
// Ensure that we've actually built for musl |
| 86 |
assert!(built_lib.is_file()); |
| 87 |
let mut file = File::open(built_lib)?; |
| 88 |
let mut buffer = Vec::new(); |
| 89 |
file.read_to_end(&mut buffer)?; |
| 90 |
let elf = Elf::parse(&buffer)?; |
| 91 |
assert_eq!(elf.libraries, Vec::<&str>::new()); |
| 92 |
|
| 93 |
Ok(true) |
| 94 |
} |
| 95 |
|
| 96 |
/// Test that we ignore non-existent Cargo.lock file listed by `cargo package --list`, |
| 97 |
/// which seems to only occur with workspaces. |
| 98 |
/// See https://github.com/rust-lang/cargo/issues/7938#issuecomment-593280660 and |
| 99 |
/// https://github.com/PyO3/maturin/issues/449 |
| 100 |
pub fn test_workspace_cargo_lock() -> Result<()> { |
| 101 |
// The first arg gets ignored |
| 102 |
let options: BuildOptions = BuildOptions::try_parse_from([ |
| 103 |
"build", |
| 104 |
"--manifest-path", |
| 105 |
"test-crates/workspace/py/Cargo.toml", |
| 106 |
"--compatibility", |
| 107 |
"linux", |
| 108 |
"--quiet", |
| 109 |
"--target-dir", |
| 110 |
"test-crates/targets/test_workspace_cargo_lock", |
| 111 |
"--out", |
| 112 |
"test-crates/wheels/test_workspace_cargo_lock", |
| 113 |
])?; |
| 114 |
|
| 115 |
let build_context = options |
| 116 |
.into_build_context() |
| 117 |
.release(false) |
| 118 |
.strip(false) |
| 119 |
.editable(false) |
| 120 |
.build()?; |
| 121 |
let source_distribution = build_context.build_source_distribution()?; |
| 122 |
assert!(source_distribution.is_some()); |
| 123 |
|
| 124 |
Ok(()) |
| 125 |
} |
| 126 |
|
| 127 |
pub fn test_source_distribution( |
| 128 |
package: impl AsRef<Path>, |
| 129 |
sdist_generator: SdistGenerator, |
| 130 |
expected_files: Expect, |
| 131 |
expected_cargo_toml: Option<(&Path, Expect)>, |
| 132 |
unique_name: &str, |
| 133 |
) -> Result<()> { |
| 134 |
let manifest_path = package.as_ref().join("Cargo.toml"); |
| 135 |
let sdist_directory = Path::new("test-crates").join("wheels").join(unique_name); |
| 136 |
|
| 137 |
let build_options = BuildOptions { |
| 138 |
out: Some(sdist_directory), |
| 139 |
cargo: CargoOptions { |
| 140 |
manifest_path: Some(manifest_path), |
| 141 |
quiet: true, |
| 142 |
target_dir: Some(PathBuf::from( |
| 143 |
"test-crates/targets/test_workspace_cargo_lock", |
| 144 |
)), |
| 145 |
..Default::default() |
| 146 |
}, |
| 147 |
..Default::default() |
| 148 |
}; |
| 149 |
|
| 150 |
let mut build_context = build_options |
| 151 |
.into_build_context() |
| 152 |
.release(false) |
| 153 |
.strip(false) |
| 154 |
.editable(false) |
| 155 |
.sdist_only(true) |
| 156 |
.build()?; |
| 157 |
|
| 158 |
// Override the sdist generator for testing |
| 159 |
let mut pyproject_toml = build_context.pyproject_toml.take().unwrap(); |
| 160 |
let mut tool = pyproject_toml.tool.clone().unwrap_or_default(); |
| 161 |
if let Some(ref mut tool_maturin) = tool.maturin { |
| 162 |
tool_maturin.sdist_generator = sdist_generator; |
| 163 |
} else { |
| 164 |
tool.maturin = Some(ToolMaturin { |
| 165 |
sdist_generator, |
| 166 |
..Default::default() |
| 167 |
}); |
| 168 |
} |
| 169 |
pyproject_toml.tool = Some(tool); |
| 170 |
build_context.pyproject_toml = Some(pyproject_toml); |
| 171 |
|
| 172 |
let (path, _) = build_context |
| 173 |
.build_source_distribution()? |
| 174 |
.context("Failed to build source distribution")?; |
| 175 |
|
| 176 |
let tar_gz = fs_err::File::open(path)?; |
| 177 |
let tar = GzDecoder::new(tar_gz); |
| 178 |
let mut archive = Archive::new(tar); |
| 179 |
let mut files = BTreeSet::new(); |
| 180 |
let mut file_count = 0; |
| 181 |
let mut cargo_toml = None; |
| 182 |
for entry in archive.entries()? { |
| 183 |
let mut entry = entry?; |
| 184 |
files.insert(format!("{}", entry.path()?.display())); |
| 185 |
file_count += 1; |
| 186 |
if let Some(cargo_toml_path) = expected_cargo_toml.as_ref().map(|(p, _)| *p) { |
| 187 |
if entry.path()? == cargo_toml_path { |
| 188 |
let mut contents = String::new(); |
| 189 |
entry.read_to_string(&mut contents)?; |
| 190 |
cargo_toml = Some(contents); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
expected_files.assert_debug_eq(&files); |
| 195 |
assert_eq!( |
| 196 |
file_count, |
| 197 |
files.len(), |
| 198 |
"duplicated files found in sdist: {:?}", |
| 199 |
files |
| 200 |
); |
| 201 |
|
| 202 |
if let Some((cargo_toml_path, expected)) = expected_cargo_toml { |
| 203 |
let cargo_toml = cargo_toml |
| 204 |
.with_context(|| format!("{} not found in sdist", cargo_toml_path.display()))?; |
| 205 |
expected.assert_eq(&cargo_toml.replace("\r\n", "\n")); |
| 206 |
} |
| 207 |
Ok(()) |
| 208 |
} |
| 209 |
|
| 210 |
fn build_wheel_files(package: impl AsRef<Path>, unique_name: &str) -> Result<ZipArchive<File>> { |
| 211 |
let manifest_path = package.as_ref().join("Cargo.toml"); |
| 212 |
let wheel_directory = Path::new("test-crates").join("wheels").join(unique_name); |
| 213 |
|
| 214 |
let build_options = BuildOptions { |
| 215 |
out: Some(wheel_directory), |
| 216 |
cargo: CargoOptions { |
| 217 |
manifest_path: Some(manifest_path), |
| 218 |
quiet: true, |
| 219 |
target_dir: Some(PathBuf::from(format!("test-crates/targets/{unique_name}"))), |
| 220 |
..Default::default() |
| 221 |
}, |
| 222 |
platform_tag: vec![PlatformTag::Linux], |
| 223 |
..Default::default() |
| 224 |
}; |
| 225 |
|
| 226 |
let build_context = build_options |
| 227 |
.into_build_context() |
| 228 |
.release(false) |
| 229 |
.strip(false) |
| 230 |
.editable(false) |
| 231 |
.build()?; |
| 232 |
let wheels = build_context |
| 233 |
.build_wheels() |
| 234 |
.context("Failed to build wheels")?; |
| 235 |
assert!(!wheels.is_empty()); |
| 236 |
let (wheel_path, _) = &wheels[0]; |
| 237 |
|
| 238 |
let wheel = ZipArchive::new(File::open(wheel_path)?)?; |
| 239 |
Ok(wheel) |
| 240 |
} |
| 241 |
|
| 242 |
pub fn check_wheel_mtimes( |
| 243 |
package: impl AsRef<Path>, |
| 244 |
expected_mtime: Vec<OffsetDateTime>, |
| 245 |
unique_name: &str, |
| 246 |
) -> Result<()> { |
| 247 |
let mut wheel = build_wheel_files(package, unique_name)?; |
| 248 |
let mut mtimes = BTreeSet::<OffsetDateTime>::new(); |
| 249 |
|
| 250 |
for idx in 0..wheel.len() { |
| 251 |
let mtime = wheel.by_index(idx)?.last_modified().unwrap().try_into()?; |
| 252 |
mtimes.insert(mtime); |
| 253 |
} |
| 254 |
|
| 255 |
assert_eq!(mtimes, expected_mtime.into_iter().collect::<BTreeSet<_>>()); |
| 256 |
|
| 257 |
Ok(()) |
| 258 |
} |
| 259 |
|
| 260 |
pub fn check_wheel_files( |
| 261 |
package: impl AsRef<Path>, |
| 262 |
expected_files: Vec<&str>, |
| 263 |
unique_name: &str, |
| 264 |
) -> Result<()> { |
| 265 |
let wheel = build_wheel_files(package, unique_name)?; |
| 266 |
let drop_platform_specific_files = |file: &&str| -> bool { |
| 267 |
!matches!(Path::new(file).extension(), Some(ext) if ext == "pyc" || ext == "pyd" || ext == "so") |
| 268 |
}; |
| 269 |
assert_eq!( |
| 270 |
wheel |
| 271 |
.file_names() |
| 272 |
.filter(drop_platform_specific_files) |
| 273 |
.collect::<BTreeSet<_>>(), |
| 274 |
expected_files.into_iter().collect::<BTreeSet<_>>() |
| 275 |
); |
| 276 |
Ok(()) |
| 277 |
} |
| 278 |
|
| 279 |
pub fn abi3_python_interpreter_args() -> Result<()> { |
| 280 |
// Case 1: maturin build without `-i`, should work |
| 281 |
let options = BuildOptions::try_parse_from(vec![ |
| 282 |
"build", |
| 283 |
"--manifest-path", |
| 284 |
"test-crates/pyo3-pure/Cargo.toml", |
| 285 |
"--quiet", |
| 286 |
])?; |
| 287 |
let result = options |
| 288 |
.into_build_context() |
| 289 |
.release(false) |
| 290 |
.strip(cfg!(feature = "faster-tests")) |
| 291 |
.editable(false) |
| 292 |
.build(); |
| 293 |
assert!(result.is_ok()); |
| 294 |
|
| 295 |
// Case 2: maturin build -i python3.10, should work because python3.10 is in bundled sysconfigs |
| 296 |
let options = BuildOptions::try_parse_from(vec![ |
| 297 |
"build", |
| 298 |
"--manifest-path", |
| 299 |
"test-crates/pyo3-pure/Cargo.toml", |
| 300 |
"--quiet", |
| 301 |
"-i", |
| 302 |
"python3.10", |
| 303 |
])?; |
| 304 |
let result = options |
| 305 |
.into_build_context() |
| 306 |
.release(false) |
| 307 |
.strip(cfg!(feature = "faster-tests")) |
| 308 |
.editable(false) |
| 309 |
.build(); |
| 310 |
assert!(result.is_ok()); |
| 311 |
|
| 312 |
// Windows is a bit different so we exclude it from case 3 & 4 |
| 313 |
|
| 314 |
// Case 3: maturin build -i python2.7, errors because python2.7 is supported |
| 315 |
#[cfg(not(windows))] |
| 316 |
{ |
| 317 |
let options = BuildOptions::try_parse_from(vec![ |
| 318 |
"build", |
| 319 |
"--manifest-path", |
| 320 |
"test-crates/pyo3-pure/Cargo.toml", |
| 321 |
"--quiet", |
| 322 |
"-i", |
| 323 |
"python2.7", |
| 324 |
])?; |
| 325 |
let result = options |
| 326 |
.into_build_context() |
| 327 |
.release(false) |
| 328 |
.strip(cfg!(feature = "faster-tests")) |
| 329 |
.editable(false) |
| 330 |
.build(); |
| 331 |
assert!(result.is_err()); |
| 332 |
|
| 333 |
// Case 4: maturin build -i python-does-not-exists, errors because python executable is not found |
| 334 |
let options = BuildOptions::try_parse_from(vec![ |
| 335 |
"build", |
| 336 |
"--manifest-path", |
| 337 |
"test-crates/pyo3-pure/Cargo.toml", |
| 338 |
"--quiet", |
| 339 |
"-i", |
| 340 |
"python-does-not-exists", |
| 341 |
])?; |
| 342 |
let result = options |
| 343 |
.into_build_context() |
| 344 |
.release(false) |
| 345 |
.strip(cfg!(feature = "faster-tests")) |
| 346 |
.editable(false) |
| 347 |
.build(); |
| 348 |
assert!(result.is_err()); |
| 349 |
} |
| 350 |
|
| 351 |
Ok(()) |
| 352 |
} |
| 353 |
|
| 354 |
pub fn abi3_without_version() -> Result<()> { |
| 355 |
// The first argument is ignored by clap |
| 356 |
let cli = vec![ |
| 357 |
"build", |
| 358 |
"--manifest-path", |
| 359 |
"test-crates/pyo3-abi3-without-version/Cargo.toml", |
| 360 |
"--quiet", |
| 361 |
"--interpreter", |
| 362 |
"python3", |
| 363 |
"--target-dir", |
| 364 |
"test-targets/wheels/abi3_without_version", |
| 365 |
]; |
| 366 |
|
| 367 |
let options = BuildOptions::try_parse_from(cli)?; |
| 368 |
let result = options |
| 369 |
.into_build_context() |
| 370 |
.release(false) |
| 371 |
.strip(cfg!(feature = "faster-tests")) |
| 372 |
.editable(false) |
| 373 |
.build(); |
| 374 |
assert!(result.is_ok()); |
| 375 |
|
| 376 |
Ok(()) |
| 377 |
} |