Skip to content

Commit

Permalink
Use --crate-type cargo option on >= 1.64.0 (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuo-ozu authored Apr 26, 2023
1 parent 684bf63 commit f2224c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Fixed
- Fix a bug where rebuilding the library would cause any running processes using it to segfault. [#295](https://github.com/PyO3/setuptools-rust/pull/295)
- Fix `setup.cfg` format for compatibility with "poetry==1.4.0". [#319](https://github.com/PyO3/setuptools-rust/pull/319)
- Fix to use `--crate-type` option of cargo if "toolchain >= 1.64". [#322](https://github.com/PyO3/setuptools-rust/pull/322)

## 1.5.2 (2022-09-19)
### Fixed
Expand Down
38 changes: 32 additions & 6 deletions setuptools_rust/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@
from ._utils import format_called_process_error
from .command import RustCommand
from .extension import Binding, RustBin, RustExtension, Strip
from .rustc_info import get_rust_host, get_rust_target_list, get_rustc_cfgs
from .rustc_info import (
get_rust_host,
get_rust_target_list,
get_rustc_cfgs,
get_rust_version,
)
from semantic_version import Version


def _check_cargo_supports_crate_type_option() -> bool:
version = get_rust_version()

if version is None:
return False

return version.major > 1 or (version.major == 1 and version.minor >= 64) # type: ignore


class build_rust(RustCommand):
Expand Down Expand Up @@ -142,6 +157,7 @@ def build_extension(

quiet = self.qbuild or ext.quiet
debug = self._is_debug_build(ext)
use_cargo_crate_type = _check_cargo_supports_crate_type_option()

cargo_args = self._cargo_args(
ext=ext, target_triple=target_triple, release=not debug, quiet=quiet
Expand All @@ -160,11 +176,18 @@ def build_extension(
]

else:
rustc_args = [
"--crate-type",
"cdylib",
*ext.rustc_flags,
]
# If toolchain >= 1.64.0, use '--crate-type' option of cargo.
# See https://github.com/PyO3/setuptools-rust/issues/320
if use_cargo_crate_type:
rustc_args = [
*ext.rustc_flags,
]
else:
rustc_args = [
"--crate-type",
"cdylib",
*ext.rustc_flags,
]

# OSX requires special linker arguments
if rustc_cfgs.get("target_os") == "macos":
Expand All @@ -189,6 +212,9 @@ def build_extension(
):
rustc_args.extend(["-C", f"link-args=-sSIDE_MODULE=2 -sWASM_BIGINT"])

if use_cargo_crate_type and "--crate-type" not in cargo_args:
cargo_args.extend(["--crate-type", "cdylib"])

command = [
self.cargo,
"rustc",
Expand Down

0 comments on commit f2224c8

Please sign in to comment.