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