-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathbuild-commons.sh
executable file
·454 lines (382 loc) · 14.7 KB
/
build-commons.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env bash
initTargetDistroRid()
{
source "$__RepoRootDir/eng/native/init-distro-rid.sh"
local passedRootfsDir=""
# Only pass ROOTFS_DIR if cross is specified.
if [[ "$__CrossBuild" == 1 ]]; then
passedRootfsDir="$ROOTFS_DIR"
fi
initDistroRidGlobal "$__TargetOS" "$__BuildArch" "$__PortableBuild" "$passedRootfsDir"
}
isMSBuildOnNETCoreSupported()
{
__IsMSBuildOnNETCoreSupported="$__msbuildonunsupportedplatform"
if [[ "$__IsMSBuildOnNETCoreSupported" == 1 ]]; then
return
fi
if [[ "$__SkipManaged" == 1 ]]; then
__IsMSBuildOnNETCoreSupported=0
return
fi
if [[ ( "$__HostOS" == "Linux" ) && ( "$__HostArch" == "x64" || "$__HostArch" == "arm" || "$__HostArch" == "armel" || "$__HostArch" == "arm64" ) ]]; then
__IsMSBuildOnNETCoreSupported=1
elif [[ ( "$__HostOS" == "OSX" || "$__HostOS" == "FreeBSD" ) && "$__HostArch" == "x64" ]]; then
__IsMSBuildOnNETCoreSupported=1
fi
}
setup_dirs()
{
echo Setting up directories for build
mkdir -p "$__RootBinDir"
mkdir -p "$__BinDir"
mkdir -p "$__IntermediatesDir"
}
# Check the system to ensure the right prereqs are in place
check_prereqs()
{
echo "Checking prerequisites..."
if [[ "$__HostOS" == "OSX" ]]; then
# Check presence of pkg-config on the path
command -v pkg-config 2>/dev/null || { echo >&2 "Please install pkg-config before running this script, see https://github.com/dotnet/runtime/blob/master/docs/workflow/requirements/macos-requirements.md"; exit 1; }
if ! pkg-config openssl ; then
# We export the proper PKG_CONFIG_PATH where openssl was installed by Homebrew
# It's important to _export_ it since build-commons.sh is sourced by other scripts such as build-native.sh
export PKG_CONFIG_PATH=/usr/local/opt/openssl@1.1/lib/pkgconfig:/usr/local/opt/openssl/lib/pkgconfig
# We try again with the PKG_CONFIG_PATH in place, if pkg-config still can't find OpenSSL, exit with an error, cmake won't find OpenSSL either
pkg-config openssl || { echo >&2 "Please install openssl before running this script, see https://github.com/dotnet/runtime/blob/master/docs/workflow/requirements/macos-requirements.md"; exit 1; }
fi
fi
if [[ "$__UseNinja" == 1 ]]; then
command -v ninja 2>/dev/null || command -v ninja-build 2>/dev/null || { echo "Unable to locate ninja!"; exit 1; }
fi
}
build_native()
{
platformArch="$1"
cmakeDir="$2"
tryrunDir="$3"
intermediatesDir="$4"
message="$5"
# All set to commence the build
echo "Commencing build of \"$message\" for $__TargetOS.$__BuildArch.$__BuildType in $intermediatesDir"
if [[ "$__UseNinja" == 1 ]]; then
generator="ninja"
buildTool="$(command -v ninja || command -v ninja-build)"
else
buildTool="make"
fi
runtimeVersionHeaderFile="$intermediatesDir/../runtime_version.h"
if [[ "$__SkipConfigure" == 0 ]]; then
# if msbuild is not supported, then set __SkipGenerateVersion to 1
if [[ "$__IsMSBuildOnNETCoreSupported" == 0 ]]; then __SkipGenerateVersion=1; fi
# Drop version.c file
__versionSourceFile="$intermediatesDir/version.c"
if [[ ! -z "${__LogsDir}" ]]; then
__binlogArg="-bl:\"$__LogsDir/GenNativeVersion_$__TargetOS.$__BuildArch.$__BuildType.binlog\""
fi
if [[ "$__SkipGenerateVersion" == 0 ]]; then
"$__RepoRootDir/eng/common/msbuild.sh" /clp:nosummary "$__ArcadeScriptArgs" "$__RepoRootDir"/eng/empty.csproj \
/p:NativeVersionFile="$__versionSourceFile" \
/p:RuntimeVersionFile="$runtimeVersionHeaderFile" \
/t:GenerateRuntimeVersionFile /restore \
$__CommonMSBuildArgs $__binlogArg $__UnprocessedBuildArgs
local exit_code="$?"
if [[ "$exit_code" != 0 ]]; then
echo "${__ErrMsgPrefix}Failed to generate native version file."
exit "$exit_code"
fi
else
# Generate the dummy version.c and runtime_version.h, but only if they didn't exist to make sure we don't trigger unnecessary rebuild
__versionSourceLine="static char sccsid[] __attribute__((used)) = \"@(#)No version information produced\";"
if [[ -e "$__versionSourceFile" ]]; then
read existingVersionSourceLine < "$__versionSourceFile"
fi
if [[ "$__versionSourceLine" != "$existingVersionSourceLine" ]]; then
cat << EOF > $runtimeVersionHeaderFile
#define RuntimeAssemblyMajorVersion 0
#define RuntimeAssemblyMinorVersion 0
#define RuntimeFileMajorVersion 0
#define RuntimeFileMinorVersion 0
#define RuntimeFileBuildVersion 0
#define RuntimeFileRevisionVersion 0
#define RuntimeProductMajorVersion 0
#define RuntimeProductMinorVersion 0
#define RuntimeProductPatchVersion 0
#define RuntimeProductVersion
EOF
echo "$__versionSourceLine" > "$__versionSourceFile"
fi
fi
if [[ "$__StaticAnalyzer" == 1 ]]; then
scan_build=scan-build
fi
engNativeDir="$__RepoRootDir/eng/native"
__CMakeArgs="-DCLR_ENG_NATIVE_DIR=\"$engNativeDir\" $__CMakeArgs"
nextCommand="\"$engNativeDir/gen-buildsys.sh\" \"$cmakeDir\" \"$tryrunDir\" \"$intermediatesDir\" $platformArch $__Compiler \"$__CompilerMajorVersion\" \"$__CompilerMinorVersion\" $__BuildType \"$generator\" $scan_build $__CMakeArgs"
echo "Invoking $nextCommand"
eval $nextCommand
local exit_code="$?"
if [[ "$exit_code" != 0 ]]; then
echo "${__ErrMsgPrefix}Failed to generate \"$message\" build project!"
exit "$exit_code"
fi
fi
# Check that the makefiles were created.
if [[ ! -f "$intermediatesDir/CMakeCache.txt" ]]; then
echo "${__ErrMsgPrefix}Unable to find generated build files for \"$message\" project!"
exit 1
fi
# Build
if [[ "$__ConfigureOnly" == 1 ]]; then
echo "Finish configuration & skipping \"$message\" build."
return
fi
SAVED_CFLAGS="${CFLAGS}"
SAVED_CXXFLAGS="${CXXFLAGS}"
SAVED_LDFLAGS="${LDFLAGS}"
# Let users provide additional compiler/linker flags via EXTRA_CFLAGS/EXTRA_CXXFLAGS/EXTRA_LDFLAGS.
# If users directly override CFLAG/CXXFLAGS/LDFLAGS, that may lead to some configure tests working incorrectly.
# See https://github.com/dotnet/runtime/issues/35727 for more information.
export CFLAGS="${CFLAGS} ${EXTRA_CFLAGS}"
export CXXFLAGS="${CXXFLAGS} ${EXTRA_CXXFLAGS}"
export LDFLAGS="${LDFLAGS} ${EXTRA_LDFLAGS}"
if [[ "$__StaticAnalyzer" == 1 ]]; then
pushd "$intermediatesDir"
buildTool="$SCAN_BUILD_COMMAND -o $__BinDir/scan-build-log $buildTool"
echo "Executing $buildTool install -j $__NumProc"
"$buildTool" install -j "$__NumProc"
popd
else
cmake_command=cmake
if [[ "$build_arch" == "wasm" ]]; then
cmake_command="emcmake $cmake_command"
fi
echo "Executing $cmake_command --build \"$intermediatesDir\" --target install -- -j $__NumProc"
$cmake_command --build "$intermediatesDir" --target install -- -j "$__NumProc"
fi
CFLAGS="${SAVED_CFLAGS}"
CXXFLAGS="${SAVED_CXXFLAGS}"
LDFLAGS="${SAVED_LDFLAGS}"
local exit_code="$?"
if [[ "$exit_code" != 0 ]]; then
echo "${__ErrMsgPrefix}Failed to build \"$message\"."
exit "$exit_code"
fi
}
usage()
{
echo "Usage: $0 <options>"
echo ""
echo "Common Options:"
echo ""
echo "BuildArch can be: -arm, -armel, -arm64, x64, x86, -wasm"
echo "BuildType can be: -debug, -checked, -release"
echo "-os: target OS (defaults to running OS)"
echo "-bindir: output directory (defaults to $__ProjectRoot/artifacts)"
echo "-ci: indicates if this is a CI build."
echo "-clang: optional argument to build using clang in PATH (default)."
echo "-clangx.y: optional argument to build using clang version x.y."
echo "-cmakeargs: user-settable additional arguments passed to CMake."
echo "-configureonly: do not perform any builds; just configure the build."
echo "-cross: optional argument to signify cross compilation,"
echo " will use ROOTFS_DIR environment variable if set."
echo "-gcc: optional argument to build using gcc in PATH."
echo "-gccx.y: optional argument to build using gcc version x.y."
echo "-msbuildonunsupportedplatform: build managed binaries even if distro is not officially supported."
echo "-ninja: target ninja instead of GNU make"
echo "-numproc: set the number of build processes."
echo "-portablebuild: pass -portablebuild=false to force a non-portable build."
echo "-skipconfigure: skip build configuration."
echo "-skipgenerateversion: disable version generation even if MSBuild is supported."
echo "-verbose: optional argument to enable verbose build output."
echo ""
echo "Additional Options:"
echo ""
for i in "${!usage_list[@]}"; do
echo "${usage_list[${i}]}"
done
echo ""
exit 1
}
source "$__RepoRootDir/eng/native/init-os-and-arch.sh"
__BuildArch=$arch
__HostArch=$arch
__TargetOS=$os
__HostOS=$os
__BuildOS=$os
__msbuildonunsupportedplatform=0
# 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.
platform="$(uname)"
if [[ "$platform" == "FreeBSD" ]]; then
__NumProc=$(sysctl hw.ncpu | awk '{ print $2+1 }')
elif [[ "$platform" == "NetBSD" || "$platform" == "SunOS" ]]; then
__NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
elif [[ "$platform" == "Darwin" ]]; then
__NumProc=$(($(getconf _NPROCESSORS_ONLN)+1))
else
__NumProc=$(nproc --all)
fi
while :; do
if [[ "$#" -le 0 ]]; then
break
fi
lowerI="$(echo "$1" | awk '{print tolower($0)}')"
case "$lowerI" in
-\?|-h|--help)
usage
exit 1
;;
arm|-arm)
__BuildArch=arm
;;
arm64|-arm64)
__BuildArch=arm64
;;
armel|-armel)
__BuildArch=armel
;;
bindir|-bindir)
if [[ -n "$2" ]]; then
__RootBinDir="$2"
if [[ ! -d "$__RootBinDir" ]]; then
mkdir "$__RootBinDir"
fi
__RootBinParent=$(dirname "$__RootBinDir")
__RootBinName="${__RootBinDir##*/}"
__RootBinDir="$(cd "$__RootBinParent" &>/dev/null && printf %s/%s "$PWD" "$__RootBinName")"
shift
else
echo "ERROR: 'bindir' requires a non-empty option argument"
exit 1
fi
;;
checked|-checked)
__BuildType=Checked
;;
ci|-ci)
__ArcadeScriptArgs="--ci"
__ErrMsgPrefix="##vso[task.logissue type=error]"
;;
clang*|-clang*)
__Compiler=clang
# clangx.y or clang-x.y
version="$(echo "$lowerI" | tr -d '[:alpha:]-=')"
parts=(${version//./ })
__CompilerMajorVersion="${parts[0]}"
__CompilerMinorVersion="${parts[1]}"
if [[ -z "$__CompilerMinorVersion" && "$__CompilerMajorVersion" -le 6 ]]; then
__CompilerMinorVersion=0;
fi
;;
cmakeargs|-cmakeargs)
if [[ -n "$2" ]]; then
__CMakeArgs="$2 $__CMakeArgs"
shift
else
echo "ERROR: 'cmakeargs' requires a non-empty option argument"
exit 1
fi
;;
configureonly|-configureonly)
__ConfigureOnly=1
__SkipMSCorLib=1
__SkipNuget=1
;;
cross|-cross)
__CrossBuild=1
;;
debug|-debug)
__BuildType=Debug
;;
gcc*|-gcc*)
__Compiler=gcc
# gccx.y or gcc-x.y
version="$(echo "$lowerI" | tr -d '[:alpha:]-=')"
parts=(${version//./ })
__CompilerMajorVersion="${parts[0]}"
__CompilerMinorVersion="${parts[1]}"
;;
msbuildonunsupportedplatform|-msbuildonunsupportedplatform)
__msbuildonunsupportedplatform=1
;;
ninja|-ninja)
__UseNinja=1
;;
numproc|-numproc)
if [[ -n "$2" ]]; then
__NumProc="$2"
shift
else
echo "ERROR: 'numproc' requires a non-empty option argument"
exit 1
fi
;;
portablebuild=false|-portablebuild=false)
__PortableBuild=0
;;
release|-release)
__BuildType=Release
;;
skipconfigure|-skipconfigure)
__SkipConfigure=1
;;
skipgenerateversion|-skipgenerateversion)
__SkipGenerateVersion=1
;;
verbose|-verbose)
__VerboseBuild=1
;;
x86|-x86)
__BuildArch=x86
;;
x64|-x64)
__BuildArch=x64
;;
wasm|-wasm)
__BuildArch=wasm
;;
os|-os)
if [[ -n "$2" ]]; then
__TargetOS="$2"
shift
else
echo "ERROR: 'os' requires a non-empty option argument"
exit 1
fi
;;
*)
handle_arguments "$1" "$2"
if [[ "$__ShiftArgs" == 1 ]]; then
shift
__ShiftArgs=0
fi
;;
esac
shift
done
__CommonMSBuildArgs="/p:TargetArchitecture=$__BuildArch /p:Configuration=$__BuildType /p:TargetOS=$__TargetOS /nodeReuse:false $__OfficialBuildIdArg $__SignTypeArg $__SkipRestoreArg"
# Configure environment if we are doing a verbose build
if [[ "$__VerboseBuild" == 1 ]]; then
VERBOSE=1
export VERBOSE
__CommonMSBuildArgs="$__CommonMSBuildArgs /v:detailed"
fi
if [[ "$__PortableBuild" == 0 ]]; then
__CommonMSBuildArgs="$__CommonMSBuildArgs /p:PortableBuild=false"
fi
# Configure environment if we are doing a cross compile.
if [[ "$__CrossBuild" == 1 ]]; then
CROSSCOMPILE=1
export CROSSCOMPILE
if [[ ! -n "$ROOTFS_DIR" ]]; then
ROOTFS_DIR="$__RepoRootDir/.tools/rootfs/$__BuildArch"
export ROOTFS_DIR
fi
fi
# init the target distro name
initTargetDistroRid
# Init if MSBuild for .NET Core is supported for this platform
isMSBuildOnNETCoreSupported