Skip to content

Commit 6cb27dc

Browse files
Meta: Refactor build_tools
1 parent a23d41d commit 6cb27dc

3 files changed

Lines changed: 107 additions & 96 deletions

File tree

build_tools/__init__.py

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -10,102 +10,10 @@
1010
This module provides some tools for building libsemigroups_pybind11.
1111
"""
1212

13-
import re
14-
from packaging import version
15-
import pkgconfig
16-
1713

1814
def minimum_libsemigroups_version():
1915
"""
20-
Returns the minimum required version of libsemigroups required to make
21-
this work.
16+
Returns the minimum required version of libsemigroups required to build
17+
libsemigroups_pybind11.
2218
"""
23-
return "3.0.0"
24-
25-
26-
def libsemigroups_version():
27-
"Get the version of libsemigroups installed using pkg-config."
28-
29-
vers = pkgconfig.modversion("libsemigroups")
30-
if re.search(r"\d+\.\d+\.\d+-\d+-\w{7}", vers):
31-
# i.e. supplied is of the form: 1.1.0-6-g8b04c08
32-
vers = re.search(r"\d+\.\d\.+\d+-\d+", vers).group(0)
33-
return vers
34-
35-
36-
def compare_version_numbers(supplied, required):
37-
"""Returns True if supplied >= required"""
38-
39-
if isinstance(supplied, str) and isinstance(required, str):
40-
if "dev" in supplied:
41-
print(
42-
(
43-
"\033[93mWarning: You are using a development version of libsemigroups. This "
44-
"may cause undocumented behaviour\033[0m"
45-
)
46-
)
47-
return True
48-
return version.parse(supplied) >= version.parse(required)
49-
raise TypeError(
50-
f"expected (string, string), got a ({supplied.__name__}, {required.__name__})"
51-
)
52-
53-
54-
def extra_link_args() -> str:
55-
"""Find extra link args"""
56-
libs_only_L = pkgconfig.libs( # pylint: disable=invalid-name
57-
"libsemigroups"
58-
)
59-
# The above pkgconfig query can return an empty string (this also happens on
60-
# the command line). This happens, for example, using pkg-config version 1.8.0
61-
# on ArchLinux. CN 27/10/2021
62-
63-
assert (
64-
len(libs_only_L) == 0 or libs_only_L[:2] == "-L"
65-
), "The first two characters of the library path to the libsemigroups.so etc should be '-L'"
66-
67-
libs_only_L = [ # pylint: disable=invalid-name
68-
x for x in libs_only_L.split(" ") if x.startswith("-L")
69-
]
70-
71-
if len(libs_only_L) == 0:
72-
libs_only_L = ["-L/usr/lib"] # pylint: disable=invalid-name
73-
return libs_only_L
74-
75-
76-
def ld_library_path() -> str:
77-
"""Construct the LD_LIBRARY_PATH"""
78-
return ":".join([x[2:] for x in extra_link_args()])
79-
80-
81-
def validate_libsemigroups():
82-
DISCLAIMER = """
83-
(You should not see this message unless you are installing
84-
libsemigroups_pybind11 from its sources. If you are not installing from the
85-
sources, please raise an issue at:
86-
https://github.com/libsemigroups/libsemigroups_pybind11)"""
87-
88-
if not pkgconfig.exists("libsemigroups"):
89-
raise ImportError(
90-
f"""cannot locate the libsemigroups library.
91-
For more information about installing the libsemigroups library see
92-
https://libsemigroups.github.io/libsemigroups_pybind11/install.html.
93-
94-
If libsemigroups is installed, then it cannot be located by pkg-config,
95-
perhaps you should add the directory containing `libsemigroups.pc' to the
96-
\"PKG_CONFIG_PATH\". The file `libsemigroups.pc' might be in one of the
97-
following directories:
98-
* /usr/local/lib/pkgconfig
99-
* $CONDA_PREFIX/lib/pkgconfig if your active conda environment has pkgconfig
100-
installed, and libsemigroups was installed with conda/mamba in this
101-
environment.
102-
{DISCLAIMER}"""
103-
)
104-
105-
if not compare_version_numbers(
106-
libsemigroups_version(), minimum_libsemigroups_version()
107-
):
108-
raise ImportError(
109-
f"libsemigroups version at least {minimum_libsemigroups_version()}"
110-
+ f" is required, found {libsemigroups_version()}"
111-
)
19+
return "3.0.2"

build_tools/packaging_helpers.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright (c) 2021-2024 J. D. Mitchell
4+
#
5+
# Distributed under the terms of the GPL license version 3.
6+
#
7+
# The full license is in the file LICENSE, distributed with this software.
8+
9+
"""
10+
This submodule provides some more tools for building libsemigroups_pybind11.
11+
These tools rely on some non-built-in packages, so they have been placed in
12+
their own submodule to separate from the functions that need to be run in a bare
13+
Python environment.
14+
"""
15+
16+
import re
17+
from packaging import version
18+
import pkgconfig
19+
from . import minimum_libsemigroups_version
20+
21+
22+
def libsemigroups_version():
23+
"Get the version of libsemigroups installed using pkg-config."
24+
25+
vers = pkgconfig.modversion("libsemigroups")
26+
if re.search(r"\d+\.\d+\.\d+-\d+-\w{7}", vers):
27+
# i.e. supplied is of the form: 1.1.0-6-g8b04c08
28+
vers = re.search(r"\d+\.\d\.+\d+-\d+", vers).group(0)
29+
return vers
30+
31+
32+
def compare_version_numbers(supplied, required):
33+
"""Returns True if supplied >= required"""
34+
35+
if isinstance(supplied, str) and isinstance(required, str):
36+
if "dev" in supplied:
37+
print(
38+
(
39+
"\033[93mWarning: You are using a development version of libsemigroups. This "
40+
"may cause undocumented behaviour\033[0m"
41+
)
42+
)
43+
return True
44+
return version.parse(supplied) >= version.parse(required)
45+
raise TypeError(f"expected (string, string), got a ({supplied.__name__}, {required.__name__})")
46+
47+
48+
def extra_link_args() -> str:
49+
"""Find extra link args"""
50+
libs_only_L = pkgconfig.libs( # pylint: disable=invalid-name
51+
"libsemigroups"
52+
)
53+
# The above pkgconfig query can return an empty string (this also happens on
54+
# the command line). This happens, for example, using pkg-config version 1.8.0
55+
# on ArchLinux. CN 27/10/2021
56+
57+
assert len(libs_only_L) == 0 or libs_only_L[:2] == "-L", (
58+
"The first two characters of the library path to the libsemigroups.so etc should be '-L'"
59+
)
60+
61+
libs_only_L = [ # pylint: disable=invalid-name
62+
x for x in libs_only_L.split(" ") if x.startswith("-L")
63+
]
64+
65+
if len(libs_only_L) == 0:
66+
libs_only_L = ["-L/usr/lib"] # pylint: disable=invalid-name
67+
return libs_only_L
68+
69+
70+
def ld_library_path() -> str:
71+
"""Construct the LD_LIBRARY_PATH"""
72+
return ":".join([x[2:] for x in extra_link_args()])
73+
74+
75+
def validate_libsemigroups():
76+
DISCLAIMER = """
77+
(You should not see this message unless you are installing
78+
libsemigroups_pybind11 from its sources. If you are not installing from the
79+
sources, please raise an issue at:
80+
https://github.com/libsemigroups/libsemigroups_pybind11)"""
81+
82+
if not pkgconfig.exists("libsemigroups"):
83+
raise ImportError(
84+
f"""cannot locate the libsemigroups library.
85+
For more information about installing the libsemigroups library see
86+
https://libsemigroups.github.io/libsemigroups_pybind11/install.html.
87+
88+
If libsemigroups is installed, then it cannot be located by pkg-config,
89+
perhaps you should add the directory containing `libsemigroups.pc' to the
90+
\"PKG_CONFIG_PATH\". The file `libsemigroups.pc' might be in one of the
91+
following directories:
92+
* /usr/local/lib/pkgconfig
93+
* $CONDA_PREFIX/lib/pkgconfig if your active conda environment has pkgconfig
94+
installed, and libsemigroups was installed with conda/mamba in this
95+
environment.
96+
{DISCLAIMER}"""
97+
)
98+
99+
if not compare_version_numbers(libsemigroups_version(), minimum_libsemigroups_version()):
100+
raise ImportError(
101+
f"libsemigroups version at least {minimum_libsemigroups_version()}"
102+
+ f" is required, found {libsemigroups_version()}"
103+
)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
3030

31-
from build_tools import ( # pylint: disable=wrong-import-position
31+
from build_tools.packaging_helpers import ( # pylint: disable=wrong-import-position
3232
validate_libsemigroups,
3333
)
3434

0 commit comments

Comments
 (0)