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