diff --git a/.gitignore b/.gitignore index 113c0681..9cb00ccb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,17 @@ +*.egg +*.egg-info +*.pyc +*.pyd +*.so +.*.swp +.DS_Store +._.DS_Store +.coverage +.tox +/.libsass-upstream-version build/ dist/ docs/_build testpkg/build/ testpkg/dist/ testpkg/testpkg/static/css/*.scss.css -._.DS_Store -.coverage -.DS_Store -.tox -.*.swp -*.egg -*.egg-info -*.so -*.pyc -*.pyd diff --git a/MANIFEST.in b/MANIFEST.in index 48a9e3ab..8b1f7906 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,5 +3,6 @@ recursive-include libsass *.cpp recursive-include libsass *.h recursive-include libsass *.hpp include libsass/Makefile +include .libsass-upstream-version include test/*.scss include README.rst diff --git a/Makefile b/Makefile index d10dbf2c..4a4db520 100644 --- a/Makefile +++ b/Makefile @@ -14,22 +14,29 @@ C_OBJECTS = $(patsubst libsass/src/%.c,build2/libsass/c/%.o,$(C_SOURCES)) CPP_SOURCES := $(wildcard libsass/src/*.cpp) CPP_OBJECTS = $(patsubst libsass/src/%.cpp,build2/libsass/cpp/%.o,$(CPP_SOURCES)) +LIBSASS_VERSION = $(shell git -C libsass describe --abbrev=4 --dirty --always --tags) + +BASEFLAGS := -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -fPIC -I./libsass/include $(PY_HEADERS) -Wno-parentheses -Werror=switch -DLIBSASS_VERSION='"$(LIBSASS_VERSION)"' +CFLAGS := $(BASEFLAGS) -Wstrict-prototypes +CPPFLAGS := $(BASEFLAGS) -std=c++0x +LFLAGS := -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fPIC -lstdc++ + all: _sass.so build2/libsass/c/%.o: libsass/src/%.c @mkdir -p build2/libsass/c/ - gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I./libsass/include $(PY_HEADERS) -c $^ -o $@ -c -O2 -fPIC -std=c++0x -Wall -Wno-parentheses -Werror=switch + gcc $(CFLAGS) -c $^ -o $@ build2/libsass/cpp/%.o: libsass/src/%.cpp @mkdir -p build2/libsass/cpp/ - gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I./libsass/include $(PY_HEADERS) -c $^ -o $@ -c -O2 -fPIC -std=c++0x -Wall -Wno-parentheses -Werror=switch + gcc $(CPPFLAGS) -c $^ -o $@ build2/pysass.o: pysass.cpp @mkdir -p build2 - gcc -pthread -fno-strict-aliasing -Wno-write-strings -DNDEBUG -g -fwrapv -O2 -Wall -fPIC -I./libsass/include $(PY_HEADERS) -c $^ -o $@ -c -O2 -fPIC -std=c++0x -Wall -Wno-parentheses -Werror=switch + gcc $(CPPFLAGS) -Wno-write-strings -c $^ -o $@ _sass.so: $(C_OBJECTS) $(CPP_OBJECTS) build2/pysass.o - g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro $^ -L./libsass -o $@ -fPIC -lstdc++ + g++ $(LFLAGS) $^ -o $@ .PHONY: clean clean: diff --git a/pysass.cpp b/pysass.cpp index 51fb3ea7..c4748615 100644 --- a/pysass.cpp +++ b/pysass.cpp @@ -634,6 +634,7 @@ PyObject* PySass_make_enum_dict() { void PySass_init_module(PyObject *module) { PyModule_AddObject(module, "OUTPUT_STYLES", PySass_make_enum_dict()); + PyModule_AddObject(module, "libsass_version", PyUnicode_FromString(libsass_version())); } #if PY_MAJOR_VERSION >= 3 diff --git a/sassc.py b/sassc.py index 0536747f..de40f8cf 100755 --- a/sassc.py +++ b/sassc.py @@ -74,13 +74,16 @@ import sys import time +import _sass from sass import __version__ as VERSION, OUTPUT_STYLES, CompileError, compile def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): parser = optparse.OptionParser( usage='%prog [options] SCSS_FILE [OUT_CSS_FILE]', - version='%prog ' + VERSION + version='%prog {0} (sass/libsass {1})'.format( + VERSION, _sass.libsass_version, + ), ) output_styles = list(OUTPUT_STYLES) output_styles = ', '.join(output_styles[:-1]) + ', or ' + output_styles[-1] diff --git a/setup.py b/setup.py index abe7f8bb..c238e4e0 100644 --- a/setup.py +++ b/setup.py @@ -5,10 +5,10 @@ import distutils.cmd import distutils.log import distutils.sysconfig -import os import os.path import platform import shutil +import subprocess import sys import tempfile @@ -16,15 +16,40 @@ LIBSASS_SOURCE_DIR = os.path.join('libsass', 'src') -if not os.path.isfile(os.path.join('libsass', 'Makefile')) and \ - os.path.isdir('.git'): +if ( + not os.path.isfile(os.path.join('libsass', 'Makefile')) and + os.path.isdir('.git') +): print(file=sys.stderr) - print('You seem to miss initializing submodules; ' - 'try the following command', 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) + sources = ['pysass.cpp'] headers = [] for directory in ( @@ -89,9 +114,7 @@ def customize_compiler(compiler): compiler.linker_so[0] = os.environ['CXX'] return compiler distutils.sysconfig.customize_compiler = customize_compiler - flags.extend([ - '-stdlib=libc++', - ]) + 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): @@ -134,10 +157,9 @@ def restore_cencode(): sass_extension = Extension( '_sass', sources, - library_dirs=[os.path.join('.', 'libsass', 'src')], include_dirs=[os.path.join('.', 'libsass', 'include')], depends=headers, - extra_compile_args=flags, + extra_compile_args=flags + [version_define], extra_link_args=link_flags, )