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