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