[Tizen] Add support for gbs build for i586, x86_64, armv7l, armv7hl, aarch64 (includi...
[platform/upstream/dotnet/runtime.git] / eng / build.sh
1 #!/usr/bin/env bash
2
3 set -ue
4
5 source="${BASH_SOURCE[0]}"
6
7 # resolve $source until the file is no longer a symlink
8 while [[ -h "$source" ]]; do
9   scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
10   source="$(readlink "$source")"
11   # if $source was a relative symlink, we need to resolve it relative to the path where the
12   # symlink file was located
13   [[ $source != /* ]] && source="$scriptroot/$source"
14 done
15 scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
16
17 usage()
18 {
19   echo "Common settings:"
20   echo "  --arch (-a)                     Target platform: x86, x64, arm, armel, arm64, s390x or wasm."
21   echo "                                  [Default: Your machine's architecture.]"
22   echo "  --binaryLog (-bl)               Output binary log."
23   echo "  --cross                         Optional argument to signify cross compilation."
24   echo "  --configuration (-c)            Build configuration: Debug, Release or Checked."
25   echo "                                  Checked is exclusive to the CLR subset. It is the same as Debug, except code is"
26   echo "                                  compiled with optimizations enabled."
27   echo "                                  [Default: Debug]"
28   echo "  --help (-h)                     Print help and exit."
29   echo "  --librariesConfiguration (-lc)  Libraries build configuration: Debug or Release."
30   echo "                                  [Default: Debug]"
31   echo "  --os                            Target operating system: windows, Linux, FreeBSD, OSX, MacCatalyst, tvOS,"
32   echo "                                  tvOSSimulator, iOS, iOSSimulator, Android, Browser, NetBSD, illumos or Solaris."
33   echo "                                  [Default: Your machine's OS.]"
34   echo "  --projects <value>              Project or solution file(s) to build."
35   echo "  --runtimeConfiguration (-rc)    Runtime build configuration: Debug, Release or Checked."
36   echo "                                  Checked is exclusive to the CLR runtime. It is the same as Debug, except code is"
37   echo "                                  compiled with optimizations enabled."
38   echo "                                  [Default: Debug]"
39   echo "  -runtimeFlavor (-rf)            Runtime flavor: CoreCLR or Mono."
40   echo "                                  [Default: CoreCLR]"
41   echo "  --subset (-s)                   Build a subset, print available subsets with -subset help."
42   echo "                                 '--subset' can be omitted if the subset is given as the first argument."
43   echo "                                  [Default: Builds the entire repo.]"
44   echo "  --verbosity (-v)                MSBuild verbosity: q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]."
45   echo "                                  [Default: Minimal]"
46   echo ""
47
48   echo "Actions (defaults to --restore --build):"
49   echo "  --build (-b)               Build all source projects."
50   echo "                             This assumes --restore has been run already."
51   echo "  --clean                    Clean the solution."
52   echo "  --pack                     Package build outputs into NuGet packages."
53   echo "  --publish                  Publish artifacts (e.g. symbols)."
54   echo "                             This assumes --build has been run already."
55   echo "  --rebuild                  Rebuild all source projects."
56   echo "  --restore (-r)             Restore dependencies."
57   echo "  --sign                     Sign build outputs."
58   echo "  --test (-t)                Incrementally builds and runs tests."
59   echo "                             Use in conjuction with --testnobuild to only run tests."
60   echo ""
61
62   echo "Libraries settings:"
63   echo "  --allconfigurations        Build packages for all build configurations."
64   echo "  --coverage                 Collect code coverage when testing."
65   echo "  --framework (-f)           Build framework: net6.0 or net48."
66   echo "                             [Default: net6.0]"
67   echo "  --testnobuild              Skip building tests when invoking -test."
68   echo "  --testscope                Test scope, allowed values: innerloop, outerloop, all."
69   echo ""
70
71   echo "Native build settings:"
72   echo "  --clang                    Optional argument to build using clang in PATH (default)."
73   echo "  --clangx                   Optional argument to build using clang version x (used for Clang 7 and newer)."
74   echo "  --clangx.y                 Optional argument to build using clang version x.y (used for Clang 6 and older)."
75   echo "  --cmakeargs                User-settable additional arguments passed to CMake."
76   echo "  --gcc                      Optional argument to build using gcc in PATH (default)."
77   echo "  --gccx.y                   Optional argument to build using gcc version x.y."
78   echo "  --portablebuild            Optional argument: set to false to force a non-portable build."
79   echo "  --keepnativesymbols        Optional argument: set to true to keep native symbols/debuginfo in generated binaries."
80   echo "  --ninja                    Optional argument: set to true to use Ninja instead of Make to run the native build."
81   echo "  --pgoinstrument            Optional argument: build PGO-instrumented runtime"
82   echo "  --nopgooptimize            Optional argument: build without PGO optimization"
83   echo ""
84
85   echo "Command line arguments starting with '/p:' are passed through to MSBuild."
86   echo "Arguments can also be passed in with a single hyphen."
87   echo ""
88
89   echo "Here are some quick examples. These assume you are on a Linux x64 machine:"
90   echo ""
91   echo "* Build CoreCLR for Linux x64 on Release configuration:"
92   echo "./build.sh clr -c release"
93   echo ""
94   echo "* Build Debug libraries with a Release runtime for Linux x64."
95   echo "./build.sh clr+libs -rc release"
96   echo ""
97   echo "* Build Release libraries and their tests with a Checked runtime for Linux x64, and run the tests."
98   echo "./build.sh clr+libs+libs.tests -rc checked -lc release -test"
99   echo ""
100   echo "* Build CoreCLR for Linux x64 on Debug configuration using Clang 9."
101   echo "./build.sh clr -clang9"
102   echo ""
103   echo "* Build CoreCLR for Linux x64 on Debug configuration using GCC 8.4."
104   echo "./build.sh clr -gcc8.4"
105   echo ""
106   echo "* Build CoreCLR for Linux x64 using extra compiler flags (-fstack-clash-protection)."
107   echo "EXTRA_CFLAGS=-fstack-clash-protection EXTRA_CXXFLAGS=-fstack-clash-protection ./build.sh clr"
108   echo ""
109   echo "* Cross-compile CoreCLR runtime for Linux ARM64 on Release configuration."
110   echo "./build.sh clr.runtime -arch arm64 -c release -cross"
111   echo ""
112   echo "However, for this example, you need to already have ROOTFS_DIR set up."
113   echo "Further information on this can be found here:"
114   echo "https://github.com/dotnet/runtime/blob/main/docs/workflow/building/coreclr/linux-instructions.md"
115   echo ""
116   echo "* Build Mono runtime for Linux x64 on Release configuration."
117   echo "./build.sh mono -c release"
118   echo ""
119   echo "* Build Release coreclr corelib, crossgen corelib and update Debug libraries testhost to run test on an updated corelib."
120   echo "./build.sh clr.corelib+clr.nativecorelib+libs.pretest -rc release"
121   echo ""
122   echo "* Build Debug mono corelib and update Release libraries testhost to run test on an updated corelib."
123   echo "./build.sh mono.corelib+libs.pretest -rc debug -c release"
124   echo ""
125   echo ""
126   echo "For more general information, check out https://github.com/dotnet/runtime/blob/main/docs/workflow/README.md"
127 }
128
129 initDistroRid()
130 {
131     source "$scriptroot"/native/init-distro-rid.sh
132
133     local passedRootfsDir=""
134     local targetOs="$1"
135     local buildArch="$2"
136     local isCrossBuild="$3"
137     local isPortableBuild="$4"
138
139     # Only pass ROOTFS_DIR if __DoCrossArchBuild is specified and the current platform is not OSX that doesn't use rootfs
140     if [[ $isCrossBuild == 1 && "$targetOs" != "OSX" ]]; then
141         passedRootfsDir=${ROOTFS_DIR}
142     fi
143     initDistroRidGlobal ${targetOs} ${buildArch} ${isPortableBuild} ${passedRootfsDir}
144 }
145
146 showSubsetHelp()
147 {
148   "$scriptroot/common/build.sh" "-restore" "-build" "/p:Subset=help" "/clp:nosummary"
149 }
150
151 arguments=''
152 cmakeargs=''
153 extraargs=''
154 crossBuild=0
155 portableBuild=1
156
157 source $scriptroot/native/init-os-and-arch.sh
158
159 hostArch=$arch
160
161 # Check if an action is passed in
162 declare -a actions=("b" "build" "r" "restore" "rebuild" "testnobuild" "sign" "publish" "clean")
163 actInt=($(comm -12 <(printf '%s\n' "${actions[@]/#/-}" | sort) <(printf '%s\n' "${@/#--/-}" | sort)))
164 firstArgumentChecked=0
165
166 while [[ $# > 0 ]]; do
167   opt="$(echo "${1/#--/-}" | tr "[:upper:]" "[:lower:]")"
168
169   if [[ $firstArgumentChecked -eq 0 && $opt =~ ^[a-zA-Z.+]+$ ]]; then
170     if [ $opt == "help" ]; then
171       showSubsetHelp
172       exit 0
173     fi
174
175     arguments="$arguments /p:Subset=$1"
176     shift 1
177     continue
178   fi
179
180   firstArgumentChecked=1
181
182   case "$opt" in
183      -help|-h|-\?|/?)
184       usage
185       exit 0
186       ;;
187
188      -subset|-s)
189       if [ -z ${2+x} ]; then
190         showSubsetHelp
191         exit 0
192       else
193         passedSubset="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
194         if [ $passedSubset == "help" ]; then
195           showSubsetHelp
196           exit 0
197         fi
198         arguments="$arguments /p:Subset=$2"
199         shift 2
200       fi
201       ;;
202
203      -arch|-a)
204       if [ -z ${2+x} ]; then
205         echo "No architecture supplied. See help (--help) for supported architectures." 1>&2
206         exit 1
207       fi
208       passedArch="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
209       case "$passedArch" in
210         x64|x86|arm|armel|arm64|s390x|wasm)
211           arch=$passedArch
212           ;;
213         *)
214           echo "Unsupported target architecture '$2'."
215           echo "The allowed values are x86, x64, arm, armel, arm64, s390x, and wasm."
216           exit 1
217           ;;
218       esac
219       shift 2
220       ;;
221
222      -configuration|-c)
223       if [ -z ${2+x} ]; then
224         echo "No configuration supplied. See help (--help) for supported configurations." 1>&2
225         exit 1
226       fi
227       passedConfig="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
228       case "$passedConfig" in
229         debug|release|checked)
230           val="$(tr '[:lower:]' '[:upper:]' <<< ${passedConfig:0:1})${passedConfig:1}"
231           ;;
232         *)
233           echo "Unsupported target configuration '$2'."
234           echo "The allowed values are Debug, Release, and Checked."
235           exit 1
236           ;;
237       esac
238       arguments="$arguments -configuration $val"
239       shift 2
240       ;;
241
242      -framework|-f)
243       if [ -z ${2+x} ]; then
244         echo "No framework supplied. See help (--help) for supported frameworks." 1>&2
245         exit 1
246       fi
247       val="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
248       arguments="$arguments /p:BuildTargetFramework=$val"
249       shift 2
250       ;;
251
252      -os)
253       if [ -z ${2+x} ]; then
254         echo "No target operating system supplied. See help (--help) for supported target operating systems." 1>&2
255         exit 1
256       fi
257       passedOS="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
258       case "$passedOS" in
259         windows)
260           os="windows" ;;
261         linux)
262           os="Linux" ;;
263         freebsd)
264           os="FreeBSD" ;;
265         osx)
266           os="OSX" ;;
267         maccatalyst)
268           os="MacCatalyst" ;;
269         tvos)
270           os="tvOS" ;;
271         tvossimulator)
272           os="tvOSSimulator" ;;
273         ios)
274           os="iOS" ;;
275         iossimulator)
276           os="iOSSimulator" ;;
277         android)
278           os="Android" ;;
279         browser)
280           os="Browser" ;;
281         illumos)
282           os="illumos" ;;
283         solaris)
284           os="Solaris" ;;
285         *)
286           echo "Unsupported target OS '$2'."
287           echo "The allowed values are windows, Linux, FreeBSD, OSX, MacCatalyst, tvOS, tvOSSimulator, iOS, iOSSimulator, Android, Browser, illumos and Solaris."
288           exit 1
289           ;;
290       esac
291       arguments="$arguments /p:TargetOS=$os"
292       shift 2
293       ;;
294
295      -allconfigurations)
296       arguments="$arguments /p:BuildAllConfigurations=true"
297       shift 1
298       ;;
299
300      -testscope)
301       if [ -z ${2+x} ]; then
302         echo "No test scope supplied. See help (--help) for supported test scope values." 1>&2
303         exit 1
304       fi
305       arguments="$arguments /p:TestScope=$2"
306       shift 2
307       ;;
308
309      -testnobuild)
310       arguments="$arguments /p:TestNoBuild=true"
311       shift 1
312       ;;
313
314      -coverage)
315       arguments="$arguments /p:Coverage=true"
316       shift 1
317       ;;
318
319      -runtimeconfiguration|-rc)
320       if [ -z ${2+x} ]; then
321         echo "No runtime configuration supplied. See help (--help) for supported runtime configurations." 1>&2
322         exit 1
323       fi
324       passedRuntimeConf="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
325       case "$passedRuntimeConf" in
326         debug|release|checked)
327           val="$(tr '[:lower:]' '[:upper:]' <<< ${passedRuntimeConf:0:1})${passedRuntimeConf:1}"
328           ;;
329         *)
330           echo "Unsupported runtime configuration '$2'."
331           echo "The allowed values are Debug, Release, and Checked."
332           exit 1
333           ;;
334       esac
335       arguments="$arguments /p:RuntimeConfiguration=$val"
336       shift 2
337       ;;
338
339      -runtimeflavor|-rf)
340       if [ -z ${2+x} ]; then
341         echo "No runtime flavor supplied. See help (--help) for supported runtime flavors." 1>&2
342         exit 1
343       fi
344       passedRuntimeFlav="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
345       case "$passedRuntimeFlav" in
346         coreclr|mono)
347           val="$(tr '[:lower:]' '[:upper:]' <<< ${passedRuntimeFlav:0:1})${passedRuntimeFlav:1}"
348           ;;
349         *)
350           echo "Unsupported runtime flavor '$2'."
351           echo "The allowed values are CoreCLR and Mono."
352           exit 1
353           ;;
354       esac
355       arguments="$arguments /p:RuntimeFlavor=$val"
356       shift 2
357       ;;
358
359      -librariesconfiguration|-lc)
360       if [ -z ${2+x} ]; then
361         echo "No libraries configuration supplied. See help (--help) for supported libraries configurations." 1>&2
362         exit 1
363       fi
364       passedLibConf="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
365       case "$passedLibConf" in
366         debug|release)
367           val="$(tr '[:lower:]' '[:upper:]' <<< ${passedLibConf:0:1})${passedLibConf:1}"
368           ;;
369         *)
370           echo "Unsupported libraries configuration '$2'."
371           echo "The allowed values are Debug and Release."
372           exit 1
373           ;;
374       esac
375       arguments="$arguments /p:LibrariesConfiguration=$val"
376       shift 2
377       ;;
378
379      -cross)
380       crossBuild=1
381       arguments="$arguments /p:CrossBuild=True"
382       shift 1
383       ;;
384
385      -clang*)
386       arguments="$arguments /p:Compiler=$opt"
387       shift 1
388       ;;
389
390      -cmakeargs)
391       if [ -z ${2+x} ]; then
392         echo "No cmake args supplied." 1>&2
393         exit 1
394       fi
395       cmakeargs="${cmakeargs} ${opt} $2"
396       shift 2
397       ;;
398
399      -gcc*)
400       arguments="$arguments /p:Compiler=$opt"
401       shift 1
402       ;;
403
404      -portablebuild)
405       if [ -z ${2+x} ]; then
406         echo "No value for portablebuild is supplied. See help (--help) for supported values." 1>&2
407         exit 1
408       fi
409       passedPortable="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
410       if [ "$passedPortable" = false ]; then
411         portableBuild=0
412         arguments="$arguments /p:PortableBuild=false"
413       fi
414       shift 2
415       ;;
416
417      -keepnativesymbols)
418       if [ -z ${2+x} ]; then
419         echo "No value for keepNativeSymbols is supplied. See help (--help) for supported values." 1>&2
420         exit 1
421       fi
422       passedKeepNativeSymbols="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
423       if [ "$passedKeepNativeSymbols" = true ]; then
424         arguments="$arguments /p:KeepNativeSymbols=true"
425       fi
426       shift 2
427       ;;
428
429
430       -ninja)
431       if [ -z ${2+x} ]; then
432         arguments="$arguments /p:Ninja=true"
433         shift 1
434       else
435         ninja="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
436         if [ "$ninja" = true ]; then
437           arguments="$arguments /p:Ninja=true"
438           shift 2
439         elif [ "$ninja" = false ]; then
440           arguments="$arguments /p:Ninja=false"
441           shift 2
442         else
443           arguments="$arguments /p:Ninja=true"
444           shift 1
445         fi
446       fi
447       ;;
448
449       -pgoinstrument)
450       arguments="$arguments /p:PgoInstrument=true"
451       shift 1
452       ;;
453
454       -nopgooptimize)
455       arguments="$arguments /p:NoPgoOptimize=true"
456       shift 1
457       ;;
458
459       *)
460       extraargs="$extraargs $1"
461       shift 1
462       ;;
463   esac
464 done
465
466 if [ ${#actInt[@]} -eq 0 ]; then
467     arguments="-restore -build $arguments"
468 fi
469
470 if [ "$os" = "Browser" ] && [ "$arch" != "wasm" ]; then
471     # override default arch for Browser, we only support wasm
472     arch=wasm
473 fi
474
475 initDistroRid $os $arch $crossBuild $portableBuild
476
477 # Disable targeting pack caching as we reference a partially constructed targeting pack and update it later.
478 # The later changes are ignored when using the cache.
479 export DOTNETSDK_ALLOW_TARGETING_PACK_CACHING=0
480
481 # URL-encode space (%20) to avoid quoting issues until the msbuild call in /eng/common/tools.sh.
482 # In *proj files (XML docs), URL-encoded string are rendered in their decoded form.
483 cmakeargs="${cmakeargs// /%20}"
484 arguments="$arguments /p:TargetArchitecture=$arch /p:BuildArchitecture=$hostArch"
485 arguments="$arguments /p:CMakeArgs=\"$cmakeargs\" $extraargs"
486 "$scriptroot/common/build.sh" $arguments