From 120e4276bb730f48353a87c7f793e8a5adf3a864 Mon Sep 17 00:00:00 2001 From: yozu Date: Wed, 29 Mar 2023 17:47:21 +0900 Subject: [PATCH 1/5] Fix compilation bug in toolchain >= 1.70.0 Fix #320 --- setuptools_rust/build.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/setuptools_rust/build.py b/setuptools_rust/build.py index a0685efa..9303caf6 100644 --- a/setuptools_rust/build.py +++ b/setuptools_rust/build.py @@ -28,7 +28,15 @@ 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 _detect_toolchain_1_70_or_later() -> bool: + version = get_rust_version() + return version.major > 1 or ( + version.major == 1 and version.minor >= 70 + ) class build_rust(RustCommand): @@ -142,6 +150,7 @@ def build_extension( quiet = self.qbuild or ext.quiet debug = self._is_debug_build(ext) + is_toolchain_1_70_or_later = _detect_toolchain_1_70_or_later() cargo_args = self._cargo_args( ext=ext, target_triple=target_triple, release=not debug, quiet=quiet @@ -160,11 +169,18 @@ def build_extension( ] else: - rustc_args = [ - "--crate-type", - "cdylib", - *ext.rustc_flags, - ] + # If toolchain >= 1.70.0, use '--crate-type' option of cargo. + # See https://github.com/PyO3/setuptools-rust/issues/320 + if is_toolchain_1_70_or_later: + 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 +205,9 @@ def build_extension( ): rustc_args.extend(["-C", f"link-args=-sSIDE_MODULE=2 -sWASM_BIGINT"]) + if is_toolchain_1_70_or_later: + cargo_args.extend(["--crate-type", "cdylib"]) + command = [ self.cargo, "rustc", From bf88283bc5f65ed9a9ca80af98edde8e876d26ed Mon Sep 17 00:00:00 2001 From: yozu Date: Wed, 29 Mar 2023 20:14:42 +0900 Subject: [PATCH 2/5] Fix fmt --- setuptools_rust/build.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/setuptools_rust/build.py b/setuptools_rust/build.py index 9303caf6..f215d5b1 100644 --- a/setuptools_rust/build.py +++ b/setuptools_rust/build.py @@ -28,15 +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, get_rust_version +from .rustc_info import ( + get_rust_host, + get_rust_target_list, + get_rustc_cfgs, + get_rust_version, +) from semantic_version import Version def _detect_toolchain_1_70_or_later() -> bool: version = get_rust_version() - return version.major > 1 or ( - version.major == 1 and version.minor >= 70 - ) + + if version is None: + return False + + return version.major > 1 or (version.major == 1 and version.minor >= 70) # type: ignore class build_rust(RustCommand): From df8ef5d33e31714b9a652f2755856eaf84b44bd5 Mon Sep 17 00:00:00 2001 From: yozu Date: Mon, 24 Apr 2023 15:29:06 +0900 Subject: [PATCH 3/5] Do not add --crate-name option if it present --- setuptools_rust/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setuptools_rust/build.py b/setuptools_rust/build.py index f215d5b1..8c38aa21 100644 --- a/setuptools_rust/build.py +++ b/setuptools_rust/build.py @@ -212,7 +212,7 @@ def build_extension( ): rustc_args.extend(["-C", f"link-args=-sSIDE_MODULE=2 -sWASM_BIGINT"]) - if is_toolchain_1_70_or_later: + if is_toolchain_1_70_or_later and "--crate-type" not in cargo_args: cargo_args.extend(["--crate-type", "cdylib"]) command = [ From b7f8a7562aef55f2ffa620f5b2def487e1414eb6 Mon Sep 17 00:00:00 2001 From: yozu Date: Wed, 26 Apr 2023 18:05:49 +0900 Subject: [PATCH 4/5] Enable new --crate-type option on cargo >= 1.64 --- setuptools_rust/build.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/setuptools_rust/build.py b/setuptools_rust/build.py index 8c38aa21..8f0afcfe 100644 --- a/setuptools_rust/build.py +++ b/setuptools_rust/build.py @@ -37,13 +37,13 @@ from semantic_version import Version -def _detect_toolchain_1_70_or_later() -> bool: +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 >= 70) # type: ignore + return version.major > 1 or (version.major == 1 and version.minor >= 64) # type: ignore class build_rust(RustCommand): @@ -157,7 +157,7 @@ def build_extension( quiet = self.qbuild or ext.quiet debug = self._is_debug_build(ext) - is_toolchain_1_70_or_later = _detect_toolchain_1_70_or_later() + 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 @@ -176,9 +176,9 @@ def build_extension( ] else: - # If toolchain >= 1.70.0, use '--crate-type' option of cargo. + # If toolchain >= 1.64.0, use '--crate-type' option of cargo. # See https://github.com/PyO3/setuptools-rust/issues/320 - if is_toolchain_1_70_or_later: + if use_cargo_crate_type: rustc_args = [ *ext.rustc_flags, ] @@ -212,7 +212,7 @@ def build_extension( ): rustc_args.extend(["-C", f"link-args=-sSIDE_MODULE=2 -sWASM_BIGINT"]) - if is_toolchain_1_70_or_later and "--crate-type" not in cargo_args: + if use_cargo_crate_type and "--crate-type" not in cargo_args: cargo_args.extend(["--crate-type", "cdylib"]) command = [ From 987e1ae55ce91074f368affce850532cd5d836e1 Mon Sep 17 00:00:00 2001 From: yozu Date: Wed, 26 Apr 2023 18:08:01 +0900 Subject: [PATCH 5/5] modify changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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