Merge pull request #13392 from noahfalk/tiered_jit_fix_tests
[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 "-sequential: force a non-parallel build ^(default is to build in parallel"
47         echo "   using all processors^)."
48         echo "-officialbuildid=^<ID^>: specify the official build ID to be used by this build."
49         echo "-Rebuild: passes /t:rebuild to the build projects."
50     echo "-stripSymbols - Optional argument to strip native symbols during the build."
51     echo "-skipgenerateversion - disable version generation even if MSBuild is supported."
52     echo "-ignorewarnings - do not treat warnings as errors"
53     echo "-cmakeargs - user-settable additional arguments passed to CMake."
54     echo "-bindir - output directory (defaults to $__ProjectRoot/bin)"
55     echo "-buildstandalonegc - builds the GC in a standalone mode. Can't be used with \"cmakeargs\"."
56     echo "-msbuildonunsupportedplatform - build managed binaries even if distro is not officially supported."
57     echo "-numproc - set the number of build processes."
58     exit 1
59 }
60
61 initHostDistroRid()
62 {
63     __HostDistroRid=""
64     if [ "$__HostOS" == "Linux" ]; then
65         if [ -e /etc/os-release ]; then
66             source /etc/os-release
67             __HostDistroRid="$ID.$VERSION_ID-$__HostArch"
68         elif [ -e /etc/redhat-release ]; then
69             local redhatRelease=$(</etc/redhat-release)
70             if [[ $redhatRelease == "CentOS release 6."* || $redhatRelease == "Red Hat Enterprise Linux Server release 6."* ]]; then
71                __HostDistroRid="rhel.6-$__HostArch"
72             fi
73         fi
74     fi
75
76     if [ "$__HostDistroRid" == "" ]; then
77         echo "WARNING: Can not determine runtime id for current distro."
78     fi
79 }
80
81 initTargetDistroRid()
82 {
83     if [ $__CrossBuild == 1 ]; then
84         if [ "$__BuildOS" == "Linux" ]; then
85             if [ ! -e $ROOTFS_DIR/etc/os-release ]; then
86                 if [ -e $ROOTFS_DIR/android_platform ]; then
87                     source $ROOTFS_DIR/android_platform
88                     export __DistroRid="$RID"
89                 else
90                     echo "WARNING: Can not determine runtime id for current distro."
91                     export __DistroRid=""
92                 fi
93             else
94                 source $ROOTFS_DIR/etc/os-release
95                 export __DistroRid="$ID.$VERSION_ID-$__BuildArch"
96             fi
97         fi
98     else
99         export __DistroRid="$__HostDistroRid"
100     fi
101
102     # Portable builds target the base RID
103     if [ $__PortableBuild == 1 ]; then
104         if [ "$__BuildOS" == "Linux" ]; then
105             export __DistroRid="linux-$__BuildArch"
106         elif [ "$__BuildOS" == "OSX" ]; then
107             export __DistroRid="osx-$__BuildArch"
108         fi
109     fi
110 }
111
112 setup_dirs()
113 {
114     echo Setting up directories for build
115
116     mkdir -p "$__RootBinDir"
117     mkdir -p "$__BinDir"
118     mkdir -p "$__LogsDir"
119     mkdir -p "$__IntermediatesDir"
120
121     if [ $__CrossBuild == 1 ]; then
122         mkdir -p "$__CrossComponentBinDir"
123         mkdir -p "$__CrossCompIntermediatesDir"
124     fi
125 }
126
127 # Check the system to ensure the right prereqs are in place
128
129 check_prereqs()
130 {
131     echo "Checking prerequisites..."
132
133     # Check presence of CMake on the path
134     hash cmake 2>/dev/null || { echo >&2 "Please install cmake before running this script"; exit 1; }
135
136
137     # Minimum required version of clang is version 3.9 for arm/armel cross build
138     if [[ $__CrossBuild == 1 && ("$__BuildArch" == "arm" || "$__BuildArch" == "armel") ]]; then
139         if ! [[ "$__ClangMajorVersion" -gt "3" || ( $__ClangMajorVersion == 3 && $__ClangMinorVersion == 9 ) ]]; then
140             echo "Please install clang3.9 or latest for arm/armel cross build"; exit 1;
141         fi
142     fi
143
144     # Check for clang
145     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; }
146
147 }
148
149 restore_optdata()
150 {
151     # we only need optdata on a Release build
152     if [[ "$__BuildType" != "Release" ]]; then __SkipRestoreOptData=1; fi
153
154     if [[ ( $__SkipRestoreOptData == 0 ) && ( $__isMSBuildOnNETCoreSupported == 1 ) ]]; then
155         echo "Restoring the OptimizationData package"
156         "$__ProjectRoot/run.sh" sync -optdata
157         if [ $? != 0 ]; then
158             echo "Failed to restore the optimization data package."
159             exit 1
160         fi
161     fi
162
163     if [ $__isMSBuildOnNETCoreSupported == 1 ]; then
164         # Parse the optdata package versions out of msbuild so that we can pass them on to CMake
165         local DotNetCli="$__ProjectRoot/Tools/dotnetcli/dotnet"
166         if [ ! -f $DotNetCli ]; then
167             "$__ProjectRoot/init-tools.sh"
168             if [ $? != 0 ]; then
169                 echo "Failed to restore buildtools."
170                 exit 1
171             fi
172         fi
173         local OptDataProjectFilePath="$__ProjectRoot/src/.nuget/optdata/optdata.csproj"
174         __PgoOptDataVersion=$(DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 $DotNetCli msbuild $OptDataProjectFilePath /t:DumpPgoDataPackageVersion /nologo | sed 's/^\s*//')
175         __IbcOptDataVersion=$(DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 $DotNetCli msbuild $OptDataProjectFilePath /t:DumpIbcDataPackageVersion /nologo | sed 's/^\s*//')
176     fi
177 }
178
179 generate_event_logging_sources()
180 {
181     if [ $__SkipCoreCLR == 1 ]; then
182         return
183     fi
184
185 # Event Logging Infrastructure
186    __GeneratedIntermediate="$__IntermediatesDir/Generated"
187    __GeneratedIntermediateEventProvider="$__GeneratedIntermediate/eventprovider_new"
188    __GeneratedIntermediateEventPipe="$__GeneratedIntermediate/eventpipe_new"
189
190     if [[ -d "$__GeneratedIntermediateEventProvider" ]]; then
191         rm -rf  "$__GeneratedIntermediateEventProvider"
192     fi
193
194     if [[ -d "$__GeneratedIntermediateEventPipe" ]]; then
195         rm -rf  "$__GeneratedIntermediateEventPipe"
196     fi
197
198     if [[ ! -d "$__GeneratedIntermediate/eventprovider" ]]; then
199         mkdir -p "$__GeneratedIntermediate/eventprovider"
200     fi
201
202     if [[ ! -d "$__GeneratedIntermediate/eventpipe" ]]; then
203         mkdir -p "$__GeneratedIntermediate/eventpipe"
204     fi
205
206     mkdir -p "$__GeneratedIntermediateEventProvider"
207     mkdir -p "$__GeneratedIntermediateEventPipe"
208
209     __PythonWarningFlags="-Wall"
210     if [[ $__IgnoreWarnings == 0 ]]; then
211         __PythonWarningFlags="$__PythonWarningFlags -Werror"
212     fi
213
214
215     if [[ $__SkipCoreCLR == 0 || $__ConfigureOnly == 1 ]]; then
216         echo "Laying out dynamically generated files consumed by the build system "
217         echo "Laying out dynamically generated Event Logging Test files"
218         $PYTHON -B $__PythonWarningFlags "$__ProjectRoot/src/scripts/genXplatEventing.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --exc "$__ProjectRoot/src/vm/ClrEtwAllMeta.lst" --testdir "$__GeneratedIntermediateEventProvider/tests"
219
220         if  [[ $? != 0 ]]; then
221             exit
222         fi
223
224         case $__BuildOS in
225             Linux)
226                 echo "Laying out dynamically generated EventPipe Implementation"
227                 $PYTHON -B $__PythonWarningFlags "$__ProjectRoot/src/scripts/genEventPipe.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --intermediate "$__GeneratedIntermediateEventPipe" --exc "$__ProjectRoot/src/vm/ClrEtwAllMeta.lst"
228                 if  [[ $? != 0 ]]; then
229                     exit
230                 fi
231                 ;;
232             *)
233                 ;;
234         esac
235
236         #determine the logging system
237         case $__BuildOS in
238             Linux)
239                 echo "Laying out dynamically generated Event Logging Implementation of Lttng"
240                 $PYTHON -B $__PythonWarningFlags "$__ProjectRoot/src/scripts/genXplatLttng.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --intermediate "$__GeneratedIntermediateEventProvider"
241                 if  [[ $? != 0 ]]; then
242                     exit
243                 fi
244                 ;;
245             *)
246                 ;;
247         esac
248     fi
249
250     echo "Cleaning the temp folder of dynamically generated Event Logging files"
251     $PYTHON -B $__PythonWarningFlags -c "import sys;sys.path.insert(0,\"$__ProjectRoot/src/scripts\"); from Utilities import *;UpdateDirectory(\"$__GeneratedIntermediate/eventprovider\",\"$__GeneratedIntermediateEventProvider\")"
252     if  [[ $? != 0 ]]; then
253         exit
254     fi
255
256     rm -rf "$__GeneratedIntermediateEventProvider"
257
258     echo "Cleaning the temp folder of dynamically generated EventPipe files"
259     $PYTHON -B $__PythonWarningFlags -c "import sys;sys.path.insert(0,\"$__ProjectRoot/src/scripts\"); from Utilities import *;UpdateDirectory(\"$__GeneratedIntermediate/eventpipe\",\"$__GeneratedIntermediateEventPipe\")"
260     if  [[ $? != 0 ]]; then
261         exit
262     fi
263
264     rm -rf "$__GeneratedIntermediateEventPipe"
265 }
266
267 build_native()
268 {
269     skipCondition=$1
270     platformArch="$2"
271     intermediatesForBuild="$3"
272     extraCmakeArguments="$4"
273     message="$5"
274
275     if [ $skipCondition == 1 ]; then
276         echo "Skipping $message build."
277         return
278     fi
279
280     # All set to commence the build
281     echo "Commencing build of $message for $__BuildOS.$__BuildArch.$__BuildType in $intermediatesForBuild"
282
283     generator=""
284     buildFile="Makefile"
285     buildTool="make"
286     if [ $__UseNinja == 1 ]; then
287         generator="ninja"
288         buildFile="build.ninja"
289         if ! buildTool=$(command -v ninja || command -v ninja-build); then
290            echo "Unable to locate ninja!" 1>&2
291            exit 1
292         fi
293     fi
294
295     if [ $__SkipConfigure == 0 ]; then
296         # if msbuild is not supported, then set __SkipGenerateVersion to 1
297         if [ $__isMSBuildOnNETCoreSupported == 0 ]; then __SkipGenerateVersion=1; fi
298         # Drop version.cpp file
299         __versionSourceFile="$intermediatesForBuild/version.cpp"
300         if [ $__SkipGenerateVersion == 0 ]; then
301             pwd
302             "$__ProjectRoot/run.sh" build -Project=$__ProjectDir/build.proj -generateHeaderUnix -NativeVersionSourceFile=$__versionSourceFile $__RunArgs $__UnprocessedBuildArgs
303         else
304             # Generate the dummy version.cpp, but only if it didn't exist to make sure we don't trigger unnecessary rebuild
305             __versionSourceLine="static char sccsid[] __attribute__((used)) = \"@(#)No version information produced\";"
306             if [ -e $__versionSourceFile ]; then
307                 read existingVersionSourceLine < $__versionSourceFile
308             fi
309             if [ "$__versionSourceLine" != "$existingVersionSourceLine" ]; then
310                 echo $__versionSourceLine > $__versionSourceFile
311             fi
312         fi
313
314
315         pushd "$intermediatesForBuild"
316         # Regenerate the CMake solution
317         echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__ProjectRoot\" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $__IncludeTests $generator $extraCmakeArguments $__cmakeargs"
318         "$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__ProjectRoot" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $__IncludeTests $generator "$extraCmakeArguments" "$__cmakeargs"
319         popd
320     fi
321
322     if [ ! -f "$intermediatesForBuild/$buildFile" ]; then
323         echo "Failed to generate $message build project!"
324         exit 1
325     fi
326     
327     # Build
328     if [ $__ConfigureOnly == 1 ]; then
329         echo "Finish configuration & skipping $message build."
330         return
331     fi
332
333     # Check that the makefiles were created.
334     pushd "$intermediatesForBuild"
335
336     echo "Executing $buildTool install -j $__NumProc"
337
338     $buildTool install -j $__NumProc
339     if [ $? != 0 ]; then
340         echo "Failed to build $message."
341         exit 1
342     fi
343
344     popd
345 }
346
347 build_cross_arch_component()
348 {
349     __SkipCrossArchBuild=1
350     TARGET_ROOTFS=""
351     # check supported cross-architecture components host(__HostArch)/target(__BuildArch) pair
352     if [[ ("$__BuildArch" == "arm" || "$__BuildArch" == "armel") && "$__CrossArch" == "x86" ]]; then
353         export CROSSCOMPILE=0
354         __SkipCrossArchBuild=0
355
356         # building x64-host/arm-target cross-architecture component need to use cross toolchain of x86
357         if [ "$__HostArch" == "x64" ]; then
358             export CROSSCOMPILE=1
359         fi
360     else
361         # not supported
362         return
363     fi
364
365     export __CMakeBinDir="$__CrossComponentBinDir"
366     export CROSSCOMPONENT=1
367     __IncludeTests=
368
369     if [ $CROSSCOMPILE == 1 ]; then
370         TARGET_ROOTFS="$ROOTFS_DIR"
371         if [ -n "$CAC_ROOTFS_DIR" ]; then
372             export ROOTFS_DIR="$CAC_ROOTFS_DIR"
373         else
374             export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__CrossArch"
375         fi
376     fi
377
378     __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"
379     build_native $__SkipCrossArchBuild "$__CrossArch" "$__CrossCompIntermediatesDir" "$__ExtraCmakeArgs" "cross-architecture component"
380
381     # restore ROOTFS_DIR, CROSSCOMPONENT, and CROSSCOMPILE
382     if [ -n "$TARGET_ROOTFS" ]; then
383         export ROOTFS_DIR="$TARGET_ROOTFS"
384     fi
385     export CROSSCOMPONENT=
386     export CROSSCOMPILE=1
387 }
388
389 isMSBuildOnNETCoreSupported()
390 {
391     __isMSBuildOnNETCoreSupported=$__msbuildonunsupportedplatform
392
393     if [ $__isMSBuildOnNETCoreSupported == 1 ]; then
394         return
395     fi
396
397     if [ "$__HostArch" == "x64" ]; then
398         if [ "$__HostOS" == "Linux" ]; then
399             __isMSBuildOnNETCoreSupported=1
400             # note: the RIDs below can use globbing patterns
401             UNSUPPORTED_RIDS=("debian.9-x64" "ubuntu.17.04-x64" "alpine.3.6.*-x64")
402             for UNSUPPORTED_RID in "${UNSUPPORTED_RIDS[@]}"
403             do
404                 if [[ $__HostDistroRid == $UNSUPPORTED_RID ]]; then
405                     __isMSBuildOnNETCoreSupported=0
406                     break
407                 fi
408             done
409         elif [ "$__HostOS" == "OSX" ]; then
410             __isMSBuildOnNETCoreSupported=1
411         fi
412     fi
413 }
414
415
416 build_CoreLib_ni()
417 {
418     if [ $__SkipCrossgen == 1 ]; then
419         echo "Skipping generating native image"
420         return
421     fi
422
423     if [ $__SkipCoreCLR == 0 -a -e $__BinDir/crossgen ]; then
424         echo "Generating native image for System.Private.CoreLib."
425         echo "$__BinDir/crossgen /Platform_Assemblies_Paths $__BinDir/IL $__IbcTuning /out $__BinDir/System.Private.CoreLib.dll $__BinDir/IL/System.Private.CoreLib.dll"
426         $__BinDir/crossgen /Platform_Assemblies_Paths $__BinDir/IL $__IbcTuning /out $__BinDir/System.Private.CoreLib.dll $__BinDir/IL/System.Private.CoreLib.dll
427         if [ $? -ne 0 ]; then
428             echo "Failed to generate native image for System.Private.CoreLib."
429             exit 1
430         fi
431
432         if [ "$__BuildOS" == "Linux" ]; then
433             echo "Generating symbol file for System.Private.CoreLib."
434             $__BinDir/crossgen /CreatePerfMap $__BinDir $__BinDir/System.Private.CoreLib.dll
435             if [ $? -ne 0 ]; then
436                 echo "Failed to generate symbol file for System.Private.CoreLib."
437                 exit 1
438             fi
439         fi
440     fi
441 }
442
443 build_CoreLib()
444 {
445
446     if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
447         echo "System.Private.CoreLib.dll build unsupported."
448         return
449     fi
450
451     if [ $__SkipMSCorLib == 1 ]; then
452        echo "Skipping building System.Private.CoreLib."
453        return
454     fi
455
456     echo "Commencing build of managed components for $__BuildOS.$__BuildArch.$__BuildType"
457
458     # Invoke MSBuild
459     __ExtraBuildArgs=""
460     if [[ "$__IbcTuning" -eq "" ]]; then
461         __ExtraBuildArgs="$__ExtraBuildArgs -OptimizationDataDir=\"$__PackagesDir/optimization.$__BuildOS-$__BuildArch.IBC.CoreCLR/$__IbcOptDataVersion/data/\""
462         __ExtraBuildArgs="$__ExtraBuildArgs -EnableProfileGuidedOptimization=true"
463     fi
464     $__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
465
466     if [ $? -ne 0 ]; then
467         echo "Failed to build managed components."
468         exit 1
469     fi
470
471     # The cross build generates a crossgen with the target architecture.
472     if [ $__CrossBuild != 1 ]; then
473        # The architecture of host pc must be same architecture with target.
474        if [[ ( "$__HostArch" == "$__BuildArch" ) ]]; then
475            build_CoreLib_ni
476        elif [[ ( "$__HostArch" == "x64" ) && ( "$__BuildArch" == "x86" ) ]]; then
477            build_CoreLib_ni
478        elif [[ ( "$__HostArch" == "arm64" ) && ( "$__BuildArch" == "arm" ) ]]; then
479            build_CoreLib_ni
480        else
481            exit 1
482        fi
483     fi
484 }
485
486 generate_NugetPackages()
487 {
488     # We can only generate nuget package if we also support building mscorlib as part of this build.
489     if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
490         echo "Nuget package generation unsupported."
491         return
492     fi
493
494     # Since we can build mscorlib for this OS, did we build the native components as well?
495     if [ $__SkipCoreCLR == 1 ]; then
496         echo "Unable to generate nuget packages since native components were not built."
497         return
498     fi
499
500     echo "Generating nuget packages for "$__BuildOS
501     echo "DistroRid is "$__DistroRid
502     echo "ROOTFS_DIR is "$ROOTFS_DIR
503     # Build the packages
504     $__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
505
506     if [ $? -ne 0 ]; then
507         echo "Failed to generate Nuget packages."
508         exit 1
509     fi
510 }
511
512 echo "Commencing CoreCLR Repo build"
513
514 # Argument types supported by this script:
515 #
516 # Build architecture - valid values are: x64, ARM.
517 # Build Type         - valid values are: Debug, Checked, Release
518 #
519 # Set the default arguments for build
520
521 # Obtain the location of the bash script to figure out where the root of the repo is.
522 __ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
523
524 # Use uname to determine what the CPU is.
525 CPUName=$(uname -p)
526 # Some Linux platforms report unknown for platform, but the arch for machine.
527 if [ "$CPUName" == "unknown" ]; then
528     CPUName=$(uname -m)
529 fi
530
531 case $CPUName in
532     i686)
533         echo "Unsupported CPU $CPUName detected, build might not succeed!"
534         __BuildArch=x86
535         __HostArch=x86
536         ;;
537
538     x86_64)
539         __BuildArch=x64
540         __HostArch=x64
541         ;;
542
543     armv7l)
544         echo "Unsupported CPU $CPUName detected, build might not succeed!"
545         __BuildArch=arm
546         __HostArch=arm
547         ;;
548
549     aarch64)
550         __BuildArch=arm64
551         __HostArch=arm64
552         ;;
553
554     *)
555         echo "Unknown CPU $CPUName detected, configuring as if for x64"
556         __BuildArch=x64
557         __HostArch=x64
558         ;;
559 esac
560
561 # Use uname to determine what the OS is.
562 OSName=$(uname -s)
563 case $OSName in
564     Linux)
565         __BuildOS=Linux
566         __HostOS=Linux
567         ;;
568
569     Darwin)
570         __BuildOS=OSX
571         __HostOS=OSX
572         ;;
573
574     FreeBSD)
575         __BuildOS=FreeBSD
576         __HostOS=FreeBSD
577         ;;
578
579     OpenBSD)
580         __BuildOS=OpenBSD
581         __HostOS=OpenBSD
582         ;;
583
584     NetBSD)
585         __BuildOS=NetBSD
586         __HostOS=NetBSD
587         ;;
588
589     SunOS)
590         __BuildOS=SunOS
591         __HostOS=SunOS
592         ;;
593
594     *)
595         echo "Unsupported OS $OSName detected, configuring as if for Linux"
596         __BuildOS=Linux
597         __HostOS=Linux
598         ;;
599 esac
600
601 __BuildType=Debug
602 __CodeCoverage=
603 __IncludeTests=Include_Tests
604 __IgnoreWarnings=0
605
606 # Set the various build properties here so that CMake and MSBuild can pick them up
607 __ProjectDir="$__ProjectRoot"
608 __SourceDir="$__ProjectDir/src"
609 __PackagesDir="$__ProjectDir/packages"
610 __RootBinDir="$__ProjectDir/bin"
611 __UnprocessedBuildArgs=
612 __RunArgs=
613 __MSBCleanBuildArgs=
614 __UseNinja=0
615 __VerboseBuild=0
616 __PgoInstrument=0
617 __PgoOptimize=1
618 __IbcTuning=""
619 __ConfigureOnly=0
620 __SkipConfigure=0
621 __SkipRestore=""
622 __SkipNuget=0
623 __SkipCoreCLR=0
624 __SkipMSCorLib=0
625 __SkipRestoreOptData=0
626 __SkipCrossgen=0
627 __CrossBuild=0
628 __ClangMajorVersion=0
629 __ClangMinorVersion=0
630 __NuGetPath="$__PackagesDir/NuGet.exe"
631 __HostDistroRid=""
632 __DistroRid=""
633 __cmakeargs=""
634 __SkipGenerateVersion=0
635 __DoCrossArchBuild=0
636 __PortableBuild=1
637 __msbuildonunsupportedplatform=0
638 __PgoOptDataVersion=""
639 __IbcOptDataVersion=""
640
641 # Get the number of processors available to the scheduler
642 # Other techniques such as `nproc` only get the number of
643 # processors available to a single process.
644 if [ `uname` = "FreeBSD" ]; then
645   __NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
646 elif [ `uname` = "NetBSD" ]; then
647   __NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
648 else
649   __NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
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