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