taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

configure.py (3249B)


      1 # This configure.py.template file is in the public domain.
      2 
      3 import os
      4 import re
      5 import shutil
      6 import subprocess
      7 
      8 from talerbuildconfig import *
      9 
     10 # Oldest Rust release that can build this tree.  Keep the reason for the number
     11 # next to it -- the point of the check is to report an unusable toolchain here,
     12 # once, instead of as a wall of parse errors in the middle of the build.
     13 #
     14 #   1.85  edition 2024 / resolver "3"   (Cargo.toml)
     15 #   1.86  Vec::pop_if                   (common/failure-injection)
     16 #   1.88  let chains                    (used throughout)
     17 #   1.93  std::fmt::from_fn             (taler-common, taler-api, taler-wise)
     18 #
     19 # Debian trixie ships 1.85, which is too old; trixie-backports and testing both
     20 # ship a new enough rustc.  Keep this in step with `rust-version` in Cargo.toml.
     21 RUST_MIN_VERSION = "1.93"
     22 
     23 
     24 def _version_tuple(version):
     25     parts = [int(p) for p in version.split(".")[:3]]
     26     return tuple(parts + [0] * (3 - len(parts)))
     27 
     28 
     29 class RustTool(Tool):
     30     """A Rust toolchain program (cargo, rustc) found on $PATH.
     31 
     32     Unlike PosixTool this honours a CARGO=/RUSTC= environment override and
     33     records the resolved path *and* version in config.mk.  Nothing here wants
     34     rustup: any cargo/rustc on $PATH will do, including the distribution's.
     35     """
     36 
     37     def __init__(self, name, min_version=None):
     38         self.name = name
     39         self.min_version = min_version
     40         self.hint = (
     41             f"install a Rust toolchain providing '{name}' -- the distribution's"
     42             f" rustc/cargo packages are fine (on Debian stable they come from"
     43             f" trixie-backports), as is rustup -- or point {name.upper()}= at one"
     44         )
     45 
     46     def args(self, parser):
     47         pass
     48 
     49     def check(self, buildconfig):
     50         prog = os.environ.get(self.name.upper()) or self.name
     51         path = shutil.which(prog)
     52         if path is None:
     53             return False
     54 
     55         version = self._version(path)
     56         buildconfig._set_tool(self.name, path, version=version)
     57 
     58         if self.min_version is None or version is None:
     59             return True
     60 
     61         if _version_tuple(version) < _version_tuple(self.min_version):
     62             # A warning, not an error: configuring is still useful (the
     63             # non-build targets work), and `rust-version` in Cargo.toml makes
     64             # cargo itself refuse the build with a precise message.
     65             buildconfig._warn(
     66                 f"{self.name} {version} is older than {self.min_version};"
     67                 f" this tree will not compile with it."
     68                 f" On Debian stable: apt install -t trixie-backports rustc cargo"
     69             )
     70         return True
     71 
     72     @staticmethod
     73     def _version(path):
     74         try:
     75             out = subprocess.run(
     76                 [path, "--version"], capture_output=True, text=True, check=True
     77             ).stdout
     78         except (OSError, subprocess.SubprocessError):
     79             return None
     80         # "cargo 1.95.0 (f2d3ce0bd 2026-03-21)", "rustc 1.95.0 (59807616e ...)"
     81         found = re.search(r"\d+(?:\.\d+)+", out)
     82         return found.group(0) if found else None
     83 
     84 
     85 b = BuildConfig()
     86 b.enable_prefix()
     87 b.enable_configmk()
     88 b.add_tool(RustTool("cargo"))
     89 b.add_tool(RustTool("rustc", min_version=RUST_MIN_VERSION))
     90 b.run()