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