Skip to content

Commit 83c11b2

Browse files
adwait-godbolesimbit18
authored andcommitted
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 <[email protected]>
1 parent 854cd0c commit 83c11b2

File tree

5 files changed

+78
-54
lines changed

5 files changed

+78
-54
lines changed

.github/workflows/build.yml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ name: Build
1515
on:
1616
pull_request:
1717
paths-ignore:
18-
- 'AUTHORS'
19-
- 'CONTRIBUTING.md'
20-
- '**/CODEOWNERS'
21-
- 'Documentation/**'
22-
- 'tools/ci/docker/linux/**'
23-
- 'tools/codeowners/*'
18+
- "AUTHORS"
19+
- "CONTRIBUTING.md"
20+
- "**/CODEOWNERS"
21+
- "Documentation/**"
22+
- "tools/ci/docker/linux/**"
23+
- "tools/codeowners/*"
2424
push:
2525
paths-ignore:
26-
- 'AUTHORS'
27-
- 'CONTRIBUTING.md'
28-
- 'Documentation/**'
26+
- "AUTHORS"
27+
- "CONTRIBUTING.md"
28+
- "Documentation/**"
2929
branches:
30-
- 'releases/*'
30+
- "releases/*"
3131
tags:
3232

3333
permissions:
@@ -38,7 +38,6 @@ concurrency:
3838
cancel-in-progress: true
3939

4040
jobs:
41-
4241
# Fetch the source from nuttx and nuttx-apps repos
4342
Fetch-Source:
4443
runs-on: ubuntu-latest
@@ -153,7 +152,6 @@ jobs:
153152
boards: ${{ fromJSON(needs.Linux-Arch.outputs.selected_builds) }}
154153

155154
steps:
156-
157155
- name: Show Disk Space
158156
run: df -h
159157

@@ -229,6 +227,13 @@ jobs:
229227
./cibuild.sh -c -A -N -R -S testlist/${{matrix.boards}}.dat
230228
fi
231229
230+
- name: Run host_info sanity check
231+
uses: ./sources/nuttx/.github/actions/ci-container
232+
with:
233+
run: |
234+
cd sources/nuttx
235+
make host_info
236+
232237
- name: Post-build Disk Space
233238
if: always()
234239
run: df -h
@@ -331,7 +336,7 @@ jobs:
331336
# https://github.com/cython/cython/issues/4500
332337
- uses: actions/setup-python@v6
333338
with:
334-
python-version: '3.10'
339+
python-version: "3.10"
335340
- name: Run Builds
336341
run: |
337342
echo "::add-matcher::sources/nuttx/.github/gcc.json"
@@ -448,7 +453,7 @@ jobs:
448453
- name: Set up Python and install kconfiglib
449454
uses: actions/setup-python@v6
450455
with:
451-
python-version: '3.10'
456+
python-version: "3.10"
452457
- name: Install kconfiglib
453458
run: |
454459
pip install kconfiglib

include/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
/metal
1515
/etl
1616
/minmea
17+
/sysinfo.h

tools/Unix.mk

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,9 @@ checkpython3:
631631
SYSINFO_PARSE_FLAGS = "$(realpath $(TOPDIR))"
632632
SYSINFO_PARSE_FLAGS += "-finclude/sysinfo.h"
633633

634-
SYSINFO_FLAGS = "-c"
635-
SYSINFO_FLAGS += "-p"
636-
SYSINFO_FLAGS += -f \""$(shell echo '$(CFLAGS)' | sed 's/"/\\\\\\"/g')"\"
637-
SYSINFO_FLAGS += \""$(shell echo '$(CXXFLAGS)' | sed 's/"/\\\\\\"/g')"\"
638-
SYSINFO_FLAGS += \""$(shell echo '$(LDFLAGS)' | sed 's/"/\\\\\\"/g')"\"
639-
SYSINFO_FLAGS += "--target_info"
634+
SYSINFO_FLAGS = -c
635+
SYSINFO_FLAGS += -p
636+
SYSINFO_FLAGS += --target_info
640637

641638
# host_info: Parse nxdiag example output file (sysinfo.h) and print
642639

tools/host_info_dump.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import os
2424
import platform
2525
import re
26+
import shlex
2627
import subprocess
2728
import sys
2829

@@ -297,6 +298,11 @@ def get_os_version():
297298
return sys_id
298299

299300

301+
FLAGS_WITH_ARGS = {
302+
"-isystem",
303+
}
304+
305+
300306
def get_compilation_flags(flags):
301307
"""
302308
Gets the compilation flags used to compile the NuttX source code and splits
@@ -310,13 +316,23 @@ def get_compilation_flags(flags):
310316
"""
311317

312318
if not flags:
313-
return [""]
319+
return []
320+
321+
tokens = shlex.split(flags)
322+
merged = []
323+
i = 0
314324

315-
flag_list = flags.split(" -")
316-
flag_list[0] = flag_list[0][1:]
317-
flag_list = ["-" + flag for flag in flag_list]
325+
while i < len(tokens):
326+
tok = tokens[i]
318327

319-
return flag_list
328+
if tok in FLAGS_WITH_ARGS and i + 1 < len(tokens):
329+
merged.append(f"{tok} {tokens[i + 1]}")
330+
i += 2
331+
else:
332+
merged.append(tok)
333+
i += 1
334+
335+
return merged
320336

321337

322338
def generate_header(args):
@@ -355,13 +371,6 @@ def generate_header(args):
355371
if args.flags:
356372
cflags, cxxflags, ldflags = args.flags
357373

358-
if cflags:
359-
cflags = cflags[1:-1]
360-
if cxxflags:
361-
cxxflags = cxxflags[1:-1]
362-
if ldflags:
363-
ldflags = ldflags[1:-1]
364-
365374
info["NUTTX_CFLAGS"] = get_compilation_flags(cflags)
366375
info["NUTTX_CXXFLAGS"] = get_compilation_flags(cxxflags)
367376
info["NUTTX_LDFLAGS"] = get_compilation_flags(ldflags)
@@ -370,24 +379,27 @@ def generate_header(args):
370379
len(info["NUTTX_CFLAGS"])
371380
)
372381
output += "static const char *NUTTX_CFLAGS[NUTTX_CFLAGS_ARRAY_SIZE] =\n{\n"
373-
for i in range(len(info["NUTTX_CFLAGS"])):
374-
output += ' "' + info["NUTTX_CFLAGS"][i] + '",\n'
382+
for flag in info["NUTTX_CFLAGS"]:
383+
flag = flag.replace('"', '\\"')
384+
output += ' "' + flag + '",\n'
375385
output += "};\n\n"
376386

377387
output += "#define NUTTX_CXXFLAGS_ARRAY_SIZE {}\n".format(
378388
len(info["NUTTX_CXXFLAGS"])
379389
)
380390
output += "static const char *NUTTX_CXXFLAGS[NUTTX_CXXFLAGS_ARRAY_SIZE] =\n{\n"
381-
for i in range(len(info["NUTTX_CXXFLAGS"])):
382-
output += ' "' + info["NUTTX_CXXFLAGS"][i] + '",\n'
391+
for flag in info["NUTTX_CXXFLAGS"]:
392+
flag = flag.replace('"', '\\"')
393+
output += ' "' + flag + '",\n'
383394
output += "};\n\n"
384395

385396
output += "#define NUTTX_LDFLAGS_ARRAY_SIZE {}\n".format(
386397
len(info["NUTTX_LDFLAGS"])
387398
)
388399
output += "static const char *NUTTX_LDFLAGS[NUTTX_LDFLAGS_ARRAY_SIZE] =\n{\n"
389-
for i in range(len(info["NUTTX_LDFLAGS"])):
390-
output += ' "' + info["NUTTX_LDFLAGS"][i] + '",\n'
400+
for flag in info["NUTTX_LDFLAGS"]:
401+
flag = flag.replace('"', '\\"')
402+
output += ' "' + flag + '",\n'
391403
output += "};\n\n"
392404

393405
# NuttX Configuration
@@ -410,8 +422,10 @@ def generate_header(args):
410422
len(info["NUTTX_CONFIG"])
411423
)
412424
output += "static const char *NUTTX_CONFIG[NUTTX_CONFIG_ARRAY_SIZE] =\n{\n"
413-
for i in range(len(info["NUTTX_CONFIG"])):
414-
output += ' "' + info["NUTTX_CONFIG"][i].replace('"', '\\"') + '",\n'
425+
for cfg in info["NUTTX_CONFIG"]:
426+
cfg = cfg.replace("\\", "\\\\")
427+
cfg = cfg.replace('"', '\\"')
428+
output += ' "' + cfg + '",\n'
415429
output += "};\n\n"
416430

417431
# OS Version

tools/host_info_parse.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# tools/parse_sysinfo.py
2+
# tools/host_info_parse.py
33
#
44
# SPDX-License-Identifier: Apache-2.0
55
#
@@ -38,7 +38,7 @@ def parse_information_from_header(file_path):
3838
"""
3939

4040
VARIABLE_NAMES_REGEX = r"static\s+const\s+char\s+\**([A-Za-z0-9_]+)\s*"
41-
VARIABLE_VALUES_REGEX = r'"([^"]*)"|{([^}]+)};'
41+
VARIABLE_VALUES_REGEX = r'\{([^}]+)\}|=\s*"([^"]*)"'
4242
result = {}
4343
var_name_to_print_dict = {
4444
"NUTTX_CFLAGS": "NuttX CFLAGS",
@@ -75,18 +75,25 @@ def parse_information_from_header(file_path):
7575

7676
# Process values to print it prettier
7777

78-
for i in range(len(values_array)):
79-
tmp_list = []
80-
for y in range(len(values_array[i])):
81-
tmp_str = values_array[i][y]
82-
tmp_str = tmp_str.replace('"', "")
83-
tmp_str = tmp_str.replace("\n ", "", 1)
84-
tmp_str = tmp_str.replace(",", "")
85-
86-
if tmp_str != "":
87-
tmp_list.append(tmp_str)
88-
89-
values_array[i] = tuple(tmp_list)
78+
processed_values = []
79+
80+
for array_block, single_value in values_array:
81+
if array_block:
82+
items = []
83+
for line in array_block.splitlines():
84+
line = line.strip()
85+
if not line or line == "{":
86+
continue
87+
line = line.rstrip(",")
88+
if line.startswith('"') and line.endswith('"'):
89+
line = line[1:-1]
90+
line = bytes(line, "utf-8").decode("unicode_escape")
91+
items.append(line)
92+
processed_values.append(tuple(items))
93+
else:
94+
processed_values.append((single_value,))
95+
96+
values_array = processed_values
9097

9198
keys_values_to_return = [var_name_to_print_dict[x] for x in keys_array]
9299
result = dict(zip(keys_values_to_return, values_array))

0 commit comments

Comments
 (0)