Merge pull request #17201 from BruceForstall/FixDiffable
[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 $__UnprocessedBuildArgs
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     local __CrossGenExec=$1
398
399     echo "Generating native image for System.Private.CoreLib.dll"
400     echo "$__CrossGenExec /Platform_Assemblies_Paths $__BinDir/IL $__IbcTuning /out $__BinDir/System.Private.CoreLib.dll $__BinDir/IL/System.Private.CoreLib.dll"
401     $__CrossGenExec /Platform_Assemblies_Paths $__BinDir/IL $__IbcTuning /out $__BinDir/System.Private.CoreLib.dll $__BinDir/IL/System.Private.CoreLib.dll
402     if [ $? -ne 0 ]; then
403         echo "Failed to generate native image for System.Private.CoreLib."
404         exit 1
405     fi
406
407     if [ "$__BuildOS" == "Linux" ]; then
408         echo "Generating symbol file for System.Private.CoreLib.dll"
409         echo "$__CrossGenExec /Platform_Assemblies_Paths $__BinDir /CreatePerfMap $__BinDir $__BinDir/System.Private.CoreLib.dll"
410         $__CrossGenExec /Platform_Assemblies_Paths $__BinDir /CreatePerfMap $__BinDir $__BinDir/System.Private.CoreLib.dll
411         if [ $? -ne 0 ]; then
412             echo "Failed to generate symbol file for System.Private.CoreLib."
413             exit 1
414         fi
415     fi
416 }
417
418 build_CoreLib()
419 {
420     if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
421         echo "System.Private.CoreLib.dll build unsupported."
422         return
423     fi
424
425     if [ $__SkipMSCorLib == 1 ]; then
426        echo "Skipping building System.Private.CoreLib."
427        return
428     fi
429
430     echo "Commencing build of managed components for $__BuildOS.$__BuildArch.$__BuildType"
431
432     # Invoke MSBuild
433     __ExtraBuildArgs=""
434     if [[ "$__IbcTuning" -eq "" ]]; then
435         __ExtraBuildArgs="$__ExtraBuildArgs -OptimizationDataDir=\"$__PackagesDir/optimization.$__BuildOS-$__BuildArch.IBC.CoreCLR/$__IbcOptDataVersion/data/\""
436         __ExtraBuildArgs="$__ExtraBuildArgs -EnableProfileGuidedOptimization=true"
437     fi
438     $__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
439
440     if [ $? -ne 0 ]; then
441         echo "Failed to build managed components."
442         exit 1
443     fi
444
445     if [ $__SkipCrossgen == 1 ]; then
446         echo "Skipping generating native image"
447         return
448     fi
449
450     # The cross build generates a crossgen with the target architecture.
451     if [ $__CrossBuild == 0 ]; then
452        if [ $__SkipCoreCLR == 1 ]; then
453            return
454        fi
455
456        # The architecture of host pc must be same architecture with target.
457        if [[ ( "$__HostArch" == "$__BuildArch" ) ]]; then
458            build_CoreLib_ni "$__BinDir/crossgen"
459        elif [[ ( "$__HostArch" == "x64" ) && ( "$__BuildArch" == "x86" ) ]]; then
460            build_CoreLib_ni "$__BinDir/crossgen"
461        elif [[ ( "$__HostArch" == "arm64" ) && ( "$__BuildArch" == "arm" ) ]]; then
462            build_CoreLib_ni "$__BinDir/crossgen"
463        else
464            exit 1
465        fi
466     elif [ $__DoCrossArchBuild == 1 ]; then
467        if [[ ( "$__CrossArch" == "x86" ) && ( "$__BuildArch" == "arm" ) ]]; then
468            build_CoreLib_ni "$__CrossComponentBinDir/crossgen"
469        fi
470     fi
471 }
472
473 generate_NugetPackages()
474 {
475     # We can only generate nuget package if we also support building mscorlib as part of this build.
476     if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
477         echo "Nuget package generation unsupported."
478         return
479     fi
480
481     # Since we can build mscorlib for this OS, did we build the native components as well?
482     if [ $__SkipCoreCLR == 1 ]; then
483         echo "Unable to generate nuget packages since native components were not built."
484         return
485     fi
486
487     echo "Generating nuget packages for "$__BuildOS
488     echo "DistroRid is "$__DistroRid
489     echo "ROOTFS_DIR is "$ROOTFS_DIR
490     # Build the packages
491     $__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
492
493     if [ $? -ne 0 ]; then
494         echo "Failed to generate Nuget packages."
495         exit 1
496     fi
497 }
498
499 echo "Commencing CoreCLR Repo build"
500
501 # Argument types supported by this script:
502 #
503 # Build architecture - valid values are: x64, ARM.
504 # Build Type         - valid values are: Debug, Checked, Release
505 #
506 # Set the default arguments for build
507
508 # Obtain the location of the bash script to figure out where the root of the repo is.
509 __ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
510
511 # Use uname to determine what the CPU is.
512 CPUName=$(uname -p)
513 # Some Linux platforms report unknown for platform, but the arch for machine.
514 if [ "$CPUName" == "unknown" ]; then
515     CPUName=$(uname -m)
516 fi
517
518 case $CPUName in
519     i686)
520         echo "Unsupported CPU $CPUName detected, build might not succeed!"
521         __BuildArch=x86
522         __HostArch=x86
523         ;;
524
525     x86_64)
526         __BuildArch=x64
527         __HostArch=x64
528         ;;
529
530     armv7l)
531         echo "Unsupported CPU $CPUName detected, build might not succeed!"
532         __BuildArch=arm
533         __HostArch=arm
534         ;;
535
536     aarch64)
537         __BuildArch=arm64
538         __HostArch=arm64
539         ;;
540
541     amd64)
542         __BuildArch=x64
543         __HostArch=x64
544         ;;
545     *)
546         echo "Unknown CPU $CPUName detected, configuring as if for x64"
547         __BuildArch=x64
548         __HostArch=x64
549         ;;
550 esac
551
552 # Use uname to determine what the OS is.
553 OSName=$(uname -s)
554 case $OSName in
555     Linux)
556         __BuildOS=Linux
557         __HostOS=Linux
558         ;;
559
560     Darwin)
561         __BuildOS=OSX
562         __HostOS=OSX
563         ;;
564
565     FreeBSD)
566         __BuildOS=FreeBSD
567         __HostOS=FreeBSD
568         ;;
569
570     OpenBSD)
571         __BuildOS=OpenBSD
572         __HostOS=OpenBSD
573         ;;
574
575     NetBSD)
576         __BuildOS=NetBSD
577         __HostOS=NetBSD
578         ;;
579
580     SunOS)
581         __BuildOS=SunOS
582         __HostOS=SunOS
583         ;;
584
585     *)
586         echo "Unsupported OS $OSName detected, configuring as if for Linux"
587         __BuildOS=Linux
588         __HostOS=Linux
589         ;;
590 esac
591
592 __BuildType=Debug
593 __CodeCoverage=
594 __IncludeTests=Include_Tests
595 __IgnoreWarnings=0
596
597 # Set the various build properties here so that CMake and MSBuild can pick them up
598 __ProjectDir="$__ProjectRoot"
599 __SourceDir="$__ProjectDir/src"
600 __PackagesDir="${DotNetRestorePackagesPath:-${__ProjectDir}/packages}"
601 __RootBinDir="$__ProjectDir/bin"
602 __UnprocessedBuildArgs=
603 __RunArgs=
604 __MSBCleanBuildArgs=
605 __UseNinja=0
606 __VerboseBuild=0
607 __PgoInstrument=0
608 __PgoOptimize=1
609 __IbcTuning=""
610 __ConfigureOnly=0
611 __SkipConfigure=0
612 __SkipRestore=""
613 __SkipNuget=0
614 __SkipCoreCLR=0
615 __SkipMSCorLib=0
616 __SkipRestoreOptData=0
617 __SkipCrossgen=0
618 __CrossBuild=0
619 __ClangMajorVersion=0
620 __ClangMinorVersion=0
621 __NuGetPath="$__PackagesDir/NuGet.exe"
622 __HostDistroRid=""
623 __DistroRid=""
624 __cmakeargs=""
625 __SkipGenerateVersion=0
626 __DoCrossArchBuild=0
627 __PortableBuild=1
628 __msbuildonunsupportedplatform=0
629 __PgoOptDataVersion=""
630 __IbcOptDataVersion=""
631
632 # Get the number of processors available to the scheduler
633 # Other techniques such as `nproc` only get the number of
634 # processors available to a single process.
635 if [ `uname` = "FreeBSD" ]; then
636   __NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
637 elif [ `uname` = "NetBSD" ]; then
638   __NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
639 elif [ `uname` = "Darwin" ]; then
640   __NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
641 else
642   __NumProc=$(nproc --all)
643 fi
644
645 while :; do
646     if [ $# -le 0 ]; then
647         break
648     fi
649
650     lowerI="$(echo $1 | awk '{print tolower($0)}')"
651     case $lowerI in
652         -\?|-h|--help)
653             usage
654             exit 1
655             ;;
656
657         x86|-x86)
658             __BuildArch=x86
659             ;;
660
661         x64|-x64)
662             __BuildArch=x64
663             ;;
664
665         arm|-arm)
666             __BuildArch=arm
667             ;;
668
669         armel|-armel)
670             __BuildArch=armel
671             ;;
672
673         arm64|-arm64)
674             __BuildArch=arm64
675             ;;
676
677         debug|-debug)
678             __BuildType=Debug
679             ;;
680
681         checked|-checked)
682             __BuildType=Checked
683             ;;
684
685         release|-release)
686             __BuildType=Release
687             ;;
688
689         coverage|-coverage)
690             __CodeCoverage=Coverage
691             ;;
692
693         cross|-cross)
694             __CrossBuild=1
695             ;;
696
697         -portablebuild=false)
698             __PortableBuild=0
699             ;;
700
701         verbose|-verbose)
702             __VerboseBuild=1
703             ;;
704
705         stripsymbols|-stripsymbols)
706             __cmakeargs="$__cmakeargs -DSTRIP_SYMBOLS=true"
707             ;;
708
709         clang3.5|-clang3.5)
710             __ClangMajorVersion=3
711             __ClangMinorVersion=5
712             ;;
713
714         clang3.6|-clang3.6)
715             __ClangMajorVersion=3
716             __ClangMinorVersion=6
717             ;;
718
719         clang3.7|-clang3.7)
720             __ClangMajorVersion=3
721             __ClangMinorVersion=7
722             ;;
723
724         clang3.8|-clang3.8)
725             __ClangMajorVersion=3
726             __ClangMinorVersion=8
727             ;;
728
729         clang3.9|-clang3.9)
730             __ClangMajorVersion=3
731             __ClangMinorVersion=9
732             ;;
733
734         clang4.0|-clang4.0)
735             __ClangMajorVersion=4
736             __ClangMinorVersion=0
737             ;;
738
739         clang5.0|-clang5.0)
740             __ClangMajorVersion=5
741             __ClangMinorVersion=0
742             ;;
743
744         clang6.0|-clang6.0)
745             __ClangMajorVersion=6
746             __ClangMinorVersion=0
747             ;;
748
749         ninja|-ninja)
750             __UseNinja=1
751             ;;
752
753         pgoinstrument|-pgoinstrument)
754             __PgoInstrument=1
755             ;;
756
757         nopgooptimize|-nopgooptimize)
758             __PgoOptimize=0
759             __SkipRestoreOptData=1
760             ;;
761
762         ibcinstrument|-ibcinstrument)
763             __IbcTuning="/Tuning"
764             ;;
765
766         configureonly|-configureonly)
767             __ConfigureOnly=1
768             __SkipMSCorLib=1
769             __SkipNuget=1
770             ;;
771
772         skipconfigure|-skipconfigure)
773             __SkipConfigure=1
774             ;;
775
776         skipnative|-skipnative)
777             # Use "skipnative" to use the same option name as build.cmd.
778             __SkipCoreCLR=1
779             ;;
780
781         skipcoreclr|-skipcoreclr)
782             # Accept "skipcoreclr" for backwards-compatibility.
783             __SkipCoreCLR=1
784             ;;
785
786         crosscomponent|-crosscomponent)
787             __DoCrossArchBuild=1
788             ;;
789
790         skipmscorlib|-skipmscorlib)
791             __SkipMSCorLib=1
792             ;;
793
794         skipgenerateversion|-skipgenerateversion)
795             __SkipGenerateVersion=1
796             ;;
797
798         skiprestoreoptdata|-skiprestoreoptdata)
799             __SkipRestoreOptData=1
800             ;;
801
802         skipcrossgen|-skipcrossgen)
803             __SkipCrossgen=1
804             ;;
805
806         includetests|-includetests)
807             ;;
808
809         skiptests|-skiptests)
810             __IncludeTests=
811             ;;
812
813         skipnuget|-skipnuget)
814             __SkipNuget=1
815             ;;
816
817         ignorewarnings|-ignorewarnings)
818             __IgnoreWarnings=1
819             __cmakeargs="$__cmakeargs -DCLR_CMAKE_WARNINGS_ARE_ERRORS=OFF"
820             ;;
821
822         cmakeargs|-cmakeargs)
823             if [ -n "$2" ]; then
824                 __cmakeargs="$__cmakeargs $2"
825                 shift
826             else
827                 echo "ERROR: 'cmakeargs' requires a non-empty option argument"
828                 exit 1
829             fi
830             ;;
831
832         bindir|-bindir)
833             if [ -n "$2" ]; then
834                 __RootBinDir="$2"
835                 if [ ! -d $__RootBinDir ]; then
836                     mkdir $__RootBinDir
837                 fi
838                 __RootBinParent=$(dirname $__RootBinDir)
839                 __RootBinName=${__RootBinDir##*/}
840                 __RootBinDir="$(cd $__RootBinParent &>/dev/null && printf %s/%s $PWD $__RootBinName)"
841                 shift
842             else
843                 echo "ERROR: 'bindir' requires a non-empty option argument"
844                 exit 1
845             fi
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         osgroup|-osgroup)
860             if [ -n "$2" ]; then
861               __BuildOS="$2"
862               shift
863             else
864               echo "ERROR: 'osgroup' requires a non-empty option argument"
865               exit 1
866             fi
867             ;;
868
869         *)
870             __UnprocessedBuildArgs="$__UnprocessedBuildArgs $1"
871             ;;
872     esac
873
874     shift
875 done
876
877 __RunArgs="-BuildArch=$__BuildArch -BuildType=$__BuildType -BuildOS=$__BuildOS"
878
879 # Configure environment if we are doing a verbose build
880 if [ $__VerboseBuild == 1 ]; then
881     export VERBOSE=1
882         __RunArgs="$__RunArgs -verbose"
883 fi
884
885 # Set default clang version
886 if [[ $__ClangMajorVersion == 0 && $__ClangMinorVersion == 0 ]]; then
887     __ClangMajorVersion=3
888     __ClangMinorVersion=9
889 fi
890
891 if [[ "$__BuildArch" == "armel" ]]; then
892     # Armel cross build is Tizen specific and does not support Portable RID build
893     __PortableBuild=0
894 fi
895
896 if [ $__PortableBuild == 0 ]; then
897         __RunArgs="$__RunArgs -PortableBuild=false"
898 fi
899
900 # Set dependent variables
901 __LogsDir="$__RootBinDir/Logs"
902
903 # init the host distro name
904 initHostDistroRid
905
906 # Set the remaining variables based upon the determined build configuration
907 __BinDir="$__RootBinDir/Product/$__BuildOS.$__BuildArch.$__BuildType"
908 __PackagesBinDir="$__BinDir/.nuget"
909 __ToolsDir="$__RootBinDir/tools"
910 __TestWorkingDir="$__RootBinDir/tests/$__BuildOS.$__BuildArch.$__BuildType"
911 export __IntermediatesDir="$__RootBinDir/obj/$__BuildOS.$__BuildArch.$__BuildType"
912 __TestIntermediatesDir="$__RootBinDir/tests/obj/$__BuildOS.$__BuildArch.$__BuildType"
913 __isMSBuildOnNETCoreSupported=0
914 __CrossComponentBinDir="$__BinDir"
915 __CrossCompIntermediatesDir="$__IntermediatesDir/crossgen"
916
917 __CrossArch="$__HostArch"
918 if [[ "$__HostArch" == "x64" && ("$__BuildArch" == "arm" || "$__BuildArch" == "armel") ]]; then
919     __CrossArch="x86"
920 fi
921 if [ $__CrossBuild == 1 ]; then
922     __CrossComponentBinDir="$__CrossComponentBinDir/$__CrossArch"
923 fi
924 __CrossgenCoreLibLog="$__LogsDir/CrossgenCoreLib_$__BuildOS.$BuildArch.$__BuildType.log"
925 __CrossgenExe="$__CrossComponentBinDir/crossgen"
926
927 # Init if MSBuild for .NET Core is supported for this platform
928 isMSBuildOnNETCoreSupported
929
930 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to set.
931 # This is needed by CLI to function.
932 if [ -z "$HOME" ]; then
933     if [ ! -d "$__ProjectDir/temp_home" ]; then
934         mkdir temp_home
935     fi
936     export HOME=$__ProjectDir/temp_home
937     echo "HOME not defined; setting it to $HOME"
938 fi
939
940 # Specify path to be set for CMAKE_INSTALL_PREFIX.
941 # This is where all built CoreClr libraries will copied to.
942 export __CMakeBinDir="$__BinDir"
943
944 # Configure environment if we are doing a cross compile.
945 if [ $__CrossBuild == 1 ]; then
946     export CROSSCOMPILE=1
947     if ! [[ -n "$ROOTFS_DIR" ]]; then
948         export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__BuildArch"
949     fi
950 fi
951
952 # init the target distro name
953 initTargetDistroRid
954
955 # Make the directories necessary for build if they don't exist
956 setup_dirs
957
958 # Check prereqs.
959 check_prereqs
960
961 # Restore the package containing profile counts for profile-guided optimizations
962 restore_optdata
963
964 # Generate event logging infrastructure sources
965 generate_event_logging
966
967 # Build the coreclr (native) components.
968 __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"
969 build_native $__SkipCoreCLR "$__BuildArch" "$__IntermediatesDir" "$__ExtraCmakeArgs" "CoreCLR component"
970
971 # Build cross-architecture components
972 if [[ $__CrossBuild == 1 && $__DoCrossArchBuild == 1 ]]; then
973     build_cross_arch_component
974 fi
975
976 # Build System.Private.CoreLib.
977
978 build_CoreLib
979
980 # Generate nuget packages
981 if [ $__SkipNuget != 1 ]; then
982     generate_NugetPackages
983 fi
984
985
986 # Build complete
987
988 echo "Repo successfully built."
989 echo "Product binaries are available at $__BinDir"
990 exit 0