setup.py 7.24 KB
#!/usr/bin/python3

from setuptools import Extension, setup
import shutil
import glob
import os
import stat
import numpy

print("="*40 + "\nSetup of the module libtt\n" + "="*40)

# ====================================
# === Distribution identification ===
# ====================================
import platform
psystem = platform.system()
if psystem == "Windows":
    distribution = platform.win32_ver()
    distro_name = psystem
    distro_version = distribution[0]
elif psystem == "Darwin":
    distribution = platform.mac_ver()
    distro_name = psystem
    distro_version = distribution[0]
elif psystem == "Linux":
    release = platform.release
    if "microsoft" in release:
        # release = '5.10.16.3-microsoft-standard-WSL2'
        distro_name = release.split("-")[3]
        distro_version = release.split("-")[0]
    else
        distribution = platform.freedesktop_os_release()
        distro_name = distribution['NAME']
        distro_version = distribution['VERSION_ID']
print(f"\n{distro_name=}")
print(f"\n{distro_version=}")

# ====================================
# === Path and general description ===
# ====================================
from pathlib import Path
this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text(encoding="utf-8")
path_external = os.path.join("audela","src","external","cfitsio")

# --- path_cfitsio_version is the directory of sources and includes
path_base = os.path.abspath(os.path.dirname(__file__))
path_cfitsio = os.path.join(path_base,"audela","src","external","cfitsio")
path_cfitsio_version = glob.glob(os.path.join(path_cfitsio, "cfitsio*"))[0]
path_libtt = os.path.join(path_base,"audela","src","audela","libtt")
path_jpeg = os.path.join(path_base,"audela","src","external","jpeg6b","jpeg-6b")

# ====================================
# === List of CFITSIO source files ===
# ====================================
makefile = os.path.join(path_cfitsio_version, "Makefile.in")
# --- Read the Makefile.in to get the list of .c files to compile
with open(makefile,"rt") as fid:
    lignes = fid.readlines()
valid = False
core_sources_cfitsio = []
for ligne in lignes:
    words = ligne.split()
    if len(words) == 0:
        valid = False
        continue
    if words[0] == "CORE_SOURCES":
        valid = True
    #if words[0] == "FITSIO_SRC":
    #    valid = True
    if valid:
        for word in words:
            if word.find(".c") > 0:
                core_sources_cfitsio.append(os.path.join(path_cfitsio_version,word))

# ====================================
# === List of JPEG source files ===
# ====================================
makefile = os.path.join(path_jpeg, "makefile.unix")
# --- Read the makefile.unix to get the list of .c files to compile
with open(makefile,"rt") as fid:
    lignes = fid.readlines()
valid = False
core_sources_jpeg = []
for ligne in lignes:
    words = ligne.split()
    if len(words) == 0:
        valid = False
        continue
    if words[0] == "#":
        valid = False
    if words[0] == "LIBSOURCES=":
        valid = True
    if words[0] == "SYSDEPSOURCES=":
        core_sources_jpeg.append(os.path.join(path_jpeg, "jmemansi.c"))
    if words[0] == "APPSOURCES=":
        valid = False
    if valid:
        for word in words:
            if word.find(".c") > 0:
                core_sources_jpeg.append(os.path.join(path_jpeg, word))

# ====================================
# === List of LIBTT source files ===
# ====================================
makefile = os.path.join(path_libtt, "linux", "Makefile")
# --- Read the Makefile to get the list of .c files to compile
with open(makefile,"rt") as fid:
    lignes = fid.readlines()
valid = False
core_sources_libtt = []
for ligne in lignes:
    words = ligne.split()
    if len(words) == 0:
        valid = False
        continue
    if words[0] == "SRC":
        valid = True
    if valid:
        for word in words:
            if word.find(".c") > 0:
                word = word.replace("$(SRCDIR)/","")
                core_sources_libtt.append(os.path.join(path_libtt, "src", word))
       
# =====================================================
# === Copy manufacturer resources (libraires, etc.) ===
# =====================================================
# --- List the resource files
srcs = []
if distro_name == "Windows":
    pass
elif distro_name == "Ubuntu" or distro_name == "Fedora":
    pass
else:
    raise("No compilation found for your distribution")
# --- Copy the resource files into the current working directory
if len(srcs) > 0:
    print("\ncopy resource files:")
for src in srcs:
    dest = os.path.join(this_directory, os.path.basename(src))
    print(f" {src=} -> {dest=}")
    shutil.copyfile(src, dest)
    if psystem != "Windows":
        # --- Modify attributes
        os.chmod(dest, stat.S_IRWXU  | stat.S_IRWXG | stat.S_IRWXO)

# =============================================
# === Include files and compilation options ===
# =============================================
# --- Specific platform includes
extra_compile_args = []
extra_link_args = []
include_dirs = []
if distro_name == "Windows":
    extra_compile_args.append("/D")
    extra_compile_args.append('"_CRT_SECURE_NO_WARNINGS"')
    extra_compile_args.append("/W3")
    extra_compile_args.append("/D")
    extra_compile_args.append("WIN32")
    #extra_link_args.append("/MACHINE:X64")
elif distro_name == "Ubuntu" or distro_name == "Fedora":
    #extra_compile_args.append("-Wwrite-strings")    
    #extra_compile_args.append("-lstdc++")   
    #extra_link_args.append("-lm")
    #extra_link_args.append("-lstdc++")    
    pass
# --- Any platform includes
include_dirs.append(numpy.get_include()) # numpy
include_dirs.append(os.path.join("audela",      "src","include")) # sysexp.h
include_dirs.append(os.path.join(path_libtt,"src")) # libtt
include_dirs.append(path_cfitsio_version) # CFITSIO
include_dirs.append(path_jpeg) # Jpeg

# =================
# === Libraries ===
# =================
library_dirs = []
libraries = []

# ===============
# === Sources ===
# ===============
sources = []
sources.extend(core_sources_cfitsio)
sources.extend(core_sources_jpeg)
sources.extend(core_sources_libtt)
sources.append("wrapper_libtt.cpp")
    
# =============
# === Setup ===
# =============
print(f"\n{include_dirs=}")
print(f"\n{extra_compile_args=}")
print(f"\n{extra_link_args=}")
print(f"\n{library_dirs=}")
print(f"\n{libraries=}")
print(f"\n{sources=}")
setup(

    description = "A Python wrapper of libtt",
    long_description = long_description,
    long_description_content_type = "text/markdown",
    version = "0.9",
    author = "Alain Klotz",
    author_email = "aklotz@irap.omp.eu",
    license = "MIT",
    
    ext_modules = [
        Extension(
            name = "libtt",  # as it would be imported. It includes packages/namespaces separated by `.`
            include_dirs = include_dirs,
            extra_compile_args = extra_compile_args,
            extra_link_args = extra_link_args,
            library_dirs = library_dirs,
            libraries = libraries,
            sources = sources, # all sources are compiled into a single binary file
        ),
    ]
)