-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSConstruct
177 lines (148 loc) · 5.93 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
# This SConstruct is for faster parallel builds.
# Use "setup.py" for normal installation.
MY_SCONS_HELP = """\
SCons rules for compiling and installing diffpy.srreal.
SCons build is much faster when run with parallel jobs (-j4).
Usage: scons [target] [var=value]
Targets:
module build Python extension module srreal_ext.so [default]
install install to default Python package location
develop copy extension module to src/diffpy/srreal/ directory
test execute unit tests
Build configuration variables:
%s
Variables can be also assigned in a user script sconsvars.py.
SCons construction environment can be customized in sconscript.local script.
"""
import os
import re
import subprocess
import platform
def subdictionary(d, keyset):
return dict(kv for kv in d.items() if kv[0] in keyset)
def getsyspaths(*names):
pall = sum((os.environ.get(n, '').split(os.pathsep) for n in names), [])
rv = [p for p in pall if os.path.exists(p)]
return rv
def pyoutput(cmd):
proc = subprocess.Popen([env['python'], '-c', cmd],
stdout=subprocess.PIPE,
universal_newlines=True)
out = proc.communicate()[0]
return out.rstrip()
def pyconfigvar(name):
cmd = ('from distutils.sysconfig import get_config_var\n'
'print(get_config_var(%r))\n') % name
return pyoutput(cmd)
# copy system environment variables related to compilation
DefaultEnvironment(ENV=subdictionary(os.environ, '''
PATH PYTHONPATH GIT_DIR
CPATH CPLUS_INCLUDE_PATH LIBRARY_PATH LD_RUN_PATH
LD_LIBRARY_PATH DYLD_LIBRARY_PATH DYLD_FALLBACK_LIBRARY_PATH
MACOSX_DEPLOYMENT_TARGET LANG
_PYTHON_SYSCONFIGDATA_NAME
_CONDA_PYTHON_SYSCONFIGDATA_NAME
'''.split())
)
# Create construction environment
env = DefaultEnvironment().Clone()
# Variables definitions below work only with 0.98 or later.
env.EnsureSConsVersion(0, 98)
# Customizable compile variables
vars = Variables('sconsvars.py')
vars.Add(PathVariable('prefix',
'installation prefix directory', None))
vars.Add(EnumVariable('build',
'compiler settings', 'fast',
allowed_values=('debug', 'fast')))
vars.Add(EnumVariable('tool',
'C++ compiler toolkit to be used', 'default',
allowed_values=('default', 'intelc')))
vars.Add(BoolVariable('profile',
'build with profiling information', False))
vars.Add('python',
'Python executable to use for installation.', 'python')
vars.Update(env)
env.Help(MY_SCONS_HELP % vars.GenerateHelpText(env))
# Use Intel C++ compiler if requested by the user.
icpc = None
if env['tool'] == 'intelc':
icpc = env.WhereIs('icpc')
if not icpc:
print("Cannot find the Intel C/C++ compiler 'icpc'.")
Exit(1)
env.Tool('intelc', topdir=icpc[:icpc.rfind('/bin')])
# Figure out compilation switches, filter away C-related items.
good_python_flag = lambda n : (
not isinstance(n, str) or
not re.match(r'(-g|-Wstrict-prototypes|-O\d|-fPIC)$', n))
# Determine python-config script name.
pyversion = pyoutput('import sys; print("%i.%i" % sys.version_info[:2])')
pycfgname = 'python%s-config' % (pyversion if pyversion[0] == '3' else '')
pybindir = os.path.dirname(env.WhereIs(env['python']))
pythonconfig = os.path.join(pybindir, pycfgname)
# Verify python-config comes from the same path as the target python.
xpython = env.WhereIs(env['python'])
xpythonconfig = env.WhereIs(pythonconfig)
if os.path.dirname(xpython) != os.path.dirname(xpythonconfig):
print("Inconsistent paths of %r and %r" % (xpython, xpythonconfig))
Exit(1)
# Process the python-config flags here.
env.ParseConfig(pythonconfig + " --cflags")
env.Replace(CCFLAGS=[f for f in env['CCFLAGS'] if good_python_flag(f)])
env.Replace(CPPDEFINES='')
# the CPPPATH directories are checked by scons dependency scanner
cpppath = getsyspaths('CPLUS_INCLUDE_PATH', 'CPATH')
env.AppendUnique(CPPPATH=cpppath)
# Insert LIBRARY_PATH explicitly because some compilers
# ignore it in the system environment.
env.PrependUnique(LIBPATH=getsyspaths('LIBRARY_PATH'))
# Add shared libraries.
# Note: libdiffpy and boost_python are added from SConscript.configure.
fast_linkflags = ['-s']
fast_shlinkflags = pyconfigvar('LDSHARED').split()[1:]
# Specify minimum C++ standard. Allow later standard from sconscript.local.
# In case of multiple `-std` options the last option holds.
env.PrependUnique(CXXFLAGS='-std=c++11', delete_existing=1)
# Platform specific intricacies.
if env['PLATFORM'] == 'darwin':
env.AppendUnique(CXXFLAGS='-ftemplate-depth-256')
darwin_shlinkflags = [n for n in env['SHLINKFLAGS']
if n != '-dynamiclib']
env.Replace(SHLINKFLAGS=darwin_shlinkflags)
env.AppendUnique(SHLINKFLAGS=['-bundle'])
env.AppendUnique(SHLINKFLAGS=['-undefined', 'dynamic_lookup'])
fast_linkflags[:] = []
# Compiler specific options
if icpc:
# options for Intel C++ compiler on hpc dev-intel07
env.AppendUnique(CCFLAGS=['-w1', '-fp-model', 'precise'])
env.PrependUnique(LIBS=['imf'])
fast_optimflags = ['-fast', '-no-ipo']
else:
# g++ options
env.AppendUnique(CCFLAGS=['-Wall'])
fast_optimflags = ['-ffast-math']
# Configure build variants
if env['build'] == 'debug':
env.AppendUnique(CCFLAGS='-g')
elif env['build'] == 'fast':
env.AppendUnique(CCFLAGS=['-O3'] + fast_optimflags)
env.AppendUnique(CPPDEFINES='NDEBUG')
env.AppendUnique(LINKFLAGS=fast_linkflags)
env.AppendUnique(SHLINKFLAGS=fast_shlinkflags)
if env['profile']:
env.AppendUnique(CCFLAGS='-pg')
env.AppendUnique(LINKFLAGS='-pg')
builddir = env.Dir('build/%s-%s' % (env['build'], platform.machine()))
Export('env', 'pyconfigvar', 'pyoutput', 'pyversion')
def GlobSources(pattern):
"""Same as Glob but also require that source node is a valid file.
"""
rv = [f for f in Glob(pattern) if f.srcnode().isfile()]
return rv
Export('GlobSources')
if os.path.isfile('sconscript.local'):
env.SConscript('sconscript.local')
env.SConscript('src/extensions/SConscript', variant_dir=builddir)
# vim: ft=python