|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# getNonPortableDistroRid |
| 4 | +# |
| 5 | +# Input: |
| 6 | +# targetOs: (str) |
| 7 | +# targetArch: (str) |
| 8 | +# rootfsDir: (str) |
| 9 | +# |
| 10 | +# Return: |
| 11 | +# non-portable rid |
| 12 | +getNonPortableDistroRid() |
| 13 | +{ |
| 14 | + local targetOs="$1" |
| 15 | + local targetArch="$2" |
| 16 | + local rootfsDir="$3" |
| 17 | + local nonPortableRid="" |
| 18 | + |
| 19 | + if [ "$targetOs" = "linux" ]; then |
| 20 | + if [ -e "${rootfsDir}/etc/os-release" ]; then |
| 21 | + source "${rootfsDir}/etc/os-release" |
| 22 | + |
| 23 | + if [[ "${ID}" == "rhel" || "${ID}" == "rocky" || "${ID}" == "alpine" ]]; then |
| 24 | + # remove the last version digit |
| 25 | + VERSION_ID="${VERSION_ID%.*}" |
| 26 | + fi |
| 27 | + |
| 28 | + if [[ "${VERSION_ID:-}" =~ ^([[:digit:]]|\.)+$ ]]; then |
| 29 | + nonPortableRid="${ID}.${VERSION_ID}-${targetArch}" |
| 30 | + else |
| 31 | + # Rolling release distros either do not set VERSION_ID, set it as blank or |
| 32 | + # set it to non-version looking string (such as TEMPLATE_VERSION_ID on ArchLinux); |
| 33 | + # so omit it here to be consistent with everything else. |
| 34 | + nonPortableRid="${ID}-${targetArch}" |
| 35 | + fi |
| 36 | + |
| 37 | + elif [ -e "${rootfsDir}/android_platform" ]; then |
| 38 | + source "$rootfsDir"/android_platform |
| 39 | + nonPortableRid="$RID" |
| 40 | + fi |
| 41 | + fi |
| 42 | + |
| 43 | + if [ "$targetOs" = "freebsd" ]; then |
| 44 | + # $rootfsDir can be empty. freebsd-version is shell script and it should always work. |
| 45 | + __freebsd_major_version=$($rootfsDir/bin/freebsd-version | { read v; echo "${v%%.*}"; }) |
| 46 | + nonPortableRid="freebsd.$__freebsd_major_version-${targetArch}" |
| 47 | + elif command -v getprop && getprop ro.product.system.model 2>&1 | grep -qi android; then |
| 48 | + __android_sdk_version=$(getprop ro.build.version.sdk) |
| 49 | + nonPortableRid="android.$__android_sdk_version-${targetArch}" |
| 50 | + elif [ "$targetOs" = "illumos" ]; then |
| 51 | + __uname_version=$(uname -v) |
| 52 | + case "$__uname_version" in |
| 53 | + omnios-*) |
| 54 | + __omnios_major_version=$(echo "${__uname_version:8:2}") |
| 55 | + nonPortableRid=omnios."$__omnios_major_version"-"$targetArch" |
| 56 | + ;; |
| 57 | + joyent_*) |
| 58 | + __smartos_major_version=$(echo "${__uname_version:7:4}") |
| 59 | + nonPortableRid=smartos."$__smartos_major_version"-"$targetArch" |
| 60 | + ;; |
| 61 | + illumos_*) |
| 62 | + nonPortableRid=openindiana-"$targetArch" |
| 63 | + ;; |
| 64 | + esac |
| 65 | + elif [ "$targetOs" = "solaris" ]; then |
| 66 | + __uname_version=$(uname -v) |
| 67 | + __solaris_major_version=$(echo "${__uname_version%.*}") |
| 68 | + nonPortableRid=solaris."$__solaris_major_version"-"$targetArch" |
| 69 | + elif [ "$targetOs" = "haiku" ]; then |
| 70 | + __uname_release=$(uname -r) |
| 71 | + nonPortableRid=haiku.r"$__uname_release"-"$targetArch" |
| 72 | + fi |
| 73 | + |
| 74 | + echo "$(echo $nonPortableRid | tr '[:upper:]' '[:lower:]')" |
| 75 | +} |
| 76 | + |
| 77 | +# initDistroRidGlobal |
| 78 | +# |
| 79 | +# Input: |
| 80 | +# os: (str) |
| 81 | +# arch: (str) |
| 82 | +# isPortable: (int) |
| 83 | +# rootfsDir?: (nullable:string) |
| 84 | +# |
| 85 | +# Return: |
| 86 | +# None |
| 87 | +# |
| 88 | +# Notes: |
| 89 | +# |
| 90 | +# It is important to note that the function does not return anything, but it |
| 91 | +# exports the following variables on success: |
| 92 | +# |
| 93 | +# __DistroRid : Non-portable rid of the target platform. |
| 94 | +# __PortableTargetOS : OS-part of the portable rid that corresponds to the target platform. |
| 95 | +# |
| 96 | +initDistroRidGlobal() |
| 97 | +{ |
| 98 | + local targetOs="$1" |
| 99 | + local targetArch="$2" |
| 100 | + local isPortable="$3" |
| 101 | + local rootfsDir="" |
| 102 | + if [ "$#" -ge 4 ]; then |
| 103 | + rootfsDir="$4" |
| 104 | + fi |
| 105 | + |
| 106 | + if [ -n "${rootfsDir}" ]; then |
| 107 | + # We may have a cross build. Check for the existence of the rootfsDir |
| 108 | + if [ ! -e "${rootfsDir}" ]; then |
| 109 | + echo "Error rootfsDir has been passed, but the location is not valid." |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | + fi |
| 113 | + |
| 114 | + __DistroRid=$(getNonPortableDistroRid "${targetOs}" "${targetArch}" "${rootfsDir}") |
| 115 | + |
| 116 | + if [ -z "${__PortableTargetOS:-}" ]; then |
| 117 | + __PortableTargetOS="$targetOs" |
| 118 | + |
| 119 | + STRINGS="$(command -v strings || true)" |
| 120 | + if [ -z "$STRINGS" ]; then |
| 121 | + STRINGS="$(command -v llvm-strings || true)" |
| 122 | + fi |
| 123 | + |
| 124 | + # Check for musl-based distros (e.g Alpine Linux, Void Linux). |
| 125 | + if "${rootfsDir}/usr/bin/ldd" --version 2>&1 | grep -q musl || |
| 126 | + ( [ -n "$STRINGS" ] && "$STRINGS" "${rootfsDir}/usr/bin/ldd" 2>&1 | grep -q musl ); then |
| 127 | + __PortableTargetOS="linux-musl" |
| 128 | + fi |
| 129 | + fi |
| 130 | + |
| 131 | + export __DistroRid __PortableTargetOS |
| 132 | +} |
0 commit comments