3 # resolve python-version to use
4 if [ "$PYTHON" == "" ] ; then
5 if ! PYTHON=$(command -v python || command -v python2 || command -v python 2.7)
7 echo "Unable to locate build-dependency python2.x!" 1>&2
12 # validate python-dependency
13 # useful in case of explicitly set option.
14 if ! command -v $PYTHON > /dev/null
16 echo "Unable to locate build-dependency python2.x ($PYTHON)!" 1>&2
22 echo "Usage: $0 [BuildArch] [BuildType] [verbose] [coverage] [cross] [clangx.y] [ninja] [configureonly] [skipconfigure] [skipnative] [skipmscorlib] [skiptests] [stripsymbols] [cmakeargs] [bindir]"
23 echo "BuildArch can be: x64, x86, arm, armel, arm64"
24 echo "BuildType can be: debug, checked, release"
25 echo "coverage - optional argument to enable code coverage build (currently supported only for Linux and OSX)."
26 echo "ninja - target ninja instead of GNU make"
27 echo "clangx.y - optional argument to build using clang version x.y."
28 echo "cross - optional argument to signify cross compilation,"
29 echo " - will use ROOTFS_DIR environment variable if set."
30 echo "crosscomponent - optional argument to build cross-architecture component,"
31 echo " - will use CAC_ROOTFS_DIR environment variable if set."
32 echo "pgoinstrument - generate instrumented code for profile guided optimization enabled binaries."
33 echo "configureonly - do not perform any builds; just configure the build."
34 echo "skipconfigure - skip build configuration."
35 echo "skipnative - do not build native components."
36 echo "skipmscorlib - do not build mscorlib.dll."
37 echo "skiptests - skip the tests in the 'tests' subdirectory."
38 echo "skipnuget - skip building nuget packages."
39 echo "skiprestoreoptdata - skip restoring optimization data used by profile-based optimizations."
40 echo "portableLinux - build for Portable Linux Distribution"
41 echo "verbose - optional argument to enable verbose build output."
42 echo "-skiprestore: skip restoring packages ^(default: packages are restored during build^)."
43 echo "-disableoss: Disable Open Source Signing for System.Private.CoreLib."
44 echo "-sequential: force a non-parallel build ^(default is to build in parallel"
45 echo " using all processors^)."
46 echo "-officialbuildid=^<ID^>: specify the official build ID to be used by this build."
47 echo "-Rebuild: passes /t:rebuild to the build projects."
48 echo "stripSymbols - Optional argument to strip native symbols during the build."
49 echo "skipgenerateversion - disable version generation even if MSBuild is supported."
50 echo "cmakeargs - user-settable additional arguments passed to CMake."
51 echo "bindir - output directory (defaults to $__ProjectRoot/bin)"
52 echo "buildstandalonegc - builds the GC in a standalone mode. Can't be used with \"cmakeargs\"."
53 echo "msbuildonunsupportedplatform - build managed binaries even if distro is not officially supported."
59 if [ "$__HostOS" == "Linux" ]; then
60 if [ ! -e /etc/os-release ]; then
61 echo "WARNING: Can not determine runtime id for current distro."
64 source /etc/os-release
65 __HostDistroRid="$ID.$VERSION_ID-$__HostArch"
72 if [ $__CrossBuild == 1 ]; then
73 if [ "$__BuildOS" == "Linux" ]; then
74 if [ ! -e $ROOTFS_DIR/etc/os-release ]; then
75 echo "WARNING: Can not determine runtime id for current distro."
78 source $ROOTFS_DIR/etc/os-release
79 export __DistroRid="$ID.$VERSION_ID-$__BuildArch"
83 export __DistroRid="$__HostDistroRid"
86 # Portable builds target the base RID only for Linux based platforms
87 if [ $__PortableLinux == 1 ]; then
88 export __DistroRid="linux-$__BuildArch"
94 echo Setting up directories for build
96 mkdir -p "$__RootBinDir"
99 mkdir -p "$__IntermediatesDir"
101 if [ $__CrossBuild == 1 ]; then
102 mkdir -p "$__CrossComponentBinDir"
103 mkdir -p "$__CrossCompIntermediatesDir"
107 # Check the system to ensure the right prereqs are in place
111 echo "Checking prerequisites..."
113 # Check presence of CMake on the path
114 hash cmake 2>/dev/null || { echo >&2 "Please install cmake before running this script"; exit 1; }
117 hash clang-$__ClangMajorVersion.$__ClangMinorVersion 2>/dev/null || hash clang$__ClangMajorVersion$__ClangMinorVersion 2>/dev/null || hash clang 2>/dev/null || { echo >&2 "Please install clang-$__ClangMajorVersion.$__ClangMinorVersion before running this script"; exit 1; }
123 # if msbuild is not supported, then set __SkipRestoreOptData to 1
124 if [ $__isMSBuildOnNETCoreSupported == 0 ]; then __SkipRestoreOptData=1; fi
125 if [ $__SkipRestoreOptData == 0 ]; then
126 echo "Restoring the OptimizationData package"
127 "$__ProjectRoot/run.sh" sync -optdata
129 echo "Failed to restore the optimization data package."
135 generate_event_logging_sources()
137 if [ $__SkipCoreCLR == 1 ]; then
141 # Event Logging Infrastructure
142 __GeneratedIntermediate="$__IntermediatesDir/Generated"
143 __GeneratedIntermediateEventProvider="$__GeneratedIntermediate/eventprovider_new"
144 if [[ -d "$__GeneratedIntermediateEventProvider" ]]; then
145 rm -rf "$__GeneratedIntermediateEventProvider"
148 if [[ ! -d "$__GeneratedIntermediate/eventprovider" ]]; then
149 mkdir -p "$__GeneratedIntermediate/eventprovider"
152 mkdir -p "$__GeneratedIntermediateEventProvider"
153 if [[ $__SkipCoreCLR == 0 || $__ConfigureOnly == 1 ]]; then
154 echo "Laying out dynamically generated files consumed by the build system "
155 echo "Laying out dynamically generated Event Logging Test files"
156 $PYTHON -B -Wall -Werror "$__ProjectRoot/src/scripts/genXplatEventing.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --exc "$__ProjectRoot/src/vm/ClrEtwAllMeta.lst" --testdir "$__GeneratedIntermediateEventProvider/tests"
158 if [[ $? != 0 ]]; then
162 #determine the logging system
165 echo "Laying out dynamically generated Event Logging Implementation of Lttng"
166 $PYTHON -B -Wall -Werror "$__ProjectRoot/src/scripts/genXplatLttng.py" --man "$__ProjectRoot/src/vm/ClrEtwAll.man" --intermediate "$__GeneratedIntermediateEventProvider"
167 if [[ $? != 0 ]]; then
176 echo "Cleaning the temp folder of dynamically generated Event Logging files"
177 $PYTHON -B -Wall -Werror -c "import sys;sys.path.insert(0,\"$__ProjectRoot/src/scripts\"); from Utilities import *;UpdateDirectory(\"$__GeneratedIntermediate/eventprovider\",\"$__GeneratedIntermediateEventProvider\")"
178 if [[ $? != 0 ]]; then
182 rm -rf "$__GeneratedIntermediateEventProvider"
189 intermediatesForBuild="$3"
190 extraCmakeArguments="$4"
193 if [ $skipCondition == 1 ]; then
194 echo "Skipping $message build."
198 # All set to commence the build
199 echo "Commencing build of $message for $__BuildOS.$__BuildArch.$__BuildType in $intermediatesForBuild"
204 if [ $__UseNinja == 1 ]; then
206 buildFile="build.ninja"
207 if ! buildTool=$(command -v ninja || command -v ninja-build); then
208 echo "Unable to locate ninja!" 1>&2
213 if [ $__SkipConfigure == 0 ]; then
214 # if msbuild is not supported, then set __SkipGenerateVersion to 1
215 if [ $__isMSBuildOnNETCoreSupported == 0 ]; then __SkipGenerateVersion=1; fi
216 # Drop version.cpp file
217 __versionSourceFile="$intermediatesForBuild/version.cpp"
218 if [ $__SkipGenerateVersion == 0 ]; then
220 "$__ProjectRoot/run.sh" build -Project=$__ProjectDir/build.proj -generateHeaderUnix -NativeVersionSourceFile=$__versionSourceFile $__RunArgs $__UnprocessedBuildArgs
222 # Generate the dummy version.cpp, but only if it didn't exist to make sure we don't trigger unnecessary rebuild
223 __versionSourceLine="static char sccsid[] __attribute__((used)) = \"@(#)No version information produced\";"
224 if [ -e $__versionSourceFile ]; then
225 read existingVersionSourceLine < $__versionSourceFile
227 if [ "$__versionSourceLine" != "$existingVersionSourceLine" ]; then
228 echo $__versionSourceLine > $__versionSourceFile
233 pushd "$intermediatesForBuild"
234 # Regenerate the CMake solution
235 echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__ProjectRoot\" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $__IncludeTests $generator $extraCmakeArguments $__cmakeargs"
236 "$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__ProjectRoot" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $__IncludeTests $generator "$extraCmakeArguments" "$__cmakeargs"
240 if [ ! -f "$intermediatesForBuild/$buildFile" ]; then
241 echo "Failed to generate $message build project!"
245 # Get the number of processors available to the scheduler
246 # Other techniques such as `nproc` only get the number of
247 # processors available to a single process.
248 if [ `uname` = "FreeBSD" ]; then
249 NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
250 elif [ `uname` = "NetBSD" ]; then
251 NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
253 NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
257 if [ $__ConfigureOnly == 1 ]; then
258 echo "Finish configuration & skipping $message build."
262 # Check that the makefiles were created.
263 pushd "$intermediatesForBuild"
265 echo "Executing $buildTool install -j $NumProc"
267 $buildTool install -j $NumProc
269 echo "Failed to build $message."
276 build_cross_arch_component()
278 __SkipCrossArchBuild=1
280 # check supported cross-architecture components host(__HostArch)/target(__BuildArch) pair
281 if [[ "$__BuildArch" == "arm" && "$__CrossArch" == "x86" ]]; then
282 export CROSSCOMPILE=0
283 __SkipCrossArchBuild=0
285 # building x64-host/arm-target cross-architecture component need to use cross toolchain of x86
286 if [ "$__HostArch" == "x64" ]; then
287 export CROSSCOMPILE=1
294 export __CMakeBinDir="$__CrossComponentBinDir"
295 export CROSSCOMPONENT=1
298 if [ $CROSSCOMPILE == 1 ]; then
299 TARGET_ROOTFS="$ROOTFS_DIR"
300 if [ -n "$CAC_ROOTFS_DIR" ]; then
301 export ROOTFS_DIR="$CAC_ROOTFS_DIR"
303 export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__CrossArch"
307 __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=$__OptDataVersion"
308 build_native $__SkipCrossArchBuild "$__CrossArch" "$__CrossCompIntermediatesDir" "$__ExtraCmakeArgs" "cross-architecture component"
310 # restore ROOTFS_DIR, CROSSCOMPONENT, and CROSSCOMPILE
311 if [ -n "$TARGET_ROOTFS" ]; then
312 export ROOTFS_DIR="$TARGET_ROOTFS"
314 export CROSSCOMPONENT=
315 export CROSSCOMPILE=1
318 isMSBuildOnNETCoreSupported()
320 # This needs to be updated alongwith corresponding changes to netci.groovy.
321 __isMSBuildOnNETCoreSupported=0
323 if [ "$__HostArch" == "x64" ]; then
324 if [ "$__HostOS" == "Linux" ]; then
325 case "$__HostDistroRid" in
327 __isMSBuildOnNETCoreSupported=1
330 __isMSBuildOnNETCoreSupported=1
333 __isMSBuildOnNETCoreSupported=1
336 __isMSBuildOnNETCoreSupported=1
339 __isMSBuildOnNETCoreSupported=1
342 __isMSBuildOnNETCoreSupported=1
345 __isMSBuildOnNETCoreSupported=1
348 __isMSBuildOnNETCoreSupported=1
351 __isMSBuildOnNETCoreSupported=1
354 __isMSBuildOnNETCoreSupported=1
357 __isMSBuildOnNETCoreSupported=$__msbuildonunsupportedplatform
359 elif [ "$__HostOS" == "OSX" ]; then
360 __isMSBuildOnNETCoreSupported=1
367 if [ $__SkipCoreCLR == 0 -a -e $__BinDir/crossgen ]; then
368 echo "Generating native image for System.Private.CoreLib."
369 $__BinDir/crossgen $__BinDir/System.Private.CoreLib.dll
370 if [ $? -ne 0 ]; then
371 echo "Failed to generate native image for System.Private.CoreLib."
375 echo "Generating native image for MScorlib Facade."
376 $__BinDir/crossgen $__BinDir/mscorlib.dll
377 if [ $? -ne 0 ]; then
378 echo "Failed to generate native image for mscorlib facade."
382 if [ "$__BuildOS" == "Linux" ]; then
383 echo "Generating symbol file for System.Private.CoreLib."
384 $__BinDir/crossgen /CreatePerfMap $__BinDir $__BinDir/System.Private.CoreLib.ni.dll
385 if [ $? -ne 0 ]; then
386 echo "Failed to generate symbol file for System.Private.CoreLib."
396 if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
397 echo "System.Private.CoreLib.dll build unsupported."
401 if [ $__SkipMSCorLib == 1 ]; then
402 echo "Skipping building System.Private.CoreLib."
406 echo "Commencing build of managed components for $__BuildOS.$__BuildArch.$__BuildType"
409 $__ProjectRoot/run.sh build -Project=$__ProjectDir/build.proj -MsBuildLog="/flp:Verbosity=normal;LogFile=$__LogsDir/System.Private.CoreLib_$__BuildOS__$__BuildArch__$__BuildType.log" -BuildTarget -__IntermediatesDir=$__IntermediatesDir -__RootBinDir=$__RootBinDir -BuildNugetPackage=false -UseSharedCompilation=false $__RunArgs $__UnprocessedBuildArgs
411 if [ $? -ne 0 ]; then
412 echo "Failed to build managed components."
416 # The cross build generates a crossgen with the target architecture.
417 if [ $__CrossBuild != 1 ]; then
418 # The architecture of host pc must be same architecture with target.
419 if [[ ( "$__HostArch" == "$__BuildArch" ) ]]; then
421 elif [[ ( "$__HostArch" == "x64" ) && ( "$__BuildArch" == "x86" ) ]]; then
423 elif [[ ( "$__HostArch" == "arm64" ) && ( "$__BuildArch" == "arm" ) ]]; then
431 generate_NugetPackages()
433 # We can only generate nuget package if we also support building mscorlib as part of this build.
434 if [ $__isMSBuildOnNETCoreSupported == 0 ]; then
435 echo "Nuget package generation unsupported."
439 # Since we can build mscorlib for this OS, did we build the native components as well?
440 if [ $__SkipCoreCLR == 1 ]; then
441 echo "Unable to generate nuget packages since native components were not built."
445 echo "Generating nuget packages for "$__BuildOS
448 $__ProjectRoot/run.sh build -Project=$__SourceDir/.nuget/packages.builds -MsBuildLog="/flp:Verbosity=normal;LogFile=$__LogsDir/Nuget_$__BuildOS__$__BuildArch__$__BuildType.log" -BuildTarget -__IntermediatesDir=$__IntermediatesDir -__RootBinDir=$__RootBinDir -BuildNugetPackage=false -UseSharedCompilation=false $__RunArgs $__UnprocessedBuildArgs
450 if [ $? -ne 0 ]; then
451 echo "Failed to generate Nuget packages."
456 echo "Commencing CoreCLR Repo build"
458 # Argument types supported by this script:
460 # Build architecture - valid values are: x64, ARM.
461 # Build Type - valid values are: Debug, Checked, Release
463 # Set the default arguments for build
465 # Obtain the location of the bash script to figure out where the root of the repo is.
466 __ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
468 # Use uname to determine what the CPU is.
470 # Some Linux platforms report unknown for platform, but the arch for machine.
471 if [ "$CPUName" == "unknown" ]; then
477 echo "Unsupported CPU $CPUName detected, build might not succeed!"
488 echo "Unsupported CPU $CPUName detected, build might not succeed!"
499 echo "Unknown CPU $CPUName detected, configuring as if for x64"
505 # Use uname to determine what the OS is.
539 echo "Unsupported OS $OSName detected, configuring as if for Linux"
547 __IncludeTests=Include_Tests
549 # Set the various build properties here so that CMake and MSBuild can pick them up
550 __ProjectDir="$__ProjectRoot"
551 __SourceDir="$__ProjectDir/src"
552 __PackagesDir="$__ProjectDir/packages"
553 __RootBinDir="$__ProjectDir/bin"
554 __UnprocessedBuildArgs=
566 __SkipRestoreOptData=0
568 __ClangMajorVersion=0
569 __ClangMinorVersion=0
570 __NuGetPath="$__PackagesDir/NuGet.exe"
574 __SkipGenerateVersion=0
577 __msbuildonunsupportedplatform=0
581 if [ $# -le 0 ]; then
585 lowerI="$(echo $1 | awk '{print tolower($0)}')"
625 __CodeCoverage=Coverage
633 if [ "$__BuildOS" == "Linux" ]; then
636 echo "ERROR: portableLinux not supported for non-Linux platforms."
646 __cmakeargs="$__cmakeargs -DSTRIP_SYMBOLS=true"
650 __ClangMajorVersion=3
651 __ClangMinorVersion=5
655 __ClangMajorVersion=3
656 __ClangMinorVersion=6
660 __ClangMajorVersion=3
661 __ClangMinorVersion=7
665 __ClangMajorVersion=3
666 __ClangMinorVersion=8
670 __ClangMajorVersion=3
671 __ClangMinorVersion=9
693 # Use "skipnative" to use the same option name as build.cmd.
698 # Accept "skipcoreclr" for backwards-compatibility.
711 __SkipGenerateVersion=1
715 __SkipRestoreOptData=1
734 echo "ERROR: 'cmakeargs' requires a non-empty option argument"
742 if [ ! -d $__RootBinDir ]; then
745 __RootBinParent=$(dirname $__RootBinDir)
746 __RootBinName=${__RootBinDir##*/}
747 __RootBinDir="$(cd $__RootBinParent &>/dev/null && printf %s/%s $PWD $__RootBinName)"
750 echo "ERROR: 'bindir' requires a non-empty option argument"
755 __cmakeargs="-DFEATURE_STANDALONE_GC=1"
757 msbuildonunsupportedplatform)
758 __msbuildonunsupportedplatform=1
761 __UnprocessedBuildArgs="$__UnprocessedBuildArgs $1"
768 __RunArgs="-BuildArch=$__BuildArch -BuildType=$__BuildType -BuildOS=$__BuildOS"
770 # Configure environment if we are doing a verbose build
771 if [ $__VerboseBuild == 1 ]; then
773 __RunArgs="$__RunArgs -verbose"
776 # Set default clang version
777 if [[ $__ClangMajorVersion == 0 && $__ClangMinorVersion == 0 ]]; then
778 if [ $__CrossBuild == 1 ]; then
779 __ClangMajorVersion=3
780 __ClangMinorVersion=6
782 __ClangMajorVersion=3
783 __ClangMinorVersion=5
787 # Set dependent variables
788 __LogsDir="$__RootBinDir/Logs"
790 # init the host distro name
793 # Set the remaining variables based upon the determined build configuration
794 __BinDir="$__RootBinDir/Product/$__BuildOS.$__BuildArch.$__BuildType"
795 __PackagesBinDir="$__BinDir/.nuget"
796 __ToolsDir="$__RootBinDir/tools"
797 __TestWorkingDir="$__RootBinDir/tests/$__BuildOS.$__BuildArch.$__BuildType"
798 export __IntermediatesDir="$__RootBinDir/obj/$__BuildOS.$__BuildArch.$__BuildType"
799 __TestIntermediatesDir="$__RootBinDir/tests/obj/$__BuildOS.$__BuildArch.$__BuildType"
800 __isMSBuildOnNETCoreSupported=0
801 __CrossComponentBinDir="$__BinDir"
802 __CrossCompIntermediatesDir="$__IntermediatesDir/crossgen"
804 __CrossArch="$__HostArch"
805 if [[ "$__HostArch" == "x64" && "$__BuildArch" == "arm" ]]; then
808 if [ $__CrossBuild == 1 ]; then
809 __CrossComponentBinDir="$__CrossComponentBinDir/$__CrossArch"
811 __CrossgenCoreLibLog="$__LogsDir/CrossgenCoreLib_$__BuildOS.$BuildArch.$__BuildType.log"
812 __CrossgenExe="$__CrossComponentBinDir/crossgen"
814 # Init if MSBuild for .NET Core is supported for this platform
815 isMSBuildOnNETCoreSupported
817 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to set.
818 # This is needed by CLI to function.
819 if [ -z "$HOME" ]; then
820 if [ ! -d "$__ProjectDir/temp_home" ]; then
823 export HOME=$__ProjectDir/temp_home
824 echo "HOME not defined; setting it to $HOME"
827 # Specify path to be set for CMAKE_INSTALL_PREFIX.
828 # This is where all built CoreClr libraries will copied to.
829 export __CMakeBinDir="$__BinDir"
831 # Configure environment if we are doing a cross compile.
832 if [ $__CrossBuild == 1 ]; then
833 export CROSSCOMPILE=1
834 if ! [[ -n "$ROOTFS_DIR" ]]; then
835 export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__BuildArch"
839 # Parse the optdata package version from its project.json file
840 optDataProjectJsonPath="$__ProjectRoot/src/.nuget/optdata/project.json"
841 if [ -f $optDataProjectJsonPath ]; then
842 __OptDataVersion=$("$__ProjectRoot/extract-from-json.py" -rf $optDataProjectJsonPath dependencies optimization.PGO.CoreCLR)
845 # init the target distro name
848 # Make the directories necessary for build if they don't exist
854 # Restore the package containing profile counts for profile-guided optimizations
857 # Generate event logging infrastructure sources
858 generate_event_logging_sources
860 # Build the coreclr (native) components.
861 __ExtraCmakeArgs="-DCLR_CMAKE_TARGET_OS=$__BuildOS -DCLR_CMAKE_PACKAGES_DIR=$__PackagesDir -DCLR_CMAKE_PGO_INSTRUMENT=$__PgoInstrument -DCLR_CMAKE_OPTDATA_VERSION=$__OptDataVersion"
862 build_native $__SkipCoreCLR "$__BuildArch" "$__IntermediatesDir" "$__ExtraCmakeArgs" "CoreCLR component"
864 # Build cross-architecture components
865 if [[ $__CrossBuild == 1 && $__DoCrossArchBuild == 1 ]]; then
866 build_cross_arch_component
869 # Build System.Private.CoreLib.
873 # Generate nuget packages
874 if [ $__SkipNuget != 1 ]; then
875 generate_NugetPackages
881 echo "Repo successfully built."
882 echo "Product binaries are available at $__BinDir"