Merge pull request #10432 from AndyAyersMS/ImproveTypes3
[platform/upstream/coreclr.git] / build.sh
1 #!/usr/bin/env bash
2
3 # resolve python-version to use
4 if [ "$PYTHON" == "" ] ; then
5     if ! PYTHON=$(command -v python || command -v python2 || command -v python 2.7)
6     then
7        echo "Unable to locate build-dependency python2.x!" 1>&2
8        exit 1
9     fi
10 fi
11
12 # validate python-dependency
13 # useful in case of explicitly set option.
14 if ! command -v $PYTHON > /dev/null
15 then
16    echo "Unable to locate build-dependency python2.x ($PYTHON)!" 1>&2
17    exit 1
18 fi
19
20 usage()
21 {
22     echo "Usage: $0 [BuildArch] [BuildType] [verbose] [coverage] [cross] [clangx.y] [ninja] [configureonly] [skipconfigure] [skipnative] [skipmscorlib] [skiptests] [stripsymbols] [cmakeargs] [bindir]"
23     echo "BuildArch can be: x64, x86, arm, armel, arm64"
24     echo "BuildType can be: debug, checked, release"
25     echo "coverage - optional argument to enable code coverage build (currently supported only for Linux and OSX)."
26     echo "ninja - target ninja instead of GNU make"
27     echo "clangx.y - optional argument to build using clang version x.y."
28     echo "cross - optional argument to signify cross compilation,"
29     echo "      - will use ROOTFS_DIR environment variable if set."
30     echo "crosscomponent - optional argument to build cross-architecture component,"
31     echo "               - will use CAC_ROOTFS_DIR environment variable if set."
32     echo "pgoinstrument - generate instrumented code for profile guided optimization enabled binaries."
33     echo "ibcinstrument - generate IBC-tuning-enabled native images when invoking crossgen."
34     echo "configureonly - do not perform any builds; just configure the build."
35     echo "skipconfigure - skip build configuration."
36     echo "skipnative - do not build native components."
37     echo "skipmscorlib - do not build mscorlib.dll."
38     echo "skiptests - skip the tests in the 'tests' subdirectory."
39     echo "skipnuget - skip building nuget packages."
40     echo "skiprestoreoptdata - skip restoring optimization data used by profile-based optimizations."
41     echo "portable - build for portable RID."
42     echo "verbose - optional argument to enable verbose build output."
43     echo "-skiprestore: skip restoring packages ^(default: packages are restored during build^)."
44         echo "-disableoss: Disable Open Source Signing for System.Private.CoreLib."
45         echo "-sequential: force a non-parallel build ^(default is to build in parallel"
46         echo "   using all processors^)."
47         echo "-officialbuildid=^<ID^>: specify the official build ID to be used by this build."
48         echo "-Rebuild: passes /t:rebuild to the build projects."
49     echo "stripSymbols - Optional argument to strip native symbols during the build."
50     echo "skipgenerateversion - disable version generation even if MSBuild is supported."
51     echo "cmakeargs - user-settable additional arguments passed to CMake."
52     echo "bindir - output directory (defaults to $__ProjectRoot/bin)"
53     echo "buildstandalonegc - builds the GC in a standalone mode. Can't be used with \"cmakeargs\"."
54     echo "msbuildonunsupportedplatform - build managed binaries even if distro is not officially supported."
55     exit 1
56 }
57
58 initHostDistroRid()
59 {
60     if [ "$__HostOS" == "Linux" ]; then
61         if [ ! -e /etc/os-release ]; then
62             echo "WARNING: Can not determine runtime id for current distro."
63             __HostDistroRid=""
64         else
65             source /etc/os-release
66             __HostDistroRid="$ID.$VERSION_ID-$__HostArch"
67         fi
68     fi
69 }
70
71 initTargetDistroRid()
72 {
73     if [ $__CrossBuild == 1 ]; then
74         if [ "$__BuildOS" == "Linux" ]; then
75             if [ ! -e $ROOTFS_DIR/etc/os-release ]; then
76                 echo "WARNING: Can not determine runtime id for current distro."
77                 export __DistroRid=""
78             else
79                 source $ROOTFS_DIR/etc/os-release
80                 export __DistroRid="$ID.$VERSION_ID-$__BuildArch"
81             fi
82         fi
83     else
84         export __DistroRid="$__HostDistroRid"
85     fi
86
87     # Portable builds target the base RID
88     if [ $__PortableBuild == 1 ]; then
89         if [ "$__BuildOS" == "Linux" ]; then
90             export __DistroRid="linux-$__BuildArch"
91         elif [ "$__BuildOS" == "OSX" ]; then
92             export __DistroRid="osx-$__BuildArch"
93         fi
94     fi
95 }
96
97 setup_dirs()
98 {
99     echo Setting up directories for build
100
101     mkdir -p "$__RootBinDir"
102     mkdir -p "$__BinDir"
103     mkdir -p "$__LogsDir"
104     mkdir -p "$__IntermediatesDir"
105
106     if [ $__CrossBuild == 1 ]; then
107         mkdir -p "$__CrossComponentBinDir"
108         mkdir -p "$__CrossCompIntermediatesDir"
109     fi
110 }
111
112 # Check the system to ensure the right prereqs are in place
113
114 check_prereqs()
115 {
116     echo "Checking prerequisites..."
117
118     # Check presence of CMake on the path
119     hash cmake 2>/dev/null || { echo >&2 "Please install cmake before running this script"; exit 1; }
120
121     # Check for clang
122     hash clang-$__ClangMajorVersion.$__ClangMinorVersion 2>/dev/null ||  hash clang$__ClangMajorVersion$__ClangMinorVersion 2>/dev/null ||  hash clang 2>/dev/null || { echo >&2 "Please install clang-$__ClangMajorVersion.$__ClangMinorVersion before running this script"; exit 1; }
123
124 }
125
126 restore_optdata()
127 {
128     # if msbuild is not supported, then set __SkipRestoreOptData to 1
129     if [ $__isMSBuildOnNETCoreSupported == 0 ]; then __SkipRestoreOptData=1; fi
130     if [ $__SkipRestoreOptData == 0 ]; then
131         echo "Restoring the OptimizationData package"
132         "$__ProjectRoot/run.sh" sync -optdata
133         if [ $? != 0 ]; then
134             echo "Failed to restore the optimization data package."
135             exit 1
136         fi
137     fi
138 }
139
140 generate_event_logging_sources()
141 {
142     if [ $__SkipCoreCLR == 1 ]; then
143         return
144     fi
145
146 # Event Logging Infrastructure
147    __GeneratedIntermediate="$__IntermediatesDir/Generated"
148    __GeneratedIntermediateEventProvider="$__GeneratedIntermediate/eventprovider_new"
149     if [[ -d "$__GeneratedIntermediateEventProvider" ]]; then
150         rm -rf  "$__GeneratedIntermediateEventProvider"
151     fi
152
153     if [[ ! -d "$__GeneratedIntermediate/eventprovider" ]]; then
154         mkdir -p "$__GeneratedIntermediate/eventprovider"
155     fi
156
157     mkdir -p "$__GeneratedIntermediateEventProvider"
158     if [[ $__SkipCoreCLR == 0 || $__ConfigureOnly == 1 ]]; then
159         echo "Laying out dynamically generated files consumed by the build system "
160         echo "Laying out dynamically generated Event Logging Test files"
161         $PYTHON -B -Wall -Werror "$__ProjectRoot/src/scripts/genXplatEventing.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --exc "$__ProjectRoot/src/vm/ClrEtwAllMeta.lst" --testdir "$__GeneratedIntermediateEventProvider/tests"
162
163         if  [[ $? != 0 ]]; then
164             exit
165         fi
166
167         #determine the logging system
168         case $__BuildOS in
169             Linux)
170                 echo "Laying out dynamically generated Event Logging Implementation of Lttng"
171                 $PYTHON -B -Wall -Werror "$__ProjectRoot/src/scripts/genXplatLttng.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --intermediate "$__GeneratedIntermediateEventProvider"
172                 if  [[ $? != 0 ]]; then
173                     exit
174                 fi
175                 ;;
176             *)
177                 ;;
178         esac
179     fi
180
181     echo "Cleaning the temp folder of dynamically generated Event Logging files"
182     $PYTHON -B -Wall -Werror -c "import sys;sys.path.insert(0,\"$__ProjectRoot/src/scripts\"); from Utilities import *;UpdateDirectory(\"$__GeneratedIntermediate/eventprovider\",\"$__GeneratedIntermediateEventProvider\")"
183     if  [[ $? != 0 ]]; then
184         exit
185     fi
186
187     rm -rf "$__GeneratedIntermediateEventProvider"
188 }
189
190 build_native()
191 {
192     skipCondition=$1
193     platformArch="$2"
194     intermediatesForBuild="$3"
195     extraCmakeArguments="$4"
196     message="$5"
197
198     if [ $skipCondition == 1 ]; then
199         echo "Skipping $message build."
200         return
201     fi
202
203     # All set to commence the build
204     echo "Commencing build of $message for $__BuildOS.$__BuildArch.$__BuildType in $intermediatesForBuild"
205
206     generator=""
207     buildFile="Makefile"
208     buildTool="make"
209     if [ $__UseNinja == 1 ]; then
210         generator="ninja"
211         buildFile="build.ninja"
212         if ! buildTool=$(command -v ninja || command -v ninja-build); then
213            echo "Unable to locate ninja!" 1>&2
214            exit 1
215         fi
216     fi
217
218     if [ $__SkipConfigure == 0 ]; then
219         # if msbuild is not supported, then set __SkipGenerateVersion to 1
220         if [ $__isMSBuildOnNETCoreSupported == 0 ]; then __SkipGenerateVersion=1; fi
221         # Drop version.cpp file
222         __versionSourceFile="$intermediatesForBuild/version.cpp"
223         if [ $__SkipGenerateVersion == 0 ]; then
224             pwd
225             "$__ProjectRoot/run.sh" build -Project=$__ProjectDir/build.proj -generateHeaderUnix -NativeVersionSourceFile=$__versionSourceFile $__RunArgs $__UnprocessedBuildArgs
226         else
227             # Generate the dummy version.cpp, but only if it didn't exist to make sure we don't trigger unnecessary rebuild
228             __versionSourceLine="static char sccsid[] __attribute__((used)) = \"@(#)No version information produced\";"
229             if [ -e $__versionSourceFile ]; then
230                 read existingVersionSourceLine < $__versionSourceFile
231             fi
232             if [ "$__versionSourceLine" != "$existingVersionSourceLine" ]; then
233                 echo $__versionSourceLine > $__versionSourceFile
234             fi
235         fi
236
237
238         pushd "$intermediatesForBuild"
239         # Regenerate the CMake solution
240         echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__ProjectRoot\" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $__IncludeTests $generator $extraCmakeArguments $__cmakeargs"
241         "$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__ProjectRoot" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $__IncludeTests $generator "$extraCmakeArguments" "$__cmakeargs"
242         popd
243     fi
244
245     if [ ! -f "$intermediatesForBuild/$buildFile" ]; then
246         echo "Failed to generate $message build project!"
247         exit 1
248     fi
249     
250     # Get the number of processors available to the scheduler
251     # Other techniques such as `nproc` only get the number of
252     # processors available to a single process.
253     if [ `uname` = "FreeBSD" ]; then
254         NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
255     elif [ `uname` = "NetBSD" ]; then
256         NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
257     else
258         NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
259     fi
260
261     # Build
262     if [ $__ConfigureOnly == 1 ]; then
263         echo "Finish configuration & skipping $message build."
264         return
265     fi
266
267     # Check that the makefiles were created.
268     pushd "$intermediatesForBuild"
269
270     echo "Executing $buildTool install -j $NumProc"
271
272     $buildTool install -j $NumProc
273     if [ $? != 0 ]; then
274         echo "Failed to build $message."
275         exit 1
276     fi
277
278     popd
279 }
280
281 build_cross_arch_component()
282 {
283     __SkipCrossArchBuild=1
284     TARGET_ROOTFS=""
285     # check supported cross-architecture components host(__HostArch)/target(__BuildArch) pair
286     if [[ "$__BuildArch" == "arm" && "$__CrossArch" == "x86" ]]; then
287         export CROSSCOMPILE=0
288         __SkipCrossArchBuild=0
289
290         # building x64-host/arm-target cross-architecture component need to use cross toolchain of x86
291         if [ "$__HostArch" == "x64" ]; then
292             export CROSSCOMPILE=1
293         fi
294     else
295         # not supported
296         return
297     fi    
298     
299     export __CMakeBinDir="$__CrossComponentBinDir"
300     export CROSSCOMPONENT=1
301     __IncludeTests=
302
303     if [ $CROSSCOMPILE == 1 ]; then
304         TARGET_ROOTFS="$ROOTFS_DIR"
305         if [ -n "$CAC_ROOTFS_DIR" ]; then
306             export ROOTFS_DIR="$CAC_ROOTFS_DIR"
307         else
308             export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__CrossArch"
309         fi
310     fi
311
312     __ExtraCmakeArgs="-DCLR_CMAKE_TARGET_ARCH=$__BuildArch -DCLR_CMAKE_TARGET_OS=$__BuildOS -DCLR_CMAKE_PACKAGES_DIR=$__PackagesDir -DCLR_CMAKE_PGO_INSTRUMENT=$__PgoInstrument -DCLR_CMAKE_OPTDATA_VERSION=$__PgoOptDataVersion"
313     build_native $__SkipCrossArchBuild "$__CrossArch" "$__CrossCompIntermediatesDir" "$__ExtraCmakeArgs" "cross-architecture component"
314    
315     # restore ROOTFS_DIR, CROSSCOMPONENT, and CROSSCOMPILE 
316     if [ -n "$TARGET_ROOTFS" ]; then
317         export ROOTFS_DIR="$TARGET_ROOTFS"
318     fi
319     export CROSSCOMPONENT=
320     export CROSSCOMPILE=1
321 }
322
323 isMSBuildOnNETCoreSupported()
324 {
325     # This needs to be updated alongwith corresponding changes to netci.groovy.
326     __isMSBuildOnNETCoreSupported=0
327
328     if [ "$__HostArch" == "x64" ]; then
329         if [ "$__HostOS" == "Linux" ]; then
330             case "$__HostDistroRid" in
331                 "centos.7-x64")
332                     __isMSBuildOnNETCoreSupported=1
333                     ;;
334                 "debian.8-x64")
335                     __isMSBuildOnNETCoreSupported=1
336                     ;;
337                 "fedora.23-x64")
338                     __isMSBuildOnNETCoreSupported=1
339                     ;;
340                 "fedora.24-x64")
341                     __isMSBuildOnNETCoreSupported=1
342                     ;;
343                 "opensuse.42.1-x64")
344                     __isMSBuildOnNETCoreSupported=1
345                     ;;
346                 "rhel.7"*"-x64")
347                     __isMSBuildOnNETCoreSupported=1
348                     ;;
349                 "ubuntu.14.04-x64")
350                     __isMSBuildOnNETCoreSupported=1
351                     ;;
352                 "ubuntu.16.04-x64")
353                     __isMSBuildOnNETCoreSupported=1
354                     ;;
355                 "ubuntu.16.10-x64")
356                     __isMSBuildOnNETCoreSupported=1
357                     ;;
358                 "alpine.3.4.3-x64")
359                     __isMSBuildOnNETCoreSupported=1
360                     ;;
361                 *)
362                 __isMSBuildOnNETCoreSupported=$__msbuildonunsupportedplatform
363             esac
364         elif [ "$__HostOS" == "OSX" ]; then
365             __isMSBuildOnNETCoreSupported=1
366         fi
367     fi
368 }
369
370 build_CoreLib_ni()
371 {
372     if [ $__SkipCoreCLR == 0 -a -e $__BinDir/crossgen ]; then
373         echo "Generating native image for System.Private.CoreLib."
374         $__BinDir/crossgen $__IbcTuning $__BinDir/System.Private.CoreLib.dll
375         if [ $? -ne 0 ]; then
376             echo "Failed to generate native image for System.Private.CoreLib."
377             exit 1
378         fi
379
380         if [ "$__BuildOS" == "Linux" ]; then
381             echo "Generating symbol file for System.Private.CoreLib."
382             $__BinDir/crossgen /CreatePerfMap $__BinDir $__BinDir/System.Private.CoreLib.ni.dll
383             if [ $? -ne 0 ]; then
384                 echo "Failed to generate symbol file for System.Private.CoreLib."
385                 exit 1
386             fi
387         fi
388     fi
389 }
390
391 build_CoreLib()
392 {
393
394     if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
395         echo "System.Private.CoreLib.dll build unsupported."
396         return
397     fi
398
399     if [ $__SkipMSCorLib == 1 ]; then
400        echo "Skipping building System.Private.CoreLib."
401        return
402     fi
403
404     echo "Commencing build of managed components for $__BuildOS.$__BuildArch.$__BuildType"
405
406     # Invoke MSBuild
407     __ExtraBuildArgs=""
408     if [[ "$__IbcTuning" -eq "" ]]; then
409         __ExtraBuildArgs="$__ExtraBuildArgs -OptimizationDataDir=\"$__PackagesDir/optimization.$__BuildOS-$__BuildArch.IBC.CoreCLR/$__IbcOptDataVersion/data/\""
410         __ExtraBuildArgs="$__ExtraBuildArgs -EnableProfileGuidedOptimization=true"
411     fi
412     $__ProjectRoot/run.sh build -Project=$__ProjectDir/build.proj -MsBuildLog="/flp:Verbosity=normal;LogFile=$__LogsDir/System.Private.CoreLib_$__BuildOS__$__BuildArch__$__BuildType.log" -BuildTarget -__IntermediatesDir=$__IntermediatesDir -__RootBinDir=$__RootBinDir -BuildNugetPackage=false -UseSharedCompilation=false $__RunArgs $__ExtraBuildArgs $__UnprocessedBuildArgs
413
414     if [ $? -ne 0 ]; then
415         echo "Failed to build managed components."
416         exit 1
417     fi
418
419     # The cross build generates a crossgen with the target architecture.
420     if [ $__CrossBuild != 1 ]; then
421        # The architecture of host pc must be same architecture with target.
422        if [[ ( "$__HostArch" == "$__BuildArch" ) ]]; then
423            build_CoreLib_ni
424        elif [[ ( "$__HostArch" == "x64" ) && ( "$__BuildArch" == "x86" ) ]]; then
425            build_CoreLib_ni
426        elif [[ ( "$__HostArch" == "arm64" ) && ( "$__BuildArch" == "arm" ) ]]; then
427            build_CoreLib_ni
428        else 
429            exit 1
430        fi
431     fi 
432 }
433
434 generate_NugetPackages()
435 {
436     # We can only generate nuget package if we also support building mscorlib as part of this build.
437     if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
438         echo "Nuget package generation unsupported."
439         return
440     fi
441
442     # Since we can build mscorlib for this OS, did we build the native components as well?
443     if [ $__SkipCoreCLR == 1 ]; then
444         echo "Unable to generate nuget packages since native components were not built."
445         return
446     fi
447
448     echo "Generating nuget packages for "$__BuildOS
449
450     # Build the packages
451     $__ProjectRoot/run.sh build -Project=$__SourceDir/.nuget/packages.builds -MsBuildLog="/flp:Verbosity=normal;LogFile=$__LogsDir/Nuget_$__BuildOS__$__BuildArch__$__BuildType.log" -BuildTarget -__IntermediatesDir=$__IntermediatesDir -__RootBinDir=$__RootBinDir -BuildNugetPackage=false -UseSharedCompilation=false $__RunArgs $__UnprocessedBuildArgs
452
453     if [ $? -ne 0 ]; then
454         echo "Failed to generate Nuget packages."
455         exit 1
456     fi
457 }
458
459 echo "Commencing CoreCLR Repo build"
460
461 # Argument types supported by this script:
462 #
463 # Build architecture - valid values are: x64, ARM.
464 # Build Type         - valid values are: Debug, Checked, Release
465 #
466 # Set the default arguments for build
467
468 # Obtain the location of the bash script to figure out where the root of the repo is.
469 __ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
470
471 # Use uname to determine what the CPU is.
472 CPUName=$(uname -p)
473 # Some Linux platforms report unknown for platform, but the arch for machine.
474 if [ "$CPUName" == "unknown" ]; then
475     CPUName=$(uname -m)
476 fi
477
478 case $CPUName in
479     i686)
480         echo "Unsupported CPU $CPUName detected, build might not succeed!"
481         __BuildArch=x86
482         __HostArch=x86
483         ;;
484
485     x86_64)
486         __BuildArch=x64
487         __HostArch=x64
488         ;;
489
490     armv7l)
491         echo "Unsupported CPU $CPUName detected, build might not succeed!"
492         __BuildArch=arm
493         __HostArch=arm
494         ;;
495
496     aarch64)
497         __BuildArch=arm64
498         __HostArch=arm64
499         ;;
500
501     *)
502         echo "Unknown CPU $CPUName detected, configuring as if for x64"
503         __BuildArch=x64
504         __HostArch=x64
505         ;;
506 esac
507
508 # Use uname to determine what the OS is.
509 OSName=$(uname -s)
510 case $OSName in
511     Linux)
512         __BuildOS=Linux
513         __HostOS=Linux
514         ;;
515
516     Darwin)
517         __BuildOS=OSX
518         __HostOS=OSX
519         ;;
520
521     FreeBSD)
522         __BuildOS=FreeBSD
523         __HostOS=FreeBSD
524         ;;
525
526     OpenBSD)
527         __BuildOS=OpenBSD
528         __HostOS=OpenBSD
529         ;;
530
531     NetBSD)
532         __BuildOS=NetBSD
533         __HostOS=NetBSD
534         ;;
535
536     SunOS)
537         __BuildOS=SunOS
538         __HostOS=SunOS
539         ;;
540
541     *)
542         echo "Unsupported OS $OSName detected, configuring as if for Linux"
543         __BuildOS=Linux
544         __HostOS=Linux
545         ;;
546 esac
547
548 __BuildType=Debug
549 __CodeCoverage=
550 __IncludeTests=Include_Tests
551
552 # Set the various build properties here so that CMake and MSBuild can pick them up
553 __ProjectDir="$__ProjectRoot"
554 __SourceDir="$__ProjectDir/src"
555 __PackagesDir="$__ProjectDir/packages"
556 __RootBinDir="$__ProjectDir/bin"
557 __UnprocessedBuildArgs=
558 __RunArgs=
559 __MSBCleanBuildArgs=
560 __UseNinja=0
561 __VerboseBuild=0
562 __PgoInstrument=0
563 __IbcTuning=""
564 __ConfigureOnly=0
565 __SkipConfigure=0
566 __SkipRestore=""
567 __SkipNuget=0
568 __SkipCoreCLR=0
569 __SkipMSCorLib=0
570 __SkipRestoreOptData=0
571 __CrossBuild=0
572 __ClangMajorVersion=0
573 __ClangMinorVersion=0
574 __NuGetPath="$__PackagesDir/NuGet.exe"
575 __HostDistroRid=""
576 __DistroRid=""
577 __cmakeargs=""
578 __SkipGenerateVersion=0
579 __DoCrossArchBuild=0
580 __PortableBuild=0
581 __msbuildonunsupportedplatform=0
582 __PgoOptDataVersion=""
583 __IbcOptDataVersion=""
584
585 while :; do
586     if [ $# -le 0 ]; then
587         break
588     fi
589
590     lowerI="$(echo $1 | awk '{print tolower($0)}')"
591     case $lowerI in
592         -\?|-h|--help)
593             usage
594             exit 1
595             ;;
596
597         x86)
598             __BuildArch=x86
599             ;;
600
601         x64)
602             __BuildArch=x64
603             ;;
604
605         arm)
606             __BuildArch=arm
607             ;;
608
609         armel)
610             __BuildArch=armel
611             ;;
612
613         arm64)
614             __BuildArch=arm64
615             ;;
616
617         debug)
618             __BuildType=Debug
619             ;;
620
621         checked)
622             __BuildType=Checked
623             ;;
624
625         release)
626             __BuildType=Release
627             ;;
628
629         coverage)
630             __CodeCoverage=Coverage
631             ;;
632
633         cross)
634             __CrossBuild=1
635             ;;
636             
637         -portable)
638             __PortableBuild=1
639             ;;
640
641         verbose)
642             __VerboseBuild=1
643             ;;
644
645         stripsymbols)
646             __cmakeargs="$__cmakeargs -DSTRIP_SYMBOLS=true"
647             ;;
648
649         clang3.5)
650             __ClangMajorVersion=3
651             __ClangMinorVersion=5
652             ;;
653
654         clang3.6)
655             __ClangMajorVersion=3
656             __ClangMinorVersion=6
657             ;;
658
659         clang3.7)
660             __ClangMajorVersion=3
661             __ClangMinorVersion=7
662             ;;
663
664         clang3.8)
665             __ClangMajorVersion=3
666             __ClangMinorVersion=8
667             ;;
668
669         clang3.9)
670             __ClangMajorVersion=3
671             __ClangMinorVersion=9
672             ;;
673
674         ninja)
675             __UseNinja=1
676             ;;
677
678         pgoinstrument)
679             __PgoInstrument=1
680             ;;
681
682         ibcinstrument)
683             __IbcTuning="/Tuning"
684             ;;
685
686         configureonly)
687             __ConfigureOnly=1
688             __SkipMSCorLib=1
689             __SkipNuget=1
690             ;;
691
692         skipconfigure)
693             __SkipConfigure=1
694             ;;
695
696         skipnative)
697             # Use "skipnative" to use the same option name as build.cmd.
698             __SkipCoreCLR=1
699             ;;
700
701         skipcoreclr)
702             # Accept "skipcoreclr" for backwards-compatibility.
703             __SkipCoreCLR=1
704             ;;
705
706         crosscomponent)
707             __DoCrossArchBuild=1
708             ;;
709
710         skipmscorlib)
711             __SkipMSCorLib=1
712             ;;
713
714         skipgenerateversion)
715             __SkipGenerateVersion=1
716             ;;
717
718         skiprestoreoptdata)
719             __SkipRestoreOptData=1
720             ;;
721
722         includetests)
723             ;;
724
725         skiptests)
726             __IncludeTests=
727             ;;
728
729         skipnuget)
730             __SkipNuget=1
731             ;;
732
733         cmakeargs)
734             if [ -n "$2" ]; then
735                 __cmakeargs="$2"
736                 shift
737             else
738                 echo "ERROR: 'cmakeargs' requires a non-empty option argument"
739                 exit 1
740             fi
741             ;;
742
743         bindir)
744             if [ -n "$2" ]; then
745                 __RootBinDir="$2"
746                 if [ ! -d $__RootBinDir ]; then
747                     mkdir $__RootBinDir
748                 fi
749                 __RootBinParent=$(dirname $__RootBinDir)
750                 __RootBinName=${__RootBinDir##*/}
751                 __RootBinDir="$(cd $__RootBinParent &>/dev/null && printf %s/%s $PWD $__RootBinName)"
752                 shift
753             else
754                 echo "ERROR: 'bindir' requires a non-empty option argument"
755                 exit 1
756             fi
757             ;;
758         buildstandalonegc)
759             __cmakeargs="-DFEATURE_STANDALONE_GC=1"
760             ;;
761         msbuildonunsupportedplatform)
762             __msbuildonunsupportedplatform=1
763             ;;
764         *)
765             __UnprocessedBuildArgs="$__UnprocessedBuildArgs $1"
766             ;;
767     esac
768
769     shift
770 done
771
772 __RunArgs="-BuildArch=$__BuildArch -BuildType=$__BuildType -BuildOS=$__BuildOS"
773
774 # Configure environment if we are doing a verbose build
775 if [ $__VerboseBuild == 1 ]; then
776     export VERBOSE=1
777         __RunArgs="$__RunArgs -verbose"
778 fi
779
780 # Set default clang version
781 if [[ $__ClangMajorVersion == 0 && $__ClangMinorVersion == 0 ]]; then
782     if [ $__CrossBuild == 1 ]; then
783         __ClangMajorVersion=3
784         __ClangMinorVersion=6
785     else
786         __ClangMajorVersion=3
787         __ClangMinorVersion=5
788     fi
789 fi
790
791 # Set dependent variables
792 __LogsDir="$__RootBinDir/Logs"
793
794 # init the host distro name
795 initHostDistroRid
796
797 # Set the remaining variables based upon the determined build configuration
798 __BinDir="$__RootBinDir/Product/$__BuildOS.$__BuildArch.$__BuildType"
799 __PackagesBinDir="$__BinDir/.nuget"
800 __ToolsDir="$__RootBinDir/tools"
801 __TestWorkingDir="$__RootBinDir/tests/$__BuildOS.$__BuildArch.$__BuildType"
802 export __IntermediatesDir="$__RootBinDir/obj/$__BuildOS.$__BuildArch.$__BuildType"
803 __TestIntermediatesDir="$__RootBinDir/tests/obj/$__BuildOS.$__BuildArch.$__BuildType"
804 __isMSBuildOnNETCoreSupported=0
805 __CrossComponentBinDir="$__BinDir"
806 __CrossCompIntermediatesDir="$__IntermediatesDir/crossgen"
807
808 __CrossArch="$__HostArch"
809 if [[ "$__HostArch" == "x64" && "$__BuildArch" == "arm" ]]; then
810     __CrossArch="x86"
811 fi
812 if [ $__CrossBuild == 1 ]; then
813     __CrossComponentBinDir="$__CrossComponentBinDir/$__CrossArch"
814 fi
815 __CrossgenCoreLibLog="$__LogsDir/CrossgenCoreLib_$__BuildOS.$BuildArch.$__BuildType.log"
816 __CrossgenExe="$__CrossComponentBinDir/crossgen"
817
818 # Init if MSBuild for .NET Core is supported for this platform
819 isMSBuildOnNETCoreSupported
820
821 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to set.
822 # This is needed by CLI to function.
823 if [ -z "$HOME" ]; then
824     if [ ! -d "$__ProjectDir/temp_home" ]; then
825         mkdir temp_home
826     fi
827     export HOME=$__ProjectDir/temp_home
828     echo "HOME not defined; setting it to $HOME"
829 fi
830
831 # Specify path to be set for CMAKE_INSTALL_PREFIX.
832 # This is where all built CoreClr libraries will copied to.
833 export __CMakeBinDir="$__BinDir"
834
835 # Configure environment if we are doing a cross compile.
836 if [ $__CrossBuild == 1 ]; then
837     export CROSSCOMPILE=1
838     if ! [[ -n "$ROOTFS_DIR" ]]; then
839         export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__BuildArch"
840     fi
841 fi
842
843 # Parse the optdata package version from its project.json file
844 optDataProjectJsonPath="$__ProjectRoot/src/.nuget/optdata/project.json"
845 if [ -f $optDataProjectJsonPath ]; then
846     __PgoOptDataVersion=$("$__ProjectRoot/extract-from-json.py" -rf $optDataProjectJsonPath dependencies optimization.PGO.CoreCLR)
847     __IbcOptDataVersion=$("$__ProjectRoot/extract-from-json.py" -rf $optDataProjectJsonPath dependencies optimization.IBC.CoreCLR)
848 fi
849
850 # init the target distro name
851 initTargetDistroRid
852
853 # Make the directories necessary for build if they don't exist
854 setup_dirs
855
856 # Check prereqs.
857 check_prereqs
858
859 # Restore the package containing profile counts for profile-guided optimizations
860 restore_optdata
861
862 # Generate event logging infrastructure sources
863 generate_event_logging_sources
864
865 # Build the coreclr (native) components.
866 __ExtraCmakeArgs="-DCLR_CMAKE_TARGET_OS=$__BuildOS -DCLR_CMAKE_PACKAGES_DIR=$__PackagesDir -DCLR_CMAKE_PGO_INSTRUMENT=$__PgoInstrument -DCLR_CMAKE_OPTDATA_VERSION=$__PgoOptDataVersion"
867 build_native $__SkipCoreCLR "$__BuildArch" "$__IntermediatesDir" "$__ExtraCmakeArgs" "CoreCLR component"
868
869 # Build cross-architecture components
870 if [[ $__CrossBuild == 1 && $__DoCrossArchBuild == 1 ]]; then
871     build_cross_arch_component
872 fi
873
874 # Build System.Private.CoreLib.
875
876 build_CoreLib
877
878 # Generate nuget packages
879 if [ $__SkipNuget != 1 ]; then
880     generate_NugetPackages
881 fi
882
883
884 # Build complete
885
886 echo "Repo successfully built."
887 echo "Product binaries are available at $__BinDir"
888 exit 0