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