forked from networkit/networkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SConstruct
370 lines (303 loc) · 10.1 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
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
import os
import subprocess
import fnmatch
import sys
if sys.version_info >= (3,0):
import configparser as cp
getConf = lambda conf, sec, name, fb: conf.get(sec, name, fallback=fb)
else:
import ConfigParser as cp
getConf = lambda conf, sec, name, fb: conf.get(sec, name, fb)
home_path = os.environ['HOME']
class DefaultConfigParser(cp.ConfigParser):
def get_default(self, section, option, default=None, raw=False, vars=None):
"""Get an option value for a given section.
If the option exists in the section, get_default behaves exactly like
get(). If not, default is returned (if not explicitly set: None).
"""
try:
return self.get(section, option, raw=raw, vars=vars)
except (cp.ConfigParser.NoSectionError, cp.ConfigParser.NoOptionError):
return default
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", "SharedLib"]:
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")
AddOption("--defines",
dest="defines",
type="string",
nargs=1,
help="used to pass defines (separated by commas)")
# ENVIRONMENT
## read environment settings from configuration file
env = Environment()
compiler = GetOption("compiler")
stdflag = GetOption("std")
defines = GetOption("defines")
if not os.path.isfile("build.conf"):
if defines is not None and defines is not []:
defines = defines.split(",")
env.Append(CPPDEFINES=defines)
if not compiler == None:
#print("{0} has been passed via command line".format(compiler))
env["CC"] = compiler
env["CXX"] = compiler
elif 'CC' in os.environ and 'CXX' in os.environ:
env["CC"] = os.environ['CC']
env["CXX"] = os.environ['CXX']
env.Append(LIBS = ["gtest"])
env.Append(LIBPATH = [os.getenv('GTEST_LIB', ""), os.getenv('OPENMP_LIB', "")])
env.Append(CPPPATH = [os.getenv('GTEST_INCLUDE', ""), os.getenv('OPENMP_INCLUDE', "")])
else:
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)
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 = DefaultConfigParser()
conf.read([confPath]) # read the configuration file
## compiler
cppComp = compiler or conf.get_default("compiler", "cpp", "gcc")
# defines are optional
defines = defines or conf.get_default("compiler", "defines", "")
defines = defines.split(",")
## C++14 support
stdflag = stdflag or conf.get_default("compiler", "std14")
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
# Evaluates the [includes] section of the build.conf file.
# Includes may include std, gtest, tbb
includes = dict(conf.items("includes"))
## libraries
# Evaluates the [libraries] section of the build.conf file.
# Libraries may include gtest, tbb
libraries = dict(conf.items("libraries"))
env["CC"] = cppComp
env["CXX"] = cppComp
env.Append(CPPDEFINES=defines)
env.Append(CPPPATH = includes.values())
env.Append(LIBS = libraries.keys())
env.Append(LIBPATH = libraries.values())
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(1)
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(1)
# 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(1)
# sanitize
if sanitize:
if sanitize == "address":
env.Append(CPPFLAGS = ["-fsanitize=address"])
env.Append(LINKFLAGS = ["-fsanitize=address"])
else:
print("ERROR: invalid sanitize option")
exit(1)
# TARGET
AddOption("--target",
dest="target",
type="string",
nargs=1,
action="store",
help="select target to build")
target = GetOption("target")
availableTargets = ["SharedLib", "Lib", "Core", "Tests"]
if target not in availableTargets:
print("ERROR: unknown target: {0}".format(target))
exit(1)
source = getSourceFiles(target,optimize)
targetName = "NetworKit-{0}-{1}".format(target, optimize)
if target == "Tests":
env.Program(targetName, source)
elif target == "Core":
# do not append executable
# env.Append(CPPDEFINES=["NOLOGGING"])
env.StaticLibrary("NetworKit-Core-{0}".format(optimize), source)
if target in ["Lib", "SharedLib"]:
if target == "Lib":
env.StaticLibrary("NetworKit-Core-{0}".format(optimize), source)
staticLibSuffix = env['LIBSUFFIX']
fileEnding = staticLibSuffix if staticLibSuffix else ".a"
else:
env.SharedLibrary("NetworKit-Core-{0}".format(optimize), source)
sharedLibSuffix = env['SHLIBSUFFIX']
fileEnding = sharedLibSuffix if sharedLibSuffix else ".so"
libFileToLink = "libNetworKit-Core-{0}{1}".format(optimize, fileEnding)
libFileTarget = "libNetworKit{0}".format(fileEnding)
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("../")