Update PgoData to release-20180712-0122 (#18883)
[platform/upstream/coreclr.git] / build.sh
1 #!/usr/bin/env bash
2
3 # Work around Jenkins CI + msbuild problem: Jenkins sometimes creates very large environment
4 # variables, and msbuild can't handle environment blocks with such large variables. So clear
5 # out the variables that might be too large.
6 export ghprbCommentBody=
7
8 # resolve python-version to use
9 if [ "$PYTHON" == "" ] ; then
10     if ! PYTHON=$(command -v python2.7 || command -v python2 || command -v python)
11     then
12        echo "Unable to locate build-dependency python2.x!" 1>&2
13        exit 1
14     fi
15 fi
16
17 # validate python-dependency
18 # useful in case of explicitly set option.
19 if ! command -v $PYTHON > /dev/null
20 then
21    echo "Unable to locate build-dependency python2.x ($PYTHON)!" 1>&2
22    exit 1
23 fi
24
25 usage()
26 {
27     echo "Usage: $0 [BuildArch] [BuildType] [-verbose] [-coverage] [-cross] [-clangx.y] [-ninja] [-configureonly] [-skipconfigure] [-skipnative] [-skipmscorlib] [-skiptests] [-stripsymbols] [-ignorewarnings] [-cmakeargs] [-bindir]"
28     echo "BuildArch can be: -x64, -x86, -arm, -armel, -arm64"
29     echo "BuildType can be: -debug, -checked, -release"
30     echo "-coverage - optional argument to enable code coverage build (currently supported only for Linux and OSX)."
31     echo "-ninja - target ninja instead of GNU make"
32     echo "-clangx.y - optional argument to build using clang version x.y."
33     echo "-cross - optional argument to signify cross compilation,"
34     echo "       - will use ROOTFS_DIR environment variable if set."
35     echo "-crosscomponent - optional argument to build cross-architecture component,"
36     echo "                - will use CAC_ROOTFS_DIR environment variable if set."
37     echo "-nopgooptimize - do not use profile guided optimizations."
38     echo "-pgoinstrument - generate instrumented code for profile guided optimization enabled binaries."
39     echo "-ibcinstrument - generate IBC-tuning-enabled native images when invoking crossgen."
40     echo "-configureonly - do not perform any builds; just configure the build."
41     echo "-skipconfigure - skip build configuration."
42     echo "-skipnative - do not build native components."
43     echo "-skipmscorlib - do not build mscorlib.dll."
44     echo "-skiptests - skip the tests in the 'tests' subdirectory."
45     echo "-skipnuget - skip building nuget packages."
46     echo "-skiprestoreoptdata - skip restoring optimization data used by profile-based optimizations."
47     echo "-skipcrossgen - skip native image generation"
48     echo "-verbose - optional argument to enable verbose build output."
49     echo "-skiprestore: skip restoring packages ^(default: packages are restored during build^)."
50     echo "-disableoss: Disable Open Source Signing for System.Private.CoreLib."
51     echo "-officialbuildid=^<ID^>: specify the official build ID to be used by this build."
52     echo "-Rebuild: passes /t:rebuild to the build projects."
53     echo "-stripSymbols - Optional argument to strip native symbols during the build."
54     echo "-skipgenerateversion - disable version generation even if MSBuild is supported."
55     echo "-ignorewarnings - do not treat warnings as errors"
56     echo "-cmakeargs - user-settable additional arguments passed to CMake."
57     echo "-bindir - output directory (defaults to $__ProjectRoot/bin)"
58     echo "-msbuildonunsupportedplatform - build managed binaries even if distro is not officially supported."
59     echo "-numproc - set the number of build processes."
60     exit 1
61 }
62
63 initHostDistroRid()
64 {
65     __HostDistroRid=""
66     if [ "$__HostOS" == "Linux" ]; then
67         if [ -e /etc/os-release ]; then
68             source /etc/os-release
69             if [[ $ID == "rhel" ]]; then
70                 # remove the last version digit
71                 VERSION_ID=${VERSION_ID%.*}
72             fi
73             __HostDistroRid="$ID.$VERSION_ID-$__HostArch"
74             if [[ $ID == "alpine" ]]; then
75                 __HostDistroRid="linux-musl-$__HostArch"
76             fi
77         elif [ -e /etc/redhat-release ]; then
78             local redhatRelease=$(</etc/redhat-release)
79             if [[ $redhatRelease == "CentOS release 6."* || $redhatRelease == "Red Hat Enterprise Linux Server release 6."* ]]; then
80                __HostDistroRid="rhel.6-$__HostArch"
81             fi
82         fi
83     fi
84     if [ "$__HostOS" == "FreeBSD" ]; then
85         __freebsd_version=`sysctl -n kern.osrelease | cut -f1 -d'.'`
86         __HostDistroRid="freebsd.$__freebsd_version-$__HostArch"
87     fi
88
89     if [ "$__HostDistroRid" == "" ]; then
90         echo "WARNING: Can not determine runtime id for current distro."
91     fi
92 }
93
94 initTargetDistroRid()
95 {
96     if [ $__CrossBuild == 1 ]; then
97         if [ "$__BuildOS" == "Linux" ]; then
98             if [ ! -e $ROOTFS_DIR/etc/os-release ]; then
99                 if [ -e $ROOTFS_DIR/android_platform ]; then
100                     source $ROOTFS_DIR/android_platform
101                     export __DistroRid="$RID"
102                 else
103                     echo "WARNING: Can not determine runtime id for current distro."
104                     export __DistroRid=""
105                 fi
106             else
107                 source $ROOTFS_DIR/etc/os-release
108                 export __DistroRid="$ID.$VERSION_ID-$__BuildArch"
109             fi
110         fi
111     else
112         export __DistroRid="$__HostDistroRid"
113     fi
114
115     if [ "$__BuildOS" == "OSX" ]; then
116         __PortableBuild=1
117     fi
118
119     # Portable builds target the base RID
120     if [ $__PortableBuild == 1 ]; then
121         if [ "$__BuildOS" == "Linux" ]; then
122             export __DistroRid="linux-$__BuildArch"
123         elif [ "$__BuildOS" == "OSX" ]; then
124             export __DistroRid="osx-$__BuildArch"
125         elif [ "$__BuildOS" == "FreeBSD" ]; then
126             export __DistroRid="freebsd-$__BuildArch"
127         fi
128     fi
129 }
130
131 setup_dirs()
132 {
133     echo Setting up directories for build
134
135     mkdir -p "$__RootBinDir"
136     mkdir -p "$__BinDir"
137     mkdir -p "$__LogsDir"
138     mkdir -p "$__IntermediatesDir"
139
140     if [ $__CrossBuild == 1 ]; then
141         mkdir -p "$__CrossComponentBinDir"
142         mkdir -p "$__CrossCompIntermediatesDir"
143     fi
144 }
145
146 # Check the system to ensure the right prereqs are in place
147
148 check_prereqs()
149 {
150     echo "Checking prerequisites..."
151
152     # Check presence of CMake on the path
153     hash cmake 2>/dev/null || { echo >&2 "Please install cmake before running this script"; exit 1; }
154
155
156     # Minimum required version of clang is version 4.0 for arm/armel cross build
157     if [[ $__CrossBuild == 1 && ("$__BuildArch" == "arm" || "$__BuildArch" == "armel") ]]; then
158         if ! [[ "$__ClangMajorVersion" -ge "4" ]]; then
159             echo "Please install clang4.0 or latest for arm/armel cross build"; exit 1;
160         fi
161     fi
162
163     # Check for clang
164     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; }
165
166 }
167
168 restore_optdata()
169 {
170     # we only need optdata on a Release build
171     if [[ "$__BuildType" != "Release" ]]; then __SkipRestoreOptData=1; fi
172
173     if [[ ( $__SkipRestoreOptData == 0 ) && ( $__isMSBuildOnNETCoreSupported == 1 ) ]]; then
174         echo "Restoring the OptimizationData package"
175         "$__ProjectRoot/run.sh" build -optdata $__RunArgs $__UnprocessedBuildArgs
176         if [ $? != 0 ]; then
177             echo "Failed to restore the optimization data package."
178             exit 1
179         fi
180     fi
181
182     if [ $__isMSBuildOnNETCoreSupported == 1 ]; then
183         # Parse the optdata package versions out of msbuild so that we can pass them on to CMake
184         local DotNetCli="$__ProjectRoot/Tools/dotnetcli/dotnet"
185         if [ ! -f $DotNetCli ]; then
186             source "$__ProjectRoot/init-tools.sh"
187             if [ $? != 0 ]; then
188                 echo "Failed to restore buildtools."
189                 exit 1
190             fi
191         fi
192         local OptDataProjectFilePath="$__ProjectRoot/src/.nuget/optdata/optdata.csproj"
193         __PgoOptDataVersion=$(DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 $DotNetCli msbuild $OptDataProjectFilePath /t:DumpPgoDataPackageVersion /nologo | sed 's/^\s*//')
194         __IbcOptDataVersion=$(DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 $DotNetCli msbuild $OptDataProjectFilePath /t:DumpIbcDataPackageVersion /nologo | sed 's/^\s*//')
195     fi
196 }
197
198 generate_event_logging_sources()
199 {
200     __OutputDir=$1
201     __ConsumingBuildSystem=$2
202
203     __OutputIncDir="$__OutputDir/src/inc"
204     __OutputEventingDir="$__OutputDir/eventing"
205     __OutputEventProviderDir="$__OutputEventingDir/eventprovider"
206
207     echo "Laying out dynamically generated files consumed by $__ConsumingBuildSystem"
208     echo "Laying out dynamically generated Event test files, etmdummy stub functions, and external linkages"
209
210     __PythonWarningFlags="-Wall"
211     if [[ $__IgnoreWarnings == 0 ]]; then
212         __PythonWarningFlags="$__PythonWarningFlags -Werror"
213     fi
214
215     $PYTHON -B $__PythonWarningFlags "$__ProjectRoot/src/scripts/genEventing.py" --inc $__OutputIncDir --dummy $__OutputIncDir/etmdummy.h --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --testdir "$__OutputEventProviderDir/tests"
216     if [[ $? != 0 ]]; then
217         exit 1
218     fi
219
220     echo "Laying out dynamically generated EventPipe Implementation"
221     $PYTHON -B $__PythonWarningFlags "$__ProjectRoot/src/scripts/genEventPipe.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --intermediate "$__OutputEventingDir/eventpipe"
222
223     # determine the logging system
224     case $__BuildOS in
225         Linux|FreeBSD)
226             echo "Laying out dynamically generated Event Logging Implementation of Lttng"
227             $PYTHON -B $__PythonWarningFlags "$__ProjectRoot/src/scripts/genLttngProvider.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --intermediate "$__OutputEventProviderDir"
228             if [[ $? != 0 ]]; then
229                 exit 1
230             fi
231             ;;
232         *)
233             echo "Laying out dummy event logging provider"
234             $PYTHON -B $__PythonWarningFlags "$__ProjectRoot/src/scripts/genDummyProvider.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --intermediate "$__OutputEventProviderDir"
235             if [[ $? != 0 ]]; then
236                 exit 1
237             fi
238             ;;
239     esac
240 }
241
242 generate_event_logging()
243 {
244     # Event Logging Infrastructure
245     if [[ $__SkipCoreCLR == 0 || $__ConfigureOnly == 1 ]]; then
246         generate_event_logging_sources "$__IntermediatesDir" "the native build system"
247     fi
248
249     if [[ $__CrossBuild == 1 && $__DoCrossArchBuild == 1 ]]; then
250         generate_event_logging_sources "$__CrossCompIntermediatesDir" "the crossarch build system"
251     fi
252  }
253
254 build_native()
255 {
256     skipCondition=$1
257     platformArch="$2"
258     intermediatesForBuild="$3"
259     extraCmakeArguments="$4"
260     message="$5"
261
262     if [ $skipCondition == 1 ]; then
263         echo "Skipping $message build."
264         return
265     fi
266
267     # All set to commence the build
268     echo "Commencing build of $message for $__BuildOS.$__BuildArch.$__BuildType in $intermediatesForBuild"
269
270     generator=""
271     buildFile="Makefile"
272     buildTool="make"
273     if [ $__UseNinja == 1 ]; then
274         generator="ninja"
275         buildFile="build.ninja"
276         if ! buildTool=$(command -v ninja || command -v ninja-build); then
277            echo "Unable to locate ninja!" 1>&2
278            exit 1
279         fi
280     fi
281
282     if [ $__SkipConfigure == 0 ]; then
283         # if msbuild is not supported, then set __SkipGenerateVersion to 1
284         if [ $__isMSBuildOnNETCoreSupported == 0 ]; then __SkipGenerateVersion=1; fi
285         # Drop version.cpp file
286         __versionSourceFile="$intermediatesForBuild/version.cpp"
287         if [ $__SkipGenerateVersion == 0 ]; then
288             pwd
289             "$__ProjectRoot/run.sh" build -Project=$__ProjectDir/build.proj -generateHeaderUnix -NativeVersionSourceFile=$__versionSourceFile -MsBuildEventLogging="/l:BinClashLogger,Tools/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log"  $__RunArgs $__UnprocessedBuildArgs
290         else
291             # Generate the dummy version.cpp, but only if it didn't exist to make sure we don't trigger unnecessary rebuild
292             __versionSourceLine="static char sccsid[] __attribute__((used)) = \"@(#)No version information produced\";"
293             if [ -e $__versionSourceFile ]; then
294                 read existingVersionSourceLine < $__versionSourceFile
295             fi
296             if [ "$__versionSourceLine" != "$existingVersionSourceLine" ]; then
297                 echo $__versionSourceLine > $__versionSourceFile
298             fi
299         fi
300
301
302         pushd "$intermediatesForBuild"
303         # Regenerate the CMake solution
304         echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__ProjectRoot\" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $__IncludeTests $generator $extraCmakeArguments $__cmakeargs"
305         "$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__ProjectRoot" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $__IncludeTests $generator "$extraCmakeArguments" "$__cmakeargs"
306         popd
307     fi
308
309     if [ ! -f "$intermediatesForBuild/$buildFile" ]; then
310         echo "Failed to generate $message build project!"
311         exit 1
312     fi
313
314     # Build
315     if [ $__ConfigureOnly == 1 ]; then
316         echo "Finish configuration & skipping $message build."
317         return
318     fi
319
320     # Check that the makefiles were created.
321     pushd "$intermediatesForBuild"
322
323     echo "Executing $buildTool install -j $__NumProc"
324
325     $buildTool install -j $__NumProc
326     if [ $? != 0 ]; then
327         echo "Failed to build $message."
328         exit 1
329     fi
330
331     popd
332 }
333
334 build_cross_arch_component()
335 {
336     __SkipCrossArchBuild=1
337     TARGET_ROOTFS=""
338     # check supported cross-architecture components host(__HostArch)/target(__BuildArch) pair
339     if [[ ("$__BuildArch" == "arm" || "$__BuildArch" == "armel") && "$__CrossArch" == "x86" ]]; then
340         export CROSSCOMPILE=0
341         __SkipCrossArchBuild=0
342
343         # building x64-host/arm-target cross-architecture component need to use cross toolchain of x86
344         if [ "$__HostArch" == "x64" ]; then
345             export CROSSCOMPILE=1
346         fi
347     elif [[ ("$__BuildArch" == "arm64") && "$__CrossArch" == "x64" ]]; then
348         export CROSSCOMPILE=0
349         __SkipCrossArchBuild=0
350     else
351         # not supported
352         return
353     fi
354
355     export __CMakeBinDir="$__CrossComponentBinDir"
356     export CROSSCOMPONENT=1
357     __IncludeTests=
358
359     if [ $CROSSCOMPILE == 1 ]; then
360         TARGET_ROOTFS="$ROOTFS_DIR"
361         if [ -n "$CAC_ROOTFS_DIR" ]; then
362             export ROOTFS_DIR="$CAC_ROOTFS_DIR"
363         else
364             export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__CrossArch"
365         fi
366     fi
367
368     __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"
369     build_native $__SkipCrossArchBuild "$__CrossArch" "$__CrossCompIntermediatesDir" "$__ExtraCmakeArgs" "cross-architecture component"
370
371     # restore ROOTFS_DIR, CROSSCOMPONENT, and CROSSCOMPILE
372     if [ -n "$TARGET_ROOTFS" ]; then
373         export ROOTFS_DIR="$TARGET_ROOTFS"
374     fi
375     export CROSSCOMPONENT=
376     export CROSSCOMPILE=1
377 }
378
379 isMSBuildOnNETCoreSupported()
380 {
381     __isMSBuildOnNETCoreSupported=$__msbuildonunsupportedplatform
382
383     if [ $__isMSBuildOnNETCoreSupported == 1 ]; then
384         return
385     fi
386
387     if [ "$__HostArch" == "x64" ]; then
388         if [ "$__HostOS" == "Linux" ]; then
389             __isMSBuildOnNETCoreSupported=1
390             # note: the RIDs below can use globbing patterns
391             UNSUPPORTED_RIDS=("debian.9-x64" "ubuntu.17.04-x64")
392             for UNSUPPORTED_RID in "${UNSUPPORTED_RIDS[@]}"
393             do
394                 if [[ $__HostDistroRid == $UNSUPPORTED_RID ]]; then
395                     __isMSBuildOnNETCoreSupported=0
396                     break
397                 fi
398             done
399         elif [ "$__HostOS" == "OSX" ]; then
400             __isMSBuildOnNETCoreSupported=1
401         fi
402     fi
403 }
404
405
406 build_CoreLib_ni()
407 {
408     local __CrossGenExec=$1
409
410     echo "Generating native image for System.Private.CoreLib.dll"
411     echo "$__CrossGenExec /Platform_Assemblies_Paths $__BinDir/IL $__IbcTuning /out $__BinDir/System.Private.CoreLib.dll $__BinDir/IL/System.Private.CoreLib.dll"
412     $__CrossGenExec /Platform_Assemblies_Paths $__BinDir/IL $__IbcTuning /out $__BinDir/System.Private.CoreLib.dll $__BinDir/IL/System.Private.CoreLib.dll
413     if [ $? -ne 0 ]; then
414         echo "Failed to generate native image for System.Private.CoreLib."
415         exit 1
416     fi
417
418     if [ "$__BuildOS" == "Linux" ]; then
419         echo "Generating symbol file for System.Private.CoreLib.dll"
420         echo "$__CrossGenExec /Platform_Assemblies_Paths $__BinDir /CreatePerfMap $__BinDir $__BinDir/System.Private.CoreLib.dll"
421         $__CrossGenExec /Platform_Assemblies_Paths $__BinDir /CreatePerfMap $__BinDir $__BinDir/System.Private.CoreLib.dll
422         if [ $? -ne 0 ]; then
423             echo "Failed to generate symbol file for System.Private.CoreLib."
424             exit 1
425         fi
426     fi
427 }
428
429 build_CoreLib()
430 {
431     if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
432         echo "System.Private.CoreLib.dll build unsupported."
433         return
434     fi
435
436     if [ $__SkipMSCorLib == 1 ]; then
437        echo "Skipping building System.Private.CoreLib."
438        return
439     fi
440
441     echo "Commencing build of managed components for $__BuildOS.$__BuildArch.$__BuildType"
442
443     # Invoke MSBuild
444     __ExtraBuildArgs=""
445     if [[ "$__IbcTuning" -eq "" ]]; then
446         __ExtraBuildArgs="$__ExtraBuildArgs -OptimizationDataDir=\"$__PackagesDir/optimization.$__BuildOS-$__BuildArch.IBC.CoreCLR/$__IbcOptDataVersion/data/\""
447         __ExtraBuildArgs="$__ExtraBuildArgs -EnableProfileGuidedOptimization=true"
448     fi
449     $__ProjectRoot/run.sh build -Project=$__ProjectDir/build.proj -MsBuildEventLogging="/l:BinClashLogger,Tools/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log" -MsBuildLog="/flp:Verbosity=normal;LogFile=$__LogsDir/System.Private.CoreLib_$__BuildOS__$__BuildArch__$__BuildType.log" -BuildTarget -__IntermediatesDir=$__IntermediatesDir -__RootBinDir=$__RootBinDir -BuildNugetPackage=false -UseSharedCompilation=false $__RunArgs $__ExtraBuildArgs $__UnprocessedBuildArgs
450
451     if [ $? -ne 0 ]; then
452         echo "Failed to build managed components."
453         exit 1
454     fi
455
456     if [ $__SkipCrossgen == 1 ]; then
457         echo "Skipping generating native image"
458         return
459     fi
460
461     # The cross build generates a crossgen with the target architecture.
462     if [ $__CrossBuild == 0 ]; then
463        if [ $__SkipCoreCLR == 1 ]; then
464            return
465        fi
466
467        # The architecture of host pc must be same architecture with target.
468        if [[ ( "$__HostArch" == "$__BuildArch" ) ]]; then
469            build_CoreLib_ni "$__BinDir/crossgen"
470        elif [[ ( "$__HostArch" == "x64" ) && ( "$__BuildArch" == "x86" ) ]]; then
471            build_CoreLib_ni "$__BinDir/crossgen"
472        elif [[ ( "$__HostArch" == "arm64" ) && ( "$__BuildArch" == "arm" ) ]]; then
473            build_CoreLib_ni "$__BinDir/crossgen"
474        else
475            exit 1
476        fi
477     elif [ $__DoCrossArchBuild == 1 ]; then
478        if [[ ( "$__CrossArch" == "x86" ) && ( "$__BuildArch" == "arm" ) ]]; then
479            build_CoreLib_ni "$__CrossComponentBinDir/crossgen"
480        elif [[ ( "$__HostArch" == "x64" ) && ( "$__BuildArch" == "arm64" ) ]]; then
481            build_CoreLib_ni "$__CrossComponentBinDir/crossgen"
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 -MsBuildEventLogging="/l:BinClashLogger,Tools/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log" -MsBuildLog="/flp:Verbosity=normal;LogFile=$__LogsDir/Nuget_$__BuildOS__$__BuildArch__$__BuildType.log" -BuildTarget -__IntermediatesDir=$__IntermediatesDir -__RootBinDir=$__RootBinDir -BuildNugetPackage=false -UseSharedCompilation=false -__DoCrossArchBuild=$__DoCrossArchBuild $__RunArgs $__UnprocessedBuildArgs
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     amd64)
555         __BuildArch=x64
556         __HostArch=x64
557         ;;
558     *)
559         echo "Unknown CPU $CPUName detected, configuring as if for x64"
560         __BuildArch=x64
561         __HostArch=x64
562         ;;
563 esac
564
565 # Use uname to determine what the OS is.
566 OSName=$(uname -s)
567 case $OSName in
568     Linux)
569         __BuildOS=Linux
570         __HostOS=Linux
571         ;;
572
573     Darwin)
574         __BuildOS=OSX
575         __HostOS=OSX
576         ;;
577
578     FreeBSD)
579         __BuildOS=FreeBSD
580         __HostOS=FreeBSD
581         ;;
582
583     OpenBSD)
584         __BuildOS=OpenBSD
585         __HostOS=OpenBSD
586         ;;
587
588     NetBSD)
589         __BuildOS=NetBSD
590         __HostOS=NetBSD
591         ;;
592
593     SunOS)
594         __BuildOS=SunOS
595         __HostOS=SunOS
596         ;;
597
598     *)
599         echo "Unsupported OS $OSName detected, configuring as if for Linux"
600         __BuildOS=Linux
601         __HostOS=Linux
602         ;;
603 esac
604
605 __BuildType=Debug
606 __CodeCoverage=
607 __IncludeTests=Include_Tests
608 __IgnoreWarnings=0
609
610 # Set the various build properties here so that CMake and MSBuild can pick them up
611 __ProjectDir="$__ProjectRoot"
612 __SourceDir="$__ProjectDir/src"
613 __PackagesDir="${DotNetRestorePackagesPath:-${__ProjectDir}/packages}"
614 __RootBinDir="$__ProjectDir/bin"
615 __UnprocessedBuildArgs=
616 __RunArgs=
617 __MSBCleanBuildArgs=
618 __UseNinja=0
619 __VerboseBuild=0
620 __PgoInstrument=0
621 __PgoOptimize=1
622 __IbcTuning=""
623 __ConfigureOnly=0
624 __SkipConfigure=0
625 __SkipRestore=""
626 __SkipNuget=0
627 __SkipCoreCLR=0
628 __SkipMSCorLib=0
629 __SkipRestoreOptData=0
630 __SkipCrossgen=0
631 __CrossBuild=0
632 __ClangMajorVersion=0
633 __ClangMinorVersion=0
634 __NuGetPath="$__PackagesDir/NuGet.exe"
635 __HostDistroRid=""
636 __DistroRid=""
637 __cmakeargs=""
638 __SkipGenerateVersion=0
639 __DoCrossArchBuild=0
640 __PortableBuild=1
641 __msbuildonunsupportedplatform=0
642 __PgoOptDataVersion=""
643 __IbcOptDataVersion=""
644
645 # Get the number of processors available to the scheduler
646 # Other techniques such as `nproc` only get the number of
647 # processors available to a single process.
648 if [ `uname` = "FreeBSD" ]; then
649   __NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
650 elif [ `uname` = "NetBSD" ]; then
651   __NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
652 elif [ `uname` = "Darwin" ]; then
653   __NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
654 else
655   __NumProc=$(nproc --all)
656 fi
657
658 while :; do
659     if [ $# -le 0 ]; then
660         break
661     fi
662
663     lowerI="$(echo $1 | awk '{print tolower($0)}')"
664     case $lowerI in
665         -\?|-h|--help)
666             usage
667             exit 1
668             ;;
669
670         x86|-x86)
671             __BuildArch=x86
672             ;;
673
674         x64|-x64)
675             __BuildArch=x64
676             ;;
677
678         arm|-arm)
679             __BuildArch=arm
680             ;;
681
682         armel|-armel)
683             __BuildArch=armel
684             ;;
685
686         arm64|-arm64)
687             __BuildArch=arm64
688             ;;
689
690         debug|-debug)
691             __BuildType=Debug
692             ;;
693
694         checked|-checked)
695             __BuildType=Checked
696             ;;
697
698         release|-release)
699             __BuildType=Release
700             ;;
701
702         coverage|-coverage)
703             __CodeCoverage=Coverage
704             ;;
705
706         cross|-cross)
707             __CrossBuild=1
708             ;;
709
710         -portablebuild=false)
711             __PortableBuild=0
712             ;;
713
714         verbose|-verbose)
715             __VerboseBuild=1
716             ;;
717
718         stripsymbols|-stripsymbols)
719             __cmakeargs="$__cmakeargs -DSTRIP_SYMBOLS=true"
720             ;;
721
722         clang3.5|-clang3.5)
723             __ClangMajorVersion=3
724             __ClangMinorVersion=5
725             ;;
726
727         clang3.6|-clang3.6)
728             __ClangMajorVersion=3
729             __ClangMinorVersion=6
730             ;;
731
732         clang3.7|-clang3.7)
733             __ClangMajorVersion=3
734             __ClangMinorVersion=7
735             ;;
736
737         clang3.8|-clang3.8)
738             __ClangMajorVersion=3
739             __ClangMinorVersion=8
740             ;;
741
742         clang3.9|-clang3.9)
743             __ClangMajorVersion=3
744             __ClangMinorVersion=9
745             ;;
746
747         clang4.0|-clang4.0)
748             __ClangMajorVersion=4
749             __ClangMinorVersion=0
750             ;;
751
752         clang5.0|-clang5.0)
753             __ClangMajorVersion=5
754             __ClangMinorVersion=0
755             ;;
756
757         clang6.0|-clang6.0)
758             __ClangMajorVersion=6
759             __ClangMinorVersion=0
760             ;;
761
762         ninja|-ninja)
763             __UseNinja=1
764             ;;
765
766         pgoinstrument|-pgoinstrument)
767             __PgoInstrument=1
768             ;;
769
770         nopgooptimize|-nopgooptimize)
771             __PgoOptimize=0
772             __SkipRestoreOptData=1
773             ;;
774
775         ibcinstrument|-ibcinstrument)
776             __IbcTuning="/Tuning"
777             ;;
778
779         configureonly|-configureonly)
780             __ConfigureOnly=1
781             __SkipMSCorLib=1
782             __SkipNuget=1
783             ;;
784
785         skipconfigure|-skipconfigure)
786             __SkipConfigure=1
787             ;;
788
789         skipnative|-skipnative)
790             # Use "skipnative" to use the same option name as build.cmd.
791             __SkipCoreCLR=1
792             ;;
793
794         skipcoreclr|-skipcoreclr)
795             # Accept "skipcoreclr" for backwards-compatibility.
796             __SkipCoreCLR=1
797             ;;
798
799         crosscomponent|-crosscomponent)
800             __DoCrossArchBuild=1
801             ;;
802
803         skipmscorlib|-skipmscorlib)
804             __SkipMSCorLib=1
805             ;;
806
807         skipgenerateversion|-skipgenerateversion)
808             __SkipGenerateVersion=1
809             ;;
810
811         skiprestoreoptdata|-skiprestoreoptdata)
812             __SkipRestoreOptData=1
813             ;;
814
815         skipcrossgen|-skipcrossgen)
816             __SkipCrossgen=1
817             ;;
818
819         includetests|-includetests)
820             ;;
821
822         skiptests|-skiptests)
823             __IncludeTests=
824             ;;
825
826         skipnuget|-skipnuget)
827             __SkipNuget=1
828             ;;
829
830         ignorewarnings|-ignorewarnings)
831             __IgnoreWarnings=1
832             __cmakeargs="$__cmakeargs -DCLR_CMAKE_WARNINGS_ARE_ERRORS=OFF"
833             ;;
834
835         cmakeargs|-cmakeargs)
836             if [ -n "$2" ]; then
837                 __cmakeargs="$__cmakeargs $2"
838                 shift
839             else
840                 echo "ERROR: 'cmakeargs' requires a non-empty option argument"
841                 exit 1
842             fi
843             ;;
844
845         bindir|-bindir)
846             if [ -n "$2" ]; then
847                 __RootBinDir="$2"
848                 if [ ! -d $__RootBinDir ]; then
849                     mkdir $__RootBinDir
850                 fi
851                 __RootBinParent=$(dirname $__RootBinDir)
852                 __RootBinName=${__RootBinDir##*/}
853                 __RootBinDir="$(cd $__RootBinParent &>/dev/null && printf %s/%s $PWD $__RootBinName)"
854                 shift
855             else
856                 echo "ERROR: 'bindir' requires a non-empty option argument"
857                 exit 1
858             fi
859             ;;
860         msbuildonunsupportedplatform|-msbuildonunsupportedplatform)
861             __msbuildonunsupportedplatform=1
862             ;;
863         numproc|-numproc)
864             if [ -n "$2" ]; then
865               __NumProc="$2"
866               shift
867             else
868               echo "ERROR: 'numproc' requires a non-empty option argument"
869               exit 1
870             fi
871             ;;
872         osgroup|-osgroup)
873             if [ -n "$2" ]; then
874               __BuildOS="$2"
875               shift
876             else
877               echo "ERROR: 'osgroup' requires a non-empty option argument"
878               exit 1
879             fi
880             ;;
881
882         *)
883             __UnprocessedBuildArgs="$__UnprocessedBuildArgs $1"
884             ;;
885     esac
886
887     shift
888 done
889
890 __RunArgs="-BuildArch=$__BuildArch -BuildType=$__BuildType -BuildOS=$__BuildOS"
891
892 # Configure environment if we are doing a verbose build
893 if [ $__VerboseBuild == 1 ]; then
894     export VERBOSE=1
895         __RunArgs="$__RunArgs -verbose"
896 fi
897
898 # Set default clang version
899 if [[ $__ClangMajorVersion == 0 && $__ClangMinorVersion == 0 ]]; then
900         if [[ "$__BuildArch" == "arm" || "$__BuildArch" == "armel" ]]; then
901                 __ClangMajorVersion=5
902                 __ClangMinorVersion=0
903         else
904                 __ClangMajorVersion=3
905                 __ClangMinorVersion=9
906         fi
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
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