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