Skip to content

Commit

Permalink
Add --target command line option for specify target triple (#136)
Browse files Browse the repository at this point in the history
* Add --target command line option for specifying target triple

* --target take precedence over CARGO_BUILD_TARGET

Co-authored-by: David Hewitt <[email protected]>
  • Loading branch information
messense and davidhewitt authored Mar 27, 2021
1 parent 068e342 commit 956e41c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

### Removed
Expand Down
8 changes: 5 additions & 3 deletions setuptools_rust/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class build_rust(RustCommand):
"t",
"directory for temporary files (cargo 'target' directory) ",
),
("target", None, "Build for the target triple"),
]
boolean_options = ["inplace", "debug", "release", "qbuild"]

Expand All @@ -48,6 +49,7 @@ def initialize_options(self):
self.qbuild = None
self.build_temp = None
self.plat_name = None
self.target = os.getenv("CARGO_BUILD_TARGET")

def finalize_options(self):
super().finalize_options()
Expand All @@ -64,9 +66,9 @@ def get_target_triple(self):
# If we are on a 64-bit machine, but running a 32-bit Python, then
# we'll target a 32-bit Rust build.
# Automatic target detection can be overridden via the CARGO_BUILD_TARGET
# environment variable.
if os.getenv("CARGO_BUILD_TARGET"):
return os.environ["CARGO_BUILD_TARGET"]
# environment variable or --target command line option
if self.target:
return self.target
elif self.plat_name == "win32":
return "i686-pc-windows-msvc"
elif self.plat_name == "win-amd64":
Expand Down
17 changes: 17 additions & 0 deletions setuptools_rust/setuptools_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,23 @@ def make_distribution(self):
dist.cmdclass["sdist"] = sdist_rust_extension

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

class build_ext_rust_extension(build_ext_base_class):
user_options = build_ext_options

def initialize_options(self):
super().initialize_options()
self.target = os.getenv("CARGO_BUILD_TARGET")

def run(self):
if self.distribution.rust_extensions:
log.info("running build_rust")
build_rust = self.get_finalized_command("build_rust")
build_rust.inplace = self.inplace
build_rust.plat_name = self.plat_name
build_rust.target = self.target
build_rust.verbose = self.verbose
build_rust.run()

Expand Down Expand Up @@ -157,9 +166,17 @@ def finalize_options(self):

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

# this is for console entries
class bdist_wheel_rust_extension(bdist_wheel_base_class):
user_options = bdist_wheel_options

def initialize_options(self):
super().initialize_options()
self.target = os.getenv("CARGO_BUILD_TARGET")

def finalize_options(self):
scripts = []
for ext in self.distribution.rust_extensions:
Expand Down

0 comments on commit 956e41c

Please sign in to comment.