forked from diffpy/pyobjcryst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SConstruct
146 lines (119 loc) · 4.62 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
# 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 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
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):
s = os.pathsep.join(filter(None, map(os.environ.get, names)))
return filter(os.path.exists, s.split(os.pathsep))
def pyoutput(cmd):
proc = subprocess.Popen([env['python'], '-c', cmd],
stdout=subprocess.PIPE)
out = proc.communicate()[0]
return out.rstrip()
def pyconfigvar(name):
cmd = '\n'.join((
'from distutils.sysconfig import get_config_var',
'print get_config_var(%r)' % 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
'''.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(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 when it is available
icpc = env.WhereIs('icpc')
if icpc:
env.Tool('intelc', topdir=icpc[:icpc.rfind('/bin')])
# Apply CFLAGS, CXXFLAGS, LDFLAGS from the system environment.
flagnames = 'CFLAGS CXXFLAGS LDFLAGS'.split()
env.MergeFlags([os.environ.get(n, '') for n in flagnames])
# Figure out compilation switches, filter away C-related items.
good_python_flags = lambda n : (
not isinstance(n, basestring) or
not re.match(r'(-g|-Wstrict-prototypes|-O\d)$', n))
env.ParseConfig("python-config --cflags")
env.Replace(CCFLAGS=filter(good_python_flags, env['CCFLAGS']))
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: ObjCryst and boost_python are added from SConscript.configure
fast_linkflags = ['-s']
fast_shlinkflags = pyconfigvar('LDSHARED').split()[1:]
# 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'])
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')
if os.path.isfile('sconscript.local'):
env.SConscript('sconscript.local')
env.SConscript('extensions/SConscript', variant_dir=builddir)
# vim: ft=python