Merge pull request #19053 from riverar/riverar/r2r_size_unavail
[platform/upstream/coreclr.git] / build-test.sh
1 #!/usr/bin/env bash
2
3 initHostDistroRid()
4 {
5     __HostDistroRid=""
6
7     # Some OS groups should default to use the portable packages
8     if [ "$__BuildOS" == "OSX" ]; then
9         __PortableBuild=1
10     fi
11
12     if [ "$__HostOS" == "Linux" ]; then
13         if [ -e /etc/redhat-release ]; then
14             local redhatRelease=$(</etc/redhat-release)
15             if [[ $redhatRelease == "CentOS release 6."* || $redhatRelease == "Red Hat Enterprise Linux Server release 6."* ]]; then
16                 __HostDistroRid="rhel.6-$__HostArch"
17             else
18                 __PortableBuild=1
19             fi
20         elif [ -e /etc/os-release ]; then
21             source /etc/os-release
22             if [[ $ID == "alpine" ]]; then
23                 __HostDistroRid="linux-musl-$__HostArch"
24             else
25                 __PortableBuild=1
26                 __HostDistroRid="$ID.$VERSION_ID-$__HostArch"
27             fi
28         fi
29     elif [ "$__HostOS" == "FreeBSD" ]; then
30         __freebsd_version=`sysctl -n kern.osrelease | cut -f1 -d'.'`
31         __HostDistroRid="freebsd.$__freebsd_version-$__HostArch"
32     fi
33
34     # Portable builds target the base RID
35     if [ "$__PortableBuild" == 1 ]; then
36         if [ "$__BuildOS" == "OSX" ]; then
37             export __HostDistroRid="osx-$__BuildArch"
38         elif [ "$__BuildOS" == "Linux" ]; then
39             export __HostDistroRid="linux-$__BuildArch"
40         fi
41     fi
42
43     if [ "$__HostDistroRid" == "" ]; then
44         echo "WARNING: Cannot determine runtime id for current distro."
45     fi
46
47     echo "Setting __HostDistroRid to $__HostDistroRid"
48 }
49
50 initTargetDistroRid()
51 {
52     if [ $__CrossBuild == 1 ]; then
53         if [ "$__BuildOS" == "Linux" ]; then
54             if [ ! -e $ROOTFS_DIR/etc/os-release ]; then
55                 if [ -e $ROOTFS_DIR/android_platform ]; then
56                     source $ROOTFS_DIR/android_platform
57                     export __DistroRid="$RID"
58                 else
59                     echo "WARNING: Cannot determine runtime id for current distro."
60                     export __DistroRid=""
61                 fi
62             else
63                 source $ROOTFS_DIR/etc/os-release
64                 export __DistroRid="$ID.$VERSION_ID-$__BuildArch"
65             fi
66         fi
67     else
68         export __DistroRid="$__HostDistroRid"
69     fi
70
71     if [ "$ID.$VERSION_ID" == "ubuntu.16.04" ]; then
72      export __DistroRid="ubuntu.14.04-$__BuildArch"
73     fi
74
75     # Portable builds target the base RID
76     if [ "$__PortableBuild" == 1 ]; then
77         if [ "$__BuildOS" == "Linux" ]; then
78             export __DistroRid="linux-$__BuildArch"
79             export __RuntimeId="linux-$__BuildArch"
80         elif [ "$__BuildOS" == "OSX" ]; then
81             export __DistroRid="osx-$__BuildArch"
82             export __RuntimeId="osx-$__BuildArch"
83         fi
84     fi
85
86     echo "__DistroRid: " $__DistroRid
87 }
88
89 isMSBuildOnNETCoreSupported()
90 {
91     __isMSBuildOnNETCoreSupported=$__msbuildonunsupportedplatform
92
93     if [ $__isMSBuildOnNETCoreSupported == 1 ]; then
94         return
95     fi
96
97     if [ "$__HostArch" == "x64" ]; then
98         if [ "$__HostOS" == "Linux" ]; then
99             __isMSBuildOnNETCoreSupported=1
100             UNSUPPORTED_RIDS=("debian.9-x64" "ubuntu.17.04-x64")
101             for UNSUPPORTED_RID in "${UNSUPPORTED_RIDS[@]}"
102             do
103                 if [ "$__HostDistroRid" == "$UNSUPPORTED_RID" ]; then
104                     __isMSBuildOnNETCoreSupported=0
105                     break
106                 fi
107             done
108         elif [ "$__HostOS" == "OSX" ]; then
109             __isMSBuildOnNETCoreSupported=1
110         fi
111     fi
112 }
113
114 generate_layout()
115 {
116     __TestDir=$__ProjectDir/tests
117     __ProjectFilesDir=$__TestDir
118     __TestBinDir=$__TestWorkingDir
119
120     if [ $__RebuildTests -ne 0 ]; then
121         if [ -d "${__TestBinDir}" ]; then
122             echo "Removing tests build dir: ${__TestBinDir}"
123             rm -rf $__TestBinDir
124         fi
125     fi
126
127     __CMakeBinDir="${__TestBinDir}"
128
129     if [ -z "$__TestIntermediateDir" ]; then
130         __TestIntermediateDir="tests/obj/${__BuildOS}.${__BuildArch}.${__BuildType}"
131     fi
132
133     echo "__BuildOS: ${__BuildOS}"
134     echo "__BuildArch: ${__BuildArch}"
135     echo "__BuildType: ${__BuildType}"
136     echo "__TestIntermediateDir: ${__TestIntermediateDir}"
137
138     if [ ! -f "$__TestBinDir" ]; then
139         echo "Creating TestBinDir: ${__TestBinDir}"
140         mkdir -p $__TestBinDir
141     fi
142     if [ ! -f "$__LogsDir" ]; then
143         echo "Creating LogsDir: ${__LogsDir}"
144         mkdir -p $__LogsDir
145     fi
146
147     __BuildProperties="-p:OSGroup=${__BuildOS} -p:BuildOS=${__BuildOS} -p:BuildArch=${__BuildArch} -p:BuildType=${__BuildType}"
148
149     # =========================================================================================
150     # ===
151     # === Restore product binaries from packages
152     # ===
153     # =========================================================================================
154
155     build_MSBuild_projects "Restore_Packages" "${__ProjectDir}/tests/build.proj" "Restore product binaries (build tests)" "-BatchRestorePackages"
156
157     if [ -n "$__UpdateInvalidPackagesArg" ]; then
158         __up=-updateinvalidpackageversion
159     fi
160
161     echo "${__MsgPrefix}Creating test overlay..."
162
163     if [ -z "$xUnitTestBinBase" ]; then
164       xUnitTestBinBase=$__TestWorkingDir
165     fi
166
167     export CORE_ROOT=$xUnitTestBinBase/Tests/Core_Root
168
169     if [ -d "${CORE_ROOT}" ]; then
170       rm -rf $CORE_ROOT
171     fi
172
173     mkdir -p $CORE_ROOT
174
175     build_MSBuild_projects "Tests_Overlay_Managed" "${__ProjectDir}/tests/runtest.proj" "Creating test overlay" "-testOverlay"
176
177     chmod +x $__BinDir/corerun
178     chmod +x $__BinDir/crossgen
179
180     # Make sure to copy over the pulled down packages
181     cp -r $__BinDir/* $CORE_ROOT/ > /dev/null
182
183 }
184
185 generate_testhost()
186 {
187     export TEST_HOST=$xUnitTestBinBase/testhost
188
189     if [ -d "${TEST_HOST}" ]; then
190         rm -rf $TEST_HOST
191     fi
192
193     echo "${__MsgPrefix}Creating test overlay..."    
194     mkdir -p $TEST_HOST
195
196     build_MSBuild_projects "Tests_Generate_TestHost" "${__ProjectDir}/tests/runtest.proj" "Creating test host" "-testHost"
197 }
198
199
200 build_Tests()
201 {
202     __TestDir=$__ProjectDir/tests
203     __ProjectFilesDir=$__TestDir
204     __TestBinDir=$__TestWorkingDir
205
206     if [ $__RebuildTests -ne 0 ]; then
207         if [ -d "${__TestBinDir}" ]; then
208             echo "Removing tests build dir: ${__TestBinDir}"
209             rm -rf $__TestBinDir
210         fi
211     fi
212
213     export __CMakeBinDir="${__TestBinDir}"
214     if [ ! -d "${__TestIntermediatesDir}" ]; then
215         mkdir -p ${__TestIntermediatesDir}
216     fi
217
218     __NativeTestIntermediatesDir="${__TestIntermediatesDir}/Native"
219     if [  ! -d "${__NativeTestIntermediatesDir}" ]; then
220         mkdir -p ${__NativeTestIntermediatesDir}
221     fi
222
223     __ManagedTestIntermediatesDir="${__TestIntermediatesDir}/Managed"
224     if [ ! -d "${__ManagedTestIntermediatesDir}" ]; then
225         mkdir -p ${__ManagedTestIntermediatesDir}
226     fi
227
228     echo "__BuildOS: ${__BuildOS}"
229     echo "__BuildArch: ${__BuildArch}"
230     echo "__BuildType: ${__BuildType}"
231     echo "__TestIntermediatesDir: ${__TestIntermediatesDir}"
232     echo "__NativeTestIntermediatesDir: ${__NativeTestIntermediatesDir}"
233     echo "__ManagedTestIntermediatesDir: ${__ManagedTestIntermediatesDir}"
234
235     if [ ! -f "$__TestBinDir" ]; then
236         echo "Creating TestBinDir: ${__TestBinDir}"
237         mkdir -p $__TestBinDir
238     fi
239     if [ ! -f "$__LogsDir" ]; then
240         echo "Creating LogsDir: ${__LogsDir}"
241         mkdir -p $__LogsDir
242     fi
243
244     __BuildProperties="-p:OSGroup=${__BuildOS} -p:BuildOS=${__BuildOS} -p:BuildArch=${__BuildArch} -p:BuildType=${__BuildType}"
245
246     # =========================================================================================
247     # ===
248     # === Restore product binaries from packages
249     # ===
250     # =========================================================================================
251
252     if [ ${__SkipRestorePackages} != 1 ]; then
253         build_MSBuild_projects "Restore_Product" "${__ProjectDir}/tests/build.proj" "Restore product binaries (build tests)" "-BatchRestorePackages"
254     fi
255
256     if [ -n "$__BuildAgainstPackagesArg" ]; then
257         build_MSBuild_projects "Tests_GenerateRuntimeLayout" "${__ProjectDir}/tests/runtest.proj" "Restore product binaries (run tests)" "-BinPlaceRef" "-BinPlaceProduct" "-CopyCrossgenToProduct"
258     fi
259
260     if [ $__SkipNative != 1 ]; then
261         build_native_projects "$__BuildArch" "${__NativeTestIntermediatesDir}"
262
263         if [ $? -ne 0 ]; then
264             echo "${__MsgPrefix}Error: build failed. Refer to the build log files for details (above)"
265             exit 1
266         fi
267     fi
268
269     echo "Starting the Managed Tests Build..."
270
271     build_MSBuild_projects "Tests_Managed" "$__ProjectDir/tests/build.proj" "Managed tests build (build tests)" "$__up"
272
273     if [ $? -ne 0 ]; then
274         echo "${__MsgPrefix}Error: build failed. Refer to the build log files for details (above)"
275         exit 1
276     else
277         echo "Checking the Managed Tests Build..."
278
279         build_MSBuild_projects "Check_Test_Build" "${__ProjectDir}/tests/runtest.proj" "Check Test Build" "-ExtraParameters:/t:CheckTestBuild"
280
281         if [ $? -ne 0 ]; then
282             echo "${__MsgPrefix}Error: Check Test Build failed."
283             exit 1
284         fi
285
286         echo "Managed tests build success!"
287     fi
288
289     if [ $__BuildTestWrappers -ne -0 ]; then
290         echo "${__MsgPrefix}Creating test wrappers..."
291
292         # Always create the test wrappers and set the exclude file.
293         export __Exclude="$__TestDir/issues.targets"
294         echo "Exclude set to $__TestDir/issues.targets"
295         build_Tests_internal "Tests_XunitWrapper" "$__ProjectDir/tests/runtest.proj" "Test Xunit Wrapper" "-BuildWrappers" "-MsBuildEventLogging= " "-TargetsWindows=false"
296
297         if [ $? -ne 0 ]; then
298             echo "${__MsgPrefix}Error: build failed. Refer to the build log files for details (above)"
299             exit 1
300         else
301             echo "XUnit Wrappers have been built."
302             echo "Create marker \"${__XUnitWrapperBuiltMarker}\""
303             touch $__XUnitWrapperBuiltMarker
304         fi
305     fi
306
307     if [ -n "$__UpdateInvalidPackagesArg" ]; then
308         __up=-updateinvalidpackageversion
309     fi
310
311     echo "${__MsgPrefix}Creating test overlay..."
312
313     generate_layout
314
315     if [ $__ZipTests -ne 0 ]; then
316         echo "${__MsgPrefix}ZIP tests packages..."
317         build_MSBuild_projects "Helix_Prep" "$__ProjectDir/tests/helixprep.proj" "Prep test binaries for Helix publishing" " "
318     fi
319 }
320
321 build_MSBuild_projects()
322 {
323     subDirectoryName=$1
324     shift
325     projectName=$1
326     shift
327     stepName="$1"
328     shift
329     extraBuildParameters=("$@")
330
331     # Set up directories and file names
332     __BuildLogRootName=$subDirectoryName
333     __BuildLog="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.log"
334     __BuildWrn="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.wrn"
335     __BuildErr="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.err"
336
337     # Use binclashlogger by default if no other logger is specified
338     if [[ "${extraBuildParameters[*]}" == *"-MsBuildEventLogging"* ]]; then
339         msbuildEventLogging=""
340     else
341         msbuildEventLogging="-MsBuildEventLogging=\"/l:BinClashLogger,Tools/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log\""
342     fi
343
344     if [[ "$subDirectoryName" == "Tests_Managed" ]]; then
345         # Execute msbuild managed test build in stages - workaround for excessive data retention in MSBuild ConfigCache
346         # See https://github.com/Microsoft/msbuild/issues/2993
347
348         # __SkipPackageRestore and __SkipTargetingPackBuild used  to control build by tests/src/dirs.proj
349         export __SkipPackageRestore=false
350         export __SkipTargetingPackBuild=false
351         export __BuildLoopCount=2
352         export __TestGroupToBuild=1
353         __AppendToLog=false
354
355         if [ -n "$__priority1" ]; then
356             export __BuildLoopCount=16
357             export __TestGroupToBuild=2
358         fi
359
360         for (( slice=1 ; slice <= __BuildLoopCount; slice = slice + 1 ))
361         do
362             __msbuildLog="\"/flp:Verbosity=normal;LogFile=${__BuildLog};Append=${__AppendToLog}\""
363             __msbuildWrn="\"/flp1:WarningsOnly;LogFile=${__BuildWrn};Append=${__AppendToLog}\""
364             __msbuildErr="\"/flp2:ErrorsOnly;LogFile=${__BuildErr};Append=${__AppendToLog}\""
365
366             export TestBuildSlice=$slice
367
368             # Generate build command
369             buildArgs=("-Project=$projectName" "-MsBuildLog=${__msbuildLog}" "-MsBuildWrn=${__msbuildWrn}" "-MsBuildErr=${__msbuildErr}")
370             buildArgs+=("$msbuildEventLogging")
371             buildArgs+=("${extraBuildParameters[@]}")
372             buildArgs+=("${__RunArgs[@]}")
373             buildArgs+=("${__UnprocessedBuildArgs[@]}")
374
375             echo "Building step '$stepName' slice=$slice via $buildCommand"
376
377             # Invoke MSBuild
378             "$__ProjectRoot/run.sh" build "${buildArgs[@]}"
379
380             # Make sure everything is OK
381             if [ $? -ne 0 ]; then
382                 echo "${__MsgPrefix}Failed to build $stepName. See the build logs:"
383                 echo "    $__BuildLog"
384                 echo "    $__BuildWrn"
385                 echo "    $__BuildErr"
386                 exit 1
387             fi
388             export __SkipPackageRestore=true
389             export __SkipTargetingPackBuild=true
390             __AppendToLog=true
391         done
392     else
393         __msbuildLog="\"/flp:Verbosity=normal;LogFile=${__BuildLog}\""
394         __msbuildWrn="\"/flp1:WarningsOnly;LogFile=${__BuildWrn}\""
395         __msbuildErr="\"/flp2:ErrorsOnly;LogFile=${__BuildErr}\""
396
397         # Generate build command
398         buildArgs=("-Project=$projectName" "-MsBuildLog=${__msbuildLog}" "-MsBuildWrn=${__msbuildWrn}" "-MsBuildErr=${__msbuildErr}")
399         buildArgs+=("$msbuildEventLogging")
400         buildArgs+=("${extraBuildParameters[@]}")
401         buildArgs+=("${__RunArgs[@]}")
402         buildArgs+=("${__UnprocessedBuildArgs[@]}")
403
404         echo "Building step '$stepName' via $buildCommand"
405
406         # Invoke MSBuild
407         "$__ProjectRoot/run.sh" build "${buildArgs[@]}"
408
409         # Make sure everything is OK
410         if [ $? -ne 0 ]; then
411             echo "${__MsgPrefix}Failed to build $stepName. See the build logs:"
412             echo "    $__BuildLog"
413             echo "    $__BuildWrn"
414             echo "    $__BuildErr"
415             exit 1
416         fi
417     fi
418 }
419
420 build_native_projects()
421 {
422     platformArch="$1"
423     intermediatesForBuild="$2"
424
425     extraCmakeArguments="-DCLR_CMAKE_TARGET_OS=${__BuildOS} -DCLR_CMAKE_HOST_ARCH=${platformArch}"
426     message="native tests assets"
427
428     # All set to commence the build
429     echo "Commencing build of $message for $__BuildOS.$__BuildArch.$__BuildType in $intermediatesForBuild"
430
431     generator=""
432     buildFile="Makefile"
433     buildTool="make"
434     if [ $__UseNinja == 1 ]; then
435         generator="ninja"
436         buildFile="build.ninja"
437         if ! buildTool=$(command -v ninja || command -v ninja-build); then
438            echo "Unable to locate ninja!" 1>&2
439            exit 1
440         fi
441     fi
442
443     if [ $__SkipConfigure == 0 ]; then
444         # if msbuild is not supported, then set __SkipGenerateVersion to 1
445         if [ $__isMSBuildOnNETCoreSupported == 0 ]; then __SkipGenerateVersion=1; fi
446         # Drop version.cpp file
447         __versionSourceFile="$intermediatesForBuild/version.cpp"
448         if [ $__SkipGenerateVersion == 0 ]; then
449             pwd
450             "$__ProjectRoot/run.sh" build -Project=$__ProjectDir/build.proj -generateHeaderUnix -NativeVersionSourceFile=$__versionSourceFile -MsBuildEventLogging="/l:BinClashLogger,Tools/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log"  $__RunArgs $__UnprocessedBuildArgs
451         else
452             # Generate the dummy version.cpp, but only if it didn't exist to make sure we don't trigger unnecessary rebuild
453             __versionSourceLine="static char sccsid[] __attribute__((used)) = \"@(#)No version information produced\";"
454             if [ -e $__versionSourceFile ]; then
455                 read existingVersionSourceLine < $__versionSourceFile
456             fi
457             if [ "$__versionSourceLine" != "$existingVersionSourceLine" ]; then
458                 echo $__versionSourceLine > $__versionSourceFile
459             fi
460         fi
461
462         pushd "$intermediatesForBuild"
463         # Regenerate the CMake solution
464         echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__TestDir\" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $generator $extraCmakeArguments $__cmakeargs"
465         "$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__TestDir" $__ClangMajorVersion $__ClangMinorVersion $platformArch $__BuildType $__CodeCoverage $generator "$extraCmakeArguments" "$__cmakeargs"
466         popd
467     fi
468
469     if [ ! -f "$intermediatesForBuild/$buildFile" ]; then
470         echo "Failed to generate $message build project!"
471         exit 1
472     fi
473
474     # Build
475     if [ $__ConfigureOnly == 1 ]; then
476         echo "Finish configuration & skipping $message build."
477         return
478     fi
479
480     pushd "$intermediatesForBuild"
481
482     echo "Executing $buildTool install -j $__NumProc"
483
484     $buildTool install -j $__NumProc
485     if [ $? != 0 ]; then
486         echo "Failed to build $message."
487         exit 1
488     fi
489
490     popd
491     echo "Native tests build success!"
492 }
493
494 usage()
495 {
496     echo "Usage: $0 [BuildArch] [BuildType] [verbose] [coverage] [cross] [clangx.y] [ninja] [runtests] [bindir]"
497     echo "BuildArch can be: x64, x86, arm, armel, arm64"
498     echo "BuildType can be: debug, checked, release"
499     echo "coverage - optional argument to enable code coverage build (currently supported only for Linux and OSX)."
500     echo "ninja - target ninja instead of GNU make"
501     echo "clangx.y - optional argument to build using clang version x.y - supported version 3.5 - 6.0"
502     echo "cross - optional argument to signify cross compilation,"
503     echo "      - will use ROOTFS_DIR environment variable if set."
504     echo "crosscomponent - optional argument to build cross-architecture component,"
505     echo "               - will use CAC_ROOTFS_DIR environment variable if set."
506     echo "portableLinux - build for Portable Linux Distribution"
507     echo "portablebuild - Use portable build."
508     echo "verbose - optional argument to enable verbose build output."
509     echo "rebuild - if tests have already been built - rebuild them"
510     echo "skipnative: skip the native tests build"
511     echo "generatelayoutonly - only pull down dependencies and build coreroot"
512     echo "generatetesthostonly - only pull down dependencies and build coreroot and the CoreFX testhost"
513     echo "buildagainstpackages - pull down and build using packages."
514     echo "skiprestorepackages - skip package restore"
515     echo "runtests - run tests after building them"
516     echo "ziptests - zips CoreCLR tests & Core_Root for a Helix run"
517     echo "bindir - output directory (defaults to $__ProjectRoot/bin)"
518     echo "msbuildonunsupportedplatform - build managed binaries even if distro is not officially supported."
519     echo "priority1 - include priority=1 tests in the build"
520     exit 1
521 }
522
523
524 # Obtain the location of the bash script to figure out where the root of the repo is.
525 __ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
526
527 # Use uname to determine what the CPU is.
528 CPUName=$(uname -p)
529
530 # Some Linux platforms report unknown for platform, but the arch for machine.
531 if [ "$CPUName" == "unknown" ]; then
532     CPUName=$(uname -m)
533 fi
534
535 case $CPUName in
536     i686)
537         echo "Unsupported CPU $CPUName detected, build might not succeed!"
538         __BuildArch=x86
539         __HostArch=x86
540         ;;
541
542     x86_64)
543         __BuildArch=x64
544         __HostArch=x64
545         ;;
546
547     armv7l)
548         echo "Unsupported CPU $CPUName detected, build might not succeed!"
549         __BuildArch=arm
550         __HostArch=arm
551         ;;
552
553     aarch64)
554         __BuildArch=arm64
555         __HostArch=arm64
556         ;;
557
558     *)
559         echo "Unknown CPU $CPUName detected, configuring as if for x64"
560         __BuildArch=x64
561         __HostArch=x64
562         ;;
563 esac
564
565 # Use uname to determine what the OS is.
566 OSName=$(uname -s)
567 case $OSName in
568     Linux)
569         __BuildOS=Linux
570         __HostOS=Linux
571         ;;
572
573     Darwin)
574         __BuildOS=OSX
575         __HostOS=OSX
576         ;;
577
578     FreeBSD)
579         __BuildOS=FreeBSD
580         __HostOS=FreeBSD
581         ;;
582
583     OpenBSD)
584         __BuildOS=OpenBSD
585         __HostOS=OpenBSD
586         ;;
587
588     NetBSD)
589         __BuildOS=NetBSD
590         __HostOS=NetBSD
591         ;;
592
593     SunOS)
594         __BuildOS=SunOS
595         __HostOS=SunOS
596         ;;
597
598     *)
599         echo "Unsupported OS $OSName detected, configuring as if for Linux"
600         __BuildOS=Linux
601         __HostOS=Linux
602         ;;
603 esac
604
605 __BuildType=Debug
606 __CodeCoverage=
607 __IncludeTests=INCLUDE_TESTS
608
609 # Set the various build properties here so that CMake and MSBuild can pick them up
610 export __ProjectDir="$__ProjectRoot"
611 __SourceDir="$__ProjectDir/src"
612 __PackagesDir="$__ProjectDir/packages"
613 __RootBinDir="$__ProjectDir/bin"
614 __BuildToolsDir="$__ProjectDir/Tools"
615 __UnprocessedBuildArgs=
616 __RunArgs=
617 __MSBCleanBuildArgs=
618 __UseNinja=0
619 __VerboseBuild=0
620 __SkipRestore=""
621 __SkipNative=1 # [REMOVE] Temporarily default to skip native
622 __SkipConfigure=0
623 __SkipGenerateVersion=0
624 __ConfigureOnly=0
625 __CrossBuild=0
626 __ClangMajorVersion=0
627 __ClangMinorVersion=0
628 __NuGetPath="$__PackagesDir/NuGet.exe"
629 __HostDistroRid=""
630 __BuildAgainstPackagesArg=
631 __SkipRestorePackages=0
632 __DistroRid=""
633 __cmakeargs=""
634 __PortableLinux=0
635 __msbuildonunsupportedplatform=0
636 __ZipTests=0
637 __NativeTestIntermediatesDir=
638 __RunTests=0
639 __RebuildTests=0
640 __BuildTestWrappers=1
641 __GenerateLayoutOnly=
642 __GenerateTestHostOnly=
643 __priority1=
644 CORE_ROOT=
645
646 while :; do
647     if [ $# -le 0 ]; then
648         break
649     fi
650
651     lowerI="$(echo $1 | awk '{print tolower($0)}')"
652     case $lowerI in
653         -\?|-h|--help)
654             usage
655             exit 1
656             ;;
657
658         x86)
659             __BuildArch=x86
660             ;;
661
662         x64)
663             __BuildArch=x64
664             ;;
665
666         arm)
667             __BuildArch=arm
668             ;;
669
670         armel)
671             __BuildArch=armel
672             ;;
673
674         arm64)
675             __BuildArch=arm64
676             ;;
677
678         debug)
679             __BuildType=Debug
680             ;;
681
682         checked)
683             __BuildType=Checked
684             ;;
685
686         release)
687             __BuildType=Release
688             ;;
689
690         coverage)
691             __CodeCoverage=Coverage
692             ;;
693
694         cross)
695             __CrossBuild=1
696             ;;
697
698         portableBuild)
699             __PortableBuild=1
700             ;;
701
702         portablelinux)
703             if [ "$__BuildOS" == "Linux" ]; then
704                 __PortableLinux=1
705             else
706                 echo "ERROR: portableLinux not supported for non-Linux platforms."
707                 exit 1
708             fi
709             ;;
710
711         verbose)
712         __VerboseBuild=1
713         ;;
714
715         clang3.5|-clang3.5)
716             __ClangMajorVersion=3
717             __ClangMinorVersion=5
718             ;;
719
720         clang3.6|-clang3.6)
721             __ClangMajorVersion=3
722             __ClangMinorVersion=6
723             ;;
724
725         clang3.7|-clang3.7)
726             __ClangMajorVersion=3
727             __ClangMinorVersion=7
728             ;;
729
730         clang3.8|-clang3.8)
731             __ClangMajorVersion=3
732             __ClangMinorVersion=8
733             ;;
734
735         clang3.9|-clang3.9)
736             __ClangMajorVersion=3
737             __ClangMinorVersion=9
738             ;;
739
740         clang4.0|-clang4.0)
741             __ClangMajorVersion=4
742             __ClangMinorVersion=0
743             ;;
744
745         clang5.0|-clang5.0)
746             __ClangMajorVersion=5
747             __ClangMinorVersion=0
748             ;;
749
750         clang6.0|-clang6.0)
751             __ClangMajorVersion=6
752             __ClangMinorVersion=0
753             ;;
754
755         ninja)
756             __UseNinja=1
757             ;;
758
759         runtests)
760             __RunTests=1
761             ;;
762
763         rebuild)
764             __RebuildTests=1
765             ;;
766
767         skipnative|-skipnative)
768             __SkipNative=1
769             ;;
770
771         # [REMOVE] Enable native build - the temporary default is to skip native
772         --skipnative)
773             __SkipNative=0
774             ;;
775
776         ziptests)
777             __ZipTests=1
778             ;;
779
780         generatelayoutonly)
781             __GenerateLayoutOnly=1
782             ;;
783         generatetesthostonly)
784             __GenerateTestHostOnly=1
785             ;;
786         buildagainstpackages)
787             __BuildAgainstPackagesArg=1
788             ;;
789         skiprestorepackages)
790             __SkipRestorePackages=1
791             ;;
792
793         bindir)
794             if [ -n "$2" ]; then
795                 __RootBinDir="$2"
796                 if [ ! -d $__RootBinDir ]; then
797                     mkdir $__RootBinDir
798                 fi
799                 __RootBinParent=$(dirname $__RootBinDir)
800                 __RootBinName=${__RootBinDir##*/}
801                 __RootBinDir="$(cd $__RootBinParent &>/dev/null && printf %s/%s $PWD $__RootBinName)"
802                 shift
803             else
804                 echo "ERROR: 'bindir' requires a non-empty option argument"
805                 exit 1
806             fi
807             ;;
808
809         msbuildonunsupportedplatform)
810             __msbuildonunsupportedplatform=1
811             ;;
812         priority1)
813             __priority1=1
814             __UnprocessedBuildArgs+=("-priority=1")
815             ;;
816         *)
817             __UnprocessedBuildArgs+=("$1")
818             ;;
819     esac
820
821     shift
822 done
823
824 # Get the number of processors available to the scheduler
825 # Other techniques such as `nproc` only get the number of
826 # processors available to a single process.
827 if [ `uname` = "FreeBSD" ]; then
828   __NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
829 elif [ `uname` = "NetBSD" ]; then
830   __NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
831 elif [ `uname` = "Darwin" ]; then
832   __NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
833 else
834   __NumProc=$(nproc --all)
835 fi
836
837 __RunArgs=("-BuildArch=$__BuildArch" "-BuildType=$__BuildType" "-BuildOS=$__BuildOS")
838
839 # Configure environment if we are doing a verbose build
840 if [ $__VerboseBuild == 1 ]; then
841     export VERBOSE=1
842     __RunArgs+=("-verbose")
843 fi
844
845 # Set default clang version
846 if [[ $__ClangMajorVersion == 0 && $__ClangMinorVersion == 0 ]]; then
847     if [ $__CrossBuild == 1 ]; then
848         __ClangMajorVersion=3
849         __ClangMinorVersion=6
850     else
851         __ClangMajorVersion=3
852         __ClangMinorVersion=5
853     fi
854 fi
855
856 # Set dependent variables
857 __LogsDir="$__RootBinDir/Logs"
858
859 # init the host distro name
860 initHostDistroRid
861
862 # Set the remaining variables based upon the determined build configuration
863 __BinDir="$__RootBinDir/Product/$__BuildOS.$__BuildArch.$__BuildType"
864 __PackagesBinDir="$__BinDir/.nuget"
865 __ToolsDir="$__RootBinDir/tools"
866 __TestDir="$__ProjectDir/tests"
867 __TestWorkingDir="$__RootBinDir/tests/$__BuildOS.$__BuildArch.$__BuildType"
868 __IntermediatesDir="$__RootBinDir/obj/$__BuildOS.$__BuildArch.$__BuildType"
869 __TestIntermediatesDir="$__RootBinDir/tests/obj/$__BuildOS.$__BuildArch.$__BuildType"
870 __isMSBuildOnNETCoreSupported=0
871 __CrossComponentBinDir="$__BinDir"
872 __CrossCompIntermediatesDir="$__IntermediatesDir/crossgen"
873
874 __CrossArch="$__HostArch"
875 if [[ "$__HostArch" == "x64" && "$__BuildArch" == "arm" ]]; then
876     __CrossArch="x86"
877 fi
878 if [ $__CrossBuild == 1 ]; then
879     __CrossComponentBinDir="$__CrossComponentBinDir/$__CrossArch"
880 fi
881 __CrossgenCoreLibLog="$__LogsDir/CrossgenCoreLib_$__BuildOS.$BuildArch.$__BuildType.log"
882 __CrossgenExe="$__CrossComponentBinDir/crossgen"
883
884 isMSBuildOnNETCoreSupported
885
886 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to it.
887 # This is needed by CLI to function.
888 if [ -z "$HOME" ]; then
889     if [ ! -d "$__ProjectDir/temp_home" ]; then
890         mkdir temp_home
891     fi
892     export HOME=$__ProjectDir/temp_home
893     echo "HOME not defined; setting it to $HOME"
894 fi
895
896 # Configure environment if we are doing a cross compile.
897 if [ $__CrossBuild == 1 ]; then
898     export CROSSCOMPILE=1
899     if ! [[ -n "$ROOTFS_DIR" ]]; then
900         export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__BuildArch"
901     fi
902 fi
903
904 # init the target distro name
905 initTargetDistroRid
906
907 # Override tool directory
908
909 __CoreClrVersion=1.1.0
910 __sharedFxDir=$__BuildToolsDir/dotnetcli/shared/Microsoft.NETCore.App/$__CoreClrVersion/
911
912 if [[ (-z "$__GenerateLayoutOnly") && (-z "$__GenerateTestHostOnly") ]]; then
913     echo "Building Tests..."
914     build_Tests
915 else
916     echo "Generating test layout..."
917     generate_layout
918     if [ ! -z "$__GenerateTestHostOnly" ]; then
919         echo "Generating test host..."
920         generate_testhost
921     fi
922 fi
923
924 if [ $? -ne 0 ]; then
925     echo "Failed to build tests"
926     exit 1
927 fi
928
929 echo "${__MsgPrefix}Test build successful."
930 echo "${__MsgPrefix}Test binaries are available at ${__TestBinDir}"
931
932 __testNativeBinDir=$__IntermediatesDir/tests
933
934 if [ $__RunTests -ne 0 ]; then
935
936     echo "Run Tests..."
937
938     echo "${__TestDir}/runtest.sh --testRootDir=$__TestBinDir --coreClrBinDir=$__BinDir --coreFxBinDir=$__sharedFxDir --testNativeBinDir=$__testNativeBinDir"
939
940     $__TestDir/runtest.sh --testRootDir=$__TestBinDir --coreClrBinDir=$__BinDir --coreFxBinDir=$CORE_ROOT --testNativeBinDir=$__testNativeBinDir
941
942     echo "Tests run successful."
943 else
944     echo "To run all tests use 'tests/runtests.sh' where:"
945     echo "    testRootDir      = $__TestBinDir"
946     echo "    coreClrBinDir    = $__BinDir"
947     echo "    coreFxBinDir     = $CORE_ROOT"
948     echo "    testNativeBinDir = $__testNativeBinDir"
949     echo " -------------------------------------------------- "
950     echo " Example runtest.sh command"
951     echo ""
952     echo " ./tests/runtest.sh --coreOverlayDir=$CORE_ROOT --testNativeBinDir=$__testNativeBinDir --testRootDir=$__TestBinDir --copyNativeTestBin"
953     echo " -------------------------------------------------- "
954     echo "To run single test use the following command:"
955     echo "    bash ${__TestBinDir}/__TEST_PATH__/__TEST_NAME__.sh -coreroot=${CORE_ROOT}"
956 fi
957