Skip to content

Commit

Permalink
System sass (#219)
Browse files Browse the repository at this point in the history
Use env var to allow system libsass
  • Loading branch information
Marcel Plch authored and asottile committed Oct 11, 2017
1 parent 8efb498 commit bcdafc3
Showing 1 changed file with 150 additions and 98 deletions.
248 changes: 150 additions & 98 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,76 +14,13 @@

from setuptools import Extension, setup

LIBSASS_SOURCE_DIR = os.path.join('libsass', 'src')

if (
not os.path.isfile(os.path.join('libsass', 'Makefile')) and
os.path.isdir('.git')
):
print(file=sys.stderr)
print('Missing the libsass sumbodule. Try:', file=sys.stderr)
print(' git submodule update --init', file=sys.stderr)
print(file=sys.stderr)
exit(1)


# 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
with open('.libsass-upstream-version', 'wb') as libsass_version_file:
libsass_version_file.write(out)

# The version file should always exist at this point
with open('.libsass-upstream-version', 'rb') as libsass_version_file:
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="\"{0}\""'.format(libsass_version)
else:
version_define = '-DLIBSASS_VERSION="{0}"'.format(libsass_version)
system_sass = os.environ.get('SYSTEM_SASS', False)

sources = ['pysass.cpp']
headers = []
for directory in (
os.path.join('libsass', 'src'),
os.path.join('libsass', 'include')
):
for pth, _, filenames in os.walk(directory):
for filename in filenames:
filename = os.path.join(pth, filename)
if filename.endswith(('.c', '.cpp')):
sources.append(filename)
elif filename.endswith('.h'):
headers.append(filename)

if sys.platform == 'win32':
from distutils.msvc9compiler import get_build_version
vscomntools_env = 'VS{0}{1}COMNTOOLS'.format(
int(get_build_version()),
int(get_build_version() * 10) % 10
)
try:
os.environ[vscomntools_env] = os.environ['VS140COMNTOOLS']
except KeyError:
distutils.log.warn('You probably need Visual Studio 2015 (14.0) '
'or higher')
from distutils import msvccompiler, msvc9compiler
if msvccompiler.get_build_version() < 14.0:
msvccompiler.get_build_version = lambda: 14.0
if get_build_version() < 14.0:
msvc9compiler.get_build_version = lambda: 14.0
msvc9compiler.VERSION = 14.0
flags = ['/Od', '/EHsc', '/MT']
link_flags = []
else:
version_define = ''

if system_sass:
flags = [
'-fPIC', '-std=gnu++0x', '-Wall', '-Wno-parentheses', '-Werror=switch',
]
Expand All @@ -104,51 +41,166 @@ def customize_compiler(compiler):
flags.append('-stdlib=libc++')
if platform.system() == 'Darwin':
flags.append('-mmacosx-version-min=10.7',)
if tuple(map(int, platform.mac_ver()[0].split('.'))) >= (10, 9):
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', # noqa
'-Wno-error=unused-command-line-' +
'argument-hard-error-in-future', # noqa
)
# 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
extern "C" {
#endif
''')
f.write(cencode_body)
f.write('''
#ifdef __cplusplus
}
#endif
''')

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

flags = ['-c', '-O3'] + flags

flags = ['-c', '-O3'] + flags

if platform.system() == 'FreeBSD':
link_flags = ['-fPIC', '-lc++']
else:
link_flags = ['-fPIC', '-lstdc++']
libraries = ['sass']
include_dirs = []
extra_compile_args = flags
extra_link_args = link_flags
else:
LIBSASS_SOURCE_DIR = os.path.join('libsass', 'src')

if (
not os.path.isfile(os.path.join('libsass', 'Makefile')) and
os.path.isdir('.git')
):
print(file=sys.stderr)
print('Missing the libsass sumbodule. Try:', file=sys.stderr)
print(' git submodule update --init', file=sys.stderr)
print(file=sys.stderr)
exit(1)

# 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
with open('.libsass-upstream-version', 'wb') as libsass_version_file:
libsass_version_file.write(out)

# The version file should always exist at this point
with open('.libsass-upstream-version', 'rb') as libsass_version_file:
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="\"{0}\""'.format(
libsass_version)
else:
version_define = '-DLIBSASS_VERSION="{0}"'.format(libsass_version)

for directory in (
os.path.join('libsass', 'src'),
os.path.join('libsass', 'include')
):
for pth, _, filenames in os.walk(directory):
for filename in filenames:
filename = os.path.join(pth, filename)
if filename.endswith(('.c', '.cpp')):
sources.append(filename)
elif filename.endswith('.h'):
headers.append(filename)

if sys.platform == 'win32':
from distutils.msvc9compiler import get_build_version
vscomntools_env = 'VS{0}{1}COMNTOOLS'.format(
int(get_build_version()),
int(get_build_version() * 10) % 10
)
try:
os.environ[vscomntools_env] = os.environ['VS140COMNTOOLS']
except KeyError:
distutils.log.warn('You probably need Visual Studio 2015 (14.0) '
'or higher')
from distutils import msvccompiler, msvc9compiler
if msvccompiler.get_build_version() < 14.0:
msvccompiler.get_build_version = lambda: 14.0
if get_build_version() < 14.0:
msvc9compiler.get_build_version = lambda: 14.0
msvc9compiler.VERSION = 14.0
flags = ['/Od', '/EHsc', '/MT']
link_flags = []
else:
flags = [
'-fPIC', '-std=gnu++0x', '-Wall',
'-Wno-parentheses', '-Werror=switch',
]
platform.mac_ver()
if platform.system() in ['Darwin', 'FreeBSD']:
os.environ.setdefault('CC', 'clang')
os.environ.setdefault('CXX', 'clang++')
orig_customize_compiler = distutils.sysconfig.customize_compiler

def customize_compiler(compiler):
orig_customize_compiler(compiler)
compiler.compiler[0] = os.environ['CC']
compiler.compiler_so[0] = os.environ['CXX']
compiler.compiler_cxx[0] = os.environ['CXX']
compiler.linker_so[0] = os.environ['CXX']
return compiler
distutils.sysconfig.customize_compiler = customize_compiler
flags.append('-stdlib=libc++')
if platform.system() == 'Darwin':
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', # noqa
)
# 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
extern "C" {
#endif
''')
f.write(cencode_body)
f.write('''
#ifdef __cplusplus
}
#endif
''')

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

flags = ['-c', '-O3'] + flags

if platform.system() == 'FreeBSD':
link_flags = ['-fPIC', '-lc++']
else:
link_flags = ['-fPIC', '-lstdc++']

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

sources.sort()
sass_extension = Extension(
'_sass',
sources,
include_dirs=[os.path.join('.', 'libsass', 'include')],
include_dirs=include_dirs,
depends=headers,
extra_compile_args=flags + [version_define],
extra_compile_args=extra_compile_args,
extra_link_args=link_flags,
libraries=libraries
)

install_requires = ['six']
Expand Down

0 comments on commit bcdafc3

Please sign in to comment.