5
5
import warnings
6
6
from distutils .errors import DistutilsSetupError
7
7
from enum import IntEnum , auto
8
- from typing import Any , Dict , List , NewType , Optional , Union
8
+ from typing import Any , Dict , List , NewType , Optional , Sequence , Union
9
9
10
10
from semantic_version import SimpleSpec
11
11
from typing_extensions import Literal
@@ -77,8 +77,10 @@ class RustExtension:
77
77
`the Cargo Book <https://doc.rust-lang.org/cargo/commands/cargo-build.html#manifest-options>`_.
78
78
For example, ``cargo_manifest_args=["--locked"]`` will require
79
79
``Cargo.lock`` files are up to date.
80
- features: A list of Cargo features to also build.
81
- rustc_flags: A list of additional flags passed to rustc.
80
+ features: Cargo `--features` to add to the build.
81
+ rustc_flags: A list of additional flags passed to `cargo rustc`. These
82
+ only affect the final artifact, usually you should set the
83
+ `RUSTFLAGS` environment variable.
82
84
rust_version: Minimum Rust compiler version required for this
83
85
extension.
84
86
quiet: Suppress Cargo's output.
@@ -88,9 +90,10 @@ class RustExtension:
88
90
and ``wheel`` builds will be release.
89
91
binding: Informs ``setuptools_rust`` which Python binding is in use.
90
92
strip: Strip symbols from final file. Does nothing for debug build.
93
+ native: Build extension or executable with ``-Ctarget-cpu=native``
94
+ (deprecated, set environment variable RUSTFLAGS=-Ctarget-cpu=native).
91
95
script: Generate console script for executable if ``Binding.Exec`` is
92
- used.
93
- native: Build extension or executable with ``--target-cpu=native``.
96
+ used (deprecated, just use ``RustBin`` instead).
94
97
optional: If it is true, a build failure in the extension will not
95
98
abort the build process, and instead simply not install the failing
96
99
extension.
@@ -117,10 +120,10 @@ def __init__(
117
120
self ,
118
121
target : Union [str , Dict [str , str ]],
119
122
path : str = "Cargo.toml" ,
120
- args : Optional [List [str ]] = None ,
121
- cargo_manifest_args : Optional [List [str ]] = None ,
122
- features : Optional [List [str ]] = None ,
123
- rustc_flags : Optional [List [str ]] = None ,
123
+ args : Optional [Sequence [str ]] = () ,
124
+ cargo_manifest_args : Optional [Sequence [str ]] = () ,
125
+ features : Optional [Sequence [str ]] = () ,
126
+ rustc_flags : Optional [Sequence [str ]] = () ,
124
127
rust_version : Optional [str ] = None ,
125
128
quiet : bool = False ,
126
129
debug : Optional [bool ] = None ,
@@ -139,33 +142,34 @@ def __init__(
139
142
140
143
self .name = name
141
144
self .target = target
142
- self .args = args
143
- self .cargo_manifest_args = cargo_manifest_args
144
- self .rustc_flags = rustc_flags
145
- self .binding = binding
145
+ self .path = os .path .relpath (path ) # relative path to Cargo manifest file
146
+ self .args = tuple (args or ())
147
+ self .cargo_manifest_args = tuple (cargo_manifest_args or ())
148
+ self .features = tuple (features or ())
149
+ self .rustc_flags = tuple (rustc_flags or ())
146
150
self .rust_version = rust_version
147
151
self .quiet = quiet
148
152
self .debug = debug
153
+ self .binding = binding
149
154
self .strip = strip
150
155
self .script = script
151
- self .native = native
152
156
self .optional = optional
153
157
self .py_limited_api = py_limited_api
154
158
155
- if features is None :
156
- features = []
157
-
158
- self .features = [s .strip () for s in features ]
159
-
160
- # get relative path to Cargo manifest file
161
- path = os .path .relpath (path )
162
- self .path = path
163
-
164
159
self ._cargo_metadata : Optional [_CargoMetadata ] = None
165
160
161
+ if native :
162
+ warnings .warn (
163
+ "`native` is deprecated, set RUSTFLAGS=-Ctarget-cpu=native instead." ,
164
+ DeprecationWarning ,
165
+ )
166
+ # match old behaviour of only setting flag for top-level crate;
167
+ # setting for `rustflags` is strictly better
168
+ self .rustc_flags = (* self .rustc_flags , "-Ctarget-cpu=native" )
169
+
166
170
if binding == Binding .Exec and script :
167
171
warnings .warn (
168
- "' Binding.Exec' with ' script=True' is deprecated, use ' RustBin' instead." ,
172
+ "` Binding.Exec` with ` script=True` is deprecated, use ` RustBin` instead." ,
169
173
DeprecationWarning ,
170
174
)
171
175
@@ -189,21 +193,22 @@ def get_rust_version(self) -> Optional[SimpleSpec]: # type: ignore[no-any-unimp
189
193
)
190
194
191
195
def get_cargo_profile (self ) -> Optional [str ]:
192
- args = self .args or []
193
196
try :
194
- index = args .index ("--profile" )
195
- return args [index + 1 ]
197
+ index = self . args .index ("--profile" )
198
+ return self . args [index + 1 ]
196
199
except ValueError :
197
200
pass
198
201
except IndexError :
199
- raise DistutilsSetupError ("Can not parse cargo profile from %s" , args )
202
+ raise DistutilsSetupError ("Can not parse cargo profile from %s" , self . args )
200
203
201
204
# Handle `--profile=<profile>`
202
- profile_args = [p for p in args if p .startswith ("--profile=" )]
205
+ profile_args = [p for p in self . args if p .startswith ("--profile=" )]
203
206
if profile_args :
204
207
profile = profile_args [0 ].split ("=" , 1 )[1 ]
205
208
if not profile :
206
- raise DistutilsSetupError ("Can not parse cargo profile from %s" , args )
209
+ raise DistutilsSetupError (
210
+ "Can not parse cargo profile from %s" , self .args
211
+ )
207
212
return profile
208
213
else :
209
214
return None
@@ -280,46 +285,45 @@ class RustBin(RustExtension):
280
285
`the Cargo Book <https://doc.rust-lang.org/cargo/commands/cargo-build.html#manifest-options>`_.
281
286
For example, ``cargo_manifest_args=["--locked"]`` will require
282
287
``Cargo.lock`` files are up to date.
283
- features: A list of Cargo features to also build.
284
- rustc_flags: A list of additional flags passed to rustc.
285
- rust_version: Minimum Rust compiler version required for this
286
- extension.
288
+ features: Cargo `--features` to add to the build.
289
+ rust_version: Minimum Rust compiler version required for this bin.
287
290
quiet: Suppress Cargo's output.
288
291
debug: Controls whether ``--debug`` or ``--release`` is passed to
289
292
Cargo. If set to `None` (the default) then build type is
290
293
automatic: ``inplace`` build will be a debug build, ``install``
291
294
and ``wheel`` builds will be release.
292
295
strip: Strip symbols from final file. Does nothing for debug build.
293
- native: Build extension or executable with ``--target-cpu=native``.
296
+ optional: If it is true, a build failure in the bin will not
297
+ abort the build process, and instead simply not install the failing
298
+ bin.
294
299
"""
295
300
296
301
def __init__ (
297
302
self ,
298
- target : str ,
303
+ target : Union [ str , Dict [ str , str ]] ,
299
304
path : str = "Cargo.toml" ,
300
- args : Optional [List [str ]] = None ,
301
- cargo_manifest_args : Optional [List [str ]] = None ,
302
- features : Optional [List [str ]] = None ,
303
- rustc_flags : Optional [List [str ]] = None ,
305
+ args : Optional [Sequence [str ]] = (),
306
+ cargo_manifest_args : Optional [Sequence [str ]] = (),
307
+ features : Optional [Sequence [str ]] = (),
304
308
rust_version : Optional [str ] = None ,
305
309
quiet : bool = False ,
306
310
debug : Optional [bool ] = None ,
307
311
strip : Strip = Strip .No ,
308
- native : bool = False ,
312
+ optional : bool = False ,
309
313
):
310
314
super ().__init__ (
311
- target ,
312
- path ,
313
- args ,
314
- cargo_manifest_args ,
315
- features ,
316
- rustc_flags ,
317
- rust_version ,
318
- quiet ,
319
- debug ,
315
+ target = target ,
316
+ path = path ,
317
+ args = args ,
318
+ cargo_manifest_args = cargo_manifest_args ,
319
+ features = features ,
320
+ rust_version = rust_version ,
321
+ quiet = quiet ,
322
+ debug = debug ,
320
323
binding = Binding .Exec ,
324
+ optional = optional ,
321
325
strip = strip ,
322
- native = native ,
326
+ py_limited_api = False ,
323
327
)
324
328
325
329
def entry_points (self ) -> List [str ]:
0 commit comments