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