-
Notifications
You must be signed in to change notification settings - Fork 0
/
SConstruct
executable file
·332 lines (272 loc) · 8.54 KB
/
SConstruct
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
import os
import subprocess
import fnmatch
import ConfigParser
home_path = os.environ['HOME']
def checkStd(compiler):
sample = open("sample.cpp", "w")
sample.write("""
#include <iostream>
[[deprecated("use the function body directly instead of wrapping it in a function.")]]
void helloWorld() {
std::cout << "Hello world" << std::endl;
}
int main (int argc, char *argv[]) {
helloWorld();
return 0;
}""")
sample.close()
FNULL = open(os.devnull, 'w')
if subprocess.call([compiler,"-o","test_build","-std=c++14","sample.cpp"],stdout=FNULL,stderr=FNULL) == 0:
stdflag = "c++14"
elif subprocess.call([compiler,"-o","test_build","-std=c++11","sample.cpp"],stdout=FNULL,stderr=FNULL) == 0:
stdflag = "c++11"
else:
# possibility to print warning/error
# assume c++11
stdflag = "c++11"
# clean up
FNULL.close()
os.remove("sample.cpp")
try:
os.remove("test_build")
except:
pass
return stdflag
# SOURCE files (including executable) will be gathered here
srcDir = "networkit/cpp"
def getSourceFiles(target, optimize):
source = []
# walk source directory and find ONLY .cpp files
for (dirpath, dirnames, filenames) in os.walk(srcDir):
for name in fnmatch.filter(filenames, "*.cpp"):
source.append(os.path.join(dirpath, name))
# exclude files depending on target, executables will be addes later
xpatterns = ["*-X.cpp"]
excluded = []
# only the target "Test" requires Benchmark and GTest files
if (target not in ["Tests"]):
# exclude files matching following patterns
xpatterns += ["*GTest.cpp","*Benchmark.cpp"]
for pattern in xpatterns:
for name in fnmatch.filter(source, pattern):
excluded.append(name)
#print("excluded source files: {0}".format(excluded))
source = [name for name in source if name not in excluded]
# add executable
if target == "Tests":
source.append(os.path.join(srcDir, "Unittests-X.cpp"))
elif target in ["Core","Lib"]:
pass # no executable
else:
print("Unknown target: {0}".format(target))
Exit(1)
# create build directory for build configuration
buildDir = ".build{0}".format(optimize)
VariantDir(buildDir, srcDir, duplicate=0)
# modify source paths for build directory
source = [name.replace(srcDir + "/", buildDir + "/") for name in source]
#print(source)
return source
AddOption("--compiler",
dest="compiler",
type="string",
nargs=1,
action="store",
help="used to pass gcc version from setup.py to SConstruct")
AddOption("--std",
dest="std",
type="string",
nargs=1,
action="store",
help="used to pass std flag from setup.py to SConstruct")
# ENVIRONMENT
## read environment settings from configuration file
env = Environment()
compiler = GetOption("compiler")
stdflag = GetOption("std")
if not os.path.isfile("build.conf") and not compiler == None:
#print("{0} has been passed via command line".format(compiler))
env["CC"] = compiler
env["CXX"] = compiler
else:
confPath = "build.conf"
if not os.path.isfile(confPath):
print("The configuration file `build.conf` does not exist. You need to create it.")
print("Use the file build.conf.example to create your build.conf")
Exit(1)
conf = ConfigParser.ConfigParser()
conf.read([confPath]) # read the configuration file
## compiler
if compiler is None:
cppComp = conf.get("compiler", "cpp", "gcc")
else:
cppComp = compiler
defines = conf.get("compiler", "defines", []) # defines are optional
if defines is not []:
defines = defines.split(",")
## C++14 support
if stdflag is None:
try:
stdflag = conf.get("compiler", "std14")
except:
pass
if stdflag is None or len(stdflag) == 0:
# do test compile
stdflag = checkStd(cppComp)
# and store it in the configuration
conf.set("compiler","std14", stdflag)
## includes
stdInclude = conf.get("includes", "std", "") # includes for the standard library - may not be needed
gtestInclude = conf.get("includes", "gtest")
if conf.has_option("includes", "tbb"):
tbbInclude = conf.get("includes", "tbb", "")
else:
tbbInclude = ""
## libraries
gtestLib = conf.get("libraries", "gtest")
if conf.has_option("libraries", "tbb"):
tbbLib = conf.get("libraries", "tbb", "")
else:
tbbLib = ""
env["CC"] = cppComp
env["CXX"] = cppComp
env.Append(CPPDEFINES=defines)
env.Append(CPPPATH = [stdInclude, gtestInclude, tbbInclude])
env.Append(LIBS = ["gtest"])
env.Append(LIBPATH = [gtestLib, tbbLib])
with open(confPath, "w") as f:
conf.write(f)
env.Append(LINKFLAGS = ["-std={}".format(stdflag)])
## CONFIGURATIONS
commonCFlags = ["-c", "-fmessage-length=0", "-std=c99", "-fPIC"]
commonCppFlags = ["-std={}".format(stdflag), "-Wall", "-c", "-fmessage-length=0", "-fPIC"]
debugCppFlags = ["-O0", "-g3", "-DLOG_LEVEL=LOG_LEVEL_TRACE"]
debugCFlags = ["-O0", "-g3"]
optimizedCppFlags = ["-O3", "-DNDEBUG", "-DLOG_LEVEL=LOG_LEVEL_INFO"]
optimizedCFlags = ["-O3"]
profileCppFlags = ["-O2", "-DNDEBUG", "-g", "-pg", "-DLOG_LEVEL=LOG_LEVEL_INFO"]
profileCFlags = ["-O2", "-DNDEBUG", "-g", "-pg"]
# select configuration
# custom command line options
AddOption("--optimize",
dest="optimize",
type="string",
nargs=1,
action="store",
help="specify the optimization level to build: D(ebug), O(ptimize), P(rofile)")
AddOption("--sanitize",
dest="sanitize",
type="string",
nargs=1,
action="store",
help="switch on address sanitizer")
try:
optimize = GetOption("optimize")
except:
print("ERROR: Missing option --optimize=<LEVEL>")
exit()
sanitize = None
try:
sanitize = GetOption("sanitize")
except:
pass
# create build directory for build configuration
# modify source paths for build directory
# moved to getSourceFiles()
# append flags
#commmon flags
env.Append(CFLAGS = commonCFlags)
env.Append(CPPFLAGS = commonCppFlags)
# logging yes or no
AddOption("--logging",
dest="logging",
type="string",
nargs=1,
action="store",
help="enable logging: yes or no")
logging = GetOption("logging")
if logging == "no":
env.Append(CPPDEFINES=["NOLOGGING"]) # logging is enabled by default
print("INFO: Logging is now disabled")
elif (logging != "yes") and (logging != None):
print("INFO: unrecognized option --logging=%s" % logging)
print("Logging is enabled by default")
# openmp yes or no
AddOption("--openmp",
dest="openmp",
type="string",
nargs=1,
action="store",
help="-fopenmp: yes or no")
openmp = GetOption("openmp")
if (openmp == "yes") or (openmp == None): # with OpenMP by default
env.Append(CPPFLAGS = ["-fopenmp"])
env.Append(LINKFLAGS = ["-fopenmp"])
elif (openmp == "no"):
env.Append(LIBS = ["pthread"])
else:
print("ERROR: unrecognized option --openmp=%s" % openmp)
exit()
# optimize flags
if optimize == "Dbg":
env.Append(CFLAGS = debugCFlags)
env.Append(CPPFLAGS = debugCppFlags)
elif optimize == "Opt":
env.Append(CFLAGS = optimizedCFlags)
env.Append(CPPFLAGS = optimizedCppFlags)
elif optimize == "Pro":
env.Append(CFLAGS = profileCFlags)
env.Append(CPPFLAGS = profileCppFlags)
else:
print("ERROR: invalid optimize: %s" % optimize)
exit()
# sanitize
if sanitize:
if sanitize == "address":
env.Append(CPPFLAGS = ["-fsanitize=address"])
env.Append(LINKFLAGS = ["-fsanitize=address"])
else:
print("ERROR: invalid sanitize option")
exit()
# TARGET
AddOption("--target",
dest="target",
type="string",
nargs=1,
action="store",
help="select target to build")
target = GetOption("target")
availableTargets = ["Lib","Core","Tests"]
if target in availableTargets:
source = getSourceFiles(target,optimize)
targetName = "NetworKit-{0}-{1}".format(target, optimize)
if target in ["Core","Lib"]:
# do not append executable
# env.Append(CPPDEFINES=["NOLOGGING"])
env.Library("NetworKit-Core-{0}".format(optimize), source)
if target == "Lib":
libFileToLink = "libNetworKit-Core-{0}.a".format(optimize)
libFileTarget = "libNetworKit.a"
if os.path.lexists(libFileTarget):
os.remove(libFileTarget)
os.symlink(libFileToLink,libFileTarget)
# SCons does not support python 3 yet...
#os.symlink("src/cpp","NetworKit",True)
# to support case insensitive file systems
# place the symlink for the include path in the folder include
if os.path.isdir("include"):
try:
os.remove("include/NetworKit")
except:
pass
os.rmdir("include")
os.mkdir("include")
os.chdir("include")
subprocess.call(["ln","-s","../networkit/cpp","NetworKit"])
os.chdir("../")
else:
env.Program(targetName, source)
else:
print("ERROR: unknown target: {0}".format(target))
exit(1)