Skip to content

Commit 7b03cf8

Browse files
Create build_tools package
1 parent 217985e commit 7b03cf8

File tree

3 files changed

+143
-4
lines changed

3 files changed

+143
-4
lines changed

build_tools/__init__.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 module provides some tools for building libsemigroups_pybind11.
11+
"""
12+
13+
import re
14+
from packaging import version
15+
import pkgconfig
16+
17+
18+
def minimum_libsemigroups_version():
19+
"""
20+
Returns the minimum required version of libsemigroups required to make
21+
this work.
22+
"""
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+
)

etc/libsemigroups_version.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
import os
33
import sys
44

5-
__dir__ = os.path.abspath(os.path.dirname(__file__))
6-
sys.path.insert(0, __dir__ + "/../libsemigroups_pybind11")
7-
8-
from tools import minimum_libsemigroups_version
5+
sys.path.append(
6+
os.path.abspath(
7+
os.path.join(
8+
os.path.dirname(__file__),
9+
"..",
10+
)
11+
)
12+
)
13+
from build_tools import minimum_libsemigroups_version
914

1015
print(minimum_libsemigroups_version())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 module provides some tools for libsemigroups_pybind11.
11+
"""
12+
13+
14+
def ordinal(n: int):
15+
"""
16+
Returns 1st from 1, 2nd from 2 etc.
17+
From https://stackoverflow.com/questions/9647202/
18+
"""
19+
if 11 <= (n % 100) <= 13:
20+
suffix = "th"
21+
else:
22+
suffix = ["th", "st", "nd", "rd", "th"][min(n % 10, 4)]
23+
return str(n) + suffix

0 commit comments

Comments
 (0)