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