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