forked from npat-efault/ppgplot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.py
More file actions
128 lines (113 loc) · 4.59 KB
/
setup.py
File metadata and controls
128 lines (113 loc) · 4.59 KB
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
26
27
28
29
30
31
32
33
34
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
from setuptools import setup, Extension
import os
import sys
import platform
import operator
# these deps are listed in pyproject.toml so should be able to import w/o probs
import numpy
import pkgconfig
def is_building_sdist():
"""Detect if we're building a source distribution (sdist)"""
# Check for sdist-related commands in sys.argv
sdist_commands = ['sdist', 'egg_info', 'dist_info']
return any(cmd in sys.argv for cmd in sdist_commands)
def add_pgplot_from_giza(ext):
# Very convenient - but also breaks the build on Linux (Deb12) *sigh*
# adds an empty string [''] to ext.extra_compile_args
pkgconfig.configure_extension(ext, 'giza', static=True)
ext.extra_compile_args = list( filter(operator.truth, ext.extra_compile_args) )
# But not sufficient ...
ext.libraries.extend( ['cpgplot', 'pgplot'] )
return ext
# Configure the Extension based on stuff found in PGPLOT_DIR
def add_pgplot_from_pgplot_dir(ext, pgplotdir):
if not os.path.isdir(pgplotdir):
raise RuntimeError(f"$PGPLOT_DIR [{pgplotdir}] is not a directory")
darwin = 'darwin' in platform.system().lower()
soext = 'dylib' if darwin else 'so'
mk_rpath = ("-Wl,-rpath,{0}" if darwin else "-Wl,-rpath={0}").format
mk_lib = f"lib{{0}}.{soext}".format
# Find libcpgplot
lib = mk_lib("cpgplot")
for path, _, files in os.walk(pgplotdir):
if lib not in files:
continue
# OK found it!
# Configure runtime library paths
ext.extra_link_args.append( mk_rpath(path) )
# Because we're overriding system settings, add
# the libraries with absolute path
ext.extra_link_args.extend( map(lambda l: os.path.join(path, l),
map(mk_lib, ['cpgplot', 'pgplot'])) )
ext.runtime_library_dirs.append( path )
ext.include_dirs.append( os.path.join(pgplotdir, "include") )
break
else:
raise RuntimeError(f"Could not find libcpgplot in $PGPLOT_DIR [{pgplotdir}]")
return ext
# Extract useful info from the numpy module
def add_numpy(ext):
ext.include_dirs.append( numpy.get_include() )
return ext
# Set up X11 libraries, searching standard (Linux...) paths
def add_X11(ext):
ext.libraries.extend(['X11', 'm'])
# Standard X11 library locations
ext.library_dirs.extend(
filter(os.path.isdir,
["/usr/lib/x86_64-linux-gnu/", "/usr/X11R6/lib/", "/opt/X11/lib", "/opt/homebrew/lib"])
)
return ext
def print_config(ext):
print("===> Extension contents")
print(f"\tname = {ext.name}")
print(f"\tsources = {ext.sources}")
print(f"\tlibraries = {ext.libraries}")
print(f"\tdefine_macros = {ext.define_macros}")
print(f"\tundef_macros = {ext.undef_macros}")
print(f"\tlibrary_dirs = {ext.library_dirs}")
print(f"\tinclude_dirs = {ext.include_dirs}")
print(f"\textra_link_args = {ext.extra_link_args}")
print(f"\truntime_library_dirs = {ext.runtime_library_dirs}")
print(f"\textra_objects = {ext.extra_objects}")
print(f"\textra_compile_args = {ext.extra_compile_args}")
print(f"\texport_symbols = {ext.export_symbols}")
print(f"\tswig_opts = {ext.swig_opts}")
print(f"\tdepends = {ext.depends}")
print(f"\tlanguage = {ext.language}")
print(f"\toptional = {ext.optional}")
print(f"\tpy_limited_api = {ext.py_limited_api}")
return ext
# This is the main Extension configuration step
# We go over the dependencies, each of which
# can modify the build env as needed
def set_extension_config(ext):
# yah ... maybe later if we grow up widen this
if os.name != "posix":
raise Exception("OS not supported")
# Skip extension configuration during sdist creation
if is_building_sdist():
print("Building sdist - skipping extension configuration")
return ext
# modify the extension to taste
add_X11(ext)
add_numpy(ext)
# Where to source pgplot from
pgplot_dir = os.environ.get('PGPLOT_DIR', None)
if pgplot_dir is not None:
add_pgplot_from_pgplot_dir(ext, pgplot_dir)
else:
add_pgplot_from_giza(ext)
# uncomment and run "pip -v install [-e] ." to see output
#print_config(ext)
return ext
###########################################################
# This triggers the whole build #
###########################################################
setup(
name="python-pgplot",
ext_modules=[
set_extension_config( Extension('ppgplot._ppgplot',
sources=[os.path.join('src', '_ppgplot.c')]) ),
]
)