forked from SoarGroup/Soar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSConstruct
396 lines (335 loc) · 14.9 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
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
#!/usr/bin/python
# Project: Soar <http://soar.googlecode.com>
# Author: Jonathan Voigt <voigtjr@gmail.com>
from __future__ import print_function
import os
import sys
import platform
import socket
import subprocess
import re
import fnmatch
from SCons.Node.Alias import default_ans
import SCons.Script
import shutil
import time
# Add the current directory to the path so we can from build_support
sys.path.append(Dir('.').srcnode().abspath)
from build_support.tcl import prepare_for_compiling_with_tcl
join = os.path.join
SOAR_VERSION = "9.6.1"
CPP_STD_VERSION = "c++17"
soarversionFile = open('soarversion', 'w')
print(SOAR_VERSION, file=soarversionFile)
soarversionFile.close()
COMPILE_DB_ALIAS = 'cdb'
SML_CSHARP_ALIAS = 'sml_csharp'
SML_JAVA_ALIAS = 'sml_java'
SML_PYTHON_ALIAS = 'sml_python'
SML_TCL_ALIAS = 'sml_tcl'
SML_PHP_ALIAS = 'sml_php'
DEF_OUT = 'out'
DEF_BUILD = 'build'
DEF_TARGETS = ['kernel', 'cli', SML_JAVA_ALIAS, 'debugger', 'headers', 'scripts', COMPILE_DB_ALIAS]
print("================================================================================")
print("Building Soar", SOAR_VERSION, " * will be built if no target specified")
print("Targets available:")
print(" Core: kernel* cli* scripts*")
print(" Testing: performance_tests tests")
print(f" SWIG: {SML_PYTHON_ALIAS} {SML_TCL_ALIAS} {SML_JAVA_ALIAS}* {SML_CSHARP_ALIAS}")
print(f" Extras: debugger* headers* tclsoarlib {COMPILE_DB_ALIAS}*")
print("Custom Settings available: *default")
print(" Build Type: --dbg, --opt*, --static")
print(" Custom Paths: --out, --build, --tcl")
print(" Compilation time: --no-svs, --scu*, --no-scu, --no-scu-kernel, --no-scu-cli")
print(" Customizations: --cc, --cxx, --cflags, --lnflags, --no-default-flags, --verbose,")
print("================================================================================")
def execute(cmd):
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
except OSError:
print(cmd[0], ' not in path')
Exit(1)
out = p.communicate()[0]
if p.returncode != 0:
print('error executing ', cmd)
Exit(1)
else:
return out.decode()
def gcc_version(cc):
version_info = execute(cc.split() + ['--version'])
if 'GCC' in version_info or 'g++' in version_info:
m = re.search(r'([0-9]+)\.([0-9]+)\.([0-9]+)', version_info)
if m:
return tuple(int(n) for n in m.groups())
if 'clang' in version_info or 'LLVM' in version_info:
return [42, 42, 42]
print('cannot identify compiler version')
Exit(1)
def vc_version():
try:
p = subprocess.Popen(['link.exe'], stdout=subprocess.PIPE, bufsize=1)
except WindowsError as e:
print("error running link.exe: {0}".format(e.strerror))
print('make sure Microsoft Visual C++ is installed and you are using the Visual Studio Command Prompt')
Exit(1)
line = p.stdout.readline()
# for line in iter(p.stdout.readline, b''):
# print(line,)
p.communicate()
m = re.search(r'Version ([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)', line.decode())
if m:
t = tuple(int(n) for n in m.groups())
return str(t[0]) + '.' + str(t[1])
print('cannot identify compiler version')
Exit(1)
# run cl (MSVC compiler) and return the target architecture (x64 or x86)
def cl_target_arch():
cl = subprocess.Popen('cl.exe /?', stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
for line in cl.stdout:
if re.search('x64', line.decode()):
return 'x64'
return 'x86'
def Mac_m64_Capable():
return execute('sysctl -n hw.optional.x86_64'.split()).strip() == '1'
# Install all files under source directory to target directory, keeping
# subdirectory structure and ignoring hidden files
def InstallDir(env, tgt, src, globstring="*"):
targets = []
tgtdir = env.GetBuildPath(tgt)
srcdir = env.GetBuildPath(src)
for dir, _, files in os.walk(srcdir):
if fnmatch.fnmatch(dir[len(srcdir) + 1:], '*/.*'):
continue
# tgtsub is the target directory plus the relative sub directory
relative = dir[len(srcdir) + 1:]
tgtsub = join(tgtdir, relative)
for f in fnmatch.filter(files, globstring):
if not f.startswith('.'):
p = join(dir, f)
targets.extend(Install(tgtsub, p))
return targets
def InstallDLLs(env):
if sys.platform == 'win32' and not GetOption('dbg'):
indlls = Glob(os.environ['VCINSTALLDIR'] + 'redist\\' + cl_target_arch() + '\\Microsoft.VC*.CRT\*')
outdir = os.path.realpath(GetOption('outdir')) + '\\'
if os.path.isfile(outdir):
os.remove(outdir)
if not os.path.exists(outdir):
os.mkdir(outdir)
for dll in indlls:
# print('copy "' + dll.rstr() + '" "' + outdir + '"')
shutil.copy(dll.rstr(), outdir)
Export('InstallDir')
AddOption('--cc', action='store', type='string', dest='cc', nargs=1, metavar='COMPILER', help='Use argument as the C compiler.')
AddOption('--cxx', action='store', type='string', dest='cxx', nargs=1, metavar='COMPILER', help='Use argument as the C++ compiler.')
AddOption('--cflags', action='store', type='string', dest='cflags', nargs=1, help='Compiler flags')
AddOption('--lnflags', action='store', type='string', dest='lnflags', nargs=1, help='Linker flags')
AddOption('--no-default-flags', action='store_false', dest='defflags', default=True, help="Don't pass any default flags to the compiler or linker")
AddOption('--no-scu', action='store_false', dest='scu', default=False, help='Don\'t build using single compilation units.')
AddOption('--no-scu-kernel', action='store_true', dest='no_scu_kernel', default=False, help='Never build kernel in a single compilation unit.')
AddOption('--no-scu-cli', action='store_true', dest='no_scu_cli', default=False, help='Never build CLI in a single compilation unit.')
AddOption('--scu', action='store_true', dest='scu', default=False, help='Build using single compilation units.')
AddOption('--out', action='store', type='string', dest='outdir', default=DEF_OUT, nargs=1, metavar='DIR', help='Directory to install binaries. Defaults to "out".')
AddOption('--build', action='store', type='string', dest='build-dir', default=DEF_BUILD, nargs=1, metavar='DIR', help='Directory to store intermediate (object) files. Defaults to "build".')
AddOption('--python', action='store', type='string', dest='python', default=sys.executable, nargs=1, help='Python executable; defaults to same executable used to run SCons')
AddOption('--tcl', action='store', type='string', dest='tcl', nargs=1, help='Active TCL (>= 8.6) libraries')
AddOption('--static', action='store_true', dest='static', default=False, help='Use static linking')
AddOption('--dbg', action='store_true', dest='dbg', default=False, help='Enable debug build. Disables compiler optimizations, includes debugging symbols, debug trace statements and assertions')
AddOption('--opt', action='store_false', dest='dbg', default=False, help='Enable optimized build. Enables compiler optimizations, removes debugging symbols, debug trace statements and assertions')
AddOption('--verbose', action='store_true', dest='verbose', default=False, help='Output full compiler commands')
AddOption('--no-svs', action='store_true', dest='nosvs', default=False, help='Build Soar without SVS functionality')
msvc_version = "12.0"
cl_target_architecture = ''
if sys.platform == 'win32':
msvc_version = vc_version()
cl_target_architecture = cl_target_arch()
print("MSVC compiler target architecture is", cl_target_architecture)
env = Environment(
MSVC_VERSION=msvc_version,
ENV=os.environ.copy(),
SCU=GetOption('scu'),
NO_SCU_KERNEL=GetOption('no_scu_kernel'),
NO_SCU_CLI=GetOption('no_scu_cli'),
BUILD_DIR=GetOption('build-dir'),
OUT_DIR=os.path.realpath(GetOption('outdir')),
SOAR_VERSION=SOAR_VERSION,
VISHIDDEN=False, # needed by swig
JAVAVERSION='1.8',
SML_CSHARP_ALIAS = SML_CSHARP_ALIAS,
SML_JAVA_ALIAS = SML_JAVA_ALIAS,
SML_PYTHON_ALIAS = SML_PYTHON_ALIAS,
SML_TCL_ALIAS = SML_TCL_ALIAS,
SML_PHP_ALIAS = SML_PHP_ALIAS,
# indentation for log formatting
INDENT = ' ',
)
env.AddMethod(prepare_for_compiling_with_tcl, 'PrepareForCompilingWithTcl')
# must be specified first or else the resulting file will not contain all compile commands
env.Tool('compilation_db')
compile_db_target = env.CompilationDatabase()
env.Alias(COMPILE_DB_ALIAS, compile_db_target)
# This creates a file for cli_version.cpp to source. For optimized builds, this guarantees
# that the build date will be correct in every build. (Turned off for debug, b/c it was adding
# extra compilation time. (for some reason, this will build it the first two times you compile after
# it exists.)
if ((GetOption('dbg') == None) or (GetOption('dbg') == False) or (FindFile('build_time_date.h', 'Core/shared/') == None)):
cli_version_dep = open('Core/shared/build_time_date.h', 'w')
print("const char* kTimestamp = __TIME__;", file=cli_version_dep)
print("const char* kDatestamp = __DATE__;", file=cli_version_dep)
print("//* Last build of Soar " + SOAR_VERSION + " occurred at " + time.ctime(time.time()) + " *//", file=cli_version_dep)
cli_version_dep.close()
else:
print("Build time stamp file was not built because this is a debug build.")
if GetOption('cc') != None:
env.Replace(CC=GetOption('cc'))
elif sys.platform == 'darwin':
env.Replace(CC='clang')
if GetOption('cxx') != None:
env.Replace(CXX=GetOption('cxx'))
elif sys.platform == 'darwin':
env.Replace(CXX='clang++')
print("Building intermediates to", env['BUILD_DIR'])
print("Installing targets to", env['OUT_DIR'])
if 'g++' in env['CXX']:
compiler = 'g++'
elif env['CXX'].endswith('cl') or (env['CXX'] == '$CC' and env['CC'].endswith('cl')):
compiler = 'msvc'
else:
compiler = os.path.split(env['CXX'])[1]
lsb_build = ('lsbc++' in env['CXX'])
Export('compiler', 'lsb_build')
cflags = []
lnflags = []
libs = ['Soar']
if compiler == 'g++':
libs += [ 'pthread', 'dl', 'm' ]
if GetOption('nosvs'):
cflags.append('-DNO_SVS')
if GetOption('defflags'):
cflags.append('-Wreturn-type')
if GetOption('dbg'):
cflags.extend(['-g'])
else:
cflags.extend(['-O3', '-DNDEBUG'])
gcc_ver = gcc_version(env['CXX'])
# check if the compiler supports -fvisibility=hidden (GCC >= 4)
if gcc_ver[0] > 3:
env.Append(CPPFLAGS='-fvisibility=hidden')
config = Configure(env)
if config.TryCompile('', '.cpp'):
cflags.append('-fvisibility=hidden')
cflags.append('-DGCC_HASCLASSVISIBILITY')
env['VISHIDDEN'] = True
else:
env['VISHIDDEN'] = False
env['CPPFLAGS'] = []
config.Finish()
if sys.platform.startswith('linux'):
lnflags.append(env.Literal(r'-Wl,-rpath,$ORIGIN'))
libs.append('rt')
elif 'freebsd' in sys.platform:
lnflags.append(env.Literal(r'-Wl,-z,origin,-rpath,$ORIGIN'))
# For OSX, use -install_name (specified in Core/SConscript)
if GetOption('static'):
cflags.extend(['-DSTATIC_LINKED', '-fPIC'])
elif compiler == 'msvc':
cflags = ['/EHsc', '/D', '_CRT_SECURE_NO_DEPRECATE', '/D', '_WIN32', '/W2', '/bigobj']
if GetOption('nosvs'):
cflags.extend(' /D NO_SVS'.split())
if GetOption('defflags'):
if GetOption('dbg'):
cflags.extend(' /MDd /Z7 /DEBUG'.split())
lnflags.extend(['/DEBUG'])
else:
cflags.extend(' /MD /O2 /D NDEBUG'.split())
if GetOption('static'):
cflags.extend(['/D', 'STATIC_LINKED'])
cflags.extend((GetOption('cflags') or '').split())
lnflags.extend((GetOption('lnflags') or '').split())
env.Replace(
CPPFLAGS=cflags,
LINKFLAGS=lnflags,
CPPPATH=[
'#Core/shared',
'#Core/SoarKernel/src/debug_code',
'#Core/SoarKernel/src/decision_process',
'#Core/SoarKernel/src/episodic_memory',
'#Core/SoarKernel/src/explanation_memory',
'#Core/SoarKernel/src/explanation_based_chunking',
'#Core/SoarKernel/src/interface',
'#Core/SoarKernel/src/output_manager',
'#Core/SoarKernel/src/parsing',
'#Core/SoarKernel/src/reinforcement_learning',
'#Core/SoarKernel/src/semantic_memory',
'#Core/SoarKernel/src/shared',
'#Core/SoarKernel/src/soar_representation',
'#Core/SoarKernel/src/visualizer',
'#Core/ElementXML/src',
'#Core/KernelSML/src',
'#Core/ConnectionSML/src',
'#Core/ClientSML/src',
'#Core/CLI/src',
],
LIBS=libs,
LIBPATH=[os.path.realpath(GetOption('outdir'))],
)
if sys.platform == 'win32':
sys_lib_path = list(filter(None, os.environ.get('PATH', '').split(';')))
sys_inc_path = list(filter(None, os.environ.get('INCLUDE', '').split(';')))
elif sys.platform == 'darwin':
sys_lib_path = list(filter(None, os.environ.get('DYLD_LIBRARY_PATH', '').split(':')))
sys_inc_path = list(filter(None, os.environ.get('CPATH', '').split(':')))
else:
sys_lib_path = list(filter(None, os.environ.get('LD_LIBRARY_PATH', '').split(':')))
sys_inc_path = list(filter(None, os.environ.get('CPATH', '').split(':')))
if sys.platform == 'win32':
env.Append(CXXFLAGS=f'/std:{CPP_STD_VERSION}')
else:
env.Append(CXXFLAGS=f'-std={CPP_STD_VERSION}')
if not sys.platform == 'darwin':
env.Append(CPPPATH=sys_inc_path, LIBPATH=sys_lib_path)
# Setting *COMSTR will replace long commands with a short message "Making <something>"
if not GetOption('verbose'):
for x in 'CC SHCC CXX SHCXX LINK SHLINK JAR SWIG'.split():
env[x + 'COMSTR'] = 'Making $TARGET'
env['JAVACCOMSTR'] = 'Making $TARGET and others'
Export('env')
g_msvs_variant = 'Debug|Win32'
if 'MSVSSolution' in env['BUILDERS']:
msvs_projs = []
Export('msvs_projs')
if (cl_target_architecture == 'x64'):
if GetOption('dbg'):
g_msvs_variant = 'Debug|x64'
else:
g_msvs_variant = 'Release|x64'
else:
if GetOption('dbg'):
g_msvs_variant = 'Debug|Win32'
else:
g_msvs_variant = 'Release|Win32'
Export('g_msvs_variant')
for d in os.listdir('.'):
if not d.startswith('.'):
script = join(d, 'SConscript')
if os.path.exists(script):
SConscript(script, variant_dir=join(GetOption('build-dir'), d), duplicate=0)
if 'MSVSSolution' in env['BUILDERS']:
msvs_solution = env.MSVSSolution(
target='soar' + env['MSVSSOLUTIONSUFFIX'],
projects=msvs_projs,
variant=g_msvs_variant,
)
env.Alias('msvs', [msvs_solution] + msvs_projs)
ALL_ALIAS = 'all'
all_aliases = list(default_ans.keys())
env.Alias(ALL_ALIAS, all_aliases)
if COMMAND_LINE_TARGETS == ['list']:
print('\n'.join(sorted(all_aliases)))
Exit()
# Set default targets
for a in DEF_TARGETS:
if a in all_aliases:
Default(a)
InstallDLLs(env)