Skip to content

Commit 6347ad4

Browse files
authored
feat: support cargo --locked flag (#234)
* feat: support cargo --locked flag * [review] use generic cargo_manifest_args, no build_rust * [review] add changelog entry * [review] update set correctly
1 parent 37381c7 commit 6347ad4

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
### Packaging
66
- Increase minimum `setuptools` version to 62.4. [#222](https://github.com/PyO3/setuptools-rust/pull/246)
77

8+
### Added
9+
- Add `cargo_manifest_args` to support locked, frozen and offline builds. [#234](https://github.com/PyO3/setuptools-rust/pull/234)
10+
811
### Fixed
912
- If the sysconfig for `BLDSHARED` has no flags, `setuptools-rust` won't crash anymore. [#241](https://github.com/PyO3/setuptools-rust/pull/241)
1013

setuptools_rust/build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ def _cargo_args(
505505
if ext.args is not None:
506506
args.extend(ext.args)
507507

508+
if ext.cargo_manifest_args is not None:
509+
args.extend(ext.cargo_manifest_args)
510+
508511
return args
509512

510513

setuptools_rust/clean.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def initialize_options(self) -> None:
1717
def run_for_extension(self, ext: RustExtension) -> None:
1818
# build cargo command
1919
args = ["cargo", "clean", "--manifest-path", ext.path]
20+
if ext.cargo_manifest_args:
21+
args.extend(ext.cargo_manifest_args)
2022

2123
if not ext.quiet:
2224
print(" ".join(args), file=sys.stderr)

setuptools_rust/extension.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class RustExtension:
6868
args: A list of extra arguments to be passed to Cargo. For example,
6969
``args=["--no-default-features"]`` will disable the default
7070
features listed in ``Cargo.toml``.
71+
cargo_manifest_args: A list of extra arguments to be passed to Cargo.
72+
These arguments will be passed to every ``cargo`` command, not just
73+
``cargo build``. For valid options, see
74+
`the Cargo Book <https://doc.rust-lang.org/cargo/commands/cargo-build.html#manifest-options>`_.
75+
For example, ``cargo_manifest_args=["--locked"]`` will require
76+
``Cargo.lock`` files are up to date.
7177
features: A list of Cargo features to also build.
7278
rustc_flags: A list of additional flags passed to rustc.
7379
rust_version: Minimum Rust compiler version required for this
@@ -109,6 +115,7 @@ def __init__(
109115
target: Union[str, Dict[str, str]],
110116
path: str = "Cargo.toml",
111117
args: Optional[List[str]] = None,
118+
cargo_manifest_args: Optional[List[str]] = None,
112119
features: Optional[List[str]] = None,
113120
rustc_flags: Optional[List[str]] = None,
114121
rust_version: Optional[str] = None,
@@ -130,6 +137,7 @@ def __init__(
130137
self.name = name
131138
self.target = target
132139
self.args = args
140+
self.cargo_manifest_args = cargo_manifest_args
133141
self.rustc_flags = rustc_flags
134142
self.binding = binding
135143
self.rust_version = rust_version
@@ -223,6 +231,8 @@ def _metadata(self) -> "_CargoMetadata":
223231
"--format-version",
224232
"1",
225233
]
234+
if self.cargo_manifest_args:
235+
metadata_command.extend(self.cargo_manifest_args)
226236
self._cargo_metadata = json.loads(subprocess.check_output(metadata_command))
227237
return self._cargo_metadata
228238

setuptools_rust/setuptools_ext.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import subprocess
33
from distutils import log
44
from distutils.command.clean import clean
5-
from typing import List, Tuple, Type, cast
5+
from typing import List, Set, Tuple, Type, cast
66

77
from setuptools.command.build_ext import build_ext
88
from setuptools.command.install import install
@@ -48,15 +48,27 @@ def initialize_options(self) -> None:
4848
def make_distribution(self) -> None:
4949
if self.vendor_crates:
5050
manifest_paths = []
51+
52+
# Collate cargo manifest options together.
53+
# We can cheat here, as the only valid options are the simple strings
54+
# --frozen, --locked, or --offline.
55+
#
56+
# https://doc.rust-lang.org/cargo/commands/cargo-build.html#manifest-options
57+
cargo_manifest_args: Set[str] = set()
5158
for ext in self.distribution.rust_extensions:
5259
manifest_paths.append(ext.path)
60+
if ext.cargo_manifest_args:
61+
cargo_manifest_args.update(ext.cargo_manifest_args)
62+
5363
if manifest_paths:
5464
base_dir = self.distribution.get_fullname()
5565
dot_cargo_path = os.path.join(base_dir, ".cargo")
5666
self.mkpath(dot_cargo_path)
5767
cargo_config_path = os.path.join(dot_cargo_path, "config.toml")
5868
vendor_path = os.path.join(dot_cargo_path, "vendor")
5969
command = ["cargo", "vendor"]
70+
if cargo_manifest_args:
71+
command.extend(sorted(cargo_manifest_args))
6072
# additional Cargo.toml for extension 1..n
6173
for extra_path in manifest_paths[1:]:
6274
command.append("--sync")

0 commit comments

Comments
 (0)