diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ae33e9f..5a77be84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/setuptools_rust/build.py b/setuptools_rust/build.py index a0685efa..8f0afcfe 100644 --- a/setuptools_rust/build.py +++ b/setuptools_rust/build.py @@ -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): @@ -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 @@ -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": @@ -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",