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