Skip to content

Commit 956e41c

Browse files
Add --target command line option for specify target triple (PyO3#136)
* Add --target command line option for specifying target triple * --target take precedence over CARGO_BUILD_TARGET Co-authored-by: David Hewitt <[email protected]>
1 parent 068e342 commit 956e41c

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
### Added
5+
- Add `--target` command line option for specifying target triple. [#136](https://github.com/PyO3/setuptools-rust/pull/136)
56
- Support very verbose cargo build.rs output. [#140](https://github.com/PyO3/setuptools-rust/pull/140)
67

78
### Removed

setuptools_rust/build.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class build_rust(RustCommand):
3737
"t",
3838
"directory for temporary files (cargo 'target' directory) ",
3939
),
40+
("target", None, "Build for the target triple"),
4041
]
4142
boolean_options = ["inplace", "debug", "release", "qbuild"]
4243

@@ -48,6 +49,7 @@ def initialize_options(self):
4849
self.qbuild = None
4950
self.build_temp = None
5051
self.plat_name = None
52+
self.target = os.getenv("CARGO_BUILD_TARGET")
5153

5254
def finalize_options(self):
5355
super().finalize_options()
@@ -64,9 +66,9 @@ def get_target_triple(self):
6466
# If we are on a 64-bit machine, but running a 32-bit Python, then
6567
# we'll target a 32-bit Rust build.
6668
# Automatic target detection can be overridden via the CARGO_BUILD_TARGET
67-
# environment variable.
68-
if os.getenv("CARGO_BUILD_TARGET"):
69-
return os.environ["CARGO_BUILD_TARGET"]
69+
# environment variable or --target command line option
70+
if self.target:
71+
return self.target
7072
elif self.plat_name == "win32":
7173
return "i686-pc-windows-msvc"
7274
elif self.plat_name == "win-amd64":

setuptools_rust/setuptools_ext.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,23 @@ def make_distribution(self):
9191
dist.cmdclass["sdist"] = sdist_rust_extension
9292

9393
build_ext_base_class = dist.cmdclass.get('build_ext', build_ext)
94+
build_ext_options = build_ext_base_class.user_options.copy()
95+
build_ext_options.append(("target", None, "Build for the target triple"))
9496

9597
class build_ext_rust_extension(build_ext_base_class):
98+
user_options = build_ext_options
99+
100+
def initialize_options(self):
101+
super().initialize_options()
102+
self.target = os.getenv("CARGO_BUILD_TARGET")
103+
96104
def run(self):
97105
if self.distribution.rust_extensions:
98106
log.info("running build_rust")
99107
build_rust = self.get_finalized_command("build_rust")
100108
build_rust.inplace = self.inplace
101109
build_rust.plat_name = self.plat_name
110+
build_rust.target = self.target
102111
build_rust.verbose = self.verbose
103112
build_rust.run()
104113

@@ -157,9 +166,17 @@ def finalize_options(self):
157166

158167
if bdist_wheel is not None:
159168
bdist_wheel_base_class = dist.cmdclass.get("bdist_wheel", bdist_wheel)
169+
bdist_wheel_options = bdist_wheel_base_class.user_options.copy()
170+
bdist_wheel_options.append(("target", None, "Build for the target triple"))
160171

161172
# this is for console entries
162173
class bdist_wheel_rust_extension(bdist_wheel_base_class):
174+
user_options = bdist_wheel_options
175+
176+
def initialize_options(self):
177+
super().initialize_options()
178+
self.target = os.getenv("CARGO_BUILD_TARGET")
179+
163180
def finalize_options(self):
164181
scripts = []
165182
for ext in self.distribution.rust_extensions:

0 commit comments

Comments
 (0)