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