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