Fix unset ZapRelocationType for fixup (#18589)
[platform/upstream/coreclr.git] / Tools / init-tools.sh
1 #!/usr/bin/env bash
2
3 # Stop script on NZEC
4 set -e
5 # Stop script if unbound variable found (use ${var:-} if intentional)
6 set -u
7 # By default cmd1 | cmd2 returns exit code of cmd2 regardless of cmd1 success
8 # This is causing it to fail
9 set -o pipefail
10
11 __PROJECT_DIR=${1:-}
12 __DOTNET_CMD=${2:-}
13 __TOOLRUNTIME_DIR=${3:-}
14 __PACKAGES_DIR=${4:-$__TOOLRUNTIME_DIR}
15 __TOOLS_DIR=$(cd "$(dirname "$0")"; pwd -P)
16 __MICROBUILD_VERSION=0.2.0
17 __PORTABLETARGETS_VERSION=0.1.1-dev
18 __ROSLYNCOMPILER_VERSION=2.8.0-beta2-62719-08
19
20 __PORTABLETARGETS_PROJECT_CONTENT="
21 <Project Sdk=\"Microsoft.NET.Sdk\">
22   <PropertyGroup>
23     <TargetFrameworks>netcoreapp1.0;net46</TargetFrameworks>
24     <DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
25   </PropertyGroup>
26   <ItemGroup>
27     <PackageReference Include=\"MicroBuild.Core\" Version=\"$__MICROBUILD_VERSION\" />
28     <PackageReference Include=\"Microsoft.Portable.Targets\" Version=\"$__PORTABLETARGETS_VERSION\" />
29     <PackageReference Include=\"Microsoft.NETCore.Compilers\" Version=\"$__ROSLYNCOMPILER_VERSION\" />
30   </ItemGroup>
31 </Project>"
32 __PUBLISH_TFM=netcoreapp2.0
33
34 __INIT_TOOLS_RESTORE_ARGS="--source https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json --source https://api.nuget.org/v3/index.json ${__INIT_TOOLS_RESTORE_ARGS:-}"
35 __TOOLRUNTIME_RESTORE_ARGS="--source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json ${__INIT_TOOLS_RESTORE_ARGS}"
36
37 if [ ! -d "$__PROJECT_DIR" ]; then
38     echo "ERROR: Cannot find project root path at '$__PROJECT_DIR'. Please pass in the source directory as the 1st parameter."
39     exit 1
40 fi
41
42 if [ ! -e "$__DOTNET_CMD" ]; then
43     echo "ERROR: Cannot find dotnet.exe at path '$__DOTNET_CMD'. Please pass in the path to dotnet.exe as the 2nd parameter."
44     exit 1
45 fi
46
47 if [ -z "$__TOOLRUNTIME_DIR" ]; then
48     echo "ERROR: Please pass in the tools directory as the 3rd parameter."
49     exit 1
50 fi
51
52 if [ ! -d "$__TOOLRUNTIME_DIR" ]; then
53     mkdir $__TOOLRUNTIME_DIR
54 fi
55
56 cp -R $__TOOLS_DIR/* $__TOOLRUNTIME_DIR
57
58 __TOOLRUNTIME_PROJECT=$__TOOLS_DIR/tool-runtime/project.csproj
59
60 echo "Running: $__DOTNET_CMD restore \"${__TOOLRUNTIME_PROJECT}\" $__TOOLRUNTIME_RESTORE_ARGS"
61 $__DOTNET_CMD restore "${__TOOLRUNTIME_PROJECT}" $__TOOLRUNTIME_RESTORE_ARGS
62
63 echo "Running: $__DOTNET_CMD publish --no-restore \"${__TOOLRUNTIME_PROJECT}\" -f ${__PUBLISH_TFM} -o $__TOOLRUNTIME_DIR"
64 $__DOTNET_CMD publish --no-restore "${__TOOLRUNTIME_PROJECT}" -f ${__PUBLISH_TFM} -o $__TOOLRUNTIME_DIR
65
66 # Copy Portable Targets Over to ToolRuntime
67 if [ ! -d "${__PACKAGES_DIR}/generated" ]; then mkdir "${__PACKAGES_DIR}/generated"; fi
68 __PORTABLETARGETS_PROJECT=${__PACKAGES_DIR}/generated/project.csproj
69
70 echo $__PORTABLETARGETS_PROJECT_CONTENT > "${__PORTABLETARGETS_PROJECT}"
71
72 echo "Running: \"$__DOTNET_CMD\" restore \"${__PORTABLETARGETS_PROJECT}\" $__INIT_TOOLS_RESTORE_ARGS --packages \"${__PACKAGES_DIR}/.\""
73 $__DOTNET_CMD restore "${__PORTABLETARGETS_PROJECT}" $__INIT_TOOLS_RESTORE_ARGS --packages "${__PACKAGES_DIR}/."
74
75 # Copy portable and MicroBuild targets from packages, allowing for lowercased package IDs.
76 cp -R "${__PACKAGES_DIR}"/[Mm]icrosoft.[Pp]ortable.[Tt]argets/"${__PORTABLETARGETS_VERSION}/contentFiles/any/any/Extensions/." "$__TOOLRUNTIME_DIR/."
77 cp -R "${__PACKAGES_DIR}"/[Mm]icro[Bb]uild.[Cc]ore/"${__MICROBUILD_VERSION}/build/." "$__TOOLRUNTIME_DIR/."
78
79 # Temporary Hacks to fix couple of issues in the msbuild and roslyn nuget packages
80 # https://github.com/dotnet/buildtools/issues/1464
81 [ -e "$__TOOLRUNTIME_DIR/Microsoft.CSharp.Targets" ] || mv "$__TOOLRUNTIME_DIR/Microsoft.CSharp.targets" "$__TOOLRUNTIME_DIR/Microsoft.CSharp.Targets"
82
83 # Copy some roslyn files over
84 cp $__TOOLRUNTIME_DIR/runtimes/any/native/* $__TOOLRUNTIME_DIR/
85
86 #Temporarily rename roslyn compilers to have exe extension
87 cp ${__TOOLRUNTIME_DIR}/csc.dll ${__TOOLRUNTIME_DIR}/csc.exe
88 cp ${__TOOLRUNTIME_DIR}/vbc.dll ${__TOOLRUNTIME_DIR}/vbc.exe
89
90 #Copy RID specific assets to the tools dir since we don't have a deps.json for .NETCore msbuild
91 cp ${__TOOLRUNTIME_DIR}/runtimes/unix/lib/netstandard1.3/*.dll $__TOOLRUNTIME_DIR/
92
93 # Override versions in runtimeconfig.json files with highest available runtime version.
94 __MNCA_FOLDER=$(dirname $__DOTNET_CMD)/shared/Microsoft.NETCore.App
95 __HIGHEST_RUNTIME_VERSION=`ls $__MNCA_FOLDER | sed 'r/\([0-9]\+\).*/\1/g' | sort -n | tail -1`
96 sed -i -e "s/1.1.0/$__HIGHEST_RUNTIME_VERSION/g" $__TOOLRUNTIME_DIR/*.runtimeconfig.json
97
98 # Restore ILAsm, if requested in the environment.
99 __ILASM_PACKAGE_VERSION="${ILASMCOMPILER_VERSION:-}"
100 if [ "$__ILASM_PACKAGE_VERSION" ]; then
101     echo "Restoring ILAsm version '$__ILASM_PACKAGE_VERSION'..."
102
103     __ILASM_PACKAGE_RID="${NATIVE_TOOLS_RID:-}"
104     if [ "$__ILASM_PACKAGE_RID" == "" ]; then
105         echo "ERROR: Please specify native package RID."
106         exit 1
107     fi
108
109     echo "Running: \"$__DOTNET_CMD\" build \"${__TOOLRUNTIME_DIR}/ilasm/ilasm.depproj\""
110     $__DOTNET_CMD build "${__TOOLRUNTIME_DIR}/ilasm/ilasm.depproj" --packages "${__PACKAGES_DIR}/." --source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json -r $__ILASM_PACKAGE_RID -p:ILAsmPackageVersion=$__ILASM_PACKAGE_VERSION
111 fi
112
113 # Download the package version props file, if passed in the environment.
114 __PACKAGE_VERSION_PROPS_URL="${PACKAGEVERSIONPROPSURL:-}"
115 __PACKAGE_VERSION_PROPS_PATH="$__TOOLRUNTIME_DIR/DownloadedPackageVersions.props"
116
117 if [ "$__PACKAGE_VERSION_PROPS_URL" ]; then
118     echo "Downloading package version props from '$__PACKAGE_VERSION_PROPS_URL' to '$__PACKAGE_VERSION_PROPS_PATH'..."
119
120     # Copied from CoreFX init-tools.sh
121     if command -v curl > /dev/null; then
122         echo "Using curl to download the the package version props"
123         curl --retry 10 -sSL --create-dirs -o "$__PACKAGE_VERSION_PROPS_PATH" "$__PACKAGE_VERSION_PROPS_URL"
124         exit_Code=$?
125         download_Method="curl"
126     else
127         echo "Using wget to download the the package version props"
128         wget -q -O "$__PACKAGE_VERSION_PROPS_PATH" "$__PACKAGE_VERSION_PROPS_URL"
129         exit_Code=$?
130         download_Method="wget"
131     fi
132
133     if [ $exit_Code -ne 0 ]; then
134         echo "$download_Method returned exit code $exit_Code"
135     fi
136
137     echo "Successfully downloaded package version props:"
138     cat "$__PACKAGE_VERSION_PROPS_PATH"
139 fi
140
141 exit 0