Skip to content

Commit

Permalink
Extract common triple decomposition logic into a helper method that r…
Browse files Browse the repository at this point in the history
…eturns a struct with the fields populated.
  • Loading branch information
Denis Koroskin authored and korDen committed Mar 9, 2022
1 parent f8a0379 commit 75ea0ae
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions rust/platform/triple_mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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

0 comments on commit 75ea0ae

Please sign in to comment.