forked from Bots-United/metamod-p
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAMBuildScript
More file actions
330 lines (312 loc) · 10.9 KB
/
Copy pathAMBuildScript
File metadata and controls
330 lines (312 loc) · 10.9 KB
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
# AMBuildScript for metamod-p
# vim: set sts=4 ts=8 sw=4 tw=99 et ft=python:
import os, sys
# ---------------------------------------------------------------------------
# Architecture detection
# ---------------------------------------------------------------------------
# The target architecture is driven by the AM_ARCH environment variable,
# which is set by the CI workflow (or by the developer on the command line).
# Supported values: 'x86' (32-bit) and 'x86_64' (64-bit AMD64).
# Defaults to 'x86' for backwards compatibility with GoldSrc servers that
# still run the 32-bit HLDS binary.
#
# Note: Valve shipped an hlds_amd64 binary and 64-bit game DLLs (e.g.
# cs_amd64.so) around 2006-2007. While that branch was never officially
# supported long-term, some servers still run it, and some plugin authors
# maintain AMD64 compatibility. Ubuntu/Debian have also dropped first-class
# i386 support in recent releases, making a native AMD64 build preferable on
# modern distros.
# ---------------------------------------------------------------------------
am_arch = os.environ.get('AM_ARCH', 'x86')
if am_arch not in ('x86', 'x86_64'):
raise Exception('AM_ARCH must be x86 or x86_64, got: ' + am_arch)
is_x86 = (am_arch == 'x86')
is_amd64 = (am_arch == 'x86_64')
# Map our arch name to what AMBuild's DetectCxx expects.
target_arch = 'x86' if is_x86 else 'x86_64'
builder.cxx = builder.DetectCxx(target_arch = target_arch)
# Include search paths
hlsdk_path = os.path.join(builder.currentSourcePath, 'hlsdk')
include_paths = [
os.path.join(hlsdk_path, 'common'),
os.path.join(hlsdk_path, 'dlls'),
os.path.join(hlsdk_path, 'engine'),
os.path.join(hlsdk_path, 'pm_shared'),
os.path.join(builder.currentSourcePath, 'metamod'),
]
builder.cxx.defines += ['AMBUILD', '__METAMOD_BUILD__']
# Add any extra C++/C flags that were passed.
if builder.options.extra_cppflags:
flags = builder.options.extra_cppflags.split()
builder.cxx.cflags += flags
builder.cxx.cxxflags += flags
# Compiler setup
if builder.cxx.target.platform == 'linux':
# Linux defines
builder.cxx.defines += ['_LINUX', 'POSIX', 'LINUX', 'linux', '_FORTIFY_SOURCE=2']
# Linux compiler C flags (shared between x86 and AMD64)
builder.cxx.cflags += [
'-pipe',
'-msse',
'-msse2',
# -mfpmath=sse is only meaningful on x86 where the default FPU is x87.
# On x86_64 SSE2 is always the FP unit so the flag is redundant (and
# some GCC versions warn about it).
]
if is_x86:
builder.cxx.cflags += [
'-mfpmath=sse',
# GoldSrc engine (HLDS) and many old mods were compiled with MSVC 6/7
# which only guaranteed 4-byte (2^2) stack alignment, not the standard
# x86 ABI 16-byte alignment. Tell GCC to assume the worst so it never
# emits movaps/movdqa with an alignment assumption that HLDS can violate,
# which would cause a SIGBUS or silent data corruption.
# AMD64 always guarantees 16-byte alignment at call boundaries so this
# flag is x86-only.
# Stack alignment flags for GoldSrc i386 compatibility.
# HLDS and old GoldSrc mods were built with MSVC 6/7 which only
# guaranteed 4-byte (2^2) stack alignment, not the modern 16-byte ABI.
#
# -mincoming-stack-boundary=2: assume the caller only provides 4-byte
# alignment on entry. Prevents GCC from emitting movaps/movdqa in
# function prologues before any re-alignment has occurred.
#
# -mpreferred-stack-boundary=2: maintain 4-byte alignment for outgoing
# calls rather than padding to 16 bytes. Eliminates the 'and $-16,
# %esp' prologue that wastes stack space and instructions.
#
# -mstackrealign: insert a realignment sequence at the entry of any
# function that uses aligned SSE loads/stores, so SSE code is safe
# even when the caller violated the alignment contract. Small
# per-function overhead but eliminates the whole class of alignment
# faults from SSE instructions.
#
# AMD64 mandates 16-byte alignment at every call boundary in the
# System V ABI so none of these flags apply there. [APG]RoboCop[CL]
'-mincoming-stack-boundary=2',
'-mpreferred-stack-boundary=2',
'-mstackrealign',
# With reduced stack alignment assumptions, -Wcast-align warnings about
# casting to more strictly aligned pointer types become expected and
# intentional rather than bugs to fix — suppress for x86 only. [APG]RoboCop[CL]
'-Wno-cast-align',
]
builder.cxx.cflags += [
'-fno-strict-aliasing',
'-fno-exceptions',
'-fno-rtti',
'-fvisibility=hidden',
'-fvisibility-inlines-hidden',
'-ffunction-sections',
'-fdata-sections',
'-fstack-protector-strong',
'-fstack-clash-protection',
'-fcf-protection',
'-Wall',
#'-Werror',
'-Wextra',
'-Wshadow',
'-Wnon-virtual-dtor',
#'-Wold-style-cast',
'-Wcast-align',
'-Wunused',
'-Woverloaded-virtual',
'-Wpedantic',
'-Wconversion',
'-Wsign-conversion',
'-Wnull-dereference',
'-Wdouble-promotion',
'-Wformat=2',
'-Wformat-security',
'-Wno-uninitialized',
'-Wno-unused',
'-Wno-switch',
]
# Architecture word-size flag
if is_x86:
builder.cxx.cflags += ['-m32']
else:
builder.cxx.cflags += ['-m64']
# GCC-specific warning suppressions
if builder.cxx.like('gcc'):
builder.cxx.cflags += [
'-Wno-attributes',
'-Wno-dangling-pointer',
'-Wno-stringop-overflow',
'-fno-aggressive-loop-optimizations',
]
# Clang-specific warning suppressions
if builder.cxx.like('clang'):
builder.cxx.cflags += [
'-Wno-unknown-attributes',
'-Wno-logical-op-parentheses',
'-Wno-return-stack-address',
'-Wno-string-plus-int',
]
# Linux compiler C++ flags
builder.cxx.cxxflags += [
'-Wno-invalid-offsetof',
'-Wno-write-strings',
'-std=c++17',
]
# -fPIC is required for all shared library object files.
# AMBuild's Library() target should set this automatically but we add it
# explicitly to ensure it is always present regardless of AMBuild version.
builder.cxx.cflags += ['-fPIC']
# Linux linker flags (applied to all build types)
if is_x86:
builder.cxx.linkflags += ['-m32']
else:
builder.cxx.linkflags += ['-m64']
builder.cxx.linkflags += [
'-ldl',
'-lm',
'-Wl,--as-needed',
]
elif builder.cxx.target.platform == 'windows':
# Windows defines
builder.cxx.defines += [
'_CRT_SECURE_NO_DEPRECATE',
'_CRT_SECURE_NO_WARNINGS',
'_CRT_NONSTDC_NO_DEPRECATE',
'NOMINMAX',
'WIN32',
'_WINDOWS'
]
# Windows compiler C flags
builder.cxx.cflags += [
'/W4',
'/GS-',
'/Gy',
]
# Windows compiler C++ flags
builder.cxx.cxxflags += [
'/std:c++17',
'/arch:SSE2',
'/fp:fast',
'/EHsc',
'/Qspectre',
'/GR-',
]
# Windows linker flags
# /MACHINE reflects the target word size; Win32 AMBuild builds are always
# x86 since there is no 64-bit GoldSrc Windows engine binary.
builder.cxx.linkflags += [
'/EXPORT:GiveFnptrsToDll=_GiveFnptrsToDll@8,@1',
'/SECTION:.data,RW',
'/MACHINE:X86',
]
# Compiler options for optimization ( --enable-optimize )
if builder.options.optimize:
# Shared optimization definitions
builder.cxx.defines += ['NDEBUG']
if builder.cxx.target.platform == 'linux':
# Linux optimization flags
builder.cxx.cflags += [
'-O2',
'-flto=auto',
]
# LTO must be in link flags too - [APG]RoboCop[CL]
# --gc-sections, --no-undefined, and --strip-all are release-only:
# --strip-all discards all debug symbols (wrong for debug builds) and
# --gc-sections with -fdata-sections/-ffunction-sections can interfere
# with GDB when sections are removed.
builder.cxx.linkflags += [
'-flto=auto',
'-s',
'-Wl,--gc-sections',
'-Wl,--no-undefined',
'-Wl,--strip-all',
'-Wl,-z,relro,-z,now',
'-Wl,-z,noexecstack',
]
elif builder.cxx.target.platform == 'windows':
# Windows optimization flags - /Ob2 needs to be after /O2, enables aggressive function inlining -caxanga334
builder.cxx.cflags += [
'/O2',
'/Ob2',
'/Oi',
'/Ot',
'/GL',
'/MD',
]
# Windows optimization link flags
builder.cxx.linkflags += [
'/OPT:ICF',
'/OPT:REF',
'/LTCG',
]
# Compiler options for debugging ( --enable-debug )
if builder.options.debug:
# Shared debug definitions
builder.cxx.defines += ['DEBUG', '_DEBUG']
if builder.cxx.target.platform == 'linux':
# Linux debug flags.
# Note: -fsanitize=address/undefined/leak are intentionally omitted.
# Metamod-P is a shared library loaded by HLDS, which is not itself
# instrumented with ASan/UBSan. Mixing an instrumented .so into an
# uninstrumented host binary causes a crash on load. Use -g3 -Og for
# debug symbols and step-through debugging only. [APG]RoboCop[CL]
# -Og: optimise for debugging experience — preserves source-to-instruction
# correspondence and variable liveness for GDB while still being fast
# enough to run a live test server. Preferable to -O0 which disables so
# much that it can mask real bugs and produces unreadably noisy disassembly.
builder.cxx.cflags += ['-g3', '-Og']
elif builder.cxx.target.platform == 'windows':
# Windows debug flags
builder.cxx.cflags += ['/Od', '/RTC1', '/MDd']
# Windows debug link flags
builder.cxx.linkflags += ['/NODEFAULTLIB:libcmt']
# Handle --enable-static-lib and --enable-shared-lib
if builder.cxx.target.platform == 'linux':
if builder.options.staticlib:
builder.cxx.linkflags += [
'-static-libgcc',
'-static-libstdc++'
]
elif builder.options.sharedlib:
builder.cxx.linkflags += [
'-shared-libgcc',
]
library = builder.cxx.Library('metamod')
library.compiler.includes += include_paths
library.sources += [
'./metamod/api_hook.cpp',
'./metamod/api_info.cpp',
'./metamod/commands_meta.cpp',
'./metamod/conf_meta.cpp',
'./metamod/dllapi.cpp',
'./metamod/engine_api.cpp',
'./metamod/engineinfo.cpp',
'./metamod/game_autodetect.cpp',
'./metamod/game_support.cpp',
'./metamod/h_export.cpp',
'./metamod/linkgame.cpp',
'./metamod/linkplug.cpp',
'./metamod/log_meta.cpp',
'./metamod/meta_eiface.cpp',
'./metamod/metamod.cpp',
'./metamod/mlist.cpp',
'./metamod/mplayer.cpp',
'./metamod/mplugin.cpp',
'./metamod/mreg.cpp',
'./metamod/mutil.cpp',
'./metamod/osdep.cpp',
'./metamod/osdep_p.cpp',
'./metamod/reg_support.cpp',
'./metamod/sdk_util.cpp',
'./metamod/studioapi.cpp',
'./metamod/support_meta.cpp',
'./metamod/vdate.cpp',
]
if builder.cxx.target.platform == 'linux':
library.sources += [
'metamod/osdep_detect_gamedll_linux.cpp',
'metamod/osdep_linkent_linux.cpp',
]
if builder.cxx.target.platform == 'windows':
library.sources += [
'metamod/osdep_detect_gamedll_win32.cpp',
'metamod/osdep_linkent_win32.cpp',
]
builder.Add(library)