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