Skip to content
Snippets Groups Projects
setup.py 7.02 KiB
Newer Older
from distutils.core import setup, Extension
# Functions
def readme():
    """Opens readme file and returns content"""
    with open("README.md") as file:
        return file.read()

def license():
    """Opens license file and returns the content"""
    with open("LICENSE.md") as file:
        return file.read()

def check_version(installed_binary_c_version, required_binary_c_versions):
    """Function to check the installed version and compare it to the required version"""
    message = """
    The binary_c version that is installed ({}) does not match the binary_c versions ({})
    that this release of the binary_c python module requires.
David Hendriks's avatar
David Hendriks committed
    """.format(
        installed_binary_c_version, required_binary_c_versions
    )
    assert installed_binary_c_version in required_binary_c_versions, message
David Hendriks's avatar
David Hendriks committed
REQUIRED_BINARY_C_VERSIONS = ["2.1.7"]
GSL_DIR = os.getenv("GSL_DIR", None)
David Hendriks's avatar
David Hendriks committed
if not GSL_DIR:
        "Warning: GSL_DIR is not set, this might lead to errors along the installation if\
        there is no other version of GSL in the include dirs"
David Hendriks's avatar
David Hendriks committed
BINARY_C_DIR = os.getenv("BINARY_C", None)
if not BINARY_C_DIR:
    print("Error: the BINARY_C environment variable is not set. Aborting setup")
    quit()

# TODO: write code to know exact parent directory of this file.
CWD = os.getcwd()
David Hendriks's avatar
David Hendriks committed
############################################################
# Getting information from binary_c
############################################################

David Hendriks's avatar
David Hendriks committed
BINARY_C_CONFIG = os.path.join(BINARY_C_DIR, "binary_c-config")
David Hendriks's avatar
David Hendriks committed
    subprocess.run([BINARY_C_CONFIG, "version"], stdout=subprocess.PIPE, check=True)
David Hendriks's avatar
David Hendriks committed
check_version(BINARY_C_VERSION[0], REQUIRED_BINARY_C_VERSIONS)
David Hendriks's avatar
David Hendriks committed
    subprocess.run(
        [BINARY_C_CONFIG, "incdirs_list"], stdout=subprocess.PIPE, check=True
    )
    .stdout.decode("utf-8")
    .split()
)
David Hendriks's avatar
David Hendriks committed
    subprocess.run(
        [BINARY_C_CONFIG, "libdirs_list"], stdout=subprocess.PIPE, check=True
    )
    .stdout.decode("utf-8")
    .split()
)
BINARY_C_CFLAGS = (
    subprocess.run([BINARY_C_CONFIG, "cflags"], stdout=subprocess.PIPE, check=True)
    .stdout.decode("utf-8")
    .split()
)
# BINARY_C_CFLAGS.remove('-fvisibility=hidden')
BINARY_C_LIBS = (
    subprocess.run([BINARY_C_CONFIG, "libs_list"], stdout=subprocess.PIPE, check=True)
    .stdout.decode("utf-8")
    .split()
)

# create list of tuples of defined macros
David Hendriks's avatar
David Hendriks committed
    subprocess.run(
        [BINARY_C_CONFIG, "define_macros"], stdout=subprocess.PIPE, check=True
    )
    .stdout.decode("utf-8")
    .split()
)
David Hendriks's avatar
David Hendriks committed

LONE = re.compile("^-D(.+)$")
PARTNER = re.compile("^-D(.+)=(.+)$")
for x in DEFINES:
    y = PARTNER.match(x)
        BINARY_C_DEFINE_MACROS.extend([(y.group(1), y.group(2))])
            BINARY_C_DEFINE_MACROS.extend([(y.group(1), None)])
# add API header file
API_h = os.path.join(BINARY_C_DIR, "src", "API", "binary_c_API.h")

David Hendriks's avatar
David Hendriks committed
############################################################
# Setting all directories and LIBRARIES to their final values
David Hendriks's avatar
David Hendriks committed
############################################################
David Hendriks's avatar
David Hendriks committed
    [
        os.path.join(BINARY_C_DIR, "src"),
        os.path.join(BINARY_C_DIR, "src", "API"),
David Hendriks's avatar
David Hendriks committed
        "include",
    ]
    + [os.path.join(GSL_DIR, "include")]
    if GSL_DIR
    else []
)
David Hendriks's avatar
David Hendriks committed

LIBRARIES = ["binary_c"] + BINARY_C_LIBS + ["binary_c_api"]
David Hendriks's avatar
David Hendriks committed

David Hendriks's avatar
David Hendriks committed
    os.path.join(BINARY_C_DIR, "src"),
    "./",
    os.path.join(CWD, "lib/"),
David Hendriks's avatar
David Hendriks committed

David Hendriks's avatar
David Hendriks committed
    os.path.join(BINARY_C_DIR, "src"),
    "./",
    os.path.join(CWD, "lib/"),
David Hendriks's avatar
David Hendriks committed

# filter out duplicates
INCLUDE_DIRS = list(dict.fromkeys(INCLUDE_DIRS))
BINARY_C_LIBS = list(dict.fromkeys(BINARY_C_LIBS))
LIBRARIES = list(dict.fromkeys(LIBRARIES))
LIBRARY_DIRS = list(dict.fromkeys(LIBRARY_DIRS))
RUNTIME_LIBRARY_DIRS = list(dict.fromkeys(RUNTIME_LIBRARY_DIRS))

# 
# print('\n')
# print("BINARY_C_CONFIG: ", str(BINARY_C_CONFIG) + "\n")
# print("incdirs: ", str(INCLUDE_DIRS) + "\n")
# print("BINARY_C_LIBS: ", str(BINARY_C_LIBS) + "\n")
# print("LIBRARIES: ", str(LIBRARIES) + "\n")
# print("LIBRARY_DIRS: ", str(LIBRARY_DIRS) + "\n")
# print("RUNTIME_LIBRARY_DIRS: ", str(RUNTIME_LIBRARY_DIRS) + "\n")
# print("BINARY_C_CFLAGS: ", str(BINARY_C_CFLAGS) + "\n")
# print("API_h: ", str(API_h) + "\n")
# print("macros: ", str(BINARY_C_DEFINE_MACROS) + "\n")
# print('\n')
David Hendriks's avatar
David Hendriks committed

David Hendriks's avatar
David Hendriks committed
############################################################
# Making the extension function
############################################################
# TODO: fix that this one also compiles the code itself

David Hendriks's avatar
David Hendriks committed
    # name="binarycpython.core.binary_c",
    name="binary_c_python_api",
    sources=["src/binary_c_python.c"],
David Hendriks's avatar
David Hendriks committed
    include_dirs=INCLUDE_DIRS,
    libraries=LIBRARIES,
    library_dirs=LIBRARY_DIRS,
David Hendriks's avatar
David Hendriks committed
    runtime_library_dirs=RUNTIME_LIBRARY_DIRS,
    define_macros=[] + BINARY_C_DEFINE_MACROS,
    extra_objects=[],
    extra_compile_args=[],
David Hendriks's avatar
David Hendriks committed
############################################################
# Making the extension function
############################################################
David Hendriks's avatar
David Hendriks committed
    version="0.2",
David Hendriks's avatar
David Hendriks committed
    This is a python API for binary_c (versions {}) by David Hendriks, Rob Izzard and collaborators.
    Based on the initial set up by Jeff andrews.
David Hendriks's avatar
David Hendriks committed
    It is tested and designed to work for versions {}, we can't guarantee proper functioning for other versions
    If you want to use a different version of binary_c, download and install a different version of this package
David Hendriks's avatar
David Hendriks committed
    """.format(
        str(REQUIRED_BINARY_C_VERSIONS), str(REQUIRED_BINARY_C_VERSIONS)
    ),
    author="David Hendriks",
    author_email="davidhendriks93@gmail.com",
    long_description=readme(),
    url="https://gitlab.eps.surrey.ac.uk/ri0005/binary_c-python",
    package_dir={
        "binarycpython": "binarycpython",
        "binarycpython.utils": "binarycpython/utils",
        "binarycpython",
        "binarycpython.utils",
    # package_data={
    #     'binarycpython.core': ['libbinary_c_api.so'],
    # },
    ext_modules=[BINARY_C_PYTHON_API_MODULE],  # binary_c must be loaded

    classifiers=[
        "Development Status :: 3 - Alpha",
        "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
        "Intended Audience :: Developers",
        "Intended Audience :: Science/Research",
        "Operating System :: POSIX :: Linux",
        "Programming Language :: Python :: 3",
        "Programming Language :: C",
        "Topic :: Education",
        "Topic :: Scientific/Engineering :: Physics",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],