setup.py 6.67 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 wrapper_flipro\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:
        try:
            # --- Python >= 3.10
            distribution = platform.freedesktop_os_release()
            distro_name = distribution['NAME']
            distro_version = distribution['VERSION_ID']
        except:
            try:
                # --- Python <= 3.7
                # platform.linux_distribution()
                # ('Red Hat Enterprise Linux', '8.8', 'Ootpa')
                dist = platform.dist()
                # ('redhat', '8.8', 'Ootpa')
                distro_name = dist[0]
                distro_version = dist[1]
            except:
                # --- Python == 3.8 or 3.9
                uname = platform.uname()
                # uname_result(system='Linux', node='ceres', release='6.2.0-33-generic', version='#33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep  7 10:33:52 UTC 2', machine='x86_64')
                version = uname[3]
                vs =  version.split()
                vvs =  vs[0].split("-")
                distro_name = vvs[-1]
                distro_version = '?'
                
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","flipro")
    
# ===========================================
# === Directory of the manufacturer files ===
# ===========================================
if distro_name == "Windows":
    path_manufacturer = "libflipro-Version-1.12.59-Windows"
elif distro_name == "Ubuntu" or distro_name == "Fedora":
    path_manufacturer = "libflipro-Version-2.0.5-"
elif distro_name == "redhat" :
    path_manufacturer = "libflipro-Version-1.12.59-CentOSLinux"
    
# =====================================================
# === Copy manufacturer resources (libraires, etc.) ===
# =====================================================
# --- List the resource files
srcs = []
if distro_name == "Windows":
    library_dirs = [os.path.join(path_external, path_manufacturer, "x64")]
    for library_dir in library_dirs:
        srcs.extend( glob.glob(os.path.join(library_dir, "*.dll")) )
elif distro_name == "Ubuntu" or distro_name == "Fedora":
    library_dirs = [os.path.join(path_external, path_manufacturer + distro_name)]
    for library_dir in library_dirs:
        srcs.extend( glob.glob(os.path.join(library_dir, "*.so*")) )
elif distro_name == "redhat":
    library_dirs = [os.path.join(path_external, path_manufacturer)]
    for library_dir in library_dirs:
        srcs.extend( glob.glob(os.path.join(library_dir, "*.so*")) )
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 = []
if distro_name == "Windows":
    include_dirs=[os.path.join(path_external, path_manufacturer)]
    extra_compile_args.append("/D")
    extra_compile_args.append('"_CRT_SECURE_NO_WARNINGS"')
    extra_compile_args.append("/W3")
elif distro_name == "Ubuntu" or distro_name == "Fedora":
    include_dirs=[os.path.join(path_external, path_manufacturer + distro_name)]
    #extra_compile_args.append("-Wwrite-strings")    
    #extra_compile_args.append("-lstdc++")   
    #extra_link_args.append("-lm")
    extra_link_args.append("-lusb-1.0") 
    #extra_link_args.append("-lstdc++")    
elif distro_name == "redhat":
    extra_link_args.append("-lusb-1.0") 
# --- 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("audela_notcl","src","include")) # tcl.h libcam/licam.h libcam/libstruc.h
include_dirs.append(os.path.join("audela",      "src","libcam","libfingerlakespro","src")) # camera.h

# =================
# === Libraries ===
# =================
libraries = ["libflipro"]

# ===============
# === Sources ===
# ===============
# --- Define the common source files to compile
sources = []
sources.append("audela/src/libcam/util.cpp")
# --- Define the specific source files to compile
sources.append("wrapper_flipro.cpp")
sources.append("audela/src/libcam/libfingerlakespro/src/camera.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 a device flipro",
    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 = "wrapper_flipro",  # 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
        ),
    ]
)