From f8a037913b01bed2fcf8f0379a96f6c9c4d66880 Mon Sep 17 00:00:00 2001 From: Denis Koroskin Date: Tue, 8 Mar 2022 20:57:58 -0800 Subject: [PATCH 1/4] Allow specifying additional target triples in rust_register_toolchains() and fix androideabi system In addition to host target, "wasm32-unknown-unknown" and "wasm32-wasi" targets are being installed, which includes downloading corresponding rust-std libraries. This CL allow specifying custom additional target triples by introducing a new parameter to rust_register_toolchains(), which defaults to ["wasm32-unknown-unknown" and "wasm32-wasi"] for compatibility. As an example, it is now possible to compile for Android if ["armv7-linux-androideabi"] is specified as an extra_target_triples. Specifically for Android, most of the required infrastructure was already in place. For example, @rules_rust//rust/platform:triple_mappings.bzl already has all the necessary information to compile Android libraries (e.g. _SYSTEM_TO_STDLIB_LINKFLAGS). However, "armv7-linux-androideabi" is not a 'proper' target tripple because 'androideabi' is not a proper system. As such, special handling is required to map 'androideabi' back to 'android'. --- rust/platform/triple_mappings.bzl | 9 ++++++++- rust/repositories.bzl | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/rust/platform/triple_mappings.bzl b/rust/platform/triple_mappings.bzl index 0614eac34e..3b45896d69 100644 --- a/rust/platform/triple_mappings.bzl +++ b/rust/platform/triple_mappings.bzl @@ -212,7 +212,10 @@ def triple_to_system(triple): if len(component_parts) < 3: fail("Expected target triple to contain at least three sections separated by '-'") - return component_parts[2] + system = component_parts[2] + if (system == "androideabi"): + system = "android" + return system def triple_to_arch(triple): """Returns a system architecture name for a given platform triple @@ -290,6 +293,10 @@ def triple_to_constraint_set(triple): system = component_parts[2] abi = None + if system == "androideabi": + system = "android" + cpu_arch = "arm" + if len(component_parts) == 4: abi = component_parts[3] diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 809e43a24d..99f2f51ed3 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -68,6 +68,7 @@ def rust_register_toolchains( register_toolchains = True, rustfmt_version = None, sha256s = None, + extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"], urls = DEFAULT_STATIC_RUST_URL_TEMPLATES, version = rust_common.default_version): """Emits a default set of toolchains for Linux, MacOS, and Freebsd @@ -97,6 +98,7 @@ def rust_register_toolchains( register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets. rustfmt_version (str, optional): The version of rustfmt. Either "nightly", "beta", or an exact version. Defaults to `version` if not specified. sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. + extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support. urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). version (str, optional): The version of Rust. Either "nightly", "beta", or an exact version. Defaults to a modern version. """ @@ -113,7 +115,7 @@ def rust_register_toolchains( dev_components = dev_components, edition = edition, exec_triple = exec_triple, - extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"], + extra_target_triples = extra_target_triples, include_rustc_srcs = include_rustc_srcs, iso_date = iso_date, register_toolchain = register_toolchains, From 75ea0ae5e57afc504c288d10f6f60fd1cbcde8cc Mon Sep 17 00:00:00 2001 From: Denis Koroskin Date: Tue, 8 Mar 2022 20:57:58 -0800 Subject: [PATCH 2/4] Extract common triple decomposition logic into a helper method that returns a struct with the fields populated. --- rust/platform/triple_mappings.bzl | 83 ++++++++++++++----------------- 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/rust/platform/triple_mappings.bzl b/rust/platform/triple_mappings.bzl index 3b45896d69..a6568fffd1 100644 --- a/rust/platform/triple_mappings.bzl +++ b/rust/platform/triple_mappings.bzl @@ -196,6 +196,36 @@ def abi_to_constraints(abi): # figure out how they're doing this return [] +def triple_to_struct(triple): + if triple == "wasm32-wasi": + return struct( + cpu_arch = "wasi", + vendor = None, + system = "wasi", + abi = None, + ) + + component_parts = triple.split("-") + if len(component_parts) < 3: + fail("Expected target triple to contain at least three sections separated by '-'") + + cpu_arch = component_parts[0] + vendor = component_parts[1] + system = component_parts[2] + if system == "androideabi": + system = "android" + + abi = None + if len(component_parts) >= 4: + abi = component_parts[3] + + return struct( + cpu_arch = cpu_arch, + vendor = vendor, + system = system, + abi = abi, + ) + def triple_to_system(triple): """Returns a system name for a given platform triple @@ -205,17 +235,7 @@ def triple_to_system(triple): Returns: str: A system name """ - if triple == "wasm32-wasi": - return "wasi" - - component_parts = triple.split("-") - if len(component_parts) < 3: - fail("Expected target triple to contain at least three sections separated by '-'") - - system = component_parts[2] - if (system == "androideabi"): - system = "android" - return system + return triple_to_struct(triple).system def triple_to_arch(triple): """Returns a system architecture name for a given platform triple @@ -226,14 +246,7 @@ def triple_to_arch(triple): Returns: str: A cpu architecture """ - if triple == "wasm32-wasi": - return "wasi" - - component_parts = triple.split("-") - if len(component_parts) < 3: - fail("Expected target triple to contain at least three sections separated by '-'") - - return component_parts[0] + return triple_to_struct(triple).cpu_arch def triple_to_abi(triple): """Returns a system abi name for a given platform triple @@ -244,13 +257,7 @@ def triple_to_abi(triple): Returns: str: The triple's abi """ - component_parts = triple.split("-") - if len(component_parts) < 3: - fail("Expected target triple to contain at least three sections separated by '-'") - - if len(component_parts) >= 4: - return component_parts[3] - return None + triple_to_struct(triple).abi def system_to_dylib_ext(system): return _SYSTEM_TO_DYLIB_EXT[system] @@ -284,26 +291,12 @@ def triple_to_constraint_set(triple): "@rules_rust//rust/platform/os:unknown", ] - component_parts = triple.split("-") - if len(component_parts) < 3: - fail("Expected target triple to contain at least three sections separated by '-'") - - cpu_arch = component_parts[0] - vendor = component_parts[1] - system = component_parts[2] - abi = None - - if system == "androideabi": - system = "android" - cpu_arch = "arm" - - if len(component_parts) == 4: - abi = component_parts[3] + triple_struct = triple_to_struct(triple) constraint_set = [] - constraint_set += cpu_arch_to_constraints(cpu_arch) - constraint_set += vendor_to_constraints(vendor) - constraint_set += system_to_constraints(system) - constraint_set += abi_to_constraints(abi) + constraint_set += cpu_arch_to_constraints(triple_struct.cpu_arch) + constraint_set += vendor_to_constraints(triple_struct.vendor) + constraint_set += system_to_constraints(triple_struct.system) + constraint_set += abi_to_constraints(triple_struct.abi) return constraint_set From 0c446118f1cb86500ddb8c1d5bc3f78368283be3 Mon Sep 17 00:00:00 2001 From: Denis Koroskin Date: Tue, 8 Mar 2022 20:57:58 -0800 Subject: [PATCH 3/4] Use triple() function from triple.bzl --- rust/platform/triple.bzl | 4 ++ rust/platform/triple_mappings.bzl | 72 +++++-------------------------- 2 files changed, 15 insertions(+), 61 deletions(-) diff --git a/rust/platform/triple.bzl b/rust/platform/triple.bzl index e5507854b5..13a59a4cab 100644 --- a/rust/platform/triple.bzl +++ b/rust/platform/triple.bzl @@ -38,6 +38,10 @@ def triple(triple): system = component_parts[2] abi = None + if system == "androideabi": + system = "android" + abi = "eabi" + if len(component_parts) == 4: abi = component_parts[3] diff --git a/rust/platform/triple_mappings.bzl b/rust/platform/triple_mappings.bzl index a6568fffd1..fada88486c 100644 --- a/rust/platform/triple_mappings.bzl +++ b/rust/platform/triple_mappings.bzl @@ -1,5 +1,7 @@ """Helpers for constructing supported Rust platform triples""" +load("//rust/platform:triple.bzl", "triple") + # All T1 Platforms should be supported, but aren't, see inline notes. SUPPORTED_T1_PLATFORM_TRIPLES = [ "i686-apple-darwin", @@ -196,68 +198,16 @@ def abi_to_constraints(abi): # figure out how they're doing this return [] -def triple_to_struct(triple): - if triple == "wasm32-wasi": - return struct( - cpu_arch = "wasi", - vendor = None, - system = "wasi", - abi = None, - ) - - component_parts = triple.split("-") - if len(component_parts) < 3: - fail("Expected target triple to contain at least three sections separated by '-'") - - cpu_arch = component_parts[0] - vendor = component_parts[1] - system = component_parts[2] - if system == "androideabi": - system = "android" - - abi = None - if len(component_parts) >= 4: - abi = component_parts[3] - - return struct( - cpu_arch = cpu_arch, - vendor = vendor, - system = system, - abi = abi, - ) - -def triple_to_system(triple): +def triple_to_system(target_triple): """Returns a system name for a given platform triple Args: - triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` + target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` Returns: str: A system name """ - return triple_to_struct(triple).system - -def triple_to_arch(triple): - """Returns a system architecture name for a given platform triple - - Args: - triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` - - Returns: - str: A cpu architecture - """ - return triple_to_struct(triple).cpu_arch - -def triple_to_abi(triple): - """Returns a system abi name for a given platform triple - - Args: - triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` - - Returns: - str: The triple's abi - """ - triple_to_struct(triple).abi + return triple(target_triple).system def system_to_dylib_ext(system): return _SYSTEM_TO_DYLIB_EXT[system] @@ -271,30 +221,30 @@ def system_to_binary_ext(system): def system_to_stdlib_linkflags(system): return _SYSTEM_TO_STDLIB_LINKFLAGS[system] -def triple_to_constraint_set(triple): +def triple_to_constraint_set(target_triple): """Returns a set of constraints for a given platform triple Args: - triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` + target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` Returns: list: A list of constraints (each represented by a list of strings) """ - if triple == "wasm32-wasi": + if target_triple == "wasm32-wasi": return [ "@rules_rust//rust/platform/cpu:wasm32", "@rules_rust//rust/platform/os:wasi", ] - if triple == "wasm32-unknown-unknown": + if target_triple == "wasm32-unknown-unknown": return [ "@rules_rust//rust/platform/cpu:wasm32", "@rules_rust//rust/platform/os:unknown", ] - triple_struct = triple_to_struct(triple) + triple_struct = triple(target_triple) constraint_set = [] - constraint_set += cpu_arch_to_constraints(triple_struct.cpu_arch) + constraint_set += cpu_arch_to_constraints(triple_struct.arch) constraint_set += vendor_to_constraints(triple_struct.vendor) constraint_set += system_to_constraints(triple_struct.system) constraint_set += abi_to_constraints(triple_struct.abi) From 14ea294f0cdfe2b424020e9dedcef716076e37c1 Mon Sep 17 00:00:00 2001 From: Denis Koroskin Date: Wed, 9 Mar 2022 10:39:24 -0800 Subject: [PATCH 4/4] Restore removed methods, and add deprecation message. --- rust/platform/triple_mappings.bzl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/rust/platform/triple_mappings.bzl b/rust/platform/triple_mappings.bzl index fada88486c..852ffe7f86 100644 --- a/rust/platform/triple_mappings.bzl +++ b/rust/platform/triple_mappings.bzl @@ -201,6 +201,8 @@ def abi_to_constraints(abi): def triple_to_system(target_triple): """Returns a system name for a given platform triple + **Deprecated**: Use triple() from triple.bzl directly. + Args: target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` @@ -209,6 +211,32 @@ def triple_to_system(target_triple): """ return triple(target_triple).system +def triple_to_arch(target_triple): + """Returns a system architecture name for a given platform triple + + **Deprecated**: Use triple() from triple.bzl directly. + + Args: + target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` + + Returns: + str: A cpu architecture + """ + return triple(target_triple).arch + +def triple_to_abi(target_triple): + """Returns a system abi name for a given platform triple + + **Deprecated**: Use triple() from triple.bzl directly. + + Args: + target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` + + Returns: + str: The triple's abi + """ + return triple(target_triple).system + def system_to_dylib_ext(system): return _SYSTEM_TO_DYLIB_EXT[system]