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