Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active March 3, 2023 18:57
Embed
What would you like to do?
Read CMakeConfigureLog.yaml from Python
#!/usr/bin/env python3
"""
read CMakeConfigureLog.yaml
"""
from __future__ import annotations
import yaml
from pathlib import Path
import argparse
from pprint import pprint
def get_log(build_dir: Path) -> dict[str, str]:
"""read CMakeConfigureLog.yaml"""
log_file = Path(build_dir).expanduser() / "CMakeFiles/CMakeConfigureLog.yaml"
with open(log_file) as f:
return yaml.safe_load(f)
def get_events(log: dict[str, str]) -> list[dict[str, str]]:
"""get events from CMakeConfigureLog.yaml"""
return log["events"]
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("build_dir", help="build directory")
args = p.parse_args()
log = get_log(args.build_dir)
for event in get_events(log):
if event["kind"].startswith("try_compile"):
for e in event["backtrace"]:
# arbitrary filter criteria as CMake does several try_compile even for targetless project.
if e.startswith("CMakeLists.txt:") and e.endswith("try_compile)"):
r = event["buildResult"]
pprint(r)
cmake_minimum_required(VERSION 3.26)
project(dummy LANGUAGES C CXX)
try_compile(c_cpp_ok
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/lib.c ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
CMAKE_FLAGS -DINCLUDE_DIRECTORIES=${CMAKE_CURRENT_SOURCE_DIR}
)
message(STATUS "try_compile: ${c_cpp_ok}")
int add_one(int x) {
return x + 1;
}
#include <cstdlib>
#include <cassert>
#include "my.h"
int main() {
assert(add_one(2) == 3);
return EXIT_SUCCESS;
}
#ifdef __cplusplus
extern "C" {
#endif
int add_one(int);
#ifdef __cplusplus
}
#endif
[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
requires-python = ">=3.8"
dependencies = ["PyYAML"]
[tool.black]
line-length = 110
[tool.mypy]
files = ["."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment