Skip to content

Commit d96e0a7

Browse files
committed
Build system: add a method to get the version of an installed dependency
1 parent 078b3a7 commit d96e0a7

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

build-system/luxmake/utils.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import logging
1010
import shutil
1111
import subprocess
12+
import tempfile
13+
import pathlib
14+
import re
1215
from dataclasses import dataclass
1316

1417
# Logger
@@ -65,7 +68,7 @@ def run_cmake(
6568
"""Run cmake statement."""
6669
cmake_app = ensure_cmake_app()
6770
args = [cmake_app] + args
68-
logger.debug(' '.join(args))
71+
logger.debug(" ".join(args))
6972
res = subprocess.run(
7073
args,
7174
shell=False,
@@ -77,6 +80,57 @@ def run_cmake(
7780
return res
7881

7982

83+
_CMAKE_FIND_PACKAGE_SNIPPET = """\
84+
cmake_minimum_required(VERSION 4.2)
85+
project(find)
86+
find_package({0})
87+
message(STATUS "@@${{{0}_VERSION}}@@")
88+
"""
89+
90+
_TOOLCHAIN = pathlib.Path(
91+
"out", "build", "generators", "conan_toolchain.cmake"
92+
)
93+
94+
95+
def _run_find_package(dep):
96+
"""Run a find_package in cmake."""
97+
with tempfile.TemporaryDirectory(delete=False) as folder:
98+
folder = pathlib.Path(folder)
99+
with open(folder / "CMakeLists.txt", "w") as cmakelists:
100+
cmakelists.write(_CMAKE_FIND_PACKAGE_SNIPPET.format(dep))
101+
cmakelists.close()
102+
res = run_cmake(
103+
[
104+
f"-S {folder}",
105+
f"-B {folder / 'build'}",
106+
f"-DCMAKE_TOOLCHAIN_FILE='{_TOOLCHAIN.absolute()}'",
107+
f"-DCMAKE_BUILD_TYPE='Debug'",
108+
],
109+
text=True,
110+
stdout=subprocess.PIPE,
111+
stderr=subprocess.STDOUT,
112+
)
113+
res.check_returncode()
114+
return res.stdout
115+
116+
117+
def get_dep_version(dep):
118+
"""Get the version of a given dependency, as foundable by cmake.
119+
120+
Important: this function assumes it is run in the root directory
121+
of the projet.
122+
"""
123+
124+
# Run cmake and parse output
125+
cmake_result = _run_find_package(dep)
126+
versions = re.findall(r"@@([A-Za-z0-9.]+)@@", cmake_result)
127+
if not versions:
128+
raise ValueError(f"No dependency '{dep}' found")
129+
version, *_ = versions
130+
131+
return version
132+
133+
80134
def unpack(path, dest):
81135
"""Unpack a wheel."""
82136
args = [

0 commit comments

Comments
 (0)