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