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