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