|
| 1 | +#!/usr/bin/python |
| 2 | +import subprocess |
| 3 | +import re |
| 4 | +from datetime import datetime |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +import sys |
| 8 | + |
| 9 | +# Execute the `cmake --system-information` command and capture its output |
| 10 | +cmake_output = subprocess.check_output( |
| 11 | + ["cmake", "--system-information"], |
| 12 | + encoding="utf-8", |
| 13 | + universal_newlines=True |
| 14 | +) |
| 15 | + |
| 16 | +# Initialize a dictionary to store (variable, value) pairs |
| 17 | +cmake_info = {} |
| 18 | + |
| 19 | +# Filter relevant section ("VARIABLES") |
| 20 | +cmake_lines = [] |
| 21 | +record = False |
| 22 | +for line in cmake_output.splitlines(): |
| 23 | + # Skip empty lines or separators |
| 24 | + if line.strip() == "" or line.startswith("//") or line.startswith("==========="): |
| 25 | + continue |
| 26 | + if line.startswith("==="): |
| 27 | + record = "VARIABLES" in line |
| 28 | + continue |
| 29 | + if record: |
| 30 | + cmake_lines.append(line) |
| 31 | + |
| 32 | +# Parse |
| 33 | +pattern = re.compile(r"CMAKE_([A-Z_]*)\s+\"(.*)\"") |
| 34 | +for line in cmake_lines: |
| 35 | + # Check for a new variable=value pair |
| 36 | + # We consider only monoline entries |
| 37 | + match = pattern.match(line) |
| 38 | + if match is None: |
| 39 | + continue |
| 40 | + key = match.group(1) |
| 41 | + value = match.group(2) |
| 42 | + cmake_info[key] = value |
| 43 | + |
| 44 | +# Filter on relevant entries |
| 45 | +KEYS = { |
| 46 | + "SYSTEM", |
| 47 | + "SYSTEM_NAME", |
| 48 | + "CXX_COMPILER", |
| 49 | + "CXX_COMPILER_ID", |
| 50 | + "CXX_COMPILER_ARCHITECTURE_ID", |
| 51 | + "CXX_COMPILER_VERSION", |
| 52 | + "C_COMPILER", |
| 53 | + "C_COMPILER_ID", |
| 54 | + "C_COMPILER_ARCHITECTURE_ID", |
| 55 | + "C_COMPILER_VERSION", |
| 56 | +} |
| 57 | +cmake_info = {key: cmake_info[key] for key in KEYS} |
| 58 | + |
| 59 | +# Add time |
| 60 | +cmake_info["TIMESTAMP"] = str(datetime.now()) |
| 61 | + |
| 62 | +# Export the dictionary in JSON format |
| 63 | +print(json.dumps(cmake_info, sort_keys=True, indent=4)) |
0 commit comments