Skip to content

Use -stdlib=libc++ flag for clang #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 54 additions & 84 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,31 @@

from setuptools import Extension, setup

system_sass = os.environ.get('SYSTEM_SASS', False)
MACOS_FLAG = ['-mmacosx-version-min=10.7']
FLAGS_POSIX = [
'-fPIC', '-std=gnu++0x', '-Wall', '-Wno-parentheses', '-Werror=switch',
]
FLAGS_CLANG = ['-c', '-O3'] + FLAGS_POSIX + ['-stdlib=libc++']
LFLAGS_POSIX = ['-fPIC', '-lstdc++']
LFLAGS_CLANG = ['-fPIC', '-stdlib=libc++']

sources = ['pysass.cpp']
headers = []
version_define = ''

if sys.platform == 'win32':
extra_compile_args = ['/Od', '/EHsc', '/MT']
extra_link_args = []
elif platform.system() == 'Darwin':
extra_compile_args = FLAGS_CLANG + MACOS_FLAG
extra_link_args = LFLAGS_CLANG + MACOS_FLAG
elif platform.system() == 'FreeBSD':
extra_compile_args = FLAGS_CLANG
extra_link_args = LFLAGS_CLANG
else:
extra_compile_args = FLAGS_POSIX
extra_link_args = LFLAGS_POSIX

def _maybe_clang(flags):
if platform.system() not in ('Darwin', 'FreeBSD'):
return

if platform.system() in {'Darwin', 'FreeBSD'}:
os.environ.setdefault('CC', 'clang')
os.environ.setdefault('CXX', 'clang++')
orig_customize_compiler = distutils.sysconfig.customize_compiler
Expand All @@ -37,37 +51,10 @@ def customize_compiler(compiler):
compiler.linker_so[0] = os.environ['CXX']
return compiler
distutils.sysconfig.customize_compiler = customize_compiler
flags[:] = ['-c', '-O3'] + flags + ['-stdlib=libc++']


def _maybe_macos(flags):
if platform.system() != 'Darwin':
return
flags.append('-mmacosx-version-min=10.7',)
macver = tuple(map(int, platform.mac_ver()[0].split('.')))
if macver >= (10, 9):
flags.append(
'-Wno-error=unused-command-line-argument-hard-error-in-future',
)


if sys.platform == 'win32':
extra_link_args = []
elif platform.system() in {'Darwin', 'FreeBSD'}:
extra_link_args = ['-fPIC', '-lc++']
else:
extra_link_args = ['-fPIC', '-lstdc++']

if system_sass:
flags = [
'-fPIC', '-std=gnu++0x', '-Wall', '-Wno-parentheses', '-Werror=switch',
]
_maybe_clang(flags)
_maybe_macos(flags)

if os.environ.get('SYSTEM_SASS', False):
libraries = ['sass']
include_dirs = []
extra_compile_args = flags
else:
LIBSASS_SOURCE_DIR = os.path.join('libsass', 'src')

Expand All @@ -83,15 +70,10 @@ def _maybe_macos(flags):

# Determine the libsass version from the git checkout
if os.path.exists(os.path.join('libsass', '.git')):
proc = subprocess.Popen(
(
'git', '-C', 'libsass', 'describe',
'--abbrev=4', '--dirty', '--always', '--tags',
),
stdout=subprocess.PIPE,
)
out, _ = proc.communicate()
assert not proc.returncode, proc.returncode
out = subprocess.check_output((
'git', '-C', 'libsass', 'describe',
'--abbrev=4', '--dirty', '--always', '--tags',
))
with open('.libsass-upstream-version', 'wb') as libsass_version_file:
libsass_version_file.write(out)

Expand All @@ -100,11 +82,9 @@ def _maybe_macos(flags):
libsass_version = libsass_version_file.read().decode('UTF-8').strip()
if sys.platform == 'win32':
# This looks wrong, but is required for some reason :(
version_define = r'/DLIBSASS_VERSION="\"{}\""'.format(
libsass_version,
)
define = r'/DLIBSASS_VERSION="\"{}\""'.format(libsass_version)
else:
version_define = '-DLIBSASS_VERSION="{}"'.format(libsass_version)
define = '-DLIBSASS_VERSION="{}"'.format(libsass_version)

for directory in (
os.path.join('libsass', 'src'),
Expand Down Expand Up @@ -137,45 +117,36 @@ def _maybe_macos(flags):
if get_build_version() < 14.0:
msvc9compiler.get_build_version = lambda: 14.0
msvc9compiler.VERSION = 14.0
flags = ['/Od', '/EHsc', '/MT']
else:
flags = [
'-fPIC', '-std=gnu++0x', '-Wall',
'-Wno-parentheses', '-Werror=switch',
]
_maybe_clang(flags)
_maybe_macos(flags)

if platform.system() in ('Darwin', 'FreeBSD'):
# Dirty workaround to avoid link error...
# Python distutils doesn't provide any way
# to configure different flags for each cc and c++.
cencode_path = os.path.join(LIBSASS_SOURCE_DIR, 'cencode.c')
cencode_body = ''
with open(cencode_path) as f:
cencode_body = f.read()
with open(cencode_path, 'w') as f:
f.write(
'#ifdef __cplusplus\n'
'extern "C" {\n'
'#endif\n',
)
f.write(cencode_body)
f.write(
'#ifdef __cplusplus\n'
'}\n'
'#endif\n',
)
elif platform.system() in ('Darwin', 'FreeBSD'):
# Dirty workaround to avoid link error...
# Python distutils doesn't provide any way
# to configure different flags for each cc and c++.
cencode_path = os.path.join(LIBSASS_SOURCE_DIR, 'cencode.c')
cencode_body = ''
with open(cencode_path) as f:
cencode_body = f.read()
with open(cencode_path, 'w') as f:
f.write(
'#ifdef __cplusplus\n'
'extern "C" {\n'
'#endif\n',
)
f.write(cencode_body)
f.write(
'#ifdef __cplusplus\n'
'}\n'
'#endif\n',
)

@atexit.register
def restore_cencode():
if os.path.isfile(cencode_path):
with open(cencode_path, 'w') as f:
f.write(cencode_body)
@atexit.register
def restore_cencode():
if os.path.isfile(cencode_path):
with open(cencode_path, 'w') as f:
f.write(cencode_body)

libraries = []
include_dirs = [os.path.join('.', 'libsass', 'include')]
extra_compile_args = flags + [version_define]
extra_compile_args.append(define)

sass_extension = Extension(
'_sass',
Expand All @@ -192,8 +163,7 @@ def version(sass_filename='sass.py'):
with open(sass_filename) as f:
tree = ast.parse(f.read(), sass_filename)
for node in tree.body:
if isinstance(node, ast.Assign) and \
len(node.targets) == 1:
if isinstance(node, ast.Assign) and len(node.targets) == 1:
target, = node.targets
if isinstance(target, ast.Name) and target.id == '__version__':
return node.value.s
Expand Down