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