[PyKDE] [PATCH] Compiling SIP with distutils

Giovanni Bajo rasky at develer.com
Thu Oct 27 15:29:23 BST 2005


Phil Thompson <phil at riverbankcomputing.co.uk> wrote:

>>> Any chance of some documentation?
>>
>> Within sipref.txt? Sure, I can write something if you agree on getting
>> this
>> included.
>
> Thanks,

OK, this is the documentation patch and the updated sipdistutils.py which
uses sipconfig to find out the name of the SIP executable. You'll still need
to make sure sipdistutils.py is installed when SIP is installed. Please, let
me know when it gets in.

Thanks!
-- 
Giovanni Bajo
-------------- next part --------------
# Subclasses disutils.command.build_ext,
# replacing it with a SIP version that compiles .sip -> .cpp
# before calling the original build_ext command.
# Written by Giovanni Bajo <rasky at develer dot com>
# Based on Pyrex.Distutils, written by Graham Fawcett and Darrel Gallion.

import distutils.command.build_ext
from distutils.dep_util import newer
import os
import sys

def replace_suffix(path, new_suffix):
    return os.path.splitext(path)[0] + new_suffix

class build_ext (distutils.command.build_ext.build_ext):

    description = "Compiler SIP descriptions, then build C/C++ extensions (compile/link to build directory)"

    def _get_sip_output_list(self, sbf):
        """
        Parse the sbf file specified to extract the name of the generated source
        files. Make them absolute assuming they reside in the temp directory.
        """
        for L in file(sbf):
            key, value = L.split("=", 1)
            if key.strip() == "sources":
                out = []
                for o in value.split():
                    out.append(os.path.join(self.build_temp, o))
                return out

        raise RuntimeError, "cannot parse SIP-generated '%s'" % sbf

    def _find_sip(self):
        import sipconfig
        cfg = sipconfig.Configuration()
        return cfg.sip_bin

    def swig_sources (self, sources, extension=None):
        if not self.extensions:
            return

        # Create the temporary directory if it does not exist already
        if not os.path.isdir(self.build_temp):
            os.makedirs(self.build_temp)

        # Collect the names of the source (.sip) files
        sip_sources = []
        sip_sources = [source for source in sources if source.endswith('.sip')]
        other_sources = [source for source in sources if not source.endswith('.sip')]
        generated_sources = []

        sip_bin = self._find_sip()

        for sip in sip_sources:
            # Use the sbf file as dependency check
            sipbasename = os.path.basename(sip)
            sbf = os.path.join(self.build_temp, replace_suffix(sipbasename, "sbf"))
            if newer(sip, sbf) or self.force:
              self._sip_compile(sip_bin, sip, sbf)
              out = self._get_sip_output_list(sbf)
              generated_sources.extend(out)

        return generated_sources + other_sources

    def _sip_compile(self, sip_bin, source, sbf):
        self.spawn([sip_bin,
                    "-c", self.build_temp,
                    "-b", sbf,
                    source])
-------------- next part --------------
--- sipref-original.txt	2005-10-24 04:30:11.000000000 +0200
+++ sipref.txt	2005-10-27 16:21:04.490875000 +0200
@@ -99,7 +99,7 @@
 
     - a sophisticated versioning system that allows the full lifetime of a C++
       class library, including any platform specific or optional features, to
-      be described in a single set of specification files 
+      be described in a single set of specification files
 
     - the ability to include documentation in the specification files which can
       be extracted and subsequently processed by external tools
@@ -108,9 +108,7 @@
       specification files that is automatically included in all generated
       source code
 
-    - a build system, written in Python, that you can extend to configure,
-      compile and install your own bindings without worrying about platform
-      specific issues
+    - support for building your extensions through distutils
 
     - SIP, and the bindings it produces, runs under UNIX, Linux, Windows and
       MacOS/X
@@ -153,7 +151,19 @@
       provides them with some common utility functions.  See also `Using the
       SIP Module in Applications`_.
 
-    - The SIP build system (``sipconfig.py``).  This is a pure Python module
+    - The SIP distutils extension (``sipdistutils.py``).  This is a distutils
+      extensions that can be used to build your extensions through distutils.
+      This is as simple as adding your .sip files to the list of files needed
+      to build the extension.
+
+.. sidebar:: Deprecation
+
+    **WARNING**: The SIP build system is deprecated and will be probably
+    removed from a future version of SIP.  Use the SIP distutils extension
+    instead.
+..
+
+    - The SIP build system (``sipconfig.py``). This is a pure Python module
       that is created when SIP is configured and encapsulates all the necessary
       information about your system including relevant directory names,
       compiler and linker flags, and version numbers.  It also includes several
@@ -162,6 +172,8 @@
       System`_.
 
 
+
+
 Qt Support
 ----------
 
@@ -344,7 +356,7 @@
     %End
 
     public:
-        Word(const char *w);
+        Word(const char *);
 
         char *reverse() const;
     };
@@ -374,7 +386,43 @@
 
 However, that still leaves us with the task of compiling the generated code and
 linking it against all the necessary libraries.  It's much easier to use the
-SIP build system to do the whole thing.
+distutils extension to do the whole thing.
+
+
+Building your extension with distutils
+--------------------------------------
+
+To build the example above with distutils, it is sufficiente to create a
+standard ``setup.py``, listing ``word.sip`` among the files to build, and
+hook-up SIP into distutils::
+
+    #!/usr/bin/env python
+    from distutils.core import setup, Extension
+    import sipdistutils
+
+    setup(
+      name = 'word',
+      versione = '1.0',
+      ext_modules=[
+        Extension("word", ["word.sip", "word.cpp"]),
+        ],
+
+      cmdclass = {'build_ext': sipdistutils.build_ext}   # Hook up SIP!
+    )
+
+As we can see, the above is a normal distutils setup script, with just
+a special line which is needed so that SIP can see and process ``word.sip``.
+Then, running ``setup.py build`` will build our extension.
+
+
+Building your extension with the SIP Build System
+-------------------------------------------------
+
+.. sidebar:: Deprecation
+
+    **WARNING**: The SIP build system is deprecated and will be probably
+    removed from a future version of SIP.  Use the SIP distutils extension
+    instead.
 
 Using the SIP build system is simply a matter of writing a small Python script.
 In this simple example we will assume that the ``word`` library we are wrapping
@@ -398,7 +446,7 @@
 
     # Run SIP to generate the code.
     os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "word.sip"]))
-    
+
     # Create the Makefile.
     makefile = sipconfig.SIPModuleMakefile(config, build_file)
 
@@ -478,7 +526,7 @@
       This tells SIP that a newly created structure is being returned and it is
       owned by Python.
 
-The ``configure.py`` build system script described in the previous example can
+The ``setup.py`` distutils script described in the previous example can
 be used for this example without change.
 
 
@@ -781,9 +829,9 @@
         file is not generated.
 -b file
         The name of the build file to generate.  This file contains the
-        information about the module needed by the SIP build system to generate
-        a platform and compiler specific Makefile for the module.  By default
-        the file is not generated.
+        information about the module needed by the SIP distutils extension
+        (or the SIP build system) to generate a platform and compiler specific
+        Makefile for the module.  By default the file is not generated.
 -c dir  The name of the directory (which must exist) into which all of the
         generated C or C++ code is placed.  By default no code is generated.
 -d file
@@ -1775,7 +1823,7 @@
     ==========
     An Example
     ==========
-    
+
     This fragment of documentation is reStructuredText and will appear in the
     module in which it is defined and all modules that %Import it.
     %End
@@ -3298,7 +3346,7 @@
     In the following description the first letter is the format character, the
     entry in parentheses is the Python object type that the format character
     will create, and the entry in brackets are the types of the C/C++ values
-    to be passed. 
+    to be passed.
 
     ``a`` (string) [char \*, int]
         Convert a C/C++ character array and its length to a Python string.  If
@@ -3600,7 +3648,7 @@
     In the following description the first letter is the format character, the
     entry in parentheses is the Python object type that the format character
     will convert, and the entry in brackets are the types of the C/C++ values
-    to be passed. 
+    to be passed.
 
     ``a`` (string) [char \*\*, int \*]
         Convert a Python string to a C/C++ character array and its length.  If
@@ -3935,6 +3983,12 @@
 The SIP Build System
 ====================
 
+.. sidebar:: Deprecation
+
+    **WARNING**: The SIP build system is deprecated and will be probably
+    removed from a future version of SIP.  Use the SIP distutils extension
+    instead.
+
 The purpose of the build system is to make it easy for you to write
 configuration scripts in Python for your own bindings.  The build system takes
 care of the details of particular combinations of platform and compiler.  It
@@ -3943,9 +3997,7 @@
 The build system is implemented as a pure Python module called ``sipconfig``
 that contains a number of classes and functions.  Using this module you can
 write bespoke configuration scripts (e.g. PyQt's ``configure.py``) or use it
-with other Python based build systems (e.g.
-`Distutils <http://www.python.org/sigs/distutils-sig/distutils.html>`_ and
-`SCons <http://www.scons.org>`_).
+with other Python based build systems (e.g. `SCons <http://www.scons.org>`_).
 
 An important feature of SIP is the ability to generate bindings that are built
 on top of existing bindings.  For example, both


More information about the PyQt mailing list