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