Allow rejit on attach (#19054)
[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_Tests_internal "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_Tests_internal "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_Tests_internal "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     __CMakeBinDir="${__TestBinDir}"
214
215     if [ -z "$__TestIntermediateDir" ]; then
216         __TestIntermediateDir="tests/obj/${__BuildOS}.${__BuildArch}.${__BuildType}"
217     fi
218
219     echo "__BuildOS: ${__BuildOS}"
220     echo "__BuildArch: ${__BuildArch}"
221     echo "__BuildType: ${__BuildType}"
222     echo "__TestIntermediateDir: ${__TestIntermediateDir}"
223
224     if [ ! -f "$__TestBinDir" ]; then
225         echo "Creating TestBinDir: ${__TestBinDir}"
226         mkdir -p $__TestBinDir
227     fi
228     if [ ! -f "$__LogsDir" ]; then
229         echo "Creating LogsDir: ${__LogsDir}"
230         mkdir -p $__LogsDir
231     fi
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     build_Tests_internal "Restore_Product" "${__ProjectDir}/tests/build.proj" "Restore product binaries (build tests)" "-BatchRestorePackages"
242
243     if [ -n "$__BuildAgainstPackagesArg" ]; then
244         build_Tests_internal "Tests_GenerateRuntimeLayout" "${__ProjectDir}/tests/runtest.proj" "Restore product binaries (run tests)" "-BinPlaceRef" "-BinPlaceProduct" "-CopyCrossgenToProduct"
245     fi
246
247     echo "Starting the Managed Tests Build..."
248
249     build_Tests_internal "Tests_Managed" "$__ProjectDir/tests/build.proj" "Managed tests build (build tests)" "$__up"
250
251     if [ $? -ne 0 ]; then
252         echo "${__MsgPrefix}Error: build failed. Refer to the build log files for details (above)"
253         exit 1
254     else
255         echo "Managed tests build success!"
256     fi
257
258     if [ $__BuildTestWrappers -ne -0 ]; then
259         echo "${__MsgPrefix}Creating test wrappers..."
260
261         __XUnitWrapperBuiltMarker=${__TestBinDir}/xunit_wrapper_build
262
263         if [ ! -f $__XUnitWrapperBuiltMarker ]; then
264
265             build_Tests_internal "Tests_XunitWrapper" "$__ProjectDir/tests/runtest.proj" "Test Xunit Wrapper" "-BuildWrappers" "-MsBuildEventLogging= " "-TargetsWindows=false"
266
267             if [ $? -ne 0 ]; then
268                 echo "${__MsgPrefix}Error: build failed. Refer to the build log files for details (above)"
269                 exit 1
270             else
271                 echo "XUnit Wrappers have been built."
272                 echo "Create marker \"${__XUnitWrapperBuiltMarker}\""
273                 touch $__XUnitWrapperBuiltMarker
274             fi
275         else
276             echo "XUnit Wrappers had been built before."
277         fi
278     fi
279
280     if [ -n "$__UpdateInvalidPackagesArg" ]; then
281         __up=-updateinvalidpackageversion
282     fi
283
284     echo "${__MsgPrefix}Creating test overlay..."
285
286     generate_layout
287
288     if [ $__ZipTests -ne 0 ]; then
289         echo "${__MsgPrefix}ZIP tests packages..."
290         build_Tests_internal "Helix_Prep" "$__ProjectDir/tests/helixprep.proj" "Prep test binaries for Helix publishing" " "
291     fi
292 }
293
294 build_Tests_internal()
295 {
296     subDirectoryName=$1
297     shift
298     projectName=$1
299     shift
300     stepName="$1"
301     shift
302     extraBuildParameters=("$@")
303
304     # Set up directories and file names
305     __BuildLogRootName=$subDirectoryName
306     __BuildLog="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.log"
307     __BuildWrn="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.wrn"
308     __BuildErr="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.err"
309
310     # Use binclashlogger by default if no other logger is specified
311     if [[ "${extraBuildParameters[*]}" == *"-MsBuildEventLogging"* ]]; then
312         msbuildEventLogging=""
313     else
314         msbuildEventLogging="-MsBuildEventLogging=\"/l:BinClashLogger,Tools/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log\""
315     fi
316
317     if [[ "$subDirectoryName" == "Tests_Managed" ]]; then
318         # Execute msbuild managed test build in stages - workaround for excessive data retention in MSBuild ConfigCache
319         # See https://github.com/Microsoft/msbuild/issues/2993
320
321         # __SkipPackageRestore and __SkipTargetingPackBuild used  to control build by tests/src/dirs.proj
322         export __SkipPackageRestore=false
323         export __SkipTargetingPackBuild=false
324         export __BuildLoopCount=2
325         export __TestGroupToBuild=1
326         __AppendToLog=false
327
328         if [ -n __priority1 ]; then
329             export __BuildLoopCount=16
330             export __TestGroupToBuild=2
331         fi
332
333         for (( slice=1 ; slice <= __BuildLoopCount; slice = slice + 1 ))
334         do
335             __msbuildLog="\"/flp:Verbosity=normal;LogFile=${__BuildLog};Append=${__AppendToLog}\""
336             __msbuildWrn="\"/flp1:WarningsOnly;LogFile=${__BuildWrn};Append=${__AppendToLog}\""
337             __msbuildErr="\"/flp2:ErrorsOnly;LogFile=${__BuildErr};Append=${__AppendToLog}\""
338
339             export TestBuildSlice=$slice
340
341             # Generate build command
342             buildArgs=("-Project=$projectName" "-MsBuildLog=${__msbuildLog}" "-MsBuildWrn=${__msbuildWrn}" "-MsBuildErr=${__msbuildErr}")
343             buildArgs+=("$msbuildEventLogging")
344             buildArgs+=("${extraBuildParameters[@]}")
345             buildArgs+=("${__RunArgs[@]}")
346             buildArgs+=("${__UnprocessedBuildArgs[@]}")
347
348             echo "Building step '$stepName' slice=$slice via $buildCommand"
349
350             # Invoke MSBuild
351             "$__ProjectRoot/run.sh" build "${buildArgs[@]}"
352
353             # Make sure everything is OK
354             if [ $? -ne 0 ]; then
355                 echo "${__MsgPrefix}Failed to build $stepName. See the build logs:"
356                 echo "    $__BuildLog"
357                 echo "    $__BuildWrn"
358                 echo "    $__BuildErr"
359                 exit 1
360             fi
361             export __SkipPackageRestore=true
362             export __SkipTargetingPackBuild=true
363             __AppendToLog=true
364         done
365     else
366         __msbuildLog="\"/flp:Verbosity=normal;LogFile=${__BuildLog}\""
367         __msbuildWrn="\"/flp1:WarningsOnly;LogFile=${__BuildWrn}\""
368         __msbuildErr="\"/flp2:ErrorsOnly;LogFile=${__BuildErr}\""
369
370         # Generate build command
371         buildArgs=("-Project=$projectName" "-MsBuildLog=${__msbuildLog}" "-MsBuildWrn=${__msbuildWrn}" "-MsBuildErr=${__msbuildErr}")
372         buildArgs+=("$msbuildEventLogging")
373         buildArgs+=("${extraBuildParameters[@]}")
374         buildArgs+=("${__RunArgs[@]}")
375         buildArgs+=("${__UnprocessedBuildArgs[@]}")
376
377         echo "Building step '$stepName' via $buildCommand"
378
379         # Invoke MSBuild
380         "$__ProjectRoot/run.sh" build "${buildArgs[@]}"
381
382         # Make sure everything is OK
383         if [ $? -ne 0 ]; then
384             echo "${__MsgPrefix}Failed to build $stepName. See the build logs:"
385             echo "    $__BuildLog"
386             echo "    $__BuildWrn"
387             echo "    $__BuildErr"
388             exit 1
389         fi
390     fi
391 }
392
393 usage()
394 {
395     echo "Usage: $0 [BuildArch] [BuildType] [verbose] [coverage] [cross] [clangx.y] [ninja] [runtests] [bindir]"
396     echo "BuildArch can be: x64, x86, arm, armel, arm64"
397     echo "BuildType can be: debug, checked, release"
398     echo "coverage - optional argument to enable code coverage build (currently supported only for Linux and OSX)."
399     echo "ninja - target ninja instead of GNU make"
400     echo "clangx.y - optional argument to build using clang version x.y - supported version 3.5 - 6.0"
401     echo "cross - optional argument to signify cross compilation,"
402     echo "      - will use ROOTFS_DIR environment variable if set."
403     echo "crosscomponent - optional argument to build cross-architecture component,"
404     echo "               - will use CAC_ROOTFS_DIR environment variable if set."
405     echo "portableLinux - build for Portable Linux Distribution"
406     echo "portablebuild - Use portable build."
407     echo "verbose - optional argument to enable verbose build output."
408     echo "rebuild - if tests have already been built - rebuild them"
409     echo "generatelayoutonly - only pull down dependencies and build coreroot"
410     echo "generatetesthostonly - only pull down dependencies and build coreroot and the CoreFX testhost"
411     echo "buildagainstpackages - pull down and build using packages."
412     echo "runtests - run tests after building them"
413     echo "ziptests - zips CoreCLR tests & Core_Root for a Helix run"
414     echo "bindir - output directory (defaults to $__ProjectRoot/bin)"
415     echo "msbuildonunsupportedplatform - build managed binaries even if distro is not officially supported."
416     echo "priority1 - include priority=1 tests in the build"
417     exit 1
418 }
419
420
421 # Obtain the location of the bash script to figure out where the root of the repo is.
422 __ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
423
424 # $__ProjectRoot/build.sh $1 $2
425
426 # Use uname to determine what the CPU is.
427 CPUName=$(uname -p)
428
429 # Some Linux platforms report unknown for platform, but the arch for machine.
430 if [ "$CPUName" == "unknown" ]; then
431     CPUName=$(uname -m)
432 fi
433
434 case $CPUName in
435     i686)
436         echo "Unsupported CPU $CPUName detected, build might not succeed!"
437         __BuildArch=x86
438         __HostArch=x86
439         ;;
440
441     x86_64)
442         __BuildArch=x64
443         __HostArch=x64
444         ;;
445
446     armv7l)
447         echo "Unsupported CPU $CPUName detected, build might not succeed!"
448         __BuildArch=arm
449         __HostArch=arm
450         ;;
451
452     aarch64)
453         __BuildArch=arm64
454         __HostArch=arm64
455         ;;
456
457     *)
458         echo "Unknown CPU $CPUName detected, configuring as if for x64"
459         __BuildArch=x64
460         __HostArch=x64
461         ;;
462 esac
463
464 # Use uname to determine what the OS is.
465 OSName=$(uname -s)
466 case $OSName in
467     Linux)
468         __BuildOS=Linux
469         __HostOS=Linux
470         ;;
471
472     Darwin)
473         __BuildOS=OSX
474         __HostOS=OSX
475         ;;
476
477     FreeBSD)
478         __BuildOS=FreeBSD
479         __HostOS=FreeBSD
480         ;;
481
482     OpenBSD)
483         __BuildOS=OpenBSD
484         __HostOS=OpenBSD
485         ;;
486
487     NetBSD)
488         __BuildOS=NetBSD
489         __HostOS=NetBSD
490         ;;
491
492     SunOS)
493         __BuildOS=SunOS
494         __HostOS=SunOS
495         ;;
496
497     *)
498         echo "Unsupported OS $OSName detected, configuring as if for Linux"
499         __BuildOS=Linux
500         __HostOS=Linux
501         ;;
502 esac
503
504 __BuildType=Debug
505 __CodeCoverage=
506 __IncludeTests=INCLUDE_TESTS
507
508 # Set the various build properties here so that CMake and MSBuild can pick them up
509 export __ProjectDir="$__ProjectRoot"
510 __SourceDir="$__ProjectDir/src"
511 __PackagesDir="$__ProjectDir/packages"
512 __RootBinDir="$__ProjectDir/bin"
513 __BuildToolsDir="$__ProjectDir/Tools"
514 __UnprocessedBuildArgs=
515 __RunArgs=
516 __MSBCleanBuildArgs=
517 __UseNinja=0
518 __VerboseBuild=0
519 __SkipRestore=""
520 __CrossBuild=0
521 __ClangMajorVersion=0
522 __ClangMinorVersion=0
523 __NuGetPath="$__PackagesDir/NuGet.exe"
524 __HostDistroRid=""
525 __BuildAgainstPackagesArg=
526 __DistroRid=""
527 __cmakeargs=""
528 __PortableLinux=0
529 __msbuildonunsupportedplatform=0
530 __ZipTests=0
531 __NativeTestIntermediatesDir=
532 __RunTests=0
533 __RebuildTests=0
534 __BuildTestWrappers=1
535 __GenerateLayoutOnly=
536 __GenerateTestHostOnly=
537 __priority1=
538 CORE_ROOT=
539
540 while :; do
541     if [ $# -le 0 ]; then
542         break
543     fi
544
545     lowerI="$(echo $1 | awk '{print tolower($0)}')"
546     case $lowerI in
547         -\?|-h|--help)
548             usage
549             exit 1
550             ;;
551
552         x86)
553             __BuildArch=x86
554             ;;
555
556         x64)
557             __BuildArch=x64
558             ;;
559
560         arm)
561             __BuildArch=arm
562             ;;
563
564         armel)
565             __BuildArch=armel
566             ;;
567
568         arm64)
569             __BuildArch=arm64
570             ;;
571
572         debug)
573             __BuildType=Debug
574             ;;
575
576         checked)
577             __BuildType=Checked
578             ;;
579
580         release)
581             __BuildType=Release
582             ;;
583
584         coverage)
585             __CodeCoverage=Coverage
586             ;;
587
588         cross)
589             __CrossBuild=1
590             ;;
591
592         portableBuild)
593             __PortableBuild=1
594             ;;
595
596         portablelinux)
597             if [ "$__BuildOS" == "Linux" ]; then
598                 __PortableLinux=1
599             else
600                 echo "ERROR: portableLinux not supported for non-Linux platforms."
601                 exit 1
602             fi
603             ;;
604
605         verbose)
606         __VerboseBuild=1
607         ;;
608
609         clang3.5|-clang3.5)
610             __ClangMajorVersion=3
611             __ClangMinorVersion=5
612             ;;
613
614         clang3.6|-clang3.6)
615             __ClangMajorVersion=3
616             __ClangMinorVersion=6
617             ;;
618
619         clang3.7|-clang3.7)
620             __ClangMajorVersion=3
621             __ClangMinorVersion=7
622             ;;
623
624         clang3.8|-clang3.8)
625             __ClangMajorVersion=3
626             __ClangMinorVersion=8
627             ;;
628
629         clang3.9|-clang3.9)
630             __ClangMajorVersion=3
631             __ClangMinorVersion=9
632             ;;
633
634         clang4.0|-clang4.0)
635             __ClangMajorVersion=4
636             __ClangMinorVersion=0
637             ;;
638
639         clang5.0|-clang5.0)
640             __ClangMajorVersion=5
641             __ClangMinorVersion=0
642             ;;
643
644         clang6.0|-clang6.0)
645             __ClangMajorVersion=6
646             __ClangMinorVersion=0
647             ;;
648
649         ninja)
650             __UseNinja=1
651             ;;
652
653         runtests)
654             __RunTests=1
655             ;;
656
657         rebuild)
658             __RebuildTests=1
659             ;;
660
661         ziptests)
662             __ZipTests=1
663             ;;
664
665         generatelayoutonly)
666             __GenerateLayoutOnly=1
667             ;;
668         generatetesthostonly)
669             __GenerateTestHostOnly=1
670             ;;
671         buildagainstpackages)
672             __BuildAgainstPackagesArg=1
673             ;;
674
675         bindir)
676             if [ -n "$2" ]; then
677                 __RootBinDir="$2"
678                 if [ ! -d $__RootBinDir ]; then
679                     mkdir $__RootBinDir
680                 fi
681                 __RootBinParent=$(dirname $__RootBinDir)
682                 __RootBinName=${__RootBinDir##*/}
683                 __RootBinDir="$(cd $__RootBinParent &>/dev/null && printf %s/%s $PWD $__RootBinName)"
684                 shift
685             else
686                 echo "ERROR: 'bindir' requires a non-empty option argument"
687                 exit 1
688             fi
689             ;;
690
691         msbuildonunsupportedplatform)
692             __msbuildonunsupportedplatform=1
693             ;;
694         priority1)
695             __priority1=1
696             __UnprocessedBuildArgs+=("-priority=1")
697             ;;
698         *)
699             __UnprocessedBuildArgs+=("$1")
700             ;;
701     esac
702
703     shift
704 done
705
706
707 __RunArgs=("-BuildArch=$__BuildArch" "-BuildType=$__BuildType" "-BuildOS=$__BuildOS")
708
709 # Configure environment if we are doing a verbose build
710 if [ $__VerboseBuild == 1 ]; then
711     export VERBOSE=1
712     __RunArgs+=("-verbose")
713 fi
714
715 # Set default clang version
716 if [[ $__ClangMajorVersion == 0 && $__ClangMinorVersion == 0 ]]; then
717     if [ $__CrossBuild == 1 ]; then
718         __ClangMajorVersion=3
719         __ClangMinorVersion=6
720     else
721         __ClangMajorVersion=3
722         __ClangMinorVersion=5
723     fi
724 fi
725
726
727 # Set dependent variables
728 __LogsDir="$__RootBinDir/Logs"
729
730 # init the host distro name
731 initHostDistroRid
732
733 # Set the remaining variables based upon the determined build configuration
734 __BinDir="$__RootBinDir/Product/$__BuildOS.$__BuildArch.$__BuildType"
735 __PackagesBinDir="$__BinDir/.nuget"
736 __ToolsDir="$__RootBinDir/tools"
737 __TestDir="$__ProjectDir/tests"
738 __TestWorkingDir="$__RootBinDir/tests/$__BuildOS.$__BuildArch.$__BuildType"
739 __IntermediatesDir="$__RootBinDir/obj/$__BuildOS.$__BuildArch.$__BuildType"
740 __TestIntermediatesDir="$__RootBinDir/tests/obj/$__BuildOS.$__BuildArch.$__BuildType"
741 __isMSBuildOnNETCoreSupported=0
742 __CrossComponentBinDir="$__BinDir"
743 __CrossCompIntermediatesDir="$__IntermediatesDir/crossgen"
744
745 __CrossArch="$__HostArch"
746 if [[ "$__HostArch" == "x64" && "$__BuildArch" == "arm" ]]; then
747     __CrossArch="x86"
748 fi
749 if [ $__CrossBuild == 1 ]; then
750     __CrossComponentBinDir="$__CrossComponentBinDir/$__CrossArch"
751 fi
752 __CrossgenCoreLibLog="$__LogsDir/CrossgenCoreLib_$__BuildOS.$BuildArch.$__BuildType.log"
753 __CrossgenExe="$__CrossComponentBinDir/crossgen"
754
755 isMSBuildOnNETCoreSupported
756
757 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to it.
758 # This is needed by CLI to function.
759 if [ -z "$HOME" ]; then
760     if [ ! -d "$__ProjectDir/temp_home" ]; then
761         mkdir temp_home
762     fi
763     export HOME=$__ProjectDir/temp_home
764     echo "HOME not defined; setting it to $HOME"
765 fi
766
767 # Specify path to be set for CMAKE_INSTALL_PREFIX.
768 # This is where all built CoreClr libraries will copied to.
769 export __CMakeBinDir="$__BinDir"
770 if [ [ ! -d "$__BinDir" ] || [ ! -d "$__BinDir/bin" ] ]; then
771     if [ [ -z "$__GenerateLayoutOnly" ] && [ -z "$__GenerateTestHostOnly" ] ]; then
772
773         echo "Cannot find build directory for the CoreCLR native tests."
774         echo "Please make sure native tests are built before building managed tests."
775         echo "Example use: './build.sh $__BuildArch $__BuildType' without -skiptests switch"
776     else
777         echo "Cannot find build directory for the CoreCLR Product."
778         echo "Please make sure CoreCLR and native tests are built before building managed tests."
779         echo "Example use: './build.sh $__BuildArch $__BuildType' "
780         fi
781     exit 1
782 fi
783
784
785
786 # Configure environment if we are doing a cross compile.
787 if [ $__CrossBuild == 1 ]; then
788     export CROSSCOMPILE=1
789     if ! [[ -n "$ROOTFS_DIR" ]]; then
790         export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__BuildArch"
791     fi
792 fi
793
794 # init the target distro name
795 initTargetDistroRid
796
797 # Override tool directory
798
799 __CoreClrVersion=1.1.0
800 __sharedFxDir=$__BuildToolsDir/dotnetcli/shared/Microsoft.NETCore.App/$__CoreClrVersion/
801
802
803 if [[ (-z "$__GenerateLayoutOnly") && (-z "$__GenerateTestHostOnly") ]]; then
804     echo "Building Tests..."
805     build_Tests
806 else
807     echo "Generating test layout..."
808     generate_layout
809     if [ ! -z "$__GenerateTestHostOnly" ]; then
810         echo "Generating test host..."        
811         generate_testhost
812     fi
813 fi
814
815 if [ $? -ne 0 ]; then
816     echo "Failed to build tests"
817     exit 1
818 fi
819
820 echo "${__MsgPrefix}Test build successful."
821 echo "${__MsgPrefix}Test binaries are available at ${__TestBinDir}"
822
823 __testNativeBinDir=$__IntermediatesDir/tests
824
825 if [ $__RunTests -ne 0 ]; then
826
827     echo "Run Tests..."
828
829     echo "${__TestDir}/runtest.sh --testRootDir=$__TestBinDir --coreClrBinDir=$__BinDir --coreFxBinDir=$__sharedFxDir --testNativeBinDir=$__testNativeBinDir"
830
831     $__TestDir/runtest.sh --testRootDir=$__TestBinDir --coreClrBinDir=$__BinDir --coreFxBinDir=$CORE_ROOT --testNativeBinDir=$__testNativeBinDir
832
833     echo "Tests run successful."
834 else
835     echo "To run all tests use 'tests/runtests.sh' where:"
836     echo "    testRootDir      = $__TestBinDir"
837     echo "    coreClrBinDir    = $__BinDir"
838     echo "    coreFxBinDir     = $CORE_ROOT"
839     echo "    testNativeBinDir = $__testNativeBinDir"
840     echo " -------------------------------------------------- "
841     echo " Example runtest.sh command"
842     echo ""
843     echo " ./tests/runtest.sh --coreOverlayDir=$CORE_ROOT --testNativeBinDir=$__testNativeBinDir --testRootDir=$__TestBinDir --copyNativeTestBin"
844     echo " -------------------------------------------------- "
845     echo "To run single test use the following command:"
846     echo "    bash ${__TestBinDir}/__TEST_PATH__/__TEST_NAME__.sh -coreroot=${CORE_ROOT}"
847 fi
848