Blame view

src/libtt/setup.py 7.24 KB
9a84dd27   Alain Klotz   libtt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/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":
e10c7309   Alain Klotz   Compatibility mul...
26
27
28
29
30
31
32
33
34
    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']
9a84dd27   Alain Klotz   libtt
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
        ),
    ]
)