-
Notifications
You must be signed in to change notification settings - Fork 18
/
SConstruct
242 lines (202 loc) · 9.04 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
# This SConstruct is for faster parallel builds.
# Use "setup.py" for normal installation.
MY_SCONS_HELP = """\
SCons rules for compiling and installing pyobjcryst.
SCons build is much faster when run with parallel jobs (-j4).
Usage: scons [target] [var=value]
Targets:
module build Python extension module _pyobjcryst.so [default]
install install to default Python package location
develop copy extension module to src/pyobjcryst/ 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
from os.path import join as pjoin
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 HOMEPATH HOMEDRIVE
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')
# Set PREFIX for installation and linking
# TODO: also amend paths when VIRTUAL_ENV variable exists ?
if 'PREFIX' in os.environ:
# building with a set prefix
vars.Add(PathVariable(
'prefix',
'installation prefix directory',
os.environ['PREFIX']))
elif 'CONDA_PREFIX' in os.environ:
# building for a conda environment
vars.Add(PathVariable(
'prefix',
'installation prefix directory',
os.environ['CONDA_PREFIX']))
else:
vars.Add(PathVariable('prefix',
'installation prefix directory', None))
vars.Update(env)
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', 'clang', 'clangxx')))
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.
if 'PY_VER' in os.environ:
pyversion = os.environ['PY_VER']
else:
pyversion = pyoutput('import sys; print("%i.%i" % sys.version_info[:2])')
if 'CONDA_BUILD' in os.environ and 'PY_VER' in os.environ:
# Messy: if CONDA_BUILD and PY_VER are in the path, we are building a conda package
# using several environment. Make sure python3.X-config points to the destination
# (host) environment
pythonconfig = pjoin(os.environ['PREFIX'], 'bin', 'python%s-config' % os.environ['PY_VER'])
print("Using $PREFIX and $PY_VER to determine python-config pth: %s" % pythonconfig)
xpython = pjoin(os.environ['PREFIX'], 'bin', 'python')
pyversion = os.environ['PY_VER']
else:
pycfgname = 'python%s-config' % (pyversion if pyversion[0] == '3' else '')
# realpath gets the real path if exec is a link (e.g. in a python environment)
xpython = os.path.realpath(env.WhereIs(env['python']))
pybindir = os.path.dirname(xpython)
pythonconfig = pjoin(pybindir, pycfgname)
# for k in sorted(os.environ.keys()):
# print(" ", k, os.environ[k])
if platform.system().lower() == "windows":
# See https://scons.org/faq.html#Linking_on_Windows_gives_me_an_error
env['ENV']['TMP'] = os.environ['TMP']
# 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'))
if env['prefix'] is not None:
env.Append(CPPPATH=[pjoin(env['prefix'], 'include')])
env.Append(CPPPATH=[pjoin(env['prefix'], 'Library', 'include')])
# Windows conda library paths are a MESS ('lib', 'libs', 'Library\lib'...)
env.Append(LIBPATH=[pjoin(env['prefix'], 'Library', 'lib')])
env.Append(LIBPATH=[pjoin(env['prefix'], 'libs')])
# This disable automated versioned named e.g. libboost_date_time-vc142-mt-s-x64-1_73.lib
# so we can use conda-installed libraries
env.AppendUnique(CPPDEFINES='BOOST_ALL_NO_LIB')
# Prevent the generation of an import lib (.lib) in addition to the dll
# env.AppendUnique(no_import_lib=1)
env.PrependUnique(CCFLAGS=['/Ox', '/EHsc', '/MD', '/DREAL=double'])
env.AppendUnique(CPPDEFINES={'NDEBUG': None})
else:
if 'CONDA_BUILD' not in os.environ:
# Verify python-config comes from the same path as the target 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='BOOST_ERROR_CODE_HEADER_ONLY')
# 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: ObjCryst 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)
# Need this to avoid missing symbol with boost<1.66
env.PrependUnique(CXXFLAGS=['-DBOOST_ERROR_CODE_HEADER_ONLY'])
# Use double precision for objcryst's REAL
env.PrependUnique(CCFLAGS=['-DREAL=double'])
# Platform specific intricacies.
if env['PLATFORM'] == 'darwin':
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', '-fno-strict-aliasing'])
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')
env.Append(CPPPATH=[pjoin(env['prefix'], 'include')])
env.Append(LIBPATH=[pjoin(env['prefix'], 'lib')])
builddir = env.Dir('build/%s-%s' % (env['build'], platform.machine()))
Export('env', 'pyconfigvar', 'pyoutput', 'pyversion')
if os.path.isfile('sconscript.local'):
env.SConscript('sconscript.local')
env.SConscript('src/extensions/SConscript', variant_dir=builddir)
# vim: ft=python