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