Skip to content

Commit f269ffe

Browse files
authored
Simplify setup.py (#221)
1 parent bcdafc3 commit f269ffe

File tree

1 file changed

+53
-73
lines changed

1 file changed

+53
-73
lines changed

setup.py

Lines changed: 53 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,43 @@
2020
headers = []
2121
version_define = ''
2222

23+
24+
def _maybe_clang(flags):
25+
if platform.system() not in ('Darwin', 'FreeBSD'):
26+
return
27+
28+
os.environ.setdefault('CC', 'clang')
29+
os.environ.setdefault('CXX', 'clang++')
30+
orig_customize_compiler = distutils.sysconfig.customize_compiler
31+
32+
def customize_compiler(compiler):
33+
orig_customize_compiler(compiler)
34+
compiler.compiler[0] = os.environ['CC']
35+
compiler.compiler_so[0] = os.environ['CXX']
36+
compiler.compiler_cxx[0] = os.environ['CXX']
37+
compiler.linker_so[0] = os.environ['CXX']
38+
return compiler
39+
distutils.sysconfig.customize_compiler = customize_compiler
40+
flags[:] = ['-c', '-O3'] + flags + ['stdlib=libc++']
41+
42+
43+
def _maybe_macos(flags):
44+
if platform.system() != 'Darwin':
45+
return
46+
flags.append('-mmacosx-version-min=10.7',)
47+
macver = tuple(map(int, platform.mac_ver()[0].split('.')))
48+
if macver >= (10, 9):
49+
flags.append(
50+
'-Wno-error=unused-command-line-argument-hard-error-in-future',
51+
)
52+
53+
2354
if system_sass:
2455
flags = [
2556
'-fPIC', '-std=gnu++0x', '-Wall', '-Wno-parentheses', '-Werror=switch',
2657
]
27-
platform.mac_ver()
28-
if platform.system() in ['Darwin', 'FreeBSD']:
29-
os.environ.setdefault('CC', 'clang')
30-
os.environ.setdefault('CXX', 'clang++')
31-
orig_customize_compiler = distutils.sysconfig.customize_compiler
32-
33-
def customize_compiler(compiler):
34-
orig_customize_compiler(compiler)
35-
compiler.compiler[0] = os.environ['CC']
36-
compiler.compiler_so[0] = os.environ['CXX']
37-
compiler.compiler_cxx[0] = os.environ['CXX']
38-
compiler.linker_so[0] = os.environ['CXX']
39-
return compiler
40-
distutils.sysconfig.customize_compiler = customize_compiler
41-
flags.append('-stdlib=libc++')
42-
if platform.system() == 'Darwin':
43-
flags.append('-mmacosx-version-min=10.7',)
44-
macver = tuple(map(int, platform.mac_ver()[0].split('.')))
45-
if macver >= (10, 9):
46-
flags.append(
47-
'-Wno-error=unused-command-line-' +
48-
'argument-hard-error-in-future', # noqa
49-
)
50-
51-
flags = ['-c', '-O3'] + flags
58+
_maybe_clang(flags)
59+
_maybe_macos(flags)
5260

5361
if platform.system() == 'FreeBSD':
5462
link_flags = ['-fPIC', '-lc++']
@@ -90,10 +98,10 @@ def customize_compiler(compiler):
9098
libsass_version = libsass_version_file.read().decode('UTF-8').strip()
9199
if sys.platform == 'win32':
92100
# This looks wrong, but is required for some reason :(
93-
version_define = r'/DLIBSASS_VERSION="\"{0}\""'.format(
101+
version_define = r'/DLIBSASS_VERSION="\"{}\""'.format(
94102
libsass_version)
95103
else:
96-
version_define = '-DLIBSASS_VERSION="{0}"'.format(libsass_version)
104+
version_define = '-DLIBSASS_VERSION="{}"'.format(libsass_version)
97105

98106
for directory in (
99107
os.path.join('libsass', 'src'),
@@ -109,7 +117,7 @@ def customize_compiler(compiler):
109117

110118
if sys.platform == 'win32':
111119
from distutils.msvc9compiler import get_build_version
112-
vscomntools_env = 'VS{0}{1}COMNTOOLS'.format(
120+
vscomntools_env = 'VS{}{}COMNTOOLS'.format(
113121
int(get_build_version()),
114122
int(get_build_version() * 10) % 10
115123
)
@@ -131,29 +139,10 @@ def customize_compiler(compiler):
131139
'-fPIC', '-std=gnu++0x', '-Wall',
132140
'-Wno-parentheses', '-Werror=switch',
133141
]
134-
platform.mac_ver()
135-
if platform.system() in ['Darwin', 'FreeBSD']:
136-
os.environ.setdefault('CC', 'clang')
137-
os.environ.setdefault('CXX', 'clang++')
138-
orig_customize_compiler = distutils.sysconfig.customize_compiler
139-
140-
def customize_compiler(compiler):
141-
orig_customize_compiler(compiler)
142-
compiler.compiler[0] = os.environ['CC']
143-
compiler.compiler_so[0] = os.environ['CXX']
144-
compiler.compiler_cxx[0] = os.environ['CXX']
145-
compiler.linker_so[0] = os.environ['CXX']
146-
return compiler
147-
distutils.sysconfig.customize_compiler = customize_compiler
148-
flags.append('-stdlib=libc++')
149-
if platform.system() == 'Darwin':
150-
flags.append('-mmacosx-version-min=10.7',)
151-
macver = tuple(map(int, platform.mac_ver()[0].split('.')))
152-
if macver >= (10, 9):
153-
flags.append(
154-
'-Wno-error=unused-command-line-' +
155-
'argument-hard-error-in-future', # noqa
156-
)
142+
_maybe_clang(flags)
143+
_maybe_macos(flags)
144+
145+
if platform.system() in ('Darwin', 'FreeBSD'):
157146
# Dirty workaround to avoid link error...
158147
# Python distutils doesn't provide any way
159148
# to configure different flags for each cc and c++.
@@ -162,26 +151,24 @@ def customize_compiler(compiler):
162151
with open(cencode_path) as f:
163152
cencode_body = f.read()
164153
with open(cencode_path, 'w') as f:
165-
f.write('''
166-
#ifdef __cplusplus
167-
extern "C" {
168-
#endif
169-
''')
154+
f.write(
155+
'#ifdef __cplusplus\n'
156+
'extern "C" {\n'
157+
'#endif\n'
158+
)
170159
f.write(cencode_body)
171-
f.write('''
172-
#ifdef __cplusplus
173-
}
174-
#endif
175-
''')
160+
f.write(
161+
'#ifdef __cplusplus\n'
162+
'}\n'
163+
'#endif\n'
164+
)
176165

177166
@atexit.register
178167
def restore_cencode():
179168
if os.path.isfile(cencode_path):
180169
with open(cencode_path, 'w') as f:
181170
f.write(cencode_body)
182171

183-
flags = ['-c', '-O3'] + flags
184-
185172
if platform.system() == 'FreeBSD':
186173
link_flags = ['-fPIC', '-lc++']
187174
else:
@@ -192,19 +179,16 @@ def restore_cencode():
192179
extra_compile_args = flags + [version_define]
193180
extra_link_args = link_flags
194181

195-
sources.sort()
196182
sass_extension = Extension(
197183
'_sass',
198-
sources,
184+
sorted(sources),
199185
include_dirs=include_dirs,
200186
depends=headers,
201187
extra_compile_args=extra_compile_args,
202188
extra_link_args=link_flags,
203189
libraries=libraries
204190
)
205191

206-
install_requires = ['six']
207-
208192

209193
def version(sass_filename='sass.py'):
210194
with open(sass_filename) as f:
@@ -287,12 +271,8 @@ def run(self):
287271
['sassc = sassc:main'],
288272
]
289273
},
290-
install_requires=install_requires,
291-
extras_require={
292-
'upload_appveyor_builds': [
293-
'twine == 1.5.0',
294-
],
295-
},
274+
install_requires=['six'],
275+
extras_require={'upload_appveyor_builds': ['twine == 1.5.0']},
296276
classifiers=[
297277
'Development Status :: 5 - Production/Stable',
298278
'Environment :: Web Environment',

0 commit comments

Comments
 (0)