-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSConstruct
More file actions
371 lines (262 loc) · 9.82 KB
/
Copy pathSConstruct
File metadata and controls
371 lines (262 loc) · 9.82 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
#!/usr/bin/env python
EnsureSConsVersion(0, 98, 1)
# System
import glob
import os
import pickle
import sys
import re
import subprocess
from collections import OrderedDict
#from compat import iteritems, isbasestring, open_utf8, decode_utf8, qualname
from SCons import Node
from SCons.Script import Glob
def isbasestring(s):
return isinstance(s, (str, bytes))
def add_source_files(self, sources, files, warn_duplicates=True):
# Convert string to list of absolute paths (including expanding wildcard)
if isbasestring(files):
# Keep SCons project-absolute path as they are (no wildcard support)
if files.startswith("#"):
if "*" in files:
print("ERROR: Wildcards can't be expanded in SCons project-absolute path: '{}'".format(files))
return
files = [files]
else:
dir_path = self.Dir(".").abspath
files = sorted(glob.glob(dir_path + "/" + files))
# Add each path as compiled Object following environment (self) configuration
for path in files:
obj = self.Object(path)
if obj in sources:
if warn_duplicates:
print('WARNING: Object "{}" already included in environment sources.'.format(obj))
else:
continue
sources.append(obj)
def add_library(env, name, sources, **args):
library = env.Library(name, sources, **args)
env.NoCache(library)
return library
def add_program(env, name, sources, **args):
program = env.Program(name, sources, **args)
env.NoCache(program)
return program
# Based on https://github.com/godotengine/godot/pull/53443
# uses the graph/topo sort from:
# https://www.geeksforgeeks.org/python-program-for-topological-sorting/
# albeit highly modified
class ModuleDepGraph:
def __init__(self, modules, dependencies: OrderedDict):
self.graph = dict()
v = []
for m in modules:
v.append(m[0])
self.vertices = v
# construct the edges
for name, deps in dependencies.items():
self.graph[name] = []
for dep_name in deps:
self.graph[name].append(dep_name)
# A recursive function used by dependency_sort
def topological_sort_util(self, v, visited, stack):
# Mark the current node as visited.
visited[v] = True
# Recur for all the vertices adjacent to this vertex
for i in self.graph[v]:
if i in visited and visited[i] == False:
self.topological_sort_util(i, visited, stack)
# Push current vertex to stack which stores result
stack.insert(0, v)
# The function to performs a topological sort, and then reverses it to obtain the dependency sort.
def dependency_sort(self):# -> []:
# Mark all the vertices as not visited
visited = dict()
for v in self.vertices:
visited[v] = False
stack = []
# Call the recursive helper function to store Topological
# Sort starting from all vertices one by one
for v in self.vertices:
if visited[v] == False:
self.topological_sort_util(v, visited, stack)
# reverse the topological sort
stack.reverse()
return stack
def sort_modules_dependencies(modules):
deps = {}
for mod in modules:
name = mod[0]
path = mod[1]
sys.path.insert(0, path)
import detect
try:
deps[name] = detect.get_module_dependencies()
except AttributeError:
deps[name] = {}
sys.path.remove(path)
sys.modules.pop("detect")
graph = ModuleDepGraph(modules, deps)
dep_sorted_names = graph.dependency_sort()
for n in dep_sorted_names:
for i in range(len(modules)):
if modules[i][0] == n:
mt = modules[i]
for j in range(i + 1, len(modules)):
modules[j - 1] = modules[j]
modules[len(modules) - 1] = mt
break
#return modules
env_base = Environment()
env_base.__class__.add_source_files = add_source_files
env_base.__class__.add_library = add_library
env_base.__class__.add_program = add_program
if "TERM" in os.environ:
env_base["ENV"]["TERM"] = os.environ["TERM"]
env_base.AppendENVPath("PATH", os.getenv("PATH"))
env_base.AppendENVPath("PKG_CONFIG_PATH", os.getenv("PKG_CONFIG_PATH"))
env_base.disabled_modules = []
env_base.use_ptrcall = False
env_base.module_version_string = ""
env_base.msvc = False
# avoid issues when building with different versions of python out of the same directory
env_base.SConsignFile(".sconsign{0}.dblite".format(pickle.HIGHEST_PROTOCOL))
# Build options
opts = Variables([], ARGUMENTS)
opts.Add(EnumVariable("target", "Compilation target", "debug", ("debug", "release_debug", "release")))
opts.Add("folders", "App folders to compile", "")
opts.Add("main_file", "The main file", "")
opts.Add("module_folders", "App module folders to compile", "")
opts.Add("databases", "Whether to have database support", False)
# Compilation environment setup
opts.Add("CXX", "C++ compiler")
opts.Add("CC", "C compiler")
opts.Add("LINK", "Linker")
opts.Add("CCFLAGS", "Custom flags for both the C and C++ compilers")
opts.Add("CFLAGS", "Custom flags for the C compiler")
opts.Add("CXXFLAGS", "Custom flags for the C++ compiler")
opts.Add("LINKFLAGS", "Custom flags for the linker")
opts.Update(env_base)
# add default include paths
env_base.Prepend(CPPPATH=["#"])
env_base.Prepend(CPPPATH=["#libs"])
env_base.Prepend(LINKFLAGS=["-lpthread"])
#env_base.Append(CXX=["-o3"])
env_base.Append(CXX=["-g"])
#env_base.Append(CXX=["-g2"])
#env_base.Append(CXX=["-fno-rtti"])
# Compilation DB requires SCons 3.1.1+.
from SCons import __version__ as scons_raw_version
scons_ver = env_base._get_major_minor_revision(scons_raw_version)
if scons_ver >= (4, 0, 0):
env_base.Tool("compilation_db")
database_list = []
if env_base["databases"]:
env_base.Append(CPPDEFINES=["DATABASES_ENABLED"])
for x in sorted(glob.glob("database_backends/*")):
if not os.path.isdir(x) or not os.path.exists(x + "/detect.py"):
continue
tmppath = "./" + x
sys.path.insert(0, tmppath)
import detect
if detect.is_active() and detect.can_build():
x = x.replace("database_backends/", "") # rest of world
x = x.replace("database_backends\\", "") # win32
database_list += [x]
sys.path.remove(tmppath)
sys.modules.pop("detect")
modfol = env_base["module_folders"].split(";")
modfol.append("modules")
modfol.append("web_modules")
modfol.append("database_modules")
#temporarily, these should be handled in a different pass
modfol.append("backends")
modfol.append("web_backends")
modfol.append("crypto_backends")
module_folders = list()
for fol in modfol:
folt = fol.strip()
if folt == "":
continue
module_folders.append(os.path.abspath(folt) + "/*")
module_list = []
for mf in module_folders:
for x in sorted(glob.glob(mf)):
if not os.path.isdir(x) or not os.path.exists(x + "/detect.py"):
continue
tmppath = os.path.realpath(os.path.expanduser(os.path.expandvars(x))) + "/"
sys.path.insert(0, tmppath)
import detect
if detect.is_active() and detect.can_build():
#x = x.replace("/", "") # rest of world
x = x.replace("\\", "/") # win32
tx = x.split("/")
module_list.append([ tx[len(tx) - 1], tmppath ])
sys.path.remove(tmppath)
sys.modules.pop("detect")
# Sort modules dependencies
sort_modules_dependencies(module_list)
# TODO defines like this should be generated into a header file, to keep the compile commands shorter
# Also generate a define for each module
#Todo ability to turn this on or off
env_base.Append(CPPDEFINES=["WEB_ENABLED"])
env = env_base.Clone()
if scons_ver >= (4, 0, 0):
env.Tool("compilation_db")
env.Alias("compiledb", env_base.CompilationDatabase())
Export("env")
SConscript("core/SCsub")
SConscript("crypto/SCsub")
SConscript("web/SCsub")
SConscript("platform/SCsub")
if env_base["databases"]:
SConscript("database/SCsub")
for d in database_list:
tmppath = "./database_backends/" + d
sys.path.insert(0, tmppath)
import detect
env_db = env_base.Clone()
if scons_ver >= (4, 0, 0):
env_db.Tool("compilation_db")
env_db.Alias("compiledb", env_base.CompilationDatabase())
detect.configure(env_db)
detect.configure(env)
Export("env_db")
SConscript("database_backends/" + d + "/SCsub")
sys.path.remove(tmppath)
sys.modules.pop("detect")
# Todo actually generate db_init
for m in module_list:
tmppath = m[1]
sys.path.insert(0, tmppath)
import detect
env_mod = env_base.Clone()
# Compilation DB requires SCons 3.1.1+.
from SCons import __version__ as scons_raw_version
scons_ver = env_mod._get_major_minor_revision(scons_raw_version)
if scons_ver >= (4, 0, 0):
env_mod.Tool("compilation_db")
env_mod.Alias("compiledb", env_base.CompilationDatabase())
detect.configure(env_mod)
detect.configure(env)
Export("env_mod")
SConscript(m[1] + "/SCsub")
sys.path.remove(tmppath)
sys.modules.pop("detect")
folders = env_base["folders"].split(";")
files = []
files.append("rcpp_framework.cpp")
for fol in folders:
folt = fol.strip()
if folt == "":
continue
ff = os.listdir(folt)
for f in ff:
if f.endswith("cpp"):
files.append(os.path.abspath(fol + "/" + f))
#files.append(fol + "/" + f)
env.prg_sources = files
libapp = env.add_library("application", env.prg_sources)
env.Prepend(LIBS=[libapp])
mfp = os.path.abspath(env_base["main_file"])
prog = env.add_program("#bin/server", [ mfp ])