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