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