diff --git a/pants-plugins/cargo-porcelain/pants_cargo_porcelain/goals/package.py b/pants-plugins/cargo-porcelain/pants_cargo_porcelain/goals/package.py index b2785d8..acd20a4 100644 --- a/pants-plugins/cargo-porcelain/pants_cargo_porcelain/goals/package.py +++ b/pants-plugins/cargo-porcelain/pants_cargo_porcelain/goals/package.py @@ -18,6 +18,7 @@ from pants.util.logging import LogLevel from pants_cargo_porcelain.internal.build import CargoBinary, CargoBinaryRequest +from pants_cargo_porcelain.subsystems import RustSubsystem from pants_cargo_porcelain.target_types import CargoBinaryNameField, CargoPackageSourcesField @@ -33,16 +34,25 @@ class CargoBinaryFieldSet(PackageFieldSet, RunFieldSet): @rule(desc="Package Cargo binary", level=LogLevel.DEBUG) -async def package_cargo_binary(field_set: CargoBinaryFieldSet) -> BuiltPackage: +async def package_cargo_binary( + field_set: CargoBinaryFieldSet, + rust: RustSubsystem, +) -> BuiltPackage: output_filename = PurePath(field_set.output_path.value_or_default(file_ending=None)) binary = await Get( CargoBinary, CargoBinaryRequest(field_set.address, field_set.sources, field_set.binary_name.value), ) + build_level = "debug" + if rust.release: + build_level = "release" + removed_prefix = await Get( Digest, - RemovePrefix(binary.digest, f".cargo-target-cache/{field_set.address.spec_path}/debug"), + RemovePrefix( + binary.digest, f".cargo-target-cache/{field_set.address.spec_path}/{build_level}" + ), ) renamed_output_digest = await Get( diff --git a/pants-plugins/cargo-porcelain/pants_cargo_porcelain/internal/build.py b/pants-plugins/cargo-porcelain/pants_cargo_porcelain/internal/build.py index 78307f4..b69e252 100644 --- a/pants-plugins/cargo-porcelain/pants_cargo_porcelain/internal/build.py +++ b/pants-plugins/cargo-porcelain/pants_cargo_porcelain/internal/build.py @@ -43,7 +43,10 @@ def platform_to_target(platform: Platform): @rule async def build_cargo_binary( - req: CargoBinaryRequest, rust: RustSubsystem, rustup: RustupTool, platform: Platform + req: CargoBinaryRequest, + rust: RustSubsystem, + rustup: RustupTool, + platform: Platform, ) -> CargoBinary: source_files, toolchain = await MultiGet( Get( @@ -58,18 +61,27 @@ async def build_cargo_binary( ), ) + extra_args = [] + if rust.release: + extra_args.append("--release") + + build_level = "debug" + if rust.release: + build_level = "release" + process_result = await Get( ProcessResult, CargoProcessRequest( toolchain, ( "build", + *extra_args, f"--manifest-path={req.address.spec_path}/Cargo.toml", "--locked", f"--bin={req.binary_name}", ), source_files.snapshot.digest, - output_files=(f"{{cache_path}}/debug/{req.binary_name}",), + output_files=(f"{{cache_path}}/{build_level}/{req.binary_name}",), cache_path=req.address.spec_path, ), ) diff --git a/pants-plugins/cargo-porcelain/pants_cargo_porcelain/subsystems.py b/pants-plugins/cargo-porcelain/pants_cargo_porcelain/subsystems.py index 5816703..fbe3639 100644 --- a/pants-plugins/cargo-porcelain/pants_cargo_porcelain/subsystems.py +++ b/pants-plugins/cargo-porcelain/pants_cargo_porcelain/subsystems.py @@ -18,6 +18,12 @@ class RustSubsystem(Subsystem): advanced=True, ) + release = BoolOption( + default=False, + help=softwrap("""If true, build in release mode."""), + advanced=True, + ) + skip = SkipOption("fmt", "lint")