From ad54dd11baddae7cc4c4c9578f33a2f21e2f8b2b Mon Sep 17 00:00:00 2001 From: K900 Date: Mon, 25 Feb 2013 02:09:49 +0400 Subject: [PATCH 1/5] Updates and fixes to the build system. 1) Strongly depend on Cython. Cython.Distutils confirmed present on win32/linux2 python2.7/python3.3 with cython 0.18. 2) Remove setup3k.py, setup.py is python2/python3 compatible now. 3) Hack around the patch.py step by defining DL_IMPORT at runtime. TODO: find out when exactly DL_IMPORT was removed and update version checks accordingly --- patch.py | 31 --------------- setup.py | 110 +++++++++++++++++++-------------------------------- setup3k.py | 114 ----------------------------------------------------- 3 files changed, 41 insertions(+), 214 deletions(-) delete mode 100755 patch.py delete mode 100644 setup3k.py diff --git a/patch.py b/patch.py deleted file mode 100755 index f418ae8..0000000 --- a/patch.py +++ /dev/null @@ -1,31 +0,0 @@ -#! /usr/bin/env python2 -# -*- coding: utf-8 -*- - -"""This script modifies some Cython-generated files. - - Currently, it's only needed to call it when building for Python 3.""" - - -import os.path -import re - - -def patch_sf_h(): - """Remove the DL_IMPORT macros in src/sfml.h.""" - - filename = os.path.join('src', 'sfml.h') - - with open(filename, 'r') as f: - source = f.read() - - source = re.sub(r'DL_IMPORT\(([\w\s:]+)\)', r'\1', source) - - with open(filename, 'w') as f: - f.write(source) - -def main(): - patch_sf_h() - - -if __name__ == '__main__': - main() diff --git a/setup.py b/setup.py index e85fb2c..f5da02f 100644 --- a/setup.py +++ b/setup.py @@ -27,89 +27,61 @@ # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. - # When creating a Windows installer, drop the SFML and dependent DLLs # in the current folder and they will be included in the installer. - -# Set to False if you don't have Cython installed. The script will -# then build the extension module from the sf.cpp file, like a regular -# extension. -USE_CYTHON = True - - import glob import os.path import sys from distutils.core import setup from distutils.extension import Extension from distutils.command.build_ext import build_ext - -if USE_CYTHON: - import Cython.Distutils - +import Cython.Distutils def src(path): return os.path.join('src', path) - -print >> sys.stderr, ("\nIf the build fails, run patch.py and try again\n" - "----------------------------------------------\n") - -libs = ['sfml-graphics', 'sfml-window', 'sfml-audio', 'sfml-system'] - -if USE_CYTHON: - ext_modules = [Extension('sfml', [src('sfml.pyx'), src('hacks.cpp')], - language='c++', - libraries=libs)] +if sys.version_info.major > 2: + cflags = ["-D", "DL_IMPORT(RTYPE)=RTYPE"] else: - ext_modules = [Extension('sfml', [src('sfml.cpp'), src('hacks.cpp')], - libraries=libs)] - + cflags = [] + +ext_modules = [Extension( + 'sfml', + [src('sfml.pyx'), src('hacks.cpp')], + language='c++', + libraries=['sfml-graphics', 'sfml-window', 'sfml-audio', 'sfml-system'], + extra_compile_args=cflags + )] + with open('README.md', 'r') as f: long_description = f.read() -kwargs = dict(name='pySFML', - ext_modules=ext_modules, - version='0.2.1', - description='A Python binding for SFML 2', - long_description=long_description, - author=u'Bastien Léonard', - author_email='bastien.leonard@gmail.com', - url='https://github.com/bastienleonard/pysfml-cython', - license='BSD', - data_files=[ - ('', glob.glob('*.dll')), - (os.path.join('lib', 'site-packages', 'pysfml-cython'), - ['LICENSE.txt', 'SFML-LICENSE.txt'])], - classifiers=[ - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: BSD License', - 'Operating System :: OS Independent', - 'Programming Language :: Cython', - 'Topic :: Games/Entertainment', - 'Topic :: Multimedia', - 'Topic :: Software Development :: Libraries :: Python Modules' - ]) - - -if USE_CYTHON: - kwargs.update(cmdclass={'build_ext': Cython.Distutils.build_ext}) -else: - class CustomBuildExt(build_ext): - """This class is used to build the Windows binary releases.""" - - def build_extensions(self): - cc = self.compiler.compiler_type - - if cc == 'mingw32': - for e in self.extensions: - # e.extra_compile_args = [] - e.extra_link_args = ['-static-libgcc', '-static-libstdc++'] - - build_ext.build_extensions(self) - - kwargs.update(cmdclass={'build_ext': CustomBuildExt}) - -setup(**kwargs) +setup( + name='pySFML', + ext_modules=ext_modules, + version='0.2.1', + description='A Python binding for SFML 2', + long_description=long_description, + author='Bastien Leonard', + author_email='bastien.leonard@gmail.com', + url='https://github.com/bastienleonard/pysfml-cython', + license='BSD', + data_files=[ + ('', glob.glob('*.dll')), + (os.path.join('lib', 'site-packages', 'pysfml-cython'), ['LICENSE.txt', 'SFML-LICENSE.txt']) + ], + classifiers=[ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: BSD License', + 'Operating System :: OS Independent', + 'Programming Language :: Cython', + 'Topic :: Games/Entertainment', + 'Topic :: Multimedia', + 'Topic :: Software Development :: Libraries :: Python Modules' + ], + cmdclass={ + 'build_ext': Cython.Distutils.build_ext + } +) \ No newline at end of file diff --git a/setup3k.py b/setup3k.py deleted file mode 100644 index fc59136..0000000 --- a/setup3k.py +++ /dev/null @@ -1,114 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright 2010, 2011, 2012 Bastien Léonard. All rights reserved. - -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: - -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. - -# 2. Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following -# disclaimer in the documentation and/or other materials provided -# with the distribution. - -# THIS SOFTWARE IS PROVIDED BY BASTIEN LÉONARD ``AS IS'' AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BASTIEN LÉONARD OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT -# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -# SUCH DAMAGE. - - -# When creating a Windows installer, drop the SFML and dependent DLLs -# in the current folder and they will be included in the installer. - - -# Set to False if you don't have Cython installed. The script will -# then build the extension module from the sf.cpp file, like a regular -# extension. -USE_CYTHON = True - - -import glob -import os.path -import sys -from distutils.core import setup -from distutils.extension import Extension -from distutils.command.build_ext import build_ext - -if USE_CYTHON: - import Cython.Distutils - - -def src(path): - return os.path.join('src', path) - - -print("\nIf the build fails, run patch.py and try again\n" - "----------------------------------------------\n", file=sys.stderr) - -libs = ['sfml-graphics', 'sfml-window', 'sfml-audio', 'sfml-system'] - -if USE_CYTHON: - ext_modules = [Extension('sfml', [src('sfml.pyx'), src('hacks.cpp')], - language='c++', - libraries=libs)] -else: - ext_modules = [Extension('sfml', [src('sfml.cpp'), src('hacks.cpp')], - libraries=libs)] - -with open('README.md', 'r') as f: - long_description = f.read() - -kwargs = dict(name='pySFML', - ext_modules=ext_modules, - version='0.2.1', - description='A Python binding for SFML 2', - long_description=long_description, - author='Bastien Léonard', - author_email='bastien.leonard@gmail.com', - url='https://github.com/bastienleonard/pysfml-cython', - license='BSD', - data_files=[ - ('', glob.glob('*.dll')), - (os.path.join('lib', 'site-packages', 'pysfml-cython'), - ['LICENSE.txt', 'SFML-LICENSE.txt'])], - classifiers=[ - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: BSD License', - 'Operating System :: OS Independent', - 'Programming Language :: Cython', - 'Topic :: Games/Entertainment', - 'Topic :: Multimedia', - 'Topic :: Software Development :: Libraries :: Python Modules' - ]) - -if USE_CYTHON: - kwargs.update(cmdclass={'build_ext': Cython.Distutils.build_ext}) -else: - class CustomBuildExt(build_ext): - """This class is used to build the Windows binary releases.""" - - def build_extensions(self): - cc = self.compiler.compiler_type - - if cc == 'mingw32': - for e in self.extensions: - # e.extra_compile_args = [] - e.extra_link_args = ['-static-libgcc', '-static-libstdc++'] - - build_ext.build_extensions(self) - - kwargs.update(cmdclass={'build_ext': CustomBuildExt}) - -setup(**kwargs) From 220ec5211839950bbeb8332c3e6be1f4d87300e2 Mon Sep 17 00:00:00 2001 From: K900 Date: Mon, 25 Feb 2013 03:00:20 +0400 Subject: [PATCH 2/5] Update documentation (quick and dirty) --- doc/sphinx/source/building.rst | 79 +++++----------------------------- 1 file changed, 11 insertions(+), 68 deletions(-) diff --git a/doc/sphinx/source/building.rst b/doc/sphinx/source/building.rst index 8e1eccd..07f6a82 100644 --- a/doc/sphinx/source/building.rst +++ b/doc/sphinx/source/building.rst @@ -108,9 +108,9 @@ because it links to another C or C++ runtime. Common build options -------------------- -You can build the module with the ``setup.py`` script (or -``setup3k.py`` for Python 3). This section discusses some common -options that you may need or find useful. +You can build the module with the ``setup.py`` script. +This section discusses some common options that you may need or +find useful. ``--inplace`` means that the module will be dropped in the current directory. I find this more practical, as it makes it easier to test @@ -128,44 +128,18 @@ In the end, the command will look something like this:: python setup.py build_ext --inplace --compiler=mingw32 -.. _building_without_cython: +.. _building: -Building without Cython ------------------------ - -.. warning:: - - Github has removed the downloads feature, so I don't plan to - package source releases anymore. Windows users can use the - installers, and it should be easier for other users to build the - module now that recent versions of Cython correctly build the - module out of the box. - -If you download a source release at the `download page -`_, you -don't need to install Cython, since the release already contains the -files that Cython would generate. - -Make sure that ``USE_CYTHON`` is set to ``False`` in setup.py (or -setup3k.py, if you're building for Python 3). You can then build the -module by typing this command:: - - python setup.py build_ext - - -.. _building_with_cython: - -Building with Cython installed +Building ------------------------------ .. warning:: With older versions of Cython, the binding won't work correctly when built straight from the Git repo. If you want to build from - the source, you're encouraged to use the latest source release. See - :ref:`building_without_cython`. If you really want to build from - Git, you need to modify the generated sfml.cpp file. You need all - these declarations:: + the source, you're encouraged to use the latest source release. + If you really want to build from Git, you need to modify the + generated sfml.cpp file. You need all these declarations:: __PYX_EXTERN_C DL_EXPORT(PyObject) *wrap_time_instance(sf::Time *); __PYX_EXTERN_C DL_EXPORT(PyObject) *wrap_render_target_instance(sf::RenderTarget *); @@ -197,10 +171,6 @@ When you've done so, you can build the module by typing this command:: python setup.py build_ext -If you get an error related with ``DL_IMPORT``, refer to the end of -the :ref:`python3` section. - - .. _python3: Building a Python 3 module @@ -209,29 +179,7 @@ Building a Python 3 module It's possible to build a Python 3 module, but you may encounter a few minor problems. -First of all, on my machine, the Cython class used in ``setup3k.py`` to -automate Cython invocation is only installed for Python 2. It's -probably possible to install it for Python 3, but it's not complicated -to invoke Cython manually:: - - cython --cplus sfml.pyx - -The next step is to invoke the ``setup3k.py`` script to build the -module. Since we called Cython already, make sure that ``USE_CYTHON`` -is set to ``False`` in ``setup3k.py``, then invoke this command:: - - python3 setup3k.py build_ext - -(Note that you may have to type ``python`` instead of ``python3``; -typically, GNU/Linux systems provide this as a way to call a specific -version of the interpreter, but I'm not sure that's the case for all -of them as well as Windows.) - -(Also note that on GNU/Linux, the generated file won't be called -``sfml.so`` but something like ``sfml.cpython-32mu.so``. Apparently, -on Windows it's still ``sfml.pyd``.) - -The second problem used to be that you had to use bytes instead of +The main problem used to be that you had to use bytes instead of Unicode e.g. when passing a filename or window title to SFML. This is now gone, except possibly in methods that I forgot to fix; make sure to report the issue if you encounter such a case. When you pass a @@ -239,10 +187,5 @@ Unicode object to these methods, they now encode it in UTF-8 before passing them to SFML. You can change the encoding by setting the :attr:`default_encoding` variable at any time. -Finally, compilation may fail because the ``src/sfml.h`` file -generated by Cython uses the deprecated ``DL_IMPORT()`` macro. At the -root of the project, there is a ``patch.py`` script that will remove -the offending macros for you. The trick is that ``src/sfml.h`` will -not exist at first; the setup script will create it, then try to -compile it and fail. That's when you need to use ``patch.py``, and -build the module again. +There were also some building issues, but they are now (hopefully) +resolved. From 75da14d995f2fe32b10f37edab7060d1a847f925 Mon Sep 17 00:00:00 2001 From: K900 Date: Mon, 25 Feb 2013 03:13:27 +0400 Subject: [PATCH 3/5] =?UTF-8?q?Fix=20L=C3=A9onard.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f5da02f..a4b26e5 100644 --- a/setup.py +++ b/setup.py @@ -38,6 +38,14 @@ from distutils.command.build_ext import build_ext import Cython.Distutils +if sys.version_info.major > 3: + def as_unicode(string): + return string +else: + import codecs + def as_unicode(string): + return codecs.unicode_escape_decode(string)[0] + def src(path): return os.path.join('src', path) @@ -63,7 +71,7 @@ def src(path): version='0.2.1', description='A Python binding for SFML 2', long_description=long_description, - author='Bastien Leonard', + author=as_unicode('Bastien L\\xe9onard'), author_email='bastien.leonard@gmail.com', url='https://github.com/bastienleonard/pysfml-cython', license='BSD', From d85ea5cd3ae937c241bcdf55e1091711aa365798 Mon Sep 17 00:00:00 2001 From: K900 Date: Tue, 26 Feb 2013 21:03:09 +0400 Subject: [PATCH 4/5] =?UTF-8?q?pep8-ify=20setup.py,=20update=20MANIFEST.in?= =?UTF-8?q?,=20fix=20L=C3=A9onard=20again.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MANIFEST.in | 2 +- setup.py | 64 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 1b02f7d..94df4f8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ -include setup.py setup3k.py patch.py README.md CHANGELOG.txt LICENSE.txt +include setup.py README.md *.txt recursive-include src *.py *.pyx *.pxd *.cpp *.hpp *.c *.h recursive-include tests *.py recursive-include examples *.py *.txt *.png *.jpg *.bmp *.sfx *.wav *.ttf diff --git a/setup.py b/setup.py index a4b26e5..16c6120 100644 --- a/setup.py +++ b/setup.py @@ -35,61 +35,69 @@ import sys from distutils.core import setup from distutils.extension import Extension -from distutils.command.build_ext import build_ext import Cython.Distutils -if sys.version_info.major > 3: - def as_unicode(string): - return string -else: - import codecs - def as_unicode(string): - return codecs.unicode_escape_decode(string)[0] def src(path): return os.path.join('src', path) if sys.version_info.major > 2: + author = 'Bastien Léonard' cflags = ["-D", "DL_IMPORT(RTYPE)=RTYPE"] else: + author = 'Bastien Léonard'.decode('utf-8') cflags = [] - -ext_modules = [Extension( - 'sfml', - [src('sfml.pyx'), src('hacks.cpp')], - language='c++', - libraries=['sfml-graphics', 'sfml-window', 'sfml-audio', 'sfml-system'], - extra_compile_args=cflags - )] - -with open('README.md', 'r') as f: - long_description = f.read() setup( name='pySFML', - ext_modules=ext_modules, version='0.2.1', description='A Python binding for SFML 2', - long_description=long_description, - author=as_unicode('Bastien L\\xe9onard'), + long_description=open('README.md').read(), + + author=author, author_email='bastien.leonard@gmail.com', url='https://github.com/bastienleonard/pysfml-cython', - license='BSD', - data_files=[ - ('', glob.glob('*.dll')), - (os.path.join('lib', 'site-packages', 'pysfml-cython'), ['LICENSE.txt', 'SFML-LICENSE.txt']) - ], + license='LICENSE.txt', + classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Cython', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 3', 'Topic :: Games/Entertainment', 'Topic :: Multimedia', 'Topic :: Software Development :: Libraries :: Python Modules' ], + + data_files=[ + ('', glob.glob('*.dll')), + (os.path.join('lib', 'site-packages', 'pysfml-cython'), + ['LICENSE.txt', 'SFML-LICENSE.txt'] + ) + ], + + ext_modules=[ + Extension( + 'sfml', + [ + src('sfml.pyx'), + src('hacks.cpp') + ], + language='c++', + libraries=[ + 'sfml-audio', + 'sfml-graphics', + 'sfml-system', + 'sfml-window' + ], + extra_compile_args=cflags + ) + ], + cmdclass={ 'build_ext': Cython.Distutils.build_ext } -) \ No newline at end of file +) From 1c290815d22f2ac1fb06d868c6c74e0ae5fc510f Mon Sep 17 00:00:00 2001 From: K900 Date: Tue, 26 Feb 2013 21:07:07 +0400 Subject: [PATCH 5/5] Update .gitignore --- .gitignore | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index d8428bd..4ef70c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ build/ -sfml.so -sfml.cpython-32mu.so -sfml.cpython-64mu.so +dist/ +MANIFEST src/sfml.cpp src/sfml.h *~