Skip to content

Commit fece44c

Browse files
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>
1 parent 28026af commit fece44c

File tree

4 files changed

+55
-36
lines changed

4 files changed

+55
-36
lines changed

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,9 @@ SYSINFO_PARSE_FLAGS += "-finclude/sysinfo.h"
633633

634634
SYSINFO_FLAGS = "-c"
635635
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')"\"
636+
SYSINFO_FLAGS += -f "$(CFLAGS)"
637+
SYSINFO_FLAGS += "$(CXXFLAGS)"
638+
SYSINFO_FLAGS += "$(LDFLAGS)"
639639
SYSINFO_FLAGS += "--target_info"
640640

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

tools/host_info_dump.py

Lines changed: 31 additions & 19 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
@@ -411,7 +423,7 @@ def generate_header(args):
411423
)
412424
output += "static const char *NUTTX_CONFIG[NUTTX_CONFIG_ARRAY_SIZE] =\n{\n"
413425
for i in range(len(info["NUTTX_CONFIG"])):
414-
output += ' "' + info["NUTTX_CONFIG"][i].replace('"', '\\"') + '",\n'
426+
output += " " + info["NUTTX_CONFIG"][i] + ",\n"
415427
output += "};\n\n"
416428

417429
# OS Version

tools/host_info_parse.py

Lines changed: 20 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,24 @@ 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+
items.append(line)
91+
processed_values.append(tuple(items))
92+
else:
93+
processed_values.append((single_value,))
94+
95+
values_array = processed_values
9096

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

0 commit comments

Comments
 (0)