[Tizen] build fix for clang-10 (#331)
[platform/upstream/coreclr.git] / init-distro-rid.sh
1 #!/usr/bin/env bash
2
3 # initNonPortableDistroRid
4 #
5 # Input:
6 #   buildOs: (str)
7 #   buildArch: (str)
8 #   isPortable: (int)
9 #   rootfsDir: (str)
10 #
11 # Return:
12 #   None
13 #
14 # Notes:
15 #
16 # initNonPortableDistroRid will attempt to initialize a non portable rid. These
17 # rids are specific to distros need to build the product/package and consume
18 # them on the same platform.
19 #
20 # If -portablebuild=false is passed a non-portable rid will be created for any
21 # distro.
22 #
23 # Below is the list of current non-portable platforms.
24 #
25 # Builds from the following *must* be non-portable:
26 #
27 #   |    OS     |           Expected RID            |
28 #   -------------------------------------------------
29 #   |   rhel6   |           rhel.6-x64              |
30 #   |  alpine*  |        linux-musl-(arch)          |
31 #   |  freeBSD  |        freebsd.(version)-x64      |
32 #
33 # It is important to note that the function does not return anything, but it 
34 # will set __DistroRid if there is a non-portable distro rid to be used.
35 #
36 initNonPortableDistroRid()
37 {
38     # Make sure out parameter is cleared.
39     __DistroRid=
40
41     local buildOs=$1
42     local buildArch=$2
43     local isPortable=$3
44     local rootfsDir=$4
45
46     if [ "$buildOs" = "Linux" ]; then
47         # RHEL 6 is the only distro we will check redHat release for.
48         if [ -e "${rootfsDir}/etc/os-release" ]; then
49             source "${rootfsDir}/etc/os-release"
50
51             # We have forced __PortableBuild=0. This is because -portablebuld
52             # has been passed as false.
53             if (( ${isPortable} == 0 )); then
54                 if [ "${ID}" == "rhel" ]; then
55                     # remove the last version digit     
56                     VERSION_ID=${VERSION_ID%.*}
57                 fi
58
59                 if [ -z ${VERSION_ID+x} ]; then
60                         # Rolling release distros do not set VERSION_ID, so omit
61                         # it here to be consistent with everything else.
62                         nonPortableBuildID="${ID}-${buildArch}"
63                 else
64                         nonPortableBuildID="${ID}.${VERSION_ID}-${buildArch}"
65                 fi
66             fi
67             
68         elif [ -e "${rootfsDir}/etc/redhat-release" ]; then
69             local redhatRelease=$(<${rootfsDir}/etc/redhat-release)
70
71             if [[ "${redhatRelease}" == "CentOS release 6."* || "$redhatRelease" == "Red Hat Enterprise Linux Server release 6."* ]]; then
72                 nonPortableBuildID="rhel.6-${buildArch}"
73             fi
74         elif [ -e "${rootfsDir}/android_platform" ]; then
75             source $rootfsDir/android_platform
76             nonPortableBuildID="$RID"
77         fi
78     fi
79
80     if [ "$buildOs" = "FreeBSD" ]; then
81         __freebsd_version=`sysctl -n kern.osrelease | cut -f1 -d'.'`
82         nonPortableBuildID="freebsd.$__freebsd_version-${buildArch}"
83     fi
84
85     if [ "${nonPortableBuildID}" != "" ]; then
86         export __DistroRid=${nonPortableBuildID}
87
88         # We are using a non-portable build rid. Force __PortableBuild to false.
89         export __PortableBuild=0
90     fi
91 }
92
93
94 # initDistroRidGlobal
95 #
96 # Input:
97 #   os: (str)
98 #   arch: (str)
99 #   isPortable: (int)
100 #   rootfsDir?: (nullable:string)
101 #
102 # Return:
103 #   None
104 #
105 # Notes:
106 #
107 # The following out parameters are returned
108 #
109 #   __DistroRid
110 #   __PortableBuild
111 #
112 initDistroRidGlobal()
113 {
114     # __DistroRid must be set at the end of the function.
115     # Previously we would create a variable __HostDistroRid and/or __DistroRid.
116     #
117     # __HostDistroRid was used in the case of a non-portable build, it has been
118     # deprecated. Now only __DistroRid is supported. It will be used for both
119     # portable and non-portable rids and will be used in build-packages.sh
120
121     local buildOs=$1
122     local buildArch=$2
123     local isPortable=$3
124     local rootfsDir=$4
125
126     # Setup whether this is a crossbuild. We can find this out if rootfsDir
127     # is set. 
128     local isCrossBuild=0
129
130     if [ -z "${rootfsDir}" ]; then
131         isCrossBuild=0
132     else
133         # We may have a cross build. Check for the existance of the rootfsDir
134         if [ -e ${rootfsDir} ]; then
135             isCrossBuild=1
136         else
137             echo "Error rootfsDir has been passed, but the location is not valid."
138             exit 1
139         fi
140     fi
141
142     initNonPortableDistroRid ${buildOs} ${buildArch} ${isPortable} ${rootfsDir}
143
144     if [ -z "${__DistroRid}" ]; then
145         # The non-portable build rid was not set. Set the portable rid.
146
147         export __PortableBuild=1
148         local distroRid=""
149
150         # Check for alpine. It is the only portable build that will will have
151         # its name in the portable build rid.
152         if [ -e "${rootfsDir}/etc/os-release" ]; then
153             source "${rootfsDir}/etc/os-release"
154             if [ "${ID}" = "alpine" ]; then
155                 distroRid="linux-musl-${buildArch}"
156             fi
157         fi
158
159         if [ "${distroRid}" == "" ]; then
160             if [ "$buildOs" = "Linux" ]; then
161                 distroRid="linux-$buildArch"
162             elif [ "$buildOs" = "OSX" ]; then
163                 distroRid="osx-$buildArch"
164             elif [ "$buildOs" = "FreeBSD" ]; then
165                 distroRid="freebsd-$buildArch"
166             fi
167         fi
168
169         export __DistroRid=${distroRid}
170     fi
171
172     if [ -z "$__DistroRid" ]; then
173         echo "DistroRid is not set. This is almost certainly an error"
174
175         exit 1
176     else
177         echo "__DistroRid: ${__DistroRid}"
178         echo "__RuntimeId: ${__DistroRid}"
179         
180         export __RuntimeId=${__DistroRid}
181     fi
182 }