Merge pull request #11803 from BruceForstall/FixRuntest
[platform/upstream/coreclr.git] / build-test.sh
1 #!/usr/bin/env bash
2
3 initHostDistroRid()
4 {
5     if [ "$__HostOS" == "Linux" ]; then
6         if [ ! -e /etc/os-release ]; then
7             echo "WARNING: Can not determine runtime id for current distro."
8             __HostDistroRid=""
9         else
10             source /etc/os-release
11             __HostDistroRid="$ID.$VERSION_ID-$__HostArch"
12         fi
13     fi
14 }
15
16 initTargetDistroRid()
17 {
18     if [ $__CrossBuild == 1 ]; then
19         if [ "$__BuildOS" == "Linux" ]; then
20             if [ ! -e $ROOTFS_DIR/etc/os-release ]; then
21                 echo "WARNING: Can not determine runtime id for current distro."
22                 export __DistroRid=""
23             else
24                 source $ROOTFS_DIR/etc/os-release
25                 export __DistroRid="$ID.$VERSION_ID-$__BuildArch"
26             fi
27         fi
28     else
29         export __DistroRid="$__HostDistroRid"
30     fi
31
32     # Portable builds target the base RID
33     if [ "$__PortableBuild" == 1 ]; then
34         if [ "$__BuildOS" == "Linux" ]; then
35             export __DistroRid="linux-$__BuildArch"
36         elif [ "$__BuildOS" == "OSX" ]; then
37             export __DistroRid="osx-$__BuildArch"
38         fi
39     fi
40 }
41
42 isMSBuildOnNETCoreSupported()
43 {
44     # This needs to be updated alongwith corresponding changes to netci.groovy.
45     __isMSBuildOnNETCoreSupported=0
46
47     if [ "$__HostArch" == "x64" ]; then
48         if [ "$__HostOS" == "Linux" ]; then
49             case "$__HostDistroRid" in
50                 "centos.7-x64")
51                     __isMSBuildOnNETCoreSupported=1
52                     ;;
53                 "debian.8-x64")
54                     __isMSBuildOnNETCoreSupported=1
55                     ;;
56                 "fedora.23-x64")
57                     __isMSBuildOnNETCoreSupported=1
58                     ;;
59                 "fedora.24-x64")
60                     __isMSBuildOnNETCoreSupported=1
61                     ;;
62                 "opensuse.42.1-x64")
63                     __isMSBuildOnNETCoreSupported=1
64                     ;;
65                 "rhel.7"*"-x64")
66                     __isMSBuildOnNETCoreSupported=1
67                     ;;
68                 "ubuntu.14.04-x64")
69                     __isMSBuildOnNETCoreSupported=1
70                     ;;
71                 "ubuntu.16.04-x64")
72                     __isMSBuildOnNETCoreSupported=1
73                     ;;
74                 "ubuntu.16.10-x64")
75                     __isMSBuildOnNETCoreSupported=1
76                     ;;
77                 "alpine.3.4.3-x64")
78                     __isMSBuildOnNETCoreSupported=1
79                     ;;
80                 *)
81                 __isMSBuildOnNETCoreSupported=$__msbuildonunsupportedplatform
82             esac
83         elif [ "$__HostOS" == "OSX" ]; then
84             __isMSBuildOnNETCoreSupported=1
85         fi
86     fi
87 }
88
89 build_Tests()
90 {
91     __TestDir=$__ProjectDir/tests
92     __ProjectFilesDir=$__TestDir
93     __TestBinDir=$__TestWorkingDir
94
95     if [ $__RebuildTests -ne 0 ]; then
96         if [ -d "${__TestBinDir}" ]; then
97             echo "Removing tests build dir: ${__TestBinDir}"
98             rm -rf $__TestBinDir
99         fi
100     fi
101
102     __CMakeBinDir="${__TestBinDir}"
103
104     if [ -z "$__TestIntermediateDir" ]; then
105         __TestIntermediateDir="tests/obj/${__BuildOS}.${__BuildArch}.${__BuildType}"
106     fi
107
108         echo "__BuildOS: ${__BuildOS}"
109         echo "__BuildArch: ${__BuildArch}"
110         echo "__BuildType: ${__BuildType}"
111         echo "__TestIntermediateDir: ${__TestIntermediateDir}"
112
113     if [ ! -f "$__TestBinDir" ]; then
114         echo "Creating TestBinDir: ${__TestBinDir}"
115         mkdir -p $__TestBinDir
116     fi
117     if [ ! -f "$__LogsDir" ]; then
118         echo "Creating LogsDir: ${__LogsDir}"
119         mkdir -p $__LogsDir
120     fi
121
122     __BuildProperties="-p:OSGroup=${__BuildOS} -p:BuildOS=${__BuildOS} -p:BuildArch=${__BuildArch} -p:BuildType=${__BuildType}"
123
124     # =========================================================================================
125     # ===
126     # === Restore product binaries from packages
127     # ===
128     # =========================================================================================
129
130     build_Tests_internal "Restore_Product" "${__ProjectDir}/tests/build.proj" " -BatchRestorePackages" "Restore product binaries (build tests)"
131
132     build_Tests_internal "Tests_GenerateRuntimeLayout" "${__ProjectDir}/tests/runtest.proj" "-BinPlaceRef -BinPlaceProduct -CopyCrossgenToProduct" "Restore product binaries (run tests)"
133
134     if [ -n "$__UpdateInvalidPackagesArg" ]; then
135         __up=-updateinvalidpackageversion
136     fi
137
138     # Work hardcoded path around
139     if [ ! -f "${__BuildToolsDir}/Microsoft.CSharp.Core.Targets" ]; then
140         ln -s "${__BuildToolsDir}/Microsoft.CSharp.Core.targets" "${__BuildToolsDir}/Microsoft.CSharp.Core.Targets"
141     fi
142     if [ ! -f "${__BuildToolsDir}/Microsoft.CSharp.targets" ]; then
143         ln -s "${__BuildToolsDir}/Microsoft.CSharp.Targets" "${__BuildToolsDir}/Microsoft.CSharp.targets"
144     fi
145
146     echo "Starting the Managed Tests Build..."
147
148     __ManagedTestBuiltMarker=${__TestBinDir}/managed_test_build
149
150     if [ ! -f $__ManagedTestBuiltMarker ]; then
151
152             build_Tests_internal "Tests_Managed" "$__ProjectDir/tests/build.proj" "$__up" "Managed tests build (build tests)"
153
154         if [ $? -ne 0 ]; then
155             echo "${__MsgPrefix}Error: build failed. Refer to the build log files for details (above)"
156             exit 1
157         else
158             echo "Tests have been built."
159             echo "Create marker \"${__ManagedTestBuiltMarker}\""
160             touch $__ManagedTestBuiltMarker
161         fi
162     else
163         echo "Managed Tests had been built before."
164     fi
165
166     if [ $__BuildTestWrappers -ne -0 ]; then
167         echo "${__MsgPrefix}Creating test wrappers..."
168
169         __XUnitWrapperBuiltMarker=${__TestBinDir}/xunit_wrapper_build
170
171         if [ ! -f $__XUnitWrapperBuiltMarker ]; then
172
173             build_Tests_internal "Tests_XunitWrapper" "$__ProjectDir/tests/runtest.proj" "-BuildWrappers -MsBuildEventLogging=\" \" " "Test Xunit Wrapper"
174
175             if [ $? -ne 0 ]; then
176                 echo "${__MsgPrefix}Error: build failed. Refer to the build log files for details (above)"
177                 exit 1
178             else
179                 echo "XUnit Wrappers have been built."
180                 echo "Create marker \"${__XUnitWrapperBuiltMarker}\""
181                 touch $__XUnitWrapperBuiltMarker
182             fi
183         else
184             echo "XUnit Wrappers had been built before."
185         fi
186     fi
187
188     echo "${__MsgPrefix}Creating test overlay..."
189
190     if [ -z "$XuintTestBinBase" ]; then
191       XuintTestBinBase=$__TestWorkingDir
192     fi
193
194     export CORE_ROOT=$XuintTestBinBase/Tests/Core_Root
195
196     if [ ! -f "${CORE_ROOT}" ]; then
197       mkdir -p $CORE_ROOT
198     else
199       rm -rf $CORE_ROOT/*
200     fi
201
202     cp -r $__BinDir/* $CORE_ROOT/ > /dev/null
203
204     build_Tests_internal "Tests_Overlay_Managed" "$__ProjectDir/tests/runtest.proj" "-testOverlay" "Creating test overlay"
205
206     if [ $__ZipTests -ne 0 ]; then
207         echo "${__MsgPrefix}ZIP tests packages..."
208         build_Tests_internal "Helix_Prep" "$__ProjectDir/tests/helixprep.proj" " " "Prep test binaries for Helix publishing"
209     fi
210 }
211
212 build_Tests_internal()
213 {
214         subDirectoryName=$1
215         projectName=$2
216         extraBuildParameters=$3
217         stepName="$4"
218
219         # Set up directories and file names
220         __BuildLogRootName=$subDirectoryName
221     __BuildLog="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.log"
222     __BuildWrn="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.wrn"
223     __BuildErr="$__LogsDir/${__BuildLogRootName}.${__BuildOS}.${__BuildArch}.${__BuildType}.err"
224     __msbuildLog="\"/flp:Verbosity=normal;LogFile=${__BuildLog}\""
225     __msbuildWrn="\"/flp1:WarningsOnly;LogFile=${__BuildWrn}\""
226     __msbuildErr="\"/flp2:ErrorsOnly;LogFile=${__BuildErr}\""
227
228     # Generate build command
229     buildCommand="$__ProjectRoot/run.sh build -Project=$projectName -MsBuildLog=${__msbuildLog} -MsBuildWrn=${__msbuildWrn} -MsBuildErr=${__msbuildErr} $extraBuildParameters $__RunArgs $__UnprocessedBuildArgs"
230
231     echo "Building step '$stepName' via $buildCommand"
232
233     # Invoke MSBuild
234     eval $buildCommand
235
236     # Invoke MSBuild
237     # $__ProjectRoot/run.sh build -Project=$projectName -MsBuildLog="$__msbuildLog" -MsBuildWrn="$__msbuildWrn" -MsBuildErr="$__msbuildErr" $extraBuildParameters $__RunArgs $__UnprocessedBuildArgs
238
239     # Make sure everything is OK
240     if [ $? -ne 0 ]; then
241         echo "${__MsgPrefix}Failed to build $stepName. See the build logs:"
242         echo "    $__BuildLog"
243         echo "    $__BuildWrn"
244         echo "    $__BuildErr"
245         exit 1
246     fi
247 }
248
249 usage()
250 {
251     echo "Usage: $0 [BuildArch] [BuildType] [verbose] [coverage] [cross] [clangx.y] [ninja] [runtests] [bindir]"
252     echo "BuildArch can be: x64, x86, arm, armel, arm64"
253     echo "BuildType can be: debug, checked, release"
254     echo "coverage - optional argument to enable code coverage build (currently supported only for Linux and OSX)."
255     echo "ninja - target ninja instead of GNU make"
256     echo "clangx.y - optional argument to build using clang version x.y."
257     echo "cross - optional argument to signify cross compilation,"
258     echo "      - will use ROOTFS_DIR environment variable if set."
259     echo "crosscomponent - optional argument to build cross-architecture component,"
260     echo "               - will use CAC_ROOTFS_DIR environment variable if set."
261     echo "portableLinux - build for Portable Linux Distribution"
262     echo "verbose - optional argument to enable verbose build output."
263     echo "rebuild - if tests have already been built - rebuild them"
264     echo "runtests - run tests after building them"
265     echo "ziptests - zips CoreCLR tests & Core_Root for a Helix run"
266     echo "bindir - output directory (defaults to $__ProjectRoot/bin)"
267     echo "msbuildonunsupportedplatform - build managed binaries even if distro is not officially supported."
268     exit 1
269 }
270
271
272 # Obtain the location of the bash script to figure out where the root of the repo is.
273 __ProjectRoot="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
274
275 # $__ProjectRoot/build.sh $1 $2
276
277 # Use uname to determine what the CPU is.
278 CPUName=$(uname -p)
279
280 # Some Linux platforms report unknown for platform, but the arch for machine.
281 if [ "$CPUName" == "unknown" ]; then
282     CPUName=$(uname -m)
283 fi
284
285 case $CPUName in
286     i686)
287         echo "Unsupported CPU $CPUName detected, build might not succeed!"
288         __BuildArch=x86
289         __HostArch=x86
290         ;;
291
292     x86_64)
293         __BuildArch=x64
294         __HostArch=x64
295         ;;
296
297     armv7l)
298         echo "Unsupported CPU $CPUName detected, build might not succeed!"
299         __BuildArch=arm
300         __HostArch=arm
301         ;;
302
303     aarch64)
304         __BuildArch=arm64
305         __HostArch=arm64
306         ;;
307
308     *)
309         echo "Unknown CPU $CPUName detected, configuring as if for x64"
310         __BuildArch=x64
311         __HostArch=x64
312         ;;
313 esac
314
315 # Use uname to determine what the OS is.
316 OSName=$(uname -s)
317 case $OSName in
318     Linux)
319         __BuildOS=Linux
320         __HostOS=Linux
321         ;;
322
323     Darwin)
324         __BuildOS=OSX
325         __HostOS=OSX
326         ;;
327
328     FreeBSD)
329         __BuildOS=FreeBSD
330         __HostOS=FreeBSD
331         ;;
332
333     OpenBSD)
334         __BuildOS=OpenBSD
335         __HostOS=OpenBSD
336         ;;
337
338     NetBSD)
339         __BuildOS=NetBSD
340         __HostOS=NetBSD
341         ;;
342
343     SunOS)
344         __BuildOS=SunOS
345         __HostOS=SunOS
346         ;;
347
348     *)
349         echo "Unsupported OS $OSName detected, configuring as if for Linux"
350         __BuildOS=Linux
351         __HostOS=Linux
352         ;;
353 esac
354
355 __BuildType=Debug
356 __CodeCoverage=
357 __IncludeTests=INCLUDE_TESTS
358
359 # Set the various build properties here so that CMake and MSBuild can pick them up
360 export __ProjectDir="$__ProjectRoot"
361 __SourceDir="$__ProjectDir/src"
362 __PackagesDir="$__ProjectDir/packages"
363 __RootBinDir="$__ProjectDir/bin"
364 __BuildToolsDir="$__ProjectDir/Tools"
365 __UnprocessedBuildArgs=
366 __RunArgs=
367 __MSBCleanBuildArgs=
368 __UseNinja=0
369 __VerboseBuild=0
370 __SkipRestore=""
371 __CrossBuild=0
372 __ClangMajorVersion=0
373 __ClangMinorVersion=0
374 __NuGetPath="$__PackagesDir/NuGet.exe"
375 __HostDistroRid=""
376 __DistroRid=""
377 __cmakeargs=""
378 __PortableLinux=0
379 __msbuildonunsupportedplatform=0
380 __ZipTests=0
381 __NativeTestIntermediatesDir=
382 __RunTests=0
383 __RebuildTests=0
384 __BuildTestWrappers=0
385 CORE_ROOT=
386
387
388 while :; do
389     if [ $# -le 0 ]; then
390         break
391     fi
392
393     lowerI="$(echo $1 | awk '{print tolower($0)}')"
394     case $lowerI in
395         -\?|-h|--help)
396             usage
397             exit 1
398             ;;
399
400         x86)
401             __BuildArch=x86
402             ;;
403
404         x64)
405             __BuildArch=x64
406             ;;
407
408         arm)
409             __BuildArch=arm
410             ;;
411
412         armel)
413             __BuildArch=armel
414             ;;
415
416         arm64)
417             __BuildArch=arm64
418             ;;
419
420         debug)
421             __BuildType=Debug
422             ;;
423
424         checked)
425             __BuildType=Checked
426             ;;
427
428         release)
429             __BuildType=Release
430             ;;
431
432         coverage)
433             __CodeCoverage=Coverage
434             ;;
435
436         cross)
437             __CrossBuild=1
438             ;;
439
440         portablelinux)
441             if [ "$__BuildOS" == "Linux" ]; then
442                 __PortableLinux=1
443             else
444                 echo "ERROR: portableLinux not supported for non-Linux platforms."
445                 exit 1
446             fi
447             ;;
448
449         verbose)
450         __VerboseBuild=1
451         ;;
452
453         clang3.5)
454             __ClangMajorVersion=3
455             __ClangMinorVersion=5
456             ;;
457
458         clang3.6)
459             __ClangMajorVersion=3
460             __ClangMinorVersion=6
461             ;;
462
463         clang3.7)
464             __ClangMajorVersion=3
465             __ClangMinorVersion=7
466             ;;
467
468         clang3.8)
469             __ClangMajorVersion=3
470             __ClangMinorVersion=8
471             ;;
472
473         clang3.9)
474             __ClangMajorVersion=3
475             __ClangMinorVersion=9
476             ;;
477
478         ninja)
479             __UseNinja=1
480             ;;
481
482         runtests)
483             __RunTests=1
484             ;;
485
486         rebuild)
487             __RebuildTests=1
488             ;;
489
490         ziptests)
491             __ZipTests=1
492             ;;
493
494         bindir)
495             if [ -n "$2" ]; then
496                 __RootBinDir="$2"
497                 if [ ! -d $__RootBinDir ]; then
498                     mkdir $__RootBinDir
499                 fi
500                 __RootBinParent=$(dirname $__RootBinDir)
501                 __RootBinName=${__RootBinDir##*/}
502                 __RootBinDir="$(cd $__RootBinParent &>/dev/null && printf %s/%s $PWD $__RootBinName)"
503                 shift
504             else
505                 echo "ERROR: 'bindir' requires a non-empty option argument"
506                 exit 1
507             fi
508             ;;
509
510         msbuildonunsupportedplatform)
511             __msbuildonunsupportedplatform=1
512             ;;
513         *)
514             __UnprocessedBuildArgs="$__UnprocessedBuildArgs $1"
515             ;;
516     esac
517
518     shift
519 done
520
521
522 __RunArgs="-BuildArch=$__BuildArch -BuildType=$__BuildType -BuildOS=$__BuildOS"
523
524 # Configure environment if we are doing a verbose build
525 if [ $__VerboseBuild == 1 ]; then
526     export VERBOSE=1
527         __RunArgs="$__RunArgs -verbose"
528 fi
529
530 # Set default clang version
531 if [[ $__ClangMajorVersion == 0 && $__ClangMinorVersion == 0 ]]; then
532     if [ $__CrossBuild == 1 ]; then
533         __ClangMajorVersion=3
534         __ClangMinorVersion=6
535     else
536         __ClangMajorVersion=3
537         __ClangMinorVersion=5
538     fi
539 fi
540
541
542 # Set dependent variables
543 __LogsDir="$__RootBinDir/Logs"
544
545 # init the host distro name
546 initHostDistroRid
547
548 # Set the remaining variables based upon the determined build configuration
549 __BinDir="$__RootBinDir/Product/$__BuildOS.$__BuildArch.$__BuildType"
550 __PackagesBinDir="$__BinDir/.nuget"
551 __ToolsDir="$__RootBinDir/tools"
552 __TestDir="$__ProjectDir/tests"
553 __TestWorkingDir="$__RootBinDir/tests/$__BuildOS.$__BuildArch.$__BuildType"
554 __IntermediatesDir="$__RootBinDir/obj/$__BuildOS.$__BuildArch.$__BuildType"
555 __TestIntermediatesDir="$__RootBinDir/tests/obj/$__BuildOS.$__BuildArch.$__BuildType"
556 __isMSBuildOnNETCoreSupported=0
557 __CrossComponentBinDir="$__BinDir"
558 __CrossCompIntermediatesDir="$__IntermediatesDir/crossgen"
559
560 __CrossArch="$__HostArch"
561 if [[ "$__HostArch" == "x64" && "$__BuildArch" == "arm" ]]; then
562     __CrossArch="x86"
563 fi
564 if [ $__CrossBuild == 1 ]; then
565     __CrossComponentBinDir="$__CrossComponentBinDir/$__CrossArch"
566 fi
567 __CrossgenCoreLibLog="$__LogsDir/CrossgenCoreLib_$__BuildOS.$BuildArch.$__BuildType.log"
568 __CrossgenExe="$__CrossComponentBinDir/crossgen"
569
570 isMSBuildOnNETCoreSupported
571
572 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to set.
573 # This is needed by CLI to function.
574 if [ -z "$HOME" ]; then
575     if [ ! -d "$__ProjectDir/temp_home" ]; then
576         mkdir temp_home
577     fi
578     export HOME=$__ProjectDir/temp_home
579     echo "HOME not defined; setting it to $HOME"
580 fi
581
582 # Specify path to be set for CMAKE_INSTALL_PREFIX.
583 # This is where all built CoreClr libraries will copied to.
584 export __CMakeBinDir="$__BinDir"
585
586 if [ ! -d "$__BinDir" ] || [ ! -d "$__BinDir/bin" ]; then
587
588     echo "Has not been found built CoreCLR instance"
589     echo "Please build it before tests using './build.sh $__BuildArch $__BuildType'"
590     exit 1
591 fi
592
593 # Configure environment if we are doing a cross compile.
594 if [ $__CrossBuild == 1 ]; then
595     export CROSSCOMPILE=1
596     if ! [[ -n "$ROOTFS_DIR" ]]; then
597         export ROOTFS_DIR="$__ProjectRoot/cross/rootfs/$__BuildArch"
598     fi
599 fi
600
601 # init the target distro name
602 initTargetDistroRid
603
604 # Override tool directory
605
606 __CoreClrVersion=1.1.0
607 __sharedFxDir=$__BuildToolsDir/dotnetcli/shared/Microsoft.NETCore.App/$__CoreClrVersion/
608
609 echo "Building Tests..."
610
611 build_Tests
612
613 if [ $? -ne 0 ]; then
614     echo "Failed to build tests"
615     exit 1
616 fi
617
618 echo "${__MsgPrefix}Test build successful."
619 echo "${__MsgPrefix}Test binaries are available at ${__TestBinDir}"
620
621 __testNativeBinDir=$__IntermediatesDir/tests
622
623 if [ $__RunTests -ne 0 ]; then
624
625     echo "Run Tests..."
626
627     echo "${__TestDir}/runtest.sh --testRootDir=$__TestBinDir --coreClrBinDir=$__BinDir --coreFxBinDir=$__sharedFxDir --testNativeBinDir=$__testNativeBinDir"
628
629     $__TestDir/runtest.sh --testRootDir=$__TestBinDir --coreClrBinDir=$__BinDir --coreFxBinDir=$CORE_ROOT --testNativeBinDir=$__testNativeBinDir
630
631     echo "Tests run successful."
632 else
633     echo "To run all tests use 'tests/runtests.sh' where:"
634     echo "    testRootDir      = $__TestBinDir"
635     echo "    coreClrBinDir    = $__BinDir"
636     echo "    coreFxBinDir     = $CORE_ROOT"
637     echo "    testNativeBinDir = $__testNativeBinDir"
638     echo " -------------------------------------------------- "
639     echo "To run single test use the following command:"
640     echo "    bash ${__TestBinDir}/__TEST_PATH__/__TEST_NAME__.sh -coreroot=${CORE_ROOT}"
641 fi
642