1414
1515from setuptools import Extension , setup
1616
17- system_sass = os .environ .get ('SYSTEM_SASS' , False )
17+ MACOS_FLAG = ['-mmacosx-version-min=10.7' ]
18+ FLAGS_POSIX = [
19+ '-fPIC' , '-std=gnu++0x' , '-Wall' , '-Wno-parentheses' , '-Werror=switch' ,
20+ ]
21+ FLAGS_CLANG = ['-c' , '-O3' ] + FLAGS_POSIX + ['-stdlib=libc++' ]
22+ LFLAGS_POSIX = ['-fPIC' , '-lstdc++' ]
23+ LFLAGS_CLANG = ['-fPIC' , '-stdlib=libc++' ]
1824
1925sources = ['pysass.cpp' ]
2026headers = []
21- version_define = ''
2227
28+ if sys .platform == 'win32' :
29+ extra_compile_args = ['/Od' , '/EHsc' , '/MT' ]
30+ extra_link_args = []
31+ elif platform .system () == 'Darwin' :
32+ extra_compile_args = FLAGS_CLANG + MACOS_FLAG
33+ extra_link_args = LFLAGS_CLANG + MACOS_FLAG
34+ elif platform .system () == 'FreeBSD' :
35+ extra_compile_args = FLAGS_CLANG
36+ extra_link_args = LFLAGS_CLANG
37+ else :
38+ extra_compile_args = FLAGS_POSIX
39+ extra_link_args = LFLAGS_POSIX
2340
24- def _maybe_clang (flags ):
25- if platform .system () not in ('Darwin' , 'FreeBSD' ):
26- return
27-
41+ if platform .system () in {'Darwin' , 'FreeBSD' }:
2842 os .environ .setdefault ('CC' , 'clang' )
2943 os .environ .setdefault ('CXX' , 'clang++' )
3044 orig_customize_compiler = distutils .sysconfig .customize_compiler
@@ -37,37 +51,10 @@ def customize_compiler(compiler):
3751 compiler .linker_so [0 ] = os .environ ['CXX' ]
3852 return compiler
3953 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-
54- if sys .platform == 'win32' :
55- extra_link_args = []
56- elif platform .system () in {'Darwin' , 'FreeBSD' }:
57- extra_link_args = ['-fPIC' , '-lc++' ]
58- else :
59- extra_link_args = ['-fPIC' , '-lstdc++' ]
60-
61- if system_sass :
62- flags = [
63- '-fPIC' , '-std=gnu++0x' , '-Wall' , '-Wno-parentheses' , '-Werror=switch' ,
64- ]
65- _maybe_clang (flags )
66- _maybe_macos (flags )
6754
55+ if os .environ .get ('SYSTEM_SASS' , False ):
6856 libraries = ['sass' ]
6957 include_dirs = []
70- extra_compile_args = flags
7158else :
7259 LIBSASS_SOURCE_DIR = os .path .join ('libsass' , 'src' )
7360
@@ -83,15 +70,10 @@ def _maybe_macos(flags):
8370
8471 # Determine the libsass version from the git checkout
8572 if os .path .exists (os .path .join ('libsass' , '.git' )):
86- proc = subprocess .Popen (
87- (
88- 'git' , '-C' , 'libsass' , 'describe' ,
89- '--abbrev=4' , '--dirty' , '--always' , '--tags' ,
90- ),
91- stdout = subprocess .PIPE ,
92- )
93- out , _ = proc .communicate ()
94- assert not proc .returncode , proc .returncode
73+ out = subprocess .check_output ((
74+ 'git' , '-C' , 'libsass' , 'describe' ,
75+ '--abbrev=4' , '--dirty' , '--always' , '--tags' ,
76+ ))
9577 with open ('.libsass-upstream-version' , 'wb' ) as libsass_version_file :
9678 libsass_version_file .write (out )
9779
@@ -100,11 +82,9 @@ def _maybe_macos(flags):
10082 libsass_version = libsass_version_file .read ().decode ('UTF-8' ).strip ()
10183 if sys .platform == 'win32' :
10284 # This looks wrong, but is required for some reason :(
103- version_define = r'/DLIBSASS_VERSION="\"{}\""' .format (
104- libsass_version ,
105- )
85+ define = r'/DLIBSASS_VERSION="\"{}\""' .format (libsass_version )
10686 else :
107- version_define = '-DLIBSASS_VERSION="{}"' .format (libsass_version )
87+ define = '-DLIBSASS_VERSION="{}"' .format (libsass_version )
10888
10989 for directory in (
11090 os .path .join ('libsass' , 'src' ),
@@ -137,45 +117,36 @@ def _maybe_macos(flags):
137117 if get_build_version () < 14.0 :
138118 msvc9compiler .get_build_version = lambda : 14.0
139119 msvc9compiler .VERSION = 14.0
140- flags = ['/Od' , '/EHsc' , '/MT' ]
141- else :
142- flags = [
143- '-fPIC' , '-std=gnu++0x' , '-Wall' ,
144- '-Wno-parentheses' , '-Werror=switch' ,
145- ]
146- _maybe_clang (flags )
147- _maybe_macos (flags )
148-
149- if platform .system () in ('Darwin' , 'FreeBSD' ):
150- # Dirty workaround to avoid link error...
151- # Python distutils doesn't provide any way
152- # to configure different flags for each cc and c++.
153- cencode_path = os .path .join (LIBSASS_SOURCE_DIR , 'cencode.c' )
154- cencode_body = ''
155- with open (cencode_path ) as f :
156- cencode_body = f .read ()
157- with open (cencode_path , 'w' ) as f :
158- f .write (
159- '#ifdef __cplusplus\n '
160- 'extern "C" {\n '
161- '#endif\n ' ,
162- )
163- f .write (cencode_body )
164- f .write (
165- '#ifdef __cplusplus\n '
166- '}\n '
167- '#endif\n ' ,
168- )
120+ elif platform .system () in ('Darwin' , 'FreeBSD' ):
121+ # Dirty workaround to avoid link error...
122+ # Python distutils doesn't provide any way
123+ # to configure different flags for each cc and c++.
124+ cencode_path = os .path .join (LIBSASS_SOURCE_DIR , 'cencode.c' )
125+ cencode_body = ''
126+ with open (cencode_path ) as f :
127+ cencode_body = f .read ()
128+ with open (cencode_path , 'w' ) as f :
129+ f .write (
130+ '#ifdef __cplusplus\n '
131+ 'extern "C" {\n '
132+ '#endif\n ' ,
133+ )
134+ f .write (cencode_body )
135+ f .write (
136+ '#ifdef __cplusplus\n '
137+ '}\n '
138+ '#endif\n ' ,
139+ )
169140
170- @atexit .register
171- def restore_cencode ():
172- if os .path .isfile (cencode_path ):
173- with open (cencode_path , 'w' ) as f :
174- f .write (cencode_body )
141+ @atexit .register
142+ def restore_cencode ():
143+ if os .path .isfile (cencode_path ):
144+ with open (cencode_path , 'w' ) as f :
145+ f .write (cencode_body )
175146
176147 libraries = []
177148 include_dirs = [os .path .join ('.' , 'libsass' , 'include' )]
178- extra_compile_args = flags + [ version_define ]
149+ extra_compile_args . append ( define )
179150
180151sass_extension = Extension (
181152 '_sass' ,
@@ -192,8 +163,7 @@ def version(sass_filename='sass.py'):
192163 with open (sass_filename ) as f :
193164 tree = ast .parse (f .read (), sass_filename )
194165 for node in tree .body :
195- if isinstance (node , ast .Assign ) and \
196- len (node .targets ) == 1 :
166+ if isinstance (node , ast .Assign ) and len (node .targets ) == 1 :
197167 target , = node .targets
198168 if isinstance (target , ast .Name ) and target .id == '__version__' :
199169 return node .value .s
0 commit comments