This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
build.sh
executable file
·367 lines (299 loc) · 10 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env bash
usage()
{
echo "Usage: $0 [managed] [native] [BuildArch] [BuildType] [clean] [verbose] [clangx.y] [platform]"
echo "managed - optional argument to build the managed code"
echo "native - optional argument to build the native code"
echo "The following arguments affect native builds only:"
echo "BuildArch can be: x64, arm"
echo "BuildType can be: Debug, Release"
echo "clean - optional argument to force a clean build."
echo "verbose - optional argument to enable verbose build output."
echo "clangx.y - optional argument to build using clang version x.y."
echo "platform can be: Windows, Linux, OSX, FreeBSD"
exit 1
}
setup_dirs()
{
echo Setting up directories for build
mkdir -p "$__BinDir"
mkdir -p "$__IntermediatesDir"
}
# Performs "clean build" type actions (deleting and remaking directories)
clean()
{
echo "Cleaning previous output for the selected configuration"
rm -rf "$__BinDir"
rm -rf "$__IntermediatesDir"
setup_dirs
}
# Check the system to ensure the right pre-reqs are in place
check_managed_prereqs()
{
__monoversion=$(mono --version | grep "version 4.[1-9]")
if [ $? -ne 0 ]; then
# if built from tarball, mono only identifies itself as 4.0.1
__monoversion=$(mono --version | egrep "version 4.0.[1-9]+(.[0-9]+)?")
if [ $? -ne 0 ]; then
echo "Mono 4.0.1.44 or later is required to build corefx. Please see https://github.com/dotnet/corefx/blob/master/Documentation/building/unix-instructions.md for more details."
exit 1
else
echo "WARNING: Mono 4.0.1.44 or later is required to build corefx. Unable to assess if current version is supported."
fi
fi
if [ ! -e "$__referenceassemblyroot/.NETPortable" ]; then
echo "PCL reference assemblies not found. Please see https://github.com/dotnet/corefx/blob/master/Documentation/building/unix-instructions.md for more details."
exit 1
fi
}
check_native_prereqs()
{
echo "Checking pre-requisites..."
# Check presence of CMake on the path
hash cmake 2>/dev/null || { echo >&2 "Please install cmake before running this script"; exit 1; }
# Check for clang
hash clang-$__ClangMajorVersion.$__ClangMinorVersion 2>/dev/null || hash clang$__ClangMajorVersion$__ClangMinorVersion 2>/dev/null || hash clang 2>/dev/null || { echo >&2 "Please install clang before running this script"; exit 1; }
}
# Prepare the system for building
prepare_managed_build()
{
# Pull NuGet.exe down if we don't have it already
if [ ! -e "$__nugetpath" ]; then
which curl wget > /dev/null 2> /dev/null
if [ $? -ne 0 -a $? -ne 1 ]; then
echo "cURL or wget is required to build corefx. Please see https://github.com/dotnet/corefx/blob/master/Documentation/building/unix-instructions.md for more details."
exit 1
fi
echo "Restoring NuGet.exe..."
# curl has HTTPS CA trust-issues less often than wget, so lets try that first.
which curl > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
mkdir -p $__packageroot
wget -q -O $__nugetpath https://api.nuget.org/downloads/nuget.exe
else
curl -sSL --create-dirs -o $__nugetpath https://api.nuget.org/downloads/nuget.exe
fi
if [ $? -ne 0 ]; then
echo "Failed to restore NuGet.exe."
exit 1
fi
fi
# Grab the MSBuild package if we don't have it already
if [ ! -e "$__msbuildpath" ]; then
echo "Restoring MSBuild..."
mono "$__nugetpath" install $__msbuildpackageid -Version $__msbuildpackageversion -source "https://www.myget.org/F/dotnet-buildtools/" -OutputDirectory "$__packageroot"
if [ $? -ne 0 ]; then
echo "Failed to restore MSBuild."
exit 1
fi
fi
}
prepare_native_build()
{
# Specify path to be set for CMAKE_INSTALL_PREFIX.
# This is where all built CoreClr libraries will copied to.
export __CMakeBinDir="$__BinDir"
# Configure environment if we are doing a clean build.
if [ $__CleanBuild == 1 ]; then
clean
fi
# Configure environment if we are doing a verbose build
if [ $__VerboseBuild == 1 ]; then
export VERBOSE=1
fi
}
build_managed_corefx()
{
__buildproj=$__scriptpath/build.proj
__buildlog=$__scriptpath/msbuild.log
MONO29679=1 ReferenceAssemblyRoot=$__referenceassemblyroot mono $__msbuildpath "$__buildproj" /nologo /verbosity:minimal "/fileloggerparameters:Verbosity=normal;LogFile=$__buildlog" /t:Build /p:OSGroup=$__BuildOS /p:UseRoslynCompiler=true /p:COMPUTERNAME=$(hostname) /p:USERNAME=$(id -un) "$@"
BUILDERRORLEVEL=$?
echo
# Pull the build summary from the log file
tail -n 4 "$__buildlog"
echo Build Exit Code = $BUILDERRORLEVEL
}
build_native_corefx()
{
# All set to commence the build
echo "Commencing build of corefx native components for $__BuildOS.$__BuildArch.$__BuildType"
cd "$__IntermediatesDir"
# Regenerate the CMake solution
echo "Invoking cmake with arguments: \"$__nativeroot\" $__CMakeArgs"
"$__nativeroot/gen-buildsys-clang.sh" "$__nativeroot" $__ClangMajorVersion $__ClangMinorVersion $__CMakeArgs
# Check that the makefiles were created.
if [ ! -f "$__IntermediatesDir/Makefile" ]; then
echo "Failed to generate native component build project!"
exit 1
fi
# Get the number of processors available to the scheduler
# Other techniques such as `nproc` only get the number of
# processors available to a single process.
if [ `uname` = "FreeBSD" ]; then
NumProc=`sysctl hw.ncpu | awk '{ print $2+1 }'`
else
NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
fi
# Build
echo "Executing make install -j $NumProc $__UnprocessedBuildArgs"
make install -j $NumProc $__UnprocessedBuildArgs
if [ $? != 0 ]; then
echo "Failed to build corefx native components."
exit 1
fi
echo "CoreFX native components successfully built."
echo "Product binaries are available at $__BinDir"
}
__scriptpath=$(cd "$(dirname "$0")"; pwd -P)
__nativeroot=$__scriptpath/src/Native
__packageroot=$__scriptpath/packages
__sourceroot=$__scriptpath/src
__nugetpath=$__packageroot/NuGet.exe
__nugetconfig=$__sourceroot/NuGet.Config
__rootbinpath="$__scriptpath/bin"
__msbuildpackageid="Microsoft.Build.Mono.Debug"
__msbuildpackageversion="14.1.0.0-prerelease"
__msbuildpath=$__packageroot/$__msbuildpackageid.$__msbuildpackageversion/lib/MSBuild.exe
__BuildArch=x64
__buildmanaged=false
__buildnative=false
# Use uname to determine what the OS is.
OSName=$(uname -s)
case $OSName in
Linux)
__HostOS=Linux
;;
Darwin)
__HostOS=OSX
;;
FreeBSD)
__HostOS=FreeBSD
;;
*)
echo "Unsupported OS $OSName detected, configuring as if for Linux"
__HostOS=Linux
;;
esac
__BuildOS=$__HostOS
__BuildType=Debug
__CMakeArgs=DEBUG
case $__HostOS in
FreeBSD)
__monoroot=/usr/local
;;
OSX)
__monoroot=/Library/Frameworks/Mono.framework/Versions/Current
;;
*)
__monoroot=/usr
;;
esac
__referenceassemblyroot=$__monoroot/lib/mono/xbuild-frameworks
BUILDERRORLEVEL=0
# Set the various build properties here so that CMake and MSBuild can pick them up
__UnprocessedBuildArgs=
__CleanBuild=false
__VerboseBuild=false
__ClangMajorVersion=3
__ClangMinorVersion=5
for i in "$@"
do
lowerI="$(echo $i | awk '{print tolower($0)}')"
case $lowerI in
-?|-h|--help)
usage
exit 1
;;
managed)
__buildmanaged=true
;;
native)
__buildnative=true
;;
x64)
__BuildArch=x64
__MSBuildBuildArch=x64
;;
arm)
__BuildArch=arm
__MSBuildBuildArch=arm
;;
debug)
__BuildType=Debug
;;
release)
__BuildType=Release
__CMakeArgs=RELEASE
;;
clean)
__CleanBuild=1
;;
verbose)
__VerboseBuild=1
;;
clang3.5)
__ClangMajorVersion=3
__ClangMinorVersion=5
;;
clang3.6)
__ClangMajorVersion=3
__ClangMinorVersion=6
;;
clang3.7)
__ClangMajorVersion=3
__ClangMinorVersion=7
;;
windows)
__BuildOS=Windows_NT
;;
linux)
__BuildOS=Linux
;;
osx)
__BuildOS=OSX
;;
freebsd)
__BuildOS=FreeBSD
;;
*)
__UnprocessedBuildArgs="$__UnprocessedBuildArgs $i"
esac
done
# If neither managed nor native are passed as arguments, default to building both
if [ "$__buildmanaged" = false -a "$__buildnative" = false ]; then
__buildmanaged=true
__buildnative=true
fi
# Disable the native build when targeting Windows.
if [ "$__BuildOS" != "$__HostOS" ]; then
echo "Warning: cross compiling native components is not yet supported"
__buildnative=false
fi
# Set the remaining variables based upon the determined build configuration
__IntermediatesDir="$__rootbinpath/obj/$__BuildOS.$__BuildArch.$__BuildType/Native"
__BinDir="$__rootbinpath/$__BuildOS.$__BuildArch.$__BuildType/Native"
# Make the directories necessary for build if they don't exist
setup_dirs
if $__buildmanaged; then
# Check prereqs.
check_managed_prereqs
# Prepare the system
prepare_managed_build
# Build the corefx native components.
build_managed_corefx
# Build complete
fi
# If managed build failed, exit with the status code of the managed build
if [ $BUILDERRORLEVEL != 0 ]; then
exit $BUILDERRORLEVEL
fi
if $__buildnative; then
# Check prereqs.
check_native_prereqs
# Prepare the system
prepare_native_build
# Build the corefx native components.
build_native_corefx
# Build complete
fi
exit $BUILDERRORLEVEL