5

I'm trying to make a setup.py for cgal-bindings. To install this, the user needs to have at least a certain version of CGAL. In addition, CGAL has a few optional targets that should be built if the user has some libraries (like Eigen3). Is there a cross-platform way in Python to check for this?

I can use find_library in ctypes.util to check if the library exists, but I don't see any easy way to get the version. <-- This doesn't actually work all the time, some libraries are header-only like eigen3, which is a C++ template library.

Using the install_requires argument of setup() only works for Python libraries and CGAL is a C/C++ library.

1 Answer 1

5

Whether a particular extension module should be compiled depending on the availability of some library version, can be accomplished by dynamically generating the ext_modules argument of setup() in setup.py.

For the _yaml.so module of ruamel.yaml, that only should be compiled when the libyaml development libraries have been installed on the system I do:

import os
from textwrap import dedent

def check_extensions():
    """check if the C module can be build by trying to compile a small 
    program against the libyaml development library"""

    import tempfile
    import shutil

    import distutils.sysconfig
    import distutils.ccompiler
    from distutils.errors import CompileError, LinkError

    libraries = ['yaml']

    # write a temporary .c file to compile
    c_code = dedent("""
    #include <yaml.h>

    int main(int argc, char* argv[])
    {
        yaml_parser_t parser;
        parser = parser;  /* prevent warning */
        return 0;
    }
    """)
    tmp_dir = tempfile.mkdtemp(prefix = 'tmp_ruamel_yaml_')
    bin_file_name = os.path.join(tmp_dir, 'test_yaml')
    file_name = bin_file_name + '.c'
    with open(file_name, 'w') as fp:
        fp.write(c_code)

    # and try to compile it
    compiler = distutils.ccompiler.new_compiler()
    assert isinstance(compiler, distutils.ccompiler.CCompiler)
    distutils.sysconfig.customize_compiler(compiler)

    try:
        compiler.link_executable(
            compiler.compile([file_name]),
            bin_file_name,
            libraries=libraries,
        )
    except CompileError:
        print('libyaml compile error')
        ret_val = None
    except LinkError:
        print('libyaml link error')
        ret_val = None
    else:
        ret_val = [
            Extension(
                '_yaml',
                sources=['ext/_yaml.c'],
                libraries=libraries,
                ),
        ]
    shutil.rmtree(tmp_dir)
    return ret_val

This way you require no extra files in the distribution. Even if you cannot fail to compile based on the version number at compile time, you should be able to run the resulting program from the temporary directory and check the exit value and/or output.

4
  • 1
    Autotools also does this so I'd say this is the way to go. Although I think you could make it more cross-platform if you used the CCompiler class in distutils.
    – sciencectn
    Mar 10, 2015 at 1:42
  • @sciencectn I updated the function ( I knew I was taking shortcuts, but I got stranded doing this with CCompiler the first time I tried). This now fails if either the header or the library is not available. It would be nice if distutils/setuptools had some option to check for availability of a library (and its version) to decide whether to include building a particular extension in setup.py or not.
    – Anthon
    Mar 10, 2015 at 8:54
  • Your comment and answer from the 4th got me going again.
    – Anthon
    Mar 10, 2015 at 10:19
  • 2
    Sweet, I'm using this. Gave you credit in my setup.py.
    – sciencectn
    Mar 11, 2015 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.