Merge pull request #11054 from wtgodbe/spacing
[platform/upstream/coreclr.git] / tests / setup-runtime-dependencies.sh
1 #!/usr/bin/env bash
2 # set -x
3
4 #
5 # Constants
6 #
7 readonly EXIT_CODE_SUCCESS=0
8
9 #
10 # This script should be located in coreclr/tests.
11 #
12
13 function print_usage {
14     echo ''
15     echo 'Download coredistools for GC stress testing'
16     echo ''
17     echo 'Command line:'
18     echo ''
19     echo './setup-gcstress.sh --outputDir=<coredistools_lib_install_path>'
20     echo ''
21     echo 'Required arguments:'
22     echo '  --outputDir=<path>         : Directory to install libcoredistools.so'
23     echo ''
24 }
25
26 # temorary directory 
27 tmpDirPath=
28
29 function exit_with_error {
30     local errorCode=$1
31     local errorMsg=$2
32
33     if [ ! -z "$2" ]; then
34         echo $2
35     fi
36     
37     if [ -e $tmpDirPath ]; then
38         rm -rf $tmpDirPath
39     fi
40     
41     exit $errorCode
42 }
43
44 function handle_ctrl_c {
45     exit_with_error 1 'Aborted by Ctrl+C'
46  }
47
48 # Register the Ctrl-C handler
49 trap handle_ctrl_c INT
50
51 # Argument variables
52 libInstallDir=
53
54 # Handle arguments
55 verbose=0
56 for i in "$@"
57 do
58     case $i in
59         -h|--help)
60             exit $EXIT_CODE_SUCCESS
61             ;;
62         -v|--verbose)
63             verbose=1
64             ;;
65         --outputDir=*)
66             libInstallDir=${i#*=}
67             ;;
68         *)
69             echo "Unknown switch: $i"
70             print_usage
71             exit $EXIT_CODE_SUCCESS
72             ;;
73     esac
74 done
75
76 if [ -z "$libInstallDir" ]; then
77     echo "--libInstallDir is required."
78     print_usage
79     exit_with_error 1
80 fi
81
82 # create temp directory
83 tmpDirPath=`mktemp -d`
84 if [ ! -e $tmpDirPath ]; then
85     exit_with_error 1 "Cannot create a temporary directory"
86 fi
87
88 # This script must be located in coreclr/tests.
89 scriptDir=$(cd "$(dirname "$0")"; pwd -P)
90
91 echo "Running init-tools.sh"
92 $scriptDir/../init-tools.sh
93
94 dotnetToolsDir=$scriptDir/../Tools
95 dotnetCmd=${dotnetToolsDir}/dotnetcli/dotnet
96 packageDir=${scriptDir}/../packages
97 jsonFilePath=${tmpDirPath}/project.json
98
99 # Check tool directory
100 if [ ! -e $dotnetToolsDir ]; then
101     exit_with_error 1 'Directory containing dotnet commandline does not exist:'$dotnetToolsDir 
102 fi
103 if [ ! -e $dotnetCmd ]; then
104     exit_with_error 1 'dotnet commandline does not exist:'$dotnetCmd
105 fi
106
107 # make package directory
108 if [ ! -e $packageDir ]; then
109     mkdir -p $packageDir
110 fi
111
112 # make output directory
113 if [ ! -e $libInstallDir ]; then
114     mkdir -p $libInstallDir
115 fi
116
117 # Query runtime Id
118 rid=`$dotnetCmd --info | grep 'RID:' | sed 's/^ *RID: *//g'`  
119 if [ -z "$rid" ]; then
120     exit_with_error 1 "Failed to query runtime Id"
121 fi    
122
123 # Write dependency information to project.json
124 packageName='runtime.'$rid'.Microsoft.NETCore.CoreDisTools'
125 echo {  \
126     \"dependencies\": { \
127     \"$packageName\": \"1.0.1-prerelease-*\" \
128     }, \
129     \"frameworks\": { \"dnxcore50\": { } } \
130     } > $jsonFilePath
131
132 # Download the package
133 echo Downloading CoreDisTools package
134 bash -c -x "$dotnetCmd restore $jsonFilePath --source https://dotnet.myget.org/F/dotnet-core/ --packages $packageDir"
135 if [ $? -ne 0 ]
136 then
137     exit_with_error 1 "Failed to restore the package"
138 fi
139
140 # Get library path
141 libPath=`find $packageDir | grep $rid | grep -m 1 libcoredistools`
142 if [ ! -e $libPath ]; then
143     exit_with_error 1 'Failed to locate the downloaded library'
144 fi
145
146 # Copy library to output directory
147 echo 'Copy library:' $libPath '-->' $libInstallDir/
148 cp -f $libPath $libInstallDir
149 if [ $? -ne 0 ]
150 then
151     exit_with_error 1 "Failed to copy the library"
152 fi
153
154 # Delete temporary files
155 rm -rf $tmpDirPath
156
157 # Return success
158 exit $EXIT_CODE_SUCCESS
159