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