|
| 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 | + ) |
0 commit comments