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