Billingegroup / cookiecutter

A cookie-cutter for DiffPy packages.
Other
0 stars 7 forks source link

Boost extensions #84

Closed Sparks29032 closed 3 weeks ago

Sparks29032 commented 3 weeks ago

Issues resolved:

Add option to cookiecutter.json asking if it is_boost_wrapper. If so, generate extensions directory and setup.py for extensions. The following is the generated setup.py when applied to diffpy.utils.

#!/usr/bin/env python

# Extensions script for diffpy.utils

import os
import re
import sys
import glob
from setuptools import setup
from setuptools import Extension

# Define extension arguments here
ext_kws = {
        'libraries' : [],
        'extra_compile_args' : [],
        'extra_link_args' : [],
        'include_dirs' : [],
}
# Figure out the tagged name of boost_python library.
def get_boost_libraries():
    """Check for installed boost_python shared library.

    Returns list of required boost_python shared libraries that are installed
    on the system. If required libraries are not found, an Exception will be
    thrown.
    """
    baselib = "boost_python"
    major, minor = (str(x) for x in sys.version_info[:2])
    pytags = [major + minor, major, '']
    mttags = ['', '-mt']
    boostlibtags = [(pt + mt) for mt in mttags for pt in pytags] + ['']
    from ctypes.util import find_library
    for tag in boostlibtags:
        lib = baselib + tag
        found = find_library(lib)
        if found: break

    # Show warning when library was not detected.
    if not found:
        import platform
        import warnings
        ldevname = 'LIBRARY_PATH'
        if platform.system() == 'Darwin':
            ldevname = 'DYLD_FALLBACK_LIBRARY_PATH'
        wmsg = ("Cannot detect name suffix for the %r library."
                "Consider setting %s.") % (baselib, ldevname)
        warnings.warn(wmsg)

    libs = [lib]
    return libs

def create_extensions():
    "Initialize Extension objects for the setup function."
    blibs = [n for n in get_boost_libraries()
            if not n in ext_kws['libraries']]
    ext_kws['libraries'] += blibs
    ext = Extension('diffpy.utils.utils_ext',
                    glob.glob('src/extensions/*.cpp'),
                    **ext_kws)
    return [ext]

# Extensions not included in pyproject.toml
setup_args = dict(
    ext_modules = [],
)

if __name__ == '__main__':
    setup_args['ext_modules'] = create_extensions()
    setup(**setup_args)
Sparks29032 commented 3 weeks ago

The following is the git diff for setup.py compared to the diffpy.srreal build: image image image Note for the above image, the Add space commit adds back the space in the diff. image