diff options
author | Chris Wailes <chriswailes@google.com> | 2024-04-15 15:51:06 -0700 |
---|---|---|
committer | Chris Wailes <chriswailes@google.com> | 2024-04-15 15:53:24 -0700 |
commit | 7d8c9a0bf038dea593936fe27b97f3a4840cf3b5 (patch) | |
tree | 15dc765e892e0ad34a1ea636ee770c07e0c2e1f1 | |
parent | 5185dd809c22dbc48a1bab07c8ced70c9e672118 (diff) | |
download | android_rust-7d8c9a0bf038dea593936fe27b97f3a4840cf3b5.tar.gz |
Better host detection/handling for Rust/Clang toolchains
This CL will cause a toolchain to raise an exception if a host hasn't
been explicitly set and autodetection fails.
Test: ./build.py
Change-Id: Ib3bde3bf75701d3779011649b84d3dfbc0019f00
-rw-r--r-- | src/android_rust/build_platform.py | 47 | ||||
-rw-r--r-- | src/android_rust/paths.py | 6 | ||||
-rw-r--r-- | src/android_rust/toolchains.py | 40 | ||||
-rw-r--r-- | src/android_rust/upstream_tests.py | 3 | ||||
-rwxr-xr-x | tools/build.py | 4 |
5 files changed, 66 insertions, 34 deletions
diff --git a/src/android_rust/build_platform.py b/src/android_rust/build_platform.py index 6cc7678..f2ab4e0 100644 --- a/src/android_rust/build_platform.py +++ b/src/android_rust/build_platform.py @@ -18,20 +18,28 @@ building under for the purpose of selecting the correct paths and targets. """ +from enum import Enum import platform -def system() -> str: +class PlatformType(Enum): + DARWIN = 1 + LINUX = 2 + WINDOWS = 3 + + +def system() -> PlatformType: """Returns a canonicalized OS type of the machine executing the script. Will be one of 'linux', 'darwin', or 'windows'.""" - sys = platform.system() - if sys == "Linux": - return "linux" - if sys == "Darwin": - return "darwin" - if sys == "Windows": - return "windows" - raise RuntimeError("Unknown System: " + sys) + match platform.system(): + case "Darwin": + return PlatformType.DARWIN + case "Linux": + return PlatformType.LINUX + case "Windows": + return PlatformType.WINDOWS + case sys: + raise RuntimeError(f"Unknown System: {sys}") def is_linux() -> bool: @@ -49,26 +57,27 @@ def is_windows() -> bool: def prebuilt() -> str: """Returns the prebuilt subdirectory for prebuilts which do not use subarch specialization.""" - return system() + "-x86" + return system().name.lower() + "-x86" def prebuilt_full() -> str: """Returns the prebuilt subdirectory for prebuilts which have subarch specialization available. """ - return system() + "-x86_64" + return system().name.lower() + "-x86_64" def triple() -> str: """Returns the target triple of the build environment.""" - build_os = system() - if build_os == "linux": - return "x86_64-unknown-linux-gnu" - if build_os == "darwin": - return "x86_64-apple-darwin" - if build_os == "windows": - return "x86_64-pc-windows-gnu" - raise RuntimeError("Unknown OS: " + build_os) + match system(): + case PlatformType.LINUX: + return "x86_64-unknown-linux-gnu" + case PlatformType.DARWIN: + return "x86_64-apple-darwin" + case PlatformType.WINDOWS: + return "x86_64-pc-windows-gnu" + case _: + raise RuntimeError("Unreachable") def rpath_origin() -> str: diff --git a/src/android_rust/paths.py b/src/android_rust/paths.py index 3201240..ec6c8ed 100644 --- a/src/android_rust/paths.py +++ b/src/android_rust/paths.py @@ -21,11 +21,11 @@ import android_rust.build_platform as build_platform RUST_STAGE0_VERSION: str = "1.77.1" RUST_STAGE0_TRIPLE: str match build_platform.system(): - case "darwin": + case build_platform.PlatformType.DARWIN: RUST_STAGE0_TRIPLE = "x86_64-apple-darwin" - case "linux": + case build_platform.PlatformType.LINUX: RUST_STAGE0_TRIPLE = "x86_64-unknown-linux-gnu" - case "windows": + case build_platform.PlatformType.WINDOWS: RUST_STAGE0_TRIPLE = "x86_64-pc-windows-gnu" case _: RUST_STAGE0_TRIPLE = "unreachable" diff --git a/src/android_rust/toolchains.py b/src/android_rust/toolchains.py index fa9585e..cbf2f2d 100644 --- a/src/android_rust/toolchains.py +++ b/src/android_rust/toolchains.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import inspect from pathlib import Path +from android_rust.build_platform import PlatformType from android_rust.paths import CLANG_VERSION, LLVM_PREBUILT_PATH_HOST, LLVM_PREBUILT_PATH_LINUX, LLVM_PREBUILT_PATH_WINDOWS, RUST_HOST_STAGE0_PATH @@ -32,10 +32,21 @@ class ClangToolchain: "readelf", ] - def __init__(self, base_dir: Path): + def __init__(self, base_dir: Path, host: PlatformType | None = None): + if host is None: + if "linux-x86" in base_dir.parts: + host = PlatformType.LINUX + elif "darwin-x86" in base_dir.parts: + host = PlatformType.DARWIN + elif "windows-x86" in base_dir.parts: + host = PlatformType.WINDOWS + else: + raise RuntimeError(f"Toolchain host autodetection failed for {base_dir}") + self.base_dir = base_dir self.bin_dir = base_dir / "bin" - self.exe = ".exe" if self.is_windows() else "" + self.host = host + self.exe = ".exe" if self.host == PlatformType.WINDOWS else "" for method_name in self.__class__.PATH_METHODS: path = getattr(self, method_name)() @@ -109,10 +120,10 @@ class ClangToolchain: # def is_linux(self) -> bool: - return "linux" in str(self.base_dir) + return self.host == PlatformType.LINUX def is_windows(self) -> bool: - return "windows" in str(self.base_dir) + return self.host == PlatformType.WINDOWS class RustToolchain: @@ -121,10 +132,21 @@ class RustToolchain: "rustc", ] - def __init__(self, base_dir: Path): + def __init__(self, base_dir: Path, host: PlatformType | None = None): + if host is None: + if "linux-x86" in base_dir.parts: + host = PlatformType.LINUX + elif "darwin-x86" in base_dir.parts: + host = PlatformType.DARWIN + elif "windows-x86" in base_dir.parts: + host = PlatformType.WINDOWS + else: + raise RuntimeError(f"Toolchain host autodetection failed for {base_dir}") + self.base_dir = base_dir self.bin_dir = base_dir / "bin" - self.exe = ".exe" if self.is_windows() else "" + self.host = host + self.exe = ".exe" if self.host == PlatformType.WINDOWS else "" for method_name in self.__class__.PATH_METHODS: path = getattr(self, method_name)() @@ -146,10 +168,10 @@ class RustToolchain: # def is_linux(self) -> bool: - return "linux" in str(self.base_dir) + return self.host == PlatformType.LINUX def is_windows(self) -> bool: - return "windows" in str(self.base_dir) + return self.host == PlatformType.WINDOWS LLVM_TOOLCHAIN_HOST = ClangToolchain(LLVM_PREBUILT_PATH_HOST) diff --git a/src/android_rust/upstream_tests.py b/src/android_rust/upstream_tests.py index 31b16ab..67a316c 100644 --- a/src/android_rust/upstream_tests.py +++ b/src/android_rust/upstream_tests.py @@ -17,6 +17,7 @@ from pathlib import Path import android_rust.build_platform as build_platform +from android_rust.build_platform import PlatformType disabled_tests = [ "--exclude", Path("compiler/rustc_target"), @@ -96,7 +97,7 @@ disabled_tests = [ ] match build_platform.system(): - case "darwin": + case PlatformType.DARWIN: # Darwin failures on 1.73.0 due to dylib error # https://buganizer.corp.google.com/issues/311035877 disabled_tests += [ diff --git a/tools/build.py b/tools/build.py index ad8d204..2c9cc6e 100755 --- a/tools/build.py +++ b/tools/build.py @@ -332,7 +332,7 @@ def initialize_rust_stage0(args: argparse.Namespace) -> None: archive_extract(args.rust_stage0, extract_dir) args.rust_stage0 = extract_dir - args.rust_stage0 = RustToolchain(args.rust_stage0) + args.rust_stage0 = RustToolchain(args.rust_stage0, host=build_platform.system()) def extract_ndk_archive(archive_path: Path) -> Path: @@ -599,7 +599,7 @@ def main(argv: list[str] | None = None) -> int: # if args.host == build_platform.triple(): - new_toolchain = RustToolchain(OUT_PATH_PACKAGE) + new_toolchain = RustToolchain(OUT_PATH_PACKAGE, host=build_platform.system()) cargo_deny = cargo.Crate( WORKSPACE_PATH / "toolchain" / "cargo-deny", env, |