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