-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_util.py
More file actions
615 lines (514 loc) · 20 KB
/
build_util.py
File metadata and controls
615 lines (514 loc) · 20 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import glob
import sys
import os
import fnmatch
class Dev:
def __init__(self, env):
self.env = env
self.build_root = "#/build/" + env["mode"] + "-" + env["tools"]
if env["arch"] != "x86":
self.build_root += "-" + env["arch"]
self.build_root += "/"
def prepare(self):
if not self.env["verbose"]:
self.env["CCCOMSTR"] = "Compiling $TARGET (static)"
self.env["SHCCCOMSTR"] = "Compiling $TARGET (shared)"
self.env["CXXCOMSTR"] = "Compiling $TARGET (static)"
self.env["SHCXXCOMSTR"] = "Compiling $TARGET (shared)"
self.env["PCHCOMSTR"] = "Compiling $TARGET (precompiled header)"
self.env["GCHCOMSTR"] = "Compiling $TARGET (static precompiled header)"
self.env["GCHSHCOMSTR"] = "Compiling $TARGET (shared precompiled header)"
self.env["SHLINKCOMSTR"] = "Linking $TARGET (shared)"
self.env["LINKCOMSTR"] = "Linking $TARGET (static)"
self.env["ARCOMSTR"] = "Archiving $TARGET"
self.env["RCCOMSTR"] = "Resource $TARGET"
self.env.SConsignFile()
self.env.SetOption("implicit_cache", "1")
self.env.SetOption("max_drift", 60 * 10)
self.env.Decider("MD5-timestamp")
if "gcc" in self.env["TOOLS"]:
# when building with GCC, honor the "prefix" setting.
prefix = self.env.get("prefix")
if prefix is None:
# no explicit prefix; set one for MinGW builds.
if "mingw" not in self.env["TOOLS"]:
prefix = ""
elif self.env["arch"] == "x86":
prefix = "i686-w64-mingw32-"
elif self.env["arch"] == "x64":
prefix = "x86_64-w64-mingw32-"
# prefix our build tools.
BUILD_TOOLS = {
"CC": "gcc",
"CXX": "g++",
"LINK": "g++",
"AR": "ar",
"RANLIB": "ranlib",
"RC": "windres",
"strip": "strip",
}
for tool_ref, tool_bin in BUILD_TOOLS.items():
self.env[tool_ref] = prefix + tool_bin
# "gcc" should always be present.
gcc_path = self.env.WhereIs(self.env["CC"])
if not gcc_path:
raise Exception('GCC bin "%s" not found.' % self.env["CC"])
# set explicit paths on missing build tools, as they are not always
# all prefixed (though the "gcc" binary always is, so we use it to
# fetch a base path).
# <https://sourceforge.net/p/mingw-w64/mailman/message/33224826/>
if "mingw" in self.env["TOOLS"]:
for tool_ref, tool_bin in BUILD_TOOLS.items():
if not self.env.WhereIs(self.env[tool_ref]):
base_tool_path = os.path.dirname(gcc_path)
print(
'Using a non-prefixed version of "%s" from "%s".'
% (tool_bin, base_tool_path)
)
self.env[tool_ref] = os.path.join(
base_tool_path,
tool_bin,
)
# when cross-compiling, be explicit about bin extensions.
if sys.platform != "win32":
self.env["PROGSUFFIX"] = ".exe"
self.env["LIBPREFIX"] = "lib"
self.env["LIBSUFFIX"] = ".a"
self.env["SHLIBSUFFIX"] = ".dll"
# some distros of windres fail when they receive Win paths as
# input, so convert...
if "RCCOM" in self.env:
self.env["RCCOM"] = self.env["RCCOM"].replace(
"-i $SOURCE",
"-i ${SOURCE.posix}",
1,
)
if self.env["msvcproj"]:
if "msvs" not in self.env["TOOLS"]:
raise Exception("This is not an MSVC build; specify tools=default")
msvcproj_arch = self.env["arch"]
if msvcproj_arch == "x86":
msvcproj_arch = "Win32"
# TODO SCons doesn't seem to support multiple configs per project,
# so each config goes to a separate directory. when this is fixed,
# generate all projects in the root dir.
self.msvcproj_path = (
"#/msvc/" + self.env["mode"] + "-" + self.env["arch"] + "/"
)
self.msvcproj_variant = self.env["mode"] + "|" + msvcproj_arch
self.msvcproj_projects = []
# set up the command called when building from the VS IDE.
self.env["MSVSSCONSFLAGS"] = (
self.env["MSVSSCONSFLAGS"]
+ " tools="
+ self.env["tools"]
+ " mode="
+ self.env["mode"]
+ " arch="
+ self.env["arch"]
)
# work around a few bugs in MSVC project generation, see
# msvcproj_workarounds for details.
from SCons.Action import Action
from SCons.Tool.msvs import GenerateProject
self.env["MSVSPROJECTCOM"] = [
Action(GenerateProject, None),
Action(msvcproj_workarounds, None),
]
def finalize(self):
if self.env["msvcproj"]:
path = self.msvcproj_path + "DCPlusPlus" + self.env["MSVSSOLUTIONSUFFIX"]
self.env.Precious(path)
self.env.MSVSSolution(
target=path,
variant=self.msvcproj_variant,
projects=self.msvcproj_projects,
)
def is_win32(self):
return sys.platform == "win32" or "mingw" in self.env["TOOLS"]
def get_build_root(self):
return self.build_root
def get_build_path(self, source_path):
return self.get_build_root() + source_path
def get_target(self, source_path, name, in_bin=True):
if in_bin:
return self.get_build_root() + "bin/" + name
else:
return self.get_build_root() + source_path + name
def get_sources(self, source_path, source_glob, recursive=False):
matches = []
for root, dirnames, filenames in os.walk("."):
for filename in fnmatch.filter(filenames, source_glob):
matches.append(root + "/" + filename)
if not recursive:
dirnames[:] = []
return list(
map(
lambda x: (os.path.normpath(self.get_build_path(source_path) + x)),
matches,
)
)
# execute the SConscript file in the specified sub-directory.
def build(self, source_path, local_env=None):
if not local_env:
local_env = self.env
return local_env.SConscript(
source_path + "SConscript",
exports={"dev": self, "source_path": source_path},
)
# create a build environment and set up sources and targets.
def prepare_build(
self,
source_path,
name,
source_glob="*.cpp",
in_bin=True,
precompiled_header=None,
recursive=False,
):
build_path = self.get_build_path(source_path)
env = self.env.Clone()
env.VariantDir(build_path, ".", duplicate=0)
sources = self.get_sources(source_path, source_glob, recursive)
if precompiled_header is not None and env["pch"] and not env["msvcproj"]:
# TODO we work around the 2 problems described on
# <http://scons.tigris.org/issues/show_bug.cgi?id=2680> - remove
# once not needed
for i, source in enumerate(sources):
if source.find(precompiled_header + ".cpp") != -1:
# the PCH/GCH builder will take care of this one
del sources[i]
if "msvc" in env["TOOLS"]:
env["PCHSTOP"] = precompiled_header + ".h"
pch = env.PCH(
build_path + precompiled_header + ".pch",
precompiled_header + ".cpp",
)
env["PCH"] = pch[0]
env["ARFLAGS"] = env["ARFLAGS"] + " " + str(pch[1])
env["LINKFLAGS"] = env["LINKFLAGS"] + " " + str(pch[1])
elif "gcc" in env["TOOLS"]:
env["Gch"] = env.Gch(
build_path + precompiled_header + ".h.gch",
precompiled_header + ".h",
)[0]
# little dance to add the pch object to include paths, while
# overriding the current directory
env["CXXCOM"] = (
env["CXXCOM"]
+ " -include "
+ env.Dir(build_path).abspath
+ "/"
+ precompiled_header
+ ".h"
)
return (env, self.get_target(source_path, name, in_bin), sources)
# helpers for the MSVC project builder (see build_lib)
@staticmethod
def simple_lib(inc_ext, src_ext):
return lambda self, env: (env.Glob("*." + inc_ext), env.Glob("*." + src_ext))
# TODO using __func__ sucks; if anyone has a better idea, chip in...
c_lib = simple_lib.__func__("h", "c")
cpp_lib = simple_lib.__func__("h", "cpp")
def build_lib(
self, env, target, sources, msvcproj_glob=None, msvcproj_name=None, shared=False
):
if env["msvcproj"]:
if msvcproj_glob is None:
return
if msvcproj_name is None:
msvcproj_name = os.path.basename(os.path.dirname(sources[0]))
glob_inc, glob_src = msvcproj_glob(env)
# when there's only 1 file, SCons strips directories from the
# path...
if len(glob_inc) == 1:
glob_inc.append(env.File("dummy"))
if len(glob_src) == 1:
glob_src.append(env.File("dummy"))
path = self.msvcproj_path + msvcproj_name + env["MSVSPROJECTSUFFIX"]
env.Precious(path)
# we cheat a bit here: only the win32 project will be buildable.
# this is to avoid simulatenous builds of all the projects at the
# same time and general mayhem.
if msvcproj_name == "win32":
self.msvcproj_projects.insert(
0,
env.MSVSProject(
path,
variant=self.msvcproj_variant,
auto_build_solution=0,
incs=[f.abspath for f in glob_inc],
srcs=[f.abspath for f in glob_src],
buildtarget=target + env["PROGSUFFIX"],
),
)
else:
self.msvcproj_projects.append(
env.MSVSProject(
path,
variant=self.msvcproj_variant,
auto_build_solution=0,
incs=[f.abspath for f in glob_inc],
srcs=[f.abspath for f in glob_src],
MSVSSCONSCOM="",
)
)
return
return (
env.SharedLibrary(target, sources)
if shared
else env.StaticLibrary(target, sources)
)
def build_program(self, env, target, sources):
return env.Program(
target,
[
sources,
self.client,
self.dwarf,
self.zlib,
self.boost,
self.bzip2,
self.maxminddb,
self.miniupnpc,
self.natpmp,
self.dwt,
self.intl,
],
)
def i18n(self, source_path, buildenv, sources, name):
if not self.env["i18n"]:
return
p_oze = glob.glob("po/*.po")
potfile = "po/" + name + ".pot"
buildenv["PACKAGE"] = name
ret = buildenv.PotBuild(potfile, sources)
for po_file in p_oze:
buildenv.Precious(buildenv.PoBuild(po_file, [potfile]))
lang = os.path.basename(po_file)[:-3]
locale_path = self.get_target(source_path, "locale/" + lang + "/")
buildenv.MoBuild(
locale_path + "LC_MESSAGES/" + name + ".mo",
po_file,
NAME_FILE=buildenv.File(locale_path + "name.txt"),
)
return ret
def add_boost(self, env):
if self.is_win32():
env.Append(CPPPATH=["#/boost"])
else:
boost_libs = ["atomic", "filesystem", "regex", "system", "thread"]
boost_libs = ["boost_" + lib for lib in boost_libs]
env.Append(LIBS=boost_libs)
def add_crashlog(self, env):
if "mingw" in env["TOOLS"]:
env.Append(CPPPATH=["#/dwarf"])
#env.Append(CPPDEFINES=["LIBDWARF_STATIC"]) # todo: msvc
#env.Append(LIBPATH=["#/dwarf"])
env.Append(LIBS=["imagehlp"]) # dwarf
elif "msvc" in env["TOOLS"]:
env.Append(LIBS=["dbghelp"])
elif not self.is_win32():
env.Append(CPPPATH=["#/dwarf"])
def add_dcpp(self, env):
if self.is_win32():
env.Append(CPPPATH=["#/bzip2"])
env.Append(CPPPATH=["#/maxminddb", "#/zlib"])
if self.is_win32():
env.Append(LIBS=["gdi32", "iphlpapi", "ole32", "ws2_32"])
else:
env.Append(LIBS=["bz2", "c", "iconv"])
def add_intl(self, env):
if self.is_win32():
env.Append(CPPPATH=["#/intl"])
else:
env.Append(LIBS=["intl"])
def add_openssl(self, env):
if self.is_win32():
env.Append(CPPPATH=["#/openssl/include"])
openssl_lib = "#/openssl/lib/"
if env["arch"] != "x86":
openssl_lib += env["arch"] + "/"
env.Append(LIBPATH=[openssl_lib])
env.Append(LIBS=["crypt32"])
if "msvc" in env["TOOLS"]:
if env["mode"] == "debug":
env.Prepend(LIBS=["libssld", "libcryptod"])
else:
env.Prepend(LIBS=["libssl", "libcrypto"])
else:
env.Prepend(LIBS=["ssl", "crypto"])
def force_console(self, env):
if "-mwindows" in env["CCFLAGS"]:
env["CCFLAGS"].remove("-mwindows")
env.Append(CCFLAGS=["-mconsole"])
if "-mwindows" in env["LINKFLAGS"]:
env["LINKFLAGS"].remove("-mwindows")
env.Append(LINKFLAGS=["-mconsole"])
if "/SUBSYSTEM:WINDOWS" in env["LINKFLAGS"]:
env["LINKFLAGS"].remove("/SUBSYSTEM:WINDOWS")
# support installs that only have an asciidoc.py file but no executable
def get_asciidoc(self):
if "PATHEXT" in self.env["ENV"]:
pathext = self.env["ENV"]["PATHEXT"] + ";.py"
else:
pathext = ""
asciidoc = self.env.WhereIs("asciidoc3", pathext=pathext)
if asciidoc is None:
return None
if asciidoc[-3:] == ".py":
if self.env.WhereIs("python") is None:
return None
asciidoc = "python " + asciidoc
return asciidoc
def set_lang_name(target, env):
lang = os.path.basename(str(target))[:-3]
file = open(str(target), "rb")
data = file.read()
file.close()
import re
data = re.sub(
b'"Language: .*\\\\n"',
bytes('"Language: ' + lang + '\\\\n"', "utf-8"),
data,
1
)
file = open(str(target), "wb")
file.write(data)
file.close()
def get_po_name(source):
""" "Rely on the comments at the beginning of the po file to find the
language name.
@param source: *one* SCons file node (not a list!) designating the .po
file.
"""
import codecs
import re
match = re.compile("^# (.+) translation.*", re.I).search(
codecs.open(str(source), "rb", "utf_8").readline()
)
if match:
name = match.group(1)
if name != "XXX":
return name
return None
def gen_po_name(source, env):
"""
@param source: *one* SCons file node (not a list!) designating the .po
file.
@param env: must contain "NAME_FILE", which is a SCons file node to the
target file.
"""
import codecs
name = get_po_name(source)
if name:
codecs.open(str(env["NAME_FILE"]), "wb", "utf_8").write(name)
def CheckPKGConfig(context, version):
context.Message("Checking for pkg-config... ")
ret = context.TryAction("pkg-config --atleast-pkgconfig-version=%s" % version)[0]
context.Result(ret)
return ret
def CheckPKG(context, name):
context.Message("Checking for %s... " % name)
ret = context.TryAction('pkg-config --exists "%s"' % name)[0]
if ret:
context.env.ParseConfig('pkg-config --cflags --libs "%s"' % name)
context.Result(ret)
return ret
def nixify(path):
return path.replace("\\", "/")
def array_remove(array, to_remove):
if to_remove in array:
array.remove(to_remove)
def get_lcid(lang):
from locale import windows_locale
lang = lang.replace("-", "_")
# look for an exact match
for identifier, name in windows_locale.items():
if name == lang:
return identifier
# ignore the "sub-language" part
lang = lang.split("_")[0]
for identifier, name in windows_locale.items():
if name.split("_")[0] == lang:
return identifier
return 0x0409 # default: en-US
def get_win_cp(lcid):
import ctypes
LOCALE_IDEFAULTANSICODEPAGE = 0x1004
LOCALE_RETURN_NUMBER = 0x20000000
buf = ctypes.c_int()
kern32 = None
if sys.platform == "cygwin":
kern32 = ctypes.CDLL("kernel32.dll", use_errno=True)
else:
kern32 = ctypes.windll.kernel32
if not kern32:
print("Error: build_util/get_win_cp failed to load kernel32")
return "cp1252"
kern32.GetLocaleInfoA(
lcid, LOCALE_RETURN_NUMBER | LOCALE_IDEFAULTANSICODEPAGE, ctypes.byref(buf), 6
)
if buf.value <= 0:
print("Error: build_util/get_win_cp failed to get locale info")
return "cp1252"
return "cp" + str(buf.value)
def html_to_rtf(string):
# escape chars: \, {, }
# <br/> -> \line + remove surrounding spaces + append a space
# remove double new lines + remove new lines at beginning and at end
# <b>...</b> -> {\b ...}
# <i>...</i> -> {\i ...}
# <u>...</u> -> {\ul ...}
import re
line = r"\\line "
return re.sub( # noqa
"<([bi])>",
r"{\\\1 ",
re.sub(
"</[biu]>",
"}",
re.sub(
"^(" + line + ")",
"",
re.sub(
"(" + line + ")$",
"",
re.sub(
"(" + line + ")+",
line,
re.sub(
r"\s*<br ?/?>\s*",
line,
string.replace("\\", "\\\\")
.replace("{", "\\{")
.replace("}", "\\}"),
),
),
),
),
),
).replace("<u>", "{\\ul ")
def msvcproj_workarounds(target, source, env):
f = open(str(target[0]), "rb")
contents = f.read()
f.close()
import re
# clean up empty commands for non-built projects to avoid build failures.
contents = re.sub(
br"echo Starting SCons &&\s*(-c)?\s*""", b"", contents
)
# remove SConstruct from included files since it points nowhere anyway.
contents = re.sub(
br'<ItemGroup>\s*<None Include="SConstruct" />\s*</ItemGroup>', b"", contents
)
# update the platform toolset to the VS 2022 one.
# TODO remove when SCons adds this.
contents = contents.replace(
b"<UseOfMfc>false</UseOfMfc>",
b"<UseOfMfc>false</UseOfMfc>\r\n\t\t"
b"<PlatformToolset>v143</PlatformToolset>",
)
f = open(str(target[0]), "wb")
f.write(contents)
f.close()