-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathmain.rs
583 lines (555 loc) · 20.9 KB
/
main.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries
//! as python packages. This file contains the CLI and keyring integration.
//!
//! Run with --help for usage information
use anyhow::{bail, Context, Result};
#[cfg(feature = "zig")]
use cargo_zigbuild::Zig;
use clap::{CommandFactory, Parser, Subcommand};
#[cfg(feature = "scaffolding")]
use maturin::{ci::GenerateCI, init_project, new_project, GenerateProjectOptions};
use maturin::{
develop, write_dist_info, BridgeModel, BuildOptions, CargoOptions, PathWriter, PlatformTag,
PythonInterpreter, Target,
};
#[cfg(feature = "upload")]
use maturin::{upload_ui, PublishOpt};
use std::env;
#[cfg(unix)]
use std::ffi::CString;
use std::io;
use std::path::PathBuf;
#[cfg(windows)]
use std::process::Command;
use tracing::debug;
#[derive(Debug, Parser)]
#[command(
version,
name = env!("CARGO_PKG_NAME"),
display_order = 1,
after_help = "Visit https://maturin.rs to learn more about maturin."
)]
#[cfg_attr(feature = "cargo-clippy", allow(clippy::large_enum_variant))]
/// Build and publish crates with pyo3, rust-cpython and cffi bindings as well
/// as rust binaries as python packages
enum Opt {
#[command(name = "build", alias = "b")]
/// Build the crate into python packages
Build {
/// Build artifacts in release mode, with optimizations
#[arg(short = 'r', long)]
release: bool,
/// Strip the library for minimum file size
#[arg(long)]
strip: bool,
/// Build a source distribution
#[arg(long)]
sdist: bool,
#[command(flatten)]
build: BuildOptions,
},
#[cfg(feature = "upload")]
#[command(name = "publish")]
/// Build and publish the crate as python packages to pypi
Publish {
/// Do not pass --release to cargo
#[arg(long)]
debug: bool,
/// Do not strip the library for minimum file size
#[arg(long = "no-strip")]
no_strip: bool,
/// Don't build a source distribution
#[arg(long = "no-sdist")]
no_sdist: bool,
#[command(flatten)]
publish: PublishOpt,
#[command(flatten)]
build: BuildOptions,
},
#[command(name = "list-python")]
/// Search and list the available python installations
ListPython {
#[arg(long)]
target: Option<String>,
},
#[command(name = "develop", alias = "dev")]
/// Install the crate as module in the current virtualenv
///
/// Note that this command doesn't create entrypoints
Develop {
/// Which kind of bindings to use
#[arg(
short = 'b',
long = "bindings",
alias = "binding-crate",
value_parser = ["pyo3", "pyo3-ffi", "rust-cpython", "cffi", "uniffi", "bin"]
)]
bindings: Option<String>,
/// Pass --release to cargo
#[arg(short = 'r', long)]
release: bool,
/// Strip the library for minimum file size
#[arg(long)]
strip: bool,
/// Install extra requires aka. optional dependencies
///
/// Use as `--extras=extra1,extra2`
#[arg(
short = 'E',
long,
value_delimiter = ',',
action = clap::ArgAction::Append
)]
extras: Vec<String>,
#[command(flatten)]
cargo_options: CargoOptions,
},
/// Build only a source distribution (sdist) without compiling.
///
/// Building a source distribution requires a pyproject.toml with a `[build-system]` table.
///
/// This command is a workaround for [pypa/pip#6041](https://github.com/pypa/pip/issues/6041)
#[command(name = "sdist")]
SDist {
#[arg(short = 'm', long = "manifest-path")]
/// The path to the Cargo.toml
manifest_path: Option<PathBuf>,
/// The directory to store the built wheels in. Defaults to a new "wheels"
/// directory in the project's target directory
#[arg(short, long)]
out: Option<PathBuf>,
},
/// Create a new cargo project in an existing directory
#[cfg(feature = "scaffolding")]
#[command(name = "init")]
InitProject {
/// Project path
path: Option<String>,
#[command(flatten)]
options: GenerateProjectOptions,
},
/// Create a new cargo project
#[cfg(feature = "scaffolding")]
#[command(name = "new")]
NewProject {
/// Project path
path: String,
#[command(flatten)]
options: GenerateProjectOptions,
},
#[cfg(feature = "scaffolding")]
#[command(name = "generate-ci")]
GenerateCI(GenerateCI),
/// Upload python packages to pypi
///
/// It is mostly similar to `twine upload`, but can only upload python wheels
/// and source distributions.
#[cfg(feature = "upload")]
#[command(name = "upload")]
Upload {
#[command(flatten)]
publish: PublishOpt,
/// The python packages to upload
#[arg(value_name = "FILE")]
files: Vec<PathBuf>,
},
/// Run python from the virtualenv in `.venv` in the current or any parent folder
///
/// On linux/mac, `maturin run <args>` is equivalent to `.venv/bin/python <args>`
#[cfg(feature = "maturin-run")]
#[structopt(external_subcommand)]
Run(Vec<String>),
/// Backend for the PEP 517 integration. Not for human consumption
///
/// The commands are meant to be called from the python PEP 517
#[command(subcommand)]
Pep517(Pep517Command),
/// Generate shell completions
#[command(name = "completions", hide = true)]
Completions {
#[arg(value_name = "SHELL")]
shell: clap_complete_command::Shell,
},
/// Zig linker wrapper
#[cfg(feature = "zig")]
#[command(subcommand, hide = true)]
Zig(Zig),
}
/// Backend for the PEP 517 integration. Not for human consumption
///
/// The commands are meant to be called from the python PEP 517
#[derive(Debug, Subcommand)]
#[command(name = "pep517", hide = true)]
enum Pep517Command {
/// The implementation of prepare_metadata_for_build_wheel
#[command(name = "write-dist-info")]
WriteDistInfo {
#[command(flatten)]
build_options: BuildOptions,
/// The metadata_directory argument to prepare_metadata_for_build_wheel
#[arg(long = "metadata-directory")]
metadata_directory: PathBuf,
/// Strip the library for minimum file size
#[arg(long)]
strip: bool,
},
#[command(name = "build-wheel")]
/// Implementation of build_wheel
///
/// --release and --strip are currently unused by the PEP 517 implementation
BuildWheel {
#[command(flatten)]
build_options: BuildOptions,
/// Strip the library for minimum file size
#[arg(long)]
strip: bool,
/// Build editable wheels
#[arg(long)]
editable: bool,
},
/// The implementation of build_sdist
#[command(name = "write-sdist")]
WriteSDist {
/// The sdist_directory argument to build_sdist
#[arg(long = "sdist-directory")]
sdist_directory: PathBuf,
#[arg(short = 'm', long = "manifest-path", value_name = "PATH")]
/// The path to the Cargo.toml
manifest_path: Option<PathBuf>,
},
}
fn detect_venv(target: &Target) -> Result<PathBuf> {
match (env::var_os("VIRTUAL_ENV"), env::var_os("CONDA_PREFIX")) {
(Some(dir), None) => return Ok(PathBuf::from(dir)),
(None, Some(dir)) => return Ok(PathBuf::from(dir)),
(Some(_), Some(_)) => {
bail!("Both VIRTUAL_ENV and CONDA_PREFIX are set. Please unset one of them")
}
(None, None) => {
// No env var, try finding .venv
}
};
let current_dir = env::current_dir().context("Failed to detect current directory ಠ_ಠ")?;
// .venv in the current or any parent directory
for dir in current_dir.ancestors() {
let dot_venv = dir.join(".venv");
if dot_venv.is_dir() {
if !dot_venv.join("pyvenv.cfg").is_file() {
bail!(
"Expected {} to be a virtual environment, but pyvenv.cfg is missing",
dot_venv.display()
);
}
let python = target.get_venv_python(&dot_venv);
if !python.is_file() {
bail!(
"Your virtualenv at {} is broken. It contains a pyvenv.cfg but no python at {}",
dot_venv.display(),
python.display()
);
}
debug!("Found a virtualenv named .venv at {}", dot_venv.display());
return Ok(dot_venv);
}
}
bail!(
"Couldn't find a virtualenv or conda environment, but you need one to use this command. \
For maturin to find your virtualenv you need to either set VIRTUAL_ENV (through activate), \
set CONDA_PREFIX (through conda activate) or have a virtualenv called .venv in the current \
or any parent folder. \
See https://virtualenv.pypa.io/en/latest/index.html on how to use virtualenv or \
use `maturin build` and `pip install <path/to/wheel>` instead."
)
}
/// Dispatches into the native implementations of the PEP 517 functions
///
/// The last line of stdout is used as return value from the python part of the implementation
fn pep517(subcommand: Pep517Command) -> Result<()> {
match subcommand {
Pep517Command::WriteDistInfo {
build_options,
metadata_directory,
strip,
} => {
assert_eq!(build_options.interpreter.len(), 1);
let context = build_options.into_build_context(true, strip, false)?;
// Since afaik all other PEP 517 backends also return linux tagged wheels, we do so too
let tags = match context.bridge() {
BridgeModel::Bindings(..) | BridgeModel::Bin(Some(..)) => {
vec![context.interpreter[0].get_tag(
&context.target,
&[PlatformTag::Linux],
context.universal2,
)?]
}
BridgeModel::BindingsAbi3(major, minor) => {
let platform = context
.target
.get_platform_tag(&[PlatformTag::Linux], context.universal2)?;
vec![format!("cp{major}{minor}-abi3-{platform}")]
}
BridgeModel::Bin(None) | BridgeModel::Cffi | BridgeModel::UniFfi => {
context
.target
.get_universal_tags(&[PlatformTag::Linux], context.universal2)?
.1
}
};
let mut writer = PathWriter::from_path(metadata_directory);
write_dist_info(&mut writer, &context.metadata21, &tags)?;
println!("{}", context.metadata21.get_dist_info_dir().display());
}
Pep517Command::BuildWheel {
build_options,
strip,
editable,
} => {
let build_context = build_options.into_build_context(true, strip, editable)?;
let wheels = build_context.build_wheels()?;
assert_eq!(wheels.len(), 1);
println!("{}", wheels[0].0.to_str().unwrap());
}
Pep517Command::WriteSDist {
sdist_directory,
manifest_path,
} => {
let build_options = BuildOptions {
out: Some(sdist_directory),
cargo: CargoOptions {
manifest_path,
..Default::default()
},
..Default::default()
};
let build_context = build_options.into_build_context(false, false, false)?;
let (path, _) = build_context
.build_source_distribution()?
.context("Failed to build source distribution, pyproject.toml not found")?;
println!("{}", path.file_name().unwrap().to_str().unwrap());
}
};
Ok(())
}
/// `maturin run` implementation. Looks for a virtualenv .venv in cwd or any parent and execve
/// python from there with args
#[cfg(feature = "maturin-run")]
fn python_run(mut args: Vec<String>) -> Result<()> {
// Not sure if it's even feasible to support other target triples here given the restrictions
// with argument parsing
let target = Target::from_target_triple(None)?;
let venv_dir = detect_venv(&target)?;
let python = target.get_venv_python(venv_dir);
// We get the args in the format ["run", "arg1", "arg2"] but python shouldn't see the "run"
assert_eq!(args[0], "run");
args.remove(0);
#[cfg(unix)]
{
debug!("launching (execv) {}", python.display());
// Sorry for all the to_string_lossy
// https://stackoverflow.com/a/38948854/3549270
let executable_c_str = CString::new(python.to_string_lossy().as_bytes())
.context("Failed to convert executable path")?;
// Python needs first entry to be the binary (as it is common on unix) so python will
// pick up the pyvenv.cfg
args.insert(0, python.to_string_lossy().to_string());
let args_c_string = args
.iter()
.map(|arg| {
CString::new(arg.as_bytes()).context("Failed to convert executable argument")
})
.collect::<Result<Vec<CString>>>()?;
// We replace the current process with the new process is it's like actually just running
// the real thing.
// Note the that this may launch a python script, a native binary or anything else
nix::unistd::execv(&executable_c_str, &args_c_string)
.context("Failed to launch process")?;
unreachable!()
}
#[cfg(windows)]
{
debug!("launching (new process) {}", python.display());
// TODO: What's the correct equivalent of execv on windows?
let status = Command::new(python)
.args(args.iter())
.status()
.context("Failed to launch process")?;
std::process::exit(
status
.code()
.context("Process didn't return an exit code")?,
)
}
#[cfg(not(any(unix, windows)))]
{
compile_error!("Unsupported Platform, please disable the maturin-run feature.")
}
}
fn run() -> Result<()> {
#[cfg(feature = "log")]
tracing_subscriber::fmt::init();
#[cfg(feature = "zig")]
{
// Allow symlink `maturin` to `ar` to invoke `zig ar`
// See https://github.com/messense/cargo-zigbuild/issues/52
let mut args = env::args();
let program_path = PathBuf::from(args.next().expect("no program path"));
let program_name = program_path.file_stem().expect("no program name");
if program_name.eq_ignore_ascii_case("ar") {
let zig = Zig::Ar {
args: args.collect(),
};
zig.execute()?;
return Ok(());
}
}
let opt = Opt::parse();
match opt {
Opt::Build {
build,
release,
strip,
sdist,
} => {
let build_context = build.into_build_context(release, strip, false)?;
if sdist {
build_context
.build_source_distribution()?
.context("Failed to build source distribution, pyproject.toml not found")?;
}
let wheels = build_context.build_wheels()?;
assert!(!wheels.is_empty());
}
#[cfg(feature = "upload")]
Opt::Publish {
build,
publish,
debug,
no_strip,
no_sdist,
} => {
let build_context = build.into_build_context(!debug, !no_strip, false)?;
if !build_context.release {
eprintln!("⚠️ Warning: You're publishing debug wheels");
}
let mut wheels = build_context.build_wheels()?;
if !no_sdist {
if let Some(sd) = build_context.build_source_distribution()? {
wheels.push(sd);
}
}
let items = wheels.into_iter().map(|wheel| wheel.0).collect::<Vec<_>>();
upload_ui(&items, &publish)?
}
Opt::ListPython { target } => {
let found = if target.is_some() {
let target = Target::from_target_triple(target)?;
PythonInterpreter::find_by_target(&target, None)
} else {
let target = Target::from_target_triple(None)?;
// We don't know the targeted bindings yet, so we use the most lenient
PythonInterpreter::find_all(&target, &BridgeModel::Cffi, None)?
};
println!("🐍 {} python interpreter found:", found.len());
for interpreter in found {
println!(" - {interpreter}");
}
}
Opt::Develop {
bindings,
release,
strip,
extras,
cargo_options,
} => {
let venv_dir = match (env::var_os("VIRTUAL_ENV"), env::var_os("CONDA_PREFIX")) {
(Some(dir), None) => PathBuf::from(dir),
(None, Some(dir)) => PathBuf::from(dir),
(Some(_), Some(_)) => {
bail!("Both VIRTUAL_ENV and CONDA_PREFIX are set. Please unset one of them")
}
(None, None) => {
bail!(
"You need to be inside a virtualenv or conda environment to use develop \
(neither VIRTUAL_ENV nor CONDA_PREFIX are set). \
See https://virtualenv.pypa.io/en/latest/index.html on how to use virtualenv or \
use `maturin build` and `pip install <path/to/wheel>` instead."
)
}
};
develop(bindings, cargo_options, &venv_dir, release, strip, extras)?;
}
Opt::SDist { manifest_path, out } => {
let build_options = BuildOptions {
out,
cargo: CargoOptions {
manifest_path,
..Default::default()
},
..Default::default()
};
let build_context = build_options.into_build_context(false, false, false)?;
build_context
.build_source_distribution()?
.context("Failed to build source distribution, pyproject.toml not found")?;
}
#[cfg(feature = "maturin-run")]
Opt::Run(args) => python_run(args)?,
Opt::Pep517(subcommand) => pep517(subcommand)?,
#[cfg(feature = "scaffolding")]
Opt::InitProject { path, options } => init_project(path, options)?,
#[cfg(feature = "scaffolding")]
Opt::NewProject { path, options } => new_project(path, options)?,
#[cfg(feature = "scaffolding")]
Opt::GenerateCI(generate_ci) => generate_ci.execute()?,
#[cfg(feature = "upload")]
Opt::Upload { publish, files } => {
if files.is_empty() {
eprintln!("⚠️ Warning: No files given, exiting.");
return Ok(());
}
upload_ui(&files, &publish)?
}
Opt::Completions { shell } => {
shell.generate(&mut Opt::command(), &mut io::stdout());
}
#[cfg(feature = "zig")]
Opt::Zig(subcommand) => {
subcommand
.execute()
.context("Failed to run zig linker wrapper")?;
}
}
Ok(())
}
#[cfg(not(debug_assertions))]
fn setup_panic_hook() {
let default_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
eprintln!("\n===================================================================");
eprintln!("maturin has panicked. This is a bug in maturin. Please report this");
eprintln!("at https://github.com/PyO3/maturin/issues/new/choose.");
eprintln!("If you can reliably reproduce this panic, include the");
eprintln!("reproduction steps and re-run with the RUST_BACKTRACE=1 environment");
eprintln!("variable set and include the backtrace in your report.");
eprintln!();
eprintln!("Platform: {} {}", env::consts::OS, env::consts::ARCH);
eprintln!("Version: {}", env!("CARGO_PKG_VERSION"));
eprintln!("Args: {}", env::args().collect::<Vec<_>>().join(" "));
eprintln!();
default_hook(panic_info);
// Rust set exit code to 101 when the process panicked,
// so here we use the same exit code
std::process::exit(101);
}));
}
fn main() {
#[cfg(not(debug_assertions))]
setup_panic_hook();
if let Err(e) = run() {
eprintln!("💥 maturin failed");
for cause in e.chain() {
eprintln!(" Caused by: {cause}");
}
std::process::exit(1);
}
}