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

feat: copy .pdb files to Editable Installation and Wheel for easier debugging on windows #2220

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ venv*/
*.o
*.so
*.py[cdo]
*.pdb
__pycache__/
*.egg-info/
*.egg
Expand Down
55 changes: 51 additions & 4 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::auditwheel::{PlatformTag, Policy};
use crate::build_options::CargoOptions;
use crate::compile::{warn_missing_py_init, CompileTarget};
use crate::module_writer::{
add_data, write_bin, write_bindings_module, write_cffi_module, write_python_part,
write_uniffi_module, write_wasm_launcher, WheelWriter,
add_data, include_artifact_for_editable_install, write_bin, write_bindings_module,
write_cffi_module, write_python_part, write_uniffi_module, write_wasm_launcher, DebugInfoType,
WheelWriter,
};
use crate::project_layout::ProjectLayout;
use crate::python_interpreter::InterpreterKind;
Expand Down Expand Up @@ -187,6 +188,11 @@ pub struct BuildContext {
pub universal2: bool,
/// Build editable wheels
pub editable: bool,
/// Whether to include debug information(`.pdb` on msvc)
/// in the wheels or editable-installs.
/// Currently only support `.pdb` files with
/// the same name as the binary(`*.exe`/`*.dll`) on `msvc` platform
pub with_debuginfo: bool,
/// Cargo build options
pub cargo_options: CargoOptions,
}
Expand Down Expand Up @@ -683,6 +689,11 @@ impl BuildContext {
)?;
self.add_external_libs(&mut writer, &[&artifact], &[ext_libs])?;

let with_debuginfo = if self.with_debuginfo {
Some(DebugInfoType::build(&self.target)?)
} else {
None
};
write_bindings_module(
&mut writer,
&self.project_layout,
Expand All @@ -692,6 +703,7 @@ impl BuildContext {
&self.target,
self.editable,
self.pyproject_toml.as_ref(),
&with_debuginfo,
)
.context("Failed to add the files to the wheel")?;

Expand Down Expand Up @@ -761,6 +773,11 @@ impl BuildContext {
)?;
self.add_external_libs(&mut writer, &[&artifact], &[ext_libs])?;

let with_debuginfo = if self.with_debuginfo {
Some(DebugInfoType::build(&self.target)?)
} else {
None
};
write_bindings_module(
&mut writer,
&self.project_layout,
Expand All @@ -770,6 +787,7 @@ impl BuildContext {
&self.target,
self.editable,
self.pyproject_toml.as_ref(),
&with_debuginfo,
)
.context("Failed to add the files to the wheel")?;

Expand Down Expand Up @@ -856,12 +874,18 @@ impl BuildContext {
if self.editable || matches!(self.auditwheel, AuditWheelMode::Skip) {
return Ok(artifact);
}

let with_debuginfo = if self.with_debuginfo {
Some(DebugInfoType::build(&self.target)?)
} else {
None
};
// auditwheel repair will edit the file, so we need to copy it to avoid errors in reruns
let artifact_path = &artifact.path;
let maturin_build = artifact_path.parent().unwrap().join("maturin");
fs::create_dir_all(&maturin_build)?;
let new_artifact_path = maturin_build.join(artifact_path.file_name().unwrap());
fs::copy(artifact_path, &new_artifact_path)?;
include_artifact_for_editable_install(artifact_path, &new_artifact_path, &with_debuginfo)?;
artifact.path = new_artifact_path;
Ok(artifact)
}
Expand All @@ -883,6 +907,11 @@ impl BuildContext {
)?;
self.add_external_libs(&mut writer, &[&artifact], &[ext_libs])?;

let with_debuginfo = if self.with_debuginfo {
Some(DebugInfoType::build(&self.target)?)
} else {
None
};
write_cffi_module(
&mut writer,
&self.project_layout,
Expand All @@ -893,6 +922,7 @@ impl BuildContext {
&self.interpreter[0].executable,
self.editable,
self.pyproject_toml.as_ref(),
&with_debuginfo,
)?;

self.add_pth(&mut writer)?;
Expand Down Expand Up @@ -949,6 +979,11 @@ impl BuildContext {
)?;
self.add_external_libs(&mut writer, &[&artifact], &[ext_libs])?;

let with_debuginfo = if self.with_debuginfo {
Some(DebugInfoType::build(&self.target)?)
} else {
None
};
write_uniffi_module(
&mut writer,
&self.project_layout,
Expand All @@ -959,6 +994,7 @@ impl BuildContext {
self.target.target_os(),
self.editable,
self.pyproject_toml.as_ref(),
&with_debuginfo,
)?;

self.add_pth(&mut writer)?;
Expand Down Expand Up @@ -1056,10 +1092,21 @@ impl BuildContext {
.context("Failed to add the python module to the package")?;
}

let with_debuginfo = if self.with_debuginfo {
Some(DebugInfoType::build(&self.target)?)
} else {
None
};
let mut artifacts_ref = Vec::with_capacity(artifacts.len());
for (artifact, bin_name) in &artifacts_and_files {
artifacts_ref.push(*artifact);
write_bin(&mut writer, &artifact.path, &self.metadata23, bin_name)?;
write_bin(
&mut writer,
&artifact.path,
&self.metadata23,
bin_name,
&with_debuginfo,
)?;
if self.target.is_wasi() {
write_wasm_launcher(&mut writer, &self.metadata23, bin_name)?;
}
Expand Down
7 changes: 7 additions & 0 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ pub struct BuildOptions {
#[arg(long)]
pub zig: bool,

/// Include debug information in the Wheel.
/// Currently only support `.pdb` files with
/// the same name as the binary(`exe/dll`) on `msvc` platform
#[arg(long)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be used together with --strip. (Not sure if clap supports this kind of setup though)

Suggested change
#[arg(long)]
#[arg(long, conflicts_with = "strip")]

Copy link
Author

@WSH032 WSH032 Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On MSVC, even if RUSTFLAGS="-Cstrip=symbols" is specified, rustc(at least with version 1.81 that I used) will still generate a .pdb file, so we can't let it conflict. Not sure how it behaves on macOS.

Copy link
Member

@messense messense Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But does the .pdb file contain useful information?

pub with_debuginfo: bool,

/// Cargo build options
#[command(flatten)]
pub cargo: CargoOptions,
Expand Down Expand Up @@ -719,6 +725,7 @@ impl BuildOptions {
cargo_metadata,
universal2,
editable,
with_debuginfo: self.with_debuginfo,
cargo_options,
})
}
Expand Down
5 changes: 5 additions & 0 deletions src/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ pub struct DevelopOptions {
/// Use `uv` to install packages instead of `pip`
#[arg(long)]
pub uv: bool,
/// The same as `--with-debuginfo` in `maturin build`
#[arg(long)]
pub with_debuginfo: bool,
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -301,6 +304,7 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
pip_path,
cargo_options,
uv,
with_debuginfo,
} = develop_options;
let mut target_triple = cargo_options.target.as_ref().map(|x| x.to_string());
let target = Target::from_target_triple(cargo_options.target)?;
Expand All @@ -326,6 +330,7 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
skip_auditwheel: false,
#[cfg(feature = "zig")]
zig: false,
with_debuginfo,
cargo: CargoOptions {
target: target_triple,
..cargo_options
Expand Down
Loading
Loading