|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import re, sys |
| 4 | + |
| 5 | +# This script designed to mimick `src/PlatformNames.jl` in `BinaryProvider.jl`, which has |
| 6 | +# a method `platform_key_abi()` to parse uname-like output into something standarized. |
| 7 | + |
| 8 | +if len(sys.argv) < 2: |
| 9 | + print("Usage: %s <host triplet> [<gcc version>]") |
| 10 | + sys.exit(1) |
| 11 | + |
| 12 | +arch_mapping = { |
| 13 | + 'x86_64': '(x86_|amd)64', |
| 14 | + 'i686': "i\\d86", |
| 15 | + 'aarch64': "aarch64", |
| 16 | + 'arm': "arm(v7l)?", |
| 17 | + 'powerpc64le': "p(ower)?pc64le", |
| 18 | +} |
| 19 | +platform_mapping = { |
| 20 | + 'darwin': "-apple-darwin[\\d\\.]*", |
| 21 | + 'freebsd': "-(.*-)?freebsd[\\d\\.]*", |
| 22 | + 'windows': "-w64-mingw32", |
| 23 | + 'linux': "-(.*-)?linux", |
| 24 | +} |
| 25 | +libc_mapping = { |
| 26 | + 'blank_libc': "", |
| 27 | + 'gnu': "-gnu", |
| 28 | + 'musl': "-musl", |
| 29 | +} |
| 30 | +call_abi_mapping = { |
| 31 | + 'blank_call_abi': "", |
| 32 | + 'eabihf': "eabihf", |
| 33 | +} |
| 34 | +gcc_version_mapping = { |
| 35 | + 'blank_gcc': "", |
| 36 | + 'gcc4': "-gcc4", |
| 37 | + 'gcc7': "-gcc7", |
| 38 | + 'gcc8': "-gcc8", |
| 39 | +} |
| 40 | +cxx_abi_mapping = { |
| 41 | + 'blank_cxx_abi': "", |
| 42 | + 'cxx03': "-cxx03", |
| 43 | + 'cxx11': "-cxx11", |
| 44 | +} |
| 45 | + |
| 46 | +# Helper function to collapse dictionary of mappings down into a regex of |
| 47 | +# named capture groups joined by "|" operators |
| 48 | +c = lambda mapping: "("+"|".join(["(?P<%s>%s)"%(k,v) for (k, v) in mapping.items()]) + ")" |
| 49 | +mondo_regex = re.compile( |
| 50 | + "^"+ |
| 51 | + c(arch_mapping)+ |
| 52 | + c(platform_mapping)+ |
| 53 | + c(libc_mapping)+ |
| 54 | + c(call_abi_mapping)+ |
| 55 | + c(gcc_version_mapping)+ |
| 56 | + c(cxx_abi_mapping)+ |
| 57 | + "$" |
| 58 | +) |
| 59 | + |
| 60 | +# Apply our mondo regex to our input: |
| 61 | +m = mondo_regex.match(sys.argv[1]) |
| 62 | +if m is None: |
| 63 | + print("ERROR: Unmatchable platform string '%s'!"%(sys.argv[1])) |
| 64 | + sys.exit(1) |
| 65 | + |
| 66 | +# Helper function to find the single named field within the giant regex |
| 67 | +# that is not `nothing` for each mapping we give it. |
| 68 | +def get_field(m, mapping): |
| 69 | + g = m.groupdict() |
| 70 | + for k in mapping: |
| 71 | + if g[k] is not None: |
| 72 | + return k |
| 73 | + |
| 74 | +arch = get_field(m, arch_mapping) |
| 75 | +platform = get_field(m, platform_mapping) |
| 76 | +libc = get_field(m, libc_mapping) |
| 77 | +call_abi = get_field(m, call_abi_mapping) |
| 78 | +gcc_version = get_field(m, gcc_version_mapping) |
| 79 | +cxx_abi = get_field(m, cxx_abi_mapping) |
| 80 | + |
| 81 | +def r(x): |
| 82 | + x = x.replace("blank_call_abi", "") |
| 83 | + x = x.replace("blank_gcc", "") |
| 84 | + x = x.replace("blank_cxx_abi", "") |
| 85 | + x = x.replace("blank_libc", "") |
| 86 | + return x |
| 87 | + |
| 88 | +def p(x): |
| 89 | + # These contain characters that can't be easily represented as |
| 90 | + # capture group names, unfortunately: |
| 91 | + os_remapping = { |
| 92 | + 'darwin': 'apple-darwin14', |
| 93 | + 'windows': 'w64-mingw32', |
| 94 | + 'freebsd': 'unknown-freebsd11.1', |
| 95 | + } |
| 96 | + x = r(x) |
| 97 | + if x: |
| 98 | + for k in os_remapping: |
| 99 | + x = x.replace(k, os_remapping[k]) |
| 100 | + return '-' + x |
| 101 | + return x |
| 102 | + |
| 103 | +# If the user passes in a GCC version (like 8.2.0) use that to force a |
| 104 | +# "-gcc8" tag at the end of the triplet, but only if it has otherwise |
| 105 | +# not been specified |
| 106 | +if gcc_version == "blank_gcc": |
| 107 | + if len(sys.argv) == 3: |
| 108 | + gcc_version = { |
| 109 | + "4": "gcc4", |
| 110 | + "5": "gcc4", |
| 111 | + "6": "gcc4", |
| 112 | + "7": "gcc7", |
| 113 | + "8": "gcc8", |
| 114 | + }[sys.argv[2][0]] |
| 115 | + |
| 116 | + |
| 117 | +print(arch+p(platform)+p(libc)+r(call_abi)+p(gcc_version)+p(cxx_abi)) |
| 118 | + |
| 119 | +# Testing suite: |
| 120 | +# triplets="i686-w64-mingw32 x86_64-pc-linux-musl arm-linux-musleabihf x86_64-linux-gnu arm-linux-gnueabihf x86_64-apple-darwin14 x86_64-unknown-freebsd11.1" |
| 121 | +# for t in $triplets; do |
| 122 | +# if [[ $(./normalize_triplet.py "$t") != "$t" ]]; then |
| 123 | +# echo "ERROR: Failed test on $t" |
| 124 | +# fi |
| 125 | +# done |
0 commit comments