Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PEP 660 support #648

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Add support for PEP 660 editable installs in [#648](https://github.com/PyO3/maturin/pull/648)
* Publish musllinux_1_1 wheels for maturin in [#651](https://github.com/PyO3/maturin/pull/651)

## [0.11.5] - 2021-10-13
Expand Down
26 changes: 25 additions & 1 deletion maturin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ def get_config() -> Dict[str, str]:


# noinspection PyUnusedLocal
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
def _build_wheel(
wheel_directory, config_settings=None, metadata_directory=None, editable=False
):
# PEP 517 specifies that only `sys.executable` points to the correct
# python interpreter
command = ["maturin", "pep517", "build-wheel", "-i", sys.executable]
if editable:
command.append("--editable")

print("Running `{}`".format(" ".join(command)))
sys.stdout.flush()
Expand All @@ -48,6 +52,11 @@ def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
return filename


# noinspection PyUnusedLocal
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
return _build_wheel(wheel_directory, config_settings, metadata_directory)


# noinspection PyUnusedLocal
def build_sdist(sdist_directory, config_settings=None):
command = ["maturin", "pep517", "write-sdist", "--sdist-directory", sdist_directory]
Expand All @@ -74,6 +83,17 @@ def get_requires_for_build_wheel(config_settings=None):
return []


# noinspection PyUnusedLocal
def build_editable(wheel_directory, config_settings=None, metadata_directory=None):
return _build_wheel(
wheel_directory, config_settings, metadata_directory, editable=True
)


# Requirements to build an editable are the same as for a wheel
get_requires_for_build_editable = get_requires_for_build_wheel


# noinspection PyUnusedLocal
def get_requires_for_build_sdist(config_settings=None):
return []
Expand Down Expand Up @@ -122,3 +142,7 @@ def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
sys.stdout.flush()
output = output.decode(errors="replace")
return output.strip().splitlines()[-1]


# Metadata for editable are the same as for a wheel
prepare_metadata_for_build_editable = prepare_metadata_for_build_wheel
38 changes: 30 additions & 8 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ pub struct BuildContext {
pub cargo_metadata: Metadata,
/// Whether to use universal2 or use the native macOS tag (off)
pub universal2: bool,
/// Build editable wheels
pub editable: bool,
}

/// The wheel file location and its Python version tag (e.g. `py3`).
Expand Down Expand Up @@ -244,6 +246,13 @@ impl BuildContext {
Ok(policy)
}

fn add_pth(&self, writer: &mut WheelWriter) -> Result<()> {
if self.editable {
writer.add_pth(&self.project_layout, &self.metadata21)?;
}
Ok(())
}

fn write_binding_wheel_abi3(
&self,
artifact: &Path,
Expand All @@ -264,9 +273,12 @@ impl BuildContext {
None,
&self.target,
false,
self.editable,
)
.context("Failed to add the files to the wheel")?;

self.add_pth(&mut writer)?;

let wheel_path = writer.finish()?;
Ok((wheel_path, format!("cp{}{}", major, min_minor)))
}
Expand Down Expand Up @@ -319,9 +331,12 @@ impl BuildContext {
Some(python_interpreter),
&self.target,
false,
self.editable,
)
.context("Failed to add the files to the wheel")?;

self.add_pth(&mut writer)?;

let wheel_path = writer.finish()?;
Ok((
wheel_path,
Expand Down Expand Up @@ -398,19 +413,22 @@ impl BuildContext {
.target
.get_universal_tags(platform_tag, self.universal2);

let mut builder = WheelWriter::new(&tag, &self.out, &self.metadata21, &tags)?;
let mut writer = WheelWriter::new(&tag, &self.out, &self.metadata21, &tags)?;

write_cffi_module(
&mut builder,
&mut writer,
&self.project_layout,
self.manifest_path.parent().unwrap(),
&self.module_name,
artifact,
&self.interpreter[0].executable,
false,
self.editable,
)?;

let wheel_path = builder.finish()?;
self.add_pth(&mut writer)?;

let wheel_path = writer.finish()?;
Ok((wheel_path, "py3".to_string()))
}

Expand Down Expand Up @@ -440,16 +458,18 @@ impl BuildContext {
bail!("Defining entrypoints and working with a binary doesn't mix well");
}

let mut builder = WheelWriter::new(&tag, &self.out, &self.metadata21, &tags)?;
let mut writer = WheelWriter::new(&tag, &self.out, &self.metadata21, &tags)?;

match self.project_layout {
ProjectLayout::Mixed {
ref python_module,
ref extension_name,
..
} => {
write_python_part(&mut builder, python_module, extension_name)
.context("Failed to add the python module to the package")?;
if !self.editable {
write_python_part(&mut writer, python_module, extension_name)
.context("Failed to add the python module to the package")?;
}
}
ProjectLayout::PureRust { .. } => {}
}
Expand All @@ -459,9 +479,11 @@ impl BuildContext {
let bin_name = artifact
.file_name()
.expect("Couldn't get the filename from the binary produced by cargo");
write_bin(&mut builder, artifact, &self.metadata21, bin_name)?;
write_bin(&mut writer, artifact, &self.metadata21, bin_name)?;

let wheel_path = builder.finish()?;
self.add_pth(&mut writer)?;

let wheel_path = writer.finish()?;
Ok((wheel_path, "py3".to_string()))
}

Expand Down
10 changes: 8 additions & 2 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ impl Default for BuildOptions {

impl BuildOptions {
/// Tries to fill the missing metadata for a BuildContext by querying cargo and python
pub fn into_build_context(self, release: bool, strip: bool) -> Result<BuildContext> {
pub fn into_build_context(
self,
release: bool,
strip: bool,
editable: bool,
) -> Result<BuildContext> {
let manifest_file = &self.manifest_path;
if !manifest_file.exists() {
let current_dir =
Expand Down Expand Up @@ -303,6 +308,7 @@ impl BuildOptions {
interpreter,
cargo_metadata,
universal2,
editable,
})
}
}
Expand Down Expand Up @@ -789,7 +795,7 @@ mod test {
let mut options = BuildOptions::default();
options.cargo_extra_args.push("--features log".to_string());
options.bindings = Some("bin".to_string());
let context = options.into_build_context(false, false).unwrap();
let context = options.into_build_context(false, false, false).unwrap();
assert_eq!(context.cargo_extra_args, vec!["--features", "log"])
}

Expand Down
5 changes: 4 additions & 1 deletion src/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn develop(
universal2: false,
};

let build_context = build_options.into_build_context(release, strip)?;
let build_context = build_options.into_build_context(release, strip, false)?;

let interpreter = PythonInterpreter::check_executable(python, &target, &build_context.bridge)?
.ok_or_else(|| {
Expand Down Expand Up @@ -146,6 +146,7 @@ pub fn develop(
&artifact,
&interpreter.executable,
true,
false,
)?;
}
BridgeModel::Bindings(_) => {
Expand All @@ -164,6 +165,7 @@ pub fn develop(
Some(&interpreter),
&target,
true,
false,
)?;
}
BridgeModel::BindingsAbi3(_, _) => {
Expand All @@ -183,6 +185,7 @@ pub fn develop(
None,
&target,
true,
false,
)?;
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ enum Pep517Command {
/// Strip the library for minimum file size
#[structopt(long)]
strip: bool,
/// Build editable wheels
#[structopt(long)]
editable: bool,
},
/// The implementation of build_sdist
#[structopt(name = "write-sdist")]
Expand Down Expand Up @@ -356,7 +359,7 @@ fn pep517(subcommand: Pep517Command) -> Result<()> {
build_options.interpreter.as_ref(),
Some(version) if version.len() == 1
));
let context = build_options.into_build_context(true, strip)?;
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 {
Expand Down Expand Up @@ -384,8 +387,9 @@ fn pep517(subcommand: Pep517Command) -> Result<()> {
Pep517Command::BuildWheel {
build_options,
strip,
editable,
} => {
let build_context = build_options.into_build_context(true, strip)?;
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());
Expand Down Expand Up @@ -507,7 +511,7 @@ fn run() -> Result<()> {
strip,
no_sdist,
} => {
let build_context = build.into_build_context(release, strip)?;
let build_context = build.into_build_context(release, strip, false)?;
if !no_sdist {
build_context.build_source_distribution()?;
}
Expand All @@ -521,7 +525,7 @@ fn run() -> Result<()> {
no_strip,
no_sdist,
} => {
let build_context = build.into_build_context(!debug, !no_strip)?;
let build_context = build.into_build_context(!debug, !no_strip, false)?;

if !build_context.release {
eprintln!("⚠️ Warning: You're publishing debug wheels");
Expand Down
Loading