tools: fix make host_info flag parsing and config string escaping

Fix incorrect flag handling and string escaping in the `make host_info`
diagnostic target.

Previously, CFLAGS, CXXFLAGS, and LDFLAGS were passed in a form that caused
improper splitting and quoting, which resulted in malformed output and
incorrectly escaped configuration values such as CONFIG_APPS_DIR.

This change ensures that:
- Compilation flags are passed as proper shell strings
- Flags are split correctly using shlex
- Configuration values are escaped exactly once when generating sysinfo.h
- Parsed output matches the contents of the .config file

This change affects diagnostic output only and does not modify the NuttX
build process or generated binaries.

Signed-off-by: Adwait Godbole <adwaitngodbole@gmail.com>
This commit is contained in:
Adwait Godbole
2026-02-02 00:28:58 +05:30
committed by simbit18
parent 854cd0ca21
commit 83c11b29a9
5 changed files with 76 additions and 52 deletions
+19 -14
View File
@@ -15,19 +15,19 @@ name: Build
on:
pull_request:
paths-ignore:
- 'AUTHORS'
- 'CONTRIBUTING.md'
- '**/CODEOWNERS'
- 'Documentation/**'
- 'tools/ci/docker/linux/**'
- 'tools/codeowners/*'
- "AUTHORS"
- "CONTRIBUTING.md"
- "**/CODEOWNERS"
- "Documentation/**"
- "tools/ci/docker/linux/**"
- "tools/codeowners/*"
push:
paths-ignore:
- 'AUTHORS'
- 'CONTRIBUTING.md'
- 'Documentation/**'
- "AUTHORS"
- "CONTRIBUTING.md"
- "Documentation/**"
branches:
- 'releases/*'
- "releases/*"
tags:
permissions:
@@ -38,7 +38,6 @@ concurrency:
cancel-in-progress: true
jobs:
# Fetch the source from nuttx and nuttx-apps repos
Fetch-Source:
runs-on: ubuntu-latest
@@ -153,7 +152,6 @@ jobs:
boards: ${{ fromJSON(needs.Linux-Arch.outputs.selected_builds) }}
steps:
- name: Show Disk Space
run: df -h
@@ -229,6 +227,13 @@ jobs:
./cibuild.sh -c -A -N -R -S testlist/${{matrix.boards}}.dat
fi
- name: Run host_info sanity check
uses: ./sources/nuttx/.github/actions/ci-container
with:
run: |
cd sources/nuttx
make host_info
- name: Post-build Disk Space
if: always()
run: df -h
@@ -331,7 +336,7 @@ jobs:
# https://github.com/cython/cython/issues/4500
- uses: actions/setup-python@v6
with:
python-version: '3.10'
python-version: "3.10"
- name: Run Builds
run: |
echo "::add-matcher::sources/nuttx/.github/gcc.json"
@@ -448,7 +453,7 @@ jobs:
- name: Set up Python and install kconfiglib
uses: actions/setup-python@v6
with:
python-version: '3.10'
python-version: "3.10"
- name: Install kconfiglib
run: |
pip install kconfiglib
+1
View File
@@ -14,3 +14,4 @@
/metal
/etl
/minmea
/sysinfo.h
+3 -6
View File
@@ -631,12 +631,9 @@ checkpython3:
SYSINFO_PARSE_FLAGS = "$(realpath $(TOPDIR))"
SYSINFO_PARSE_FLAGS += "-finclude/sysinfo.h"
SYSINFO_FLAGS = "-c"
SYSINFO_FLAGS += "-p"
SYSINFO_FLAGS += -f \""$(shell echo '$(CFLAGS)' | sed 's/"/\\\\\\"/g')"\"
SYSINFO_FLAGS += \""$(shell echo '$(CXXFLAGS)' | sed 's/"/\\\\\\"/g')"\"
SYSINFO_FLAGS += \""$(shell echo '$(LDFLAGS)' | sed 's/"/\\\\\\"/g')"\"
SYSINFO_FLAGS += "--target_info"
SYSINFO_FLAGS = -c
SYSINFO_FLAGS += -p
SYSINFO_FLAGS += --target_info
# host_info: Parse nxdiag example output file (sysinfo.h) and print
+34 -20
View File
@@ -23,6 +23,7 @@ import importlib.util
import os
import platform
import re
import shlex
import subprocess
import sys
@@ -297,6 +298,11 @@ def get_os_version():
return sys_id
FLAGS_WITH_ARGS = {
"-isystem",
}
def get_compilation_flags(flags):
"""
Gets the compilation flags used to compile the NuttX source code and splits
@@ -310,13 +316,23 @@ def get_compilation_flags(flags):
"""
if not flags:
return [""]
return []
flag_list = flags.split(" -")
flag_list[0] = flag_list[0][1:]
flag_list = ["-" + flag for flag in flag_list]
tokens = shlex.split(flags)
merged = []
i = 0
return flag_list
while i < len(tokens):
tok = tokens[i]
if tok in FLAGS_WITH_ARGS and i + 1 < len(tokens):
merged.append(f"{tok} {tokens[i + 1]}")
i += 2
else:
merged.append(tok)
i += 1
return merged
def generate_header(args):
@@ -355,13 +371,6 @@ def generate_header(args):
if args.flags:
cflags, cxxflags, ldflags = args.flags
if cflags:
cflags = cflags[1:-1]
if cxxflags:
cxxflags = cxxflags[1:-1]
if ldflags:
ldflags = ldflags[1:-1]
info["NUTTX_CFLAGS"] = get_compilation_flags(cflags)
info["NUTTX_CXXFLAGS"] = get_compilation_flags(cxxflags)
info["NUTTX_LDFLAGS"] = get_compilation_flags(ldflags)
@@ -370,24 +379,27 @@ def generate_header(args):
len(info["NUTTX_CFLAGS"])
)
output += "static const char *NUTTX_CFLAGS[NUTTX_CFLAGS_ARRAY_SIZE] =\n{\n"
for i in range(len(info["NUTTX_CFLAGS"])):
output += ' "' + info["NUTTX_CFLAGS"][i] + '",\n'
for flag in info["NUTTX_CFLAGS"]:
flag = flag.replace('"', '\\"')
output += ' "' + flag + '",\n'
output += "};\n\n"
output += "#define NUTTX_CXXFLAGS_ARRAY_SIZE {}\n".format(
len(info["NUTTX_CXXFLAGS"])
)
output += "static const char *NUTTX_CXXFLAGS[NUTTX_CXXFLAGS_ARRAY_SIZE] =\n{\n"
for i in range(len(info["NUTTX_CXXFLAGS"])):
output += ' "' + info["NUTTX_CXXFLAGS"][i] + '",\n'
for flag in info["NUTTX_CXXFLAGS"]:
flag = flag.replace('"', '\\"')
output += ' "' + flag + '",\n'
output += "};\n\n"
output += "#define NUTTX_LDFLAGS_ARRAY_SIZE {}\n".format(
len(info["NUTTX_LDFLAGS"])
)
output += "static const char *NUTTX_LDFLAGS[NUTTX_LDFLAGS_ARRAY_SIZE] =\n{\n"
for i in range(len(info["NUTTX_LDFLAGS"])):
output += ' "' + info["NUTTX_LDFLAGS"][i] + '",\n'
for flag in info["NUTTX_LDFLAGS"]:
flag = flag.replace('"', '\\"')
output += ' "' + flag + '",\n'
output += "};\n\n"
# NuttX Configuration
@@ -410,8 +422,10 @@ def generate_header(args):
len(info["NUTTX_CONFIG"])
)
output += "static const char *NUTTX_CONFIG[NUTTX_CONFIG_ARRAY_SIZE] =\n{\n"
for i in range(len(info["NUTTX_CONFIG"])):
output += ' "' + info["NUTTX_CONFIG"][i].replace('"', '\\"') + '",\n'
for cfg in info["NUTTX_CONFIG"]:
cfg = cfg.replace("\\", "\\\\")
cfg = cfg.replace('"', '\\"')
output += ' "' + cfg + '",\n'
output += "};\n\n"
# OS Version
+19 -12
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# tools/parse_sysinfo.py
# tools/host_info_parse.py
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -38,7 +38,7 @@ def parse_information_from_header(file_path):
"""
VARIABLE_NAMES_REGEX = r"static\s+const\s+char\s+\**([A-Za-z0-9_]+)\s*"
VARIABLE_VALUES_REGEX = r'"([^"]*)"|{([^}]+)};'
VARIABLE_VALUES_REGEX = r'\{([^}]+)\}|=\s*"([^"]*)"'
result = {}
var_name_to_print_dict = {
"NUTTX_CFLAGS": "NuttX CFLAGS",
@@ -75,18 +75,25 @@ def parse_information_from_header(file_path):
# Process values to print it prettier
for i in range(len(values_array)):
tmp_list = []
for y in range(len(values_array[i])):
tmp_str = values_array[i][y]
tmp_str = tmp_str.replace('"', "")
tmp_str = tmp_str.replace("\n ", "", 1)
tmp_str = tmp_str.replace(",", "")
processed_values = []
if tmp_str != "":
tmp_list.append(tmp_str)
for array_block, single_value in values_array:
if array_block:
items = []
for line in array_block.splitlines():
line = line.strip()
if not line or line == "{":
continue
line = line.rstrip(",")
if line.startswith('"') and line.endswith('"'):
line = line[1:-1]
line = bytes(line, "utf-8").decode("unicode_escape")
items.append(line)
processed_values.append(tuple(items))
else:
processed_values.append((single_value,))
values_array[i] = tuple(tmp_list)
values_array = processed_values
keys_values_to_return = [var_name_to_print_dict[x] for x in keys_array]
result = dict(zip(keys_values_to_return, values_array))