-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsetup.py
215 lines (178 loc) · 6.24 KB
/
setup.py
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
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
# -*- coding: utf-8 -*-
# Copyright (c) 2021, J. D. Mitchell + Maria Tsalakou
#
# Distributed under the terms of the GPL license version 3.
#
# The full license is in the file LICENSE, distributed with this software.
"""
This package provides a python interface to the C++ library
libsemigroups (libsemigroups.rtfd.io).
"""
import glob
import os
import sys
from pprint import pprint
import pkgconfig
from pybind11.setup_helpers import Pybind11Extension, build_ext
from setuptools import setup, find_packages
__dir__ = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, __dir__ + "/libsemigroups_pybind11")
from tools import ( # pylint: disable=import-error, wrong-import-position
libsemigroups_version,
minimum_libsemigroups_version,
compare_version_numbers,
)
__version__ = "0.10.1"
pprint(os.environ)
if "PKG_CONFIG_PATH" not in os.environ:
os.environ["PKG_CONFIG_PATH"] = ""
pkg_config_path = os.environ["PKG_CONFIG_PATH"].split(":")
if "CONDA_PREFIX" in os.environ:
conda_env_pkg_config = os.path.join(
os.environ["CONDA_PREFIX"], "lib", "pkgconfig"
)
if (
os.path.exists(conda_env_pkg_config)
and not conda_env_pkg_config in pkg_config_path
):
os.environ["PKG_CONFIG_PATH"] += ":" + conda_env_pkg_config
if "CONDA_DEFAULT_ENV" in os.environ and "CONDA_ENVS_PATH" in os.environ:
conda_env_pkg_config = os.path.join(
os.environ["CONDA_ENVS_PATH"],
os.environ["CONDA_DEFAULT_ENV"],
"lib",
"pkgconfig",
)
if (
os.path.exists(conda_env_pkg_config)
and not conda_env_pkg_config in pkg_config_path
):
os.environ["PKG_CONFIG_PATH"] += ":" + conda_env_pkg_config
if "/usr/local/lib/pkgconfig" not in pkg_config_path:
if (
"PKG_CONFIG_PATH" in os.environ
and not len(os.environ["PKG_CONFIG_PATH"]) == 0
):
os.environ["PKG_CONFIG_PATH"] += ":/usr/local/lib/pkgconfig"
else:
os.environ["PKG_CONFIG_PATH"] = "/usr/local/lib/pkgconfig"
if not pkgconfig.exists("libsemigroups"):
raise ImportError("cannot locate libsemigroups library")
if not compare_version_numbers(
libsemigroups_version(), minimum_libsemigroups_version()
):
raise ImportError(
f"libsemigroups version at least {minimum_libsemigroups_version()}"
+ f" is required, found {libsemigroups_version()}"
)
class GetPyBind11Include: # pylint: disable=too-few-public-methods
"""
Helper class to determine the pybind11 include path
The purpose of this class is to postpone importing pybind11
until it is actually installed, so that the ``get_include()``
method can be invoked.
"""
def __init__(self, user=False):
self.user = user
def __str__(self):
import pybind11 # pylint: disable=import-outside-toplevel
return pybind11.get_include(self.user)
LIBRARY_PATH = pkgconfig.pkgconfig._query( # pylint: disable=protected-access
"libsemigroups", "--libs-only-L"
)
# The above pkgconfig query can return an empty string (this also happens on
# the command line). This happens, for example, using pkg-config version 1.8.0
# on ArchLinux. CN 27/10/2021
assert (
len(LIBRARY_PATH) == 0 or LIBRARY_PATH[:2] == "-L"
), "The first two characters of the library path to the libsemigroups.so etc should be '-L'"
# Try to use pkg-config to add the path to libsemigroups.so etc to
# LD_LIBRARY_PATH so that python can find it.
if len(LIBRARY_PATH) != 0:
LIBRARY_PATH_NO_L = LIBRARY_PATH[2:]
else:
LIBRARY_PATH_NO_L = "/usr/lib"
LIBRARY_PATH = "-L/usr/lib"
if os.path.exists(LIBRARY_PATH_NO_L):
if (
"LD_LIBRARY_PATH" in os.environ
and len(os.environ["LD_LIBRARY_PATH"]) != 0
):
LD_LIBRARY_PATH = os.environ["LD_LIBRARY_PATH"]
if LD_LIBRARY_PATH.find(LIBRARY_PATH_NO_L) == -1:
PREFIX = "" if LD_LIBRARY_PATH[-1] == ":" else ":"
os.environ["LD_LIBRARY_PATH"] += PREFIX + LIBRARY_PATH_NO_L
else:
os.environ["LD_LIBRARY_PATH"] = LIBRARY_PATH_NO_L
print("LD_LIBRARY_PATH is:")
print(os.environ["LD_LIBRARY_PATH"])
LIBSEMIGROUPS_CFLAGS_ONLY_I = (
pkgconfig.pkgconfig._query( # pylint: disable=protected-access
"libsemigroups", "--cflags-only-I"
)
)
include_path = [
GetPyBind11Include(),
GetPyBind11Include(user=True),
"/usr/local/include",
"/usr/local/include/libsemigroups",
]
if len(LIBSEMIGROUPS_CFLAGS_ONLY_I) != 0:
include_path.extend([x[2:] for x in LIBSEMIGROUPS_CFLAGS_ONLY_I.split(" ")])
if "CONDA_PREFIX" in os.environ:
include_path.append(os.path.join(os.environ["CONDA_PREFIX"], "include"))
include_path.append(
os.path.join(os.environ["CONDA_PREFIX"], "include", "eigen3")
)
if "CONDA_DEFAULT_ENV" in os.environ and "CONDA_ENVS_PATH" in os.environ:
include_path.append(
os.path.join(
os.environ["CONDA_ENVS_PATH"],
os.environ["CONDA_DEFAULT_ENV"],
"include",
)
)
include_path.append(
os.path.join(
os.environ["CONDA_ENVS_PATH"],
os.environ["CONDA_DEFAULT_ENV"],
"include",
"eigen3",
)
)
print("Include directories are:")
pprint(include_path)
ext_modules = [
Pybind11Extension(
"_libsemigroups_pybind11",
glob.glob("src/*.cpp"),
include_dirs=include_path,
language="c++",
libraries=["semigroups"],
extra_link_args=[LIBRARY_PATH, "-L/usr/local/lib"],
),
]
with open(os.path.join(__dir__, "README.md"), "r", encoding="utf-8") as readme:
long_description = readme.read()
setup(
name="libsemigroups_pybind11",
version=__version__,
author="James D. Mitchell",
author_email="[email protected]",
url="https://github.com/libsemigroups/libsemigroups_pybind11",
description="A python package for the libsemigroups C++ library",
long_description=long_description,
long_description_content_type="text/markdown",
ext_modules=ext_modules,
packages=find_packages(),
setup_requires=["pkgconfig>=1.5.0"],
install_requires=[
"pybind11>=2.10.1",
"packaging>=20.4",
"pkgconfig>=1.5.0",
],
tests_require=["pytest==6.2.4"],
cmdclass={"build_ext": build_ext},
zip_safe=False,
python_requires=">=3.7.0",
)