testing: Add application to gather debug information (nxdiag)

This commit aims to add an application to gather debug information about the host and target systems. It can also perform some diagnostic checks on the host and target systems.
This will facilitate the process of users seeking assistance for solving some problem.

Current capabilities:
- Get host OS version;
- Get host python modules;
- Get host system packages;
- Get host PATH;
- Get host compilation flags for the target;
- Get target NuttX configuration;
- Get target OS version, hostname, build and architecture;
- Capable of adding custom, vendor specific, information. Currently gathering only Espressif related info:
  - Get the bootloader version of detected image files;
  - Get the version of different toolchains used by Espressif chips;
  - Get Esptool version.
This commit is contained in:
Lucas Saavedra Vaz 2023-05-24 20:55:44 -03:00 committed by Alan Carvalho de Assis
parent 6f4546f597
commit eb36c15171
6 changed files with 1143 additions and 0 deletions

1
testing/nxdiag/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
sysinfo.h

71
testing/nxdiag/Kconfig Normal file
View File

@ -0,0 +1,71 @@
config TESTING_NXDIAG
bool "System information and diagnostic (nxdiag)"
default n
---help---
Enable the nxdiag application to gather and display host
and target system information. It can also perform some
diagnostic checks on the host and target systems.
if TESTING_NXDIAG
config TESTING_NXDIAG_PRIORITY
int "Nxdiag task priority"
default 100
config TESTING_NXDIAG_STACKSIZE
int "Nxdiag stack size"
default DEFAULT_TASK_STACKSIZE
comment "NuttX system information"
config TESTING_NXDIAG_CONF
bool "Get NuttX configuration"
default n
---help---
Enable the nxdiag application to list the configuration options
used to compile NuttX. This is useful for debugging the host and
target systems. Enables the "-c" and "--nuttx-config" options.
config TESTING_NXDIAG_COMP_FLAGS
bool "Get NuttX compilation flags"
default n
---help---
Enable the nxdiag application to list the NuttX compilation
flags. This is useful for debugging the host and target
systems. Enables the "-f" and "--nuttx-flags" options.
comment "Host system infromation"
config TESTING_NXDIAG_HOST_PATH
bool "Get host system PATH"
default n
---help---
Enable the nxdiag application to list the host system PATH
variable. This is useful for debugging the host system.
Enables the "-p" and "--host-path" options.
config TESTING_NXDIAG_HOST_PACKAGES
bool "Get host system packages"
default n
---help---
Enable the nxdiag application to list the installed packages
on the host system. This is useful for debugging the host
system. Enables the "-k" and "--host-packages" options.
config TESTING_NXDIAG_HOST_MODULES
bool "Get host system python modules"
default n
---help---
Enable the nxdiag application to list the installed Python
modules on the host system. This is useful for debugging the
host system. Enables the "-m" and "--host-modules" options.
comment "Vendor specific information"
config TESTING_NXDIAG_ESPRESSIF
bool "Espressif"
default n
---help---
Enable Espressif-specific information and checks.
endif

23
testing/nxdiag/Make.defs Normal file
View File

@ -0,0 +1,23 @@
############################################################################
# apps/testing/nxdiag/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
ifneq ($(CONFIG_TESTING_NXDIAG),)
CONFIGURED_APPS += $(APPDIR)/testing/nxdiag
endif

88
testing/nxdiag/Makefile Normal file
View File

@ -0,0 +1,88 @@
############################################################################
# apps/testing/nxdiag/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(APPDIR)/Make.defs
NXTOOLSDIR = $(APPDIR)/tools
# Sysinfo application info
PROGNAME = nxdiag
PRIORITY = $(CONFIG_TESTING_NXDIAG_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_NXDIAG_STACKSIZE)
MODULE = $(CONFIG_TESTING_NXDIAG)
# Sysinfo application
MAINSRC = nxdiag.c
NXDIAG_FLAGS = "$(realpath $(TOPDIR))"
ifeq ($(CONFIG_TESTING_NXDIAG_CONF),y)
NXDIAG_FLAGS += --config
endif
ifeq ($(CONFIG_TESTING_NXDIAG_COMP_FLAGS),y)
NXDIAG_FLAGS += --flags \""$(shell echo '$(CFLAGS)' | sed 's/"/\\\\\\"/g')"\"
NXDIAG_FLAGS += \""$(shell echo '$(CXXFLAGS)' | sed 's/"/\\\\\\"/g')"\"
NXDIAG_FLAGS += \""$(shell echo '$(LDFLAGS)' | sed 's/"/\\\\\\"/g')"\"
endif
ifeq ($(CONFIG_TESTING_NXDIAG_HOST_PACKAGES),y)
NXDIAG_FLAGS += --packages
endif
ifeq ($(CONFIG_TESTING_NXDIAG_HOST_MODULES),y)
NXDIAG_FLAGS += --modules
endif
ifeq ($(CONFIG_TESTING_NXDIAG_HOST_PATH),y)
NXDIAG_FLAGS += --path
endif
# Vendor-specific checks
ifeq ($(CONFIG_TESTING_NXDIAG_ESPRESSIF),y)
ifdef ESPTOOL_BINDIR
NXDIAG_FLAGS += --espressif "$(ESPTOOL_BINDIR)"
else
NXDIAG_FLAGS += --espressif "$(TOPDIR)"
endif
endif
# Common build
.PHONY: sysinfo.h
checkpython3:
@if [ -z "$$(which python3)" ]; then \
echo "ERROR: python3 not found in PATH"; \
echo " Please install python3 or fix the PATH"; \
exit 1; \
fi
sysinfo.h : checkpython3
@python3 $(NXTOOLSDIR)$(DELIM)host_sysinfo.py $(NXDIAG_FLAGS) > sysinfo.h || (echo "host_sysinfo.py failed $$?"; exit 1)
context:: sysinfo.h
distclean::
$(call DELFILE, sysinfo.h)
include $(APPDIR)/Application.mk

350
testing/nxdiag/nxdiag.c Normal file
View File

@ -0,0 +1,350 @@
/****************************************************************************
* apps/testing/nxdiag/nxdiag.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>
#include <nuttx/config.h>
#include <nuttx/version.h>
#include "sysinfo.h"
/****************************************************************************
* Private Data
****************************************************************************/
/* Valid Command Line Arguments */
static const char *g_valid_args[] =
{
"-h",
"--help",
"-n",
"--nuttx",
#ifdef CONFIG_TESTING_NXDIAG_COMP_FLAGS
"-f",
"--flags",
#endif
#ifdef CONFIG_TESTING_NXDIAG_CONF
"-c",
"--config",
#endif
"-o",
"--host-os",
#ifdef CONFIG_TESTING_NXDIAG_HOST_PATH
"-p",
"--host-path",
#endif
#ifdef CONFIG_TESTING_NXDIAG_HOST_PACKAGES
"-k",
"--host-packages",
#endif
#ifdef CONFIG_TESTING_NXDIAG_HOST_MODULES
"-m",
"--host-modules",
#endif
"-v",
"--vendor-specific",
"--all"
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: search_str_in_arr
*
* Description:
* Search for a string in an array of strings.
*
* Input Parameters:
* size - Size of the array.
* arr - Array of strings.
* str - String to search for.
*
* Returned Value:
* True if string is found, false otherwise.
*
****************************************************************************/
bool search_str_in_arr(int size, char *arr[], char *str)
{
int i;
for (i = 0; i < size; i++)
{
if (strcmp(arr[i], str) == 0)
{
return true;
}
}
return false;
}
/****************************************************************************
* Name: are_valid_args
*
* Description:
* Check if provided command line arguments are valid. Valid arguments are
* defined in g_valid_args array.
*
* Input Parameters:
* argc - Number of arguments.
* argv - Array of arguments.
*
* Returned Value:
* True if all arguments are valid, false otherwise.
*
****************************************************************************/
bool are_valid_args(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++)
{
if (!search_str_in_arr(nitems(g_valid_args),
(char **)g_valid_args, argv[i]))
{
return false;
}
}
return true;
}
/****************************************************************************
* Name: print_array
*
* Description:
* Print a constant array of strings.
*
* Input Parameters:
* arr - Array of strings.
*
* Returned Value:
* None.
*
****************************************************************************/
void print_array(const char *arr[], int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("\t%s\n", arr[i]);
}
printf("\n");
}
/****************************************************************************
* Name: print_array
*
* Description:
* Print an array of strings.
*
* Input Parameters:
* arr - Array of strings.
*
* Returned Value:
* None.
*
****************************************************************************/
static void print_usage(char *prg)
{
fprintf(stderr, "%s - Get host and target system debug information.\n",
prg);
fprintf(stderr, "\nUsage: %s [options]\n", prg);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -h \
Show this message\n");
fprintf(stderr, " -n, --nuttx \
Output the NuttX operational system information.\n");
#ifdef CONFIG_TESTING_NXDIAG_COMP_FLAGS
fprintf(stderr, " -f, --flags \
Output the NuttX compilation and linker flags used.\n");
#endif
#ifdef CONFIG_TESTING_NXDIAG_CONF
fprintf(stderr, " -c, --config \
Output the NuttX configuration options used.\n");
#endif
fprintf(stderr, " -o, --host-os \
Output the host system operational system information.\n");
#ifdef CONFIG_TESTING_NXDIAG_HOST_PATH
fprintf(stderr, " -p, --host-path \
Output the host PATH environment variable.\n");
#endif
#ifdef CONFIG_TESTING_NXDIAG_HOST_PACKAGES
fprintf(stderr, " -k, --host-packages \
Output the host installed system packages.\n");
#endif
#ifdef CONFIG_TESTING_NXDIAG_HOST_MODULES
fprintf(stderr, " -m, --host-modules \
Output the host installed Python modules.\n");
#endif
fprintf(stderr, " -v, --vendor-specific \
Output vendor specific information.\n");
fprintf(stderr, " --all \
Output all available information.\n");
fprintf(stderr, "\n");
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char *argv[])
{
if (argc == 1 || !are_valid_args(argc, argv))
{
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
if (search_str_in_arr(argc, argv, "-h") ||
search_str_in_arr(argc, argv, "--help"))
{
print_usage(argv[0]);
exit(EXIT_SUCCESS);
}
printf("Nxdiag Report:\n\n");
/* NuttX Info */
if (search_str_in_arr(argc, argv, "-n") ||
search_str_in_arr(argc, argv, "--nuttx") ||
search_str_in_arr(argc, argv, "--all"))
{
char hostname[HOST_NAME_MAX];
gethostname(hostname, HOST_NAME_MAX);
hostname[HOST_NAME_MAX - 1] = '\0';
printf("NuttX RTOS info:\n");
printf("\tHostname: %s\n", hostname);
printf("\tRelease: %s\n", CONFIG_VERSION_STRING);
#if defined(__DATE__) && defined(__TIME__)
printf("\tBuild: %s %s %s\n",
CONFIG_VERSION_BUILD, __DATE__, __TIME__);
#else
printf("\tBuild: %s\n", CONFIG_VERSION_BUILD);
#endif
printf("\tArch: %s\n\n", CONFIG_ARCH);
}
#ifdef CONFIG_TESTING_NXDIAG_COMP_FLAGS
if (search_str_in_arr(argc, argv, "-f") ||
search_str_in_arr(argc, argv, "--flags") ||
search_str_in_arr(argc, argv, "--all"))
{
printf("NuttX CFLAGS:\n");
print_array(NUTTX_CFLAGS, NUTTX_CFLAGS_ARRAY_SIZE);
printf("NuttX CXXFLAGS:\n");
print_array(NUTTX_CXXFLAGS, NUTTX_CXXFLAGS_ARRAY_SIZE);
printf("NuttX LDFLAGS:\n");
print_array(NUTTX_LDFLAGS, NUTTX_LDFLAGS_ARRAY_SIZE);
}
#endif
#ifdef CONFIG_TESTING_NXDIAG_CONF
if (search_str_in_arr(argc, argv, "-c") ||
search_str_in_arr(argc, argv, "--config") ||
search_str_in_arr(argc, argv, "--all"))
{
printf("NuttX configuration options:\n");
print_array(NUTTX_CONFIG, NUTTX_CONFIG_ARRAY_SIZE);
}
#endif
/* Host Info */
if (search_str_in_arr(argc, argv, "-o") ||
search_str_in_arr(argc, argv, "--host-os") ||
search_str_in_arr(argc, argv, "--all"))
{
printf("Host system OS:\n");
printf("\t%s\n\n", OS_VERSION);
}
#ifdef CONFIG_TESTING_NXDIAG_HOST_PATH
if (search_str_in_arr(argc, argv, "-p") ||
search_str_in_arr(argc, argv, "--host-path") ||
search_str_in_arr(argc, argv, "--all"))
{
printf("Host system PATH:\n");
print_array(SYSTEM_PATH, SYSTEM_PATH_ARRAY_SIZE);
}
#endif
#ifdef CONFIG_TESTING_NXDIAG_HOST_PACKAGES
if (search_str_in_arr(argc, argv, "-k") ||
search_str_in_arr(argc, argv, "--host-packages") ||
search_str_in_arr(argc, argv, "--all"))
{
printf("Host system installed packages:\n");
print_array(INSTALLED_PACKAGES, INSTALLED_PACKAGES_ARRAY_SIZE);
}
#endif
#ifdef CONFIG_TESTING_NXDIAG_HOST_MODULES
if (search_str_in_arr(argc, argv, "-m") ||
search_str_in_arr(argc, argv, "--host-modules") ||
search_str_in_arr(argc, argv, "--all"))
{
printf("Host system installed python modules:\n");
print_array(PYTHON_MODULES, PYTHON_MODULES_ARRAY_SIZE);
}
#endif
/* Vendor Specific Info */
if (search_str_in_arr(argc, argv, "-v") ||
search_str_in_arr(argc, argv, "--vendor-specific") ||
search_str_in_arr(argc, argv, "--all"))
{
/* Please don't forget to add the vendor specific information
* in alphabetical order. Also, please update the documentation
* in Documentation/applications/nxdiag
*/
#ifdef CONFIG_TESTING_NXDIAG_ESPRESSIF
printf("Espressif specific information:\n\n");
printf("Bootloader version:\n");
print_array(ESPRESSIF_BOOTLOADER, ESPRESSIF_BOOTLOADER_ARRAY_SIZE);
printf("Toolchain version:\n");
print_array(ESPRESSIF_TOOLCHAIN, ESPRESSIF_TOOLCHAIN_ARRAY_SIZE);
printf("Esptool version: %s\n\n", ESPRESSIF_ESPTOOL);
#endif
}
return 0;
}

610
tools/host_sysinfo.py Executable file
View File

@ -0,0 +1,610 @@
#!/usr/bin/env python3
import argparse
import os
import platform
import re
import subprocess
import sys
# Custom argparse actions #
class validate_flags_arg(argparse.Action):
"""
Custom argparse action to validate the number of parameters passed to the
--flags argument. Exactly three parameters are required.
"""
def __call__(self, parser, namespace, values, option_string=None):
if len(values) != 3:
raise argparse.ArgumentError(
self,
"Invalid number of parameters for --flags. Exactly three parameters are required.",
)
setattr(namespace, self.dest, values)
# Common functions #
def get_installed_packages():
"""
Gets the list of packages installed on the host system. This function works on
Linux (Debian, Red Hat, and Arch-based distros), Windows, and macOS.
Args:
None.
Returns:
list: Packages (with version) installed on the host system.
"""
packages = []
if platform.system() == "Linux":
package_managers = [
{
"name": "Dpkg",
"command": ["dpkg", "-l"],
"skip_lines": 5,
"info_name": 1,
"info_version": 2,
},
{
"name": "Rpm",
"command": ["rpm", "-qa", "--queryformat", '"%{NAME} %{VERSION}\\n"'],
"skip_lines": 0,
"info_name": 0,
"info_version": 1,
},
{
"name": "Pacman",
"command": ["pacman", "-Q", "--queryformat", '"%n %v\\n"'],
"skip_lines": 0,
"info_name": 0,
"info_version": 1,
},
]
for manager in package_managers:
try:
process = subprocess.Popen(manager["command"], stdout=subprocess.PIPE)
output, error = process.communicate()
lines = output.decode("utf-8").splitlines()
# Skip the specified number of lines based on the package manager
if lines:
lines = lines[manager["skip_lines"] :]
current_packages = []
for line in lines:
package_info = line.split()
package = package_info[manager["info_name"]]
version = package_info[manager["info_version"]]
current_packages.append(f"{package} ({version})")
if current_packages:
packages.extend(current_packages)
break
except (FileNotFoundError, subprocess.CalledProcessError):
pass
elif platform.system() == "Windows":
try:
output = subprocess.check_output(
[
"powershell",
"Get-ItemProperty HKLM:\\Software\\Microsoft\\Windows\\"
+ "CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion",
]
)
output = output.decode("utf-8", errors="ignore").split("\r\n")
for line in output[3:]:
line = line.strip()
if line:
match = re.match(r"^(.*?)(\s{2,}[^ ]+)?$", line)
if match:
name = match.group(1).strip()
version = (
match.group(2).strip() if match.group(2) else "Unknown"
)
packages.append(f"{name} ({version})")
except subprocess.CalledProcessError:
pass
elif platform.system() == "Darwin":
try:
output = subprocess.check_output(["pkgutil", "--pkgs"])
output = output.decode("utf-8").split("\n")
for package in output:
if "." in package:
try:
info = subprocess.check_output(
["pkgutil", "--pkg-info", package]
)
info = info.decode("utf-8")
version = info.split("version: ")[1].split("\n")[0]
except subprocess.CalledProcessError:
version = "Unknown"
packages.append(f"{package} ({version})")
except subprocess.CalledProcessError:
pass
packages.sort()
return packages
def get_python_modules():
"""
Gets the list of python modules installed on the host system.
Args:
None.
Returns:
list: Python modules (with version) installed on the host system.
"""
modules = []
output = subprocess.check_output(
["pip", "list", "--format=freeze"], universal_newlines=True
)
for line in output.splitlines():
if line.startswith("#"):
continue
package_info = line.split("==")
if len(package_info) > 1:
modules.append("{}-{}".format(package_info[0], package_info[1]))
else:
modules.append(package_info[0])
return modules
def get_system_path():
"""
Gets the PATH environment variable.
Args:
None.
Returns:
str: The PATH environment variable.
"""
return os.environ.get("PATH", "")
def get_os_version():
"""
Gets the OS distribution and version. This function works on Linux, Windows,
and macOS. On Linux, if the distro package is installed, it will be used to
get the OS distribution. Otherwise, the platform.system() function will be
used.
Args:
None.
Returns:
str: The OS distribution and version.
"""
os_name = platform.system()
os_version = platform.release()
os_distro = ""
if os_name == "Windows":
os_distro = "Windows"
os_version = platform.win32_ver()[0]
elif os_name == "Darwin":
os_distro = "macOS"
os_version = platform.mac_ver()[0]
elif os_name == "Linux":
try:
import distro
return distro.name(pretty=True)
except ImportError:
os_distro = platform.system()
return f"{os_distro} {os_version}"
def get_compilation_flags(flags):
"""
Gets the compilation flags used to compile the NuttX source code and splits
them into a list.
Args:
arg (str): The compilation flags.
Returns:
list: The compilation flags.
"""
if not flags:
return [""]
flag_list = flags.split(" -")
flag_list[0] = flag_list[0][1:]
flag_list = ["-" + flag for flag in flag_list]
return flag_list
def generate_header(args):
"""
Generates the sysinfo.h header file that contains information about the host system
and NuttX configuration that can be used by NuttX applications.
Args:
args (argparse.Namespace): The command line arguments.
Returns:
str: The contents of the sysinfo.h header file.
"""
info = {}
output = ""
output += "/****************************************************************************\n"
output += " * sysinfo.h\n"
output += " *\n"
output += " * Auto-generated by a Python script. Do not edit!\n"
output += " *\n"
output += (
" * This file contains information about the host and target systems that\n"
)
output += " * can be used by NuttX applications.\n"
output += " *\n"
output += " ****************************************************************************/\n\n"
output += "#ifndef __SYSTEM_INFO_H\n"
output += "#define __SYSTEM_INFO_H\n\n"
# NuttX Compilation Flags
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)
output += "#define NUTTX_CFLAGS_ARRAY_SIZE {}\n".format(
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'
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'
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'
output += "};\n\n"
# NuttX Configuration
if args.config:
info["NUTTX_CONFIG"] = []
config_path = os.path.abspath("./.config")
try:
with open(config_path, "r") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
info["NUTTX_CONFIG"].append(line)
output += "#define NUTTX_CONFIG_ARRAY_SIZE {}\n".format(
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'
output += "};\n\n"
except FileNotFoundError:
print("Error: NuttX configuration file not found: {}".format(config_path))
sys.exit(1)
# OS Version
info["OS_VERSION"] = get_os_version()
output += 'static const char OS_VERSION[] = "{}";\n\n'.format(info["OS_VERSION"])
# System Path
if args.path:
info["SYSTEM_PATH"] = str(get_system_path()).split(":")
output += "#define SYSTEM_PATH_ARRAY_SIZE {}\n".format(len(info["SYSTEM_PATH"]))
output += "static const char *SYSTEM_PATH[SYSTEM_PATH_ARRAY_SIZE] =\n{\n"
for i in range(len(info["SYSTEM_PATH"])):
output += ' "' + info["SYSTEM_PATH"][i] + '",\n'
output += "};\n\n"
# Installed Packages
if args.packages:
info["INSTALLED_PACKAGES"] = [str(x) for x in get_installed_packages()]
output += "#define INSTALLED_PACKAGES_ARRAY_SIZE {}\n".format(
len(info["INSTALLED_PACKAGES"])
)
output += "static const char *INSTALLED_PACKAGES[INSTALLED_PACKAGES_ARRAY_SIZE] =\n{\n"
for i in range(len(info["INSTALLED_PACKAGES"])):
output += ' "' + info["INSTALLED_PACKAGES"][i] + '",\n'
output += "};\n\n"
# Python Modules
if args.modules:
info["PYTHON_MODULES"] = [str(x) for x in get_python_modules()]
output += "#define PYTHON_MODULES_ARRAY_SIZE {}\n".format(
len(info["PYTHON_MODULES"])
)
output += "static const char *PYTHON_MODULES[PYTHON_MODULES_ARRAY_SIZE] =\n{\n"
for i in range(len(info["PYTHON_MODULES"])):
output += ' "' + info["PYTHON_MODULES"][i] + '",\n'
output += "};\n\n"
# Vendor Specific
# Espressif
if args.espressif:
# Espressif bootloader version
info["ESPRESSIF_BOOTLOADER"] = get_espressif_bootloader_version(
args.espressif[0]
)
output += "#define ESPRESSIF_BOOTLOADER_ARRAY_SIZE {}\n".format(
len(info["ESPRESSIF_BOOTLOADER"])
)
output += "static const char *ESPRESSIF_BOOTLOADER[ESPRESSIF_BOOTLOADER_ARRAY_SIZE] =\n{\n"
for key, value in info["ESPRESSIF_BOOTLOADER"].items():
output += ' "{}: {}",\n'.format(key, value)
output += "};\n\n"
# Espressif toolchain version
info["ESPRESSIF_TOOLCHAIN"] = get_espressif_toolchain_version()
output += "#define ESPRESSIF_TOOLCHAIN_ARRAY_SIZE {}\n".format(
len(info["ESPRESSIF_TOOLCHAIN"])
)
output += "static const char *ESPRESSIF_TOOLCHAIN[ESPRESSIF_TOOLCHAIN_ARRAY_SIZE] =\n{\n"
for key, value in info["ESPRESSIF_TOOLCHAIN"].items():
output += ' "{}: {}",\n'.format(key, value)
output += "};\n\n"
# Espressif esptool version
info["ESPRESSIF_ESPTOOL"] = next(
(s for s in get_python_modules() if "esptool" in s), "Not found"
)
output += 'static const char ESPRESSIF_ESPTOOL[] = "{}";\n\n'.format(
info["ESPRESSIF_ESPTOOL"].split("-")[1]
)
output += "#endif /* __SYSTEM_INFO_H */\n"
return output
# Vendor specific functions #
def get_espressif_bootloader_version(bindir):
"""
Get the bootloader version for Espressif chips from the bootloader binary. This
function works on Linux, Windows, and macOS.
Args:
bindir (str): The path to the bootloader binary directory.
Returns:
dict: A dictionary containing the bootloader version for each chip.
"""
regex = r"^(?=.*\bv\d+(\.\d+){1,2}\b).+$"
bootloader_chips = [
"esp32",
"esp32s2",
"esp32s3",
"esp32c2",
"esp32c3",
"esp32c6",
"esp32h2",
]
bootloader_version = {}
for chip in bootloader_chips:
file = "bootloader-{}.bin".format(chip)
path = os.path.join(bindir, file)
if os.path.isfile(path):
if platform.system() == "Linux":
process = subprocess.Popen(["strings", path], stdout=subprocess.PIPE)
elif platform.system() == "Windows":
process = subprocess.Popen(
[
"powershell",
"Get-Content -Raw -Encoding Byte {} |".format(path)
+ " % { [char[]]$_ -join \"\" } | Select-String -Pattern '[\\x20-\\x7E]+'"
+ " -AllMatches | % { $_.Matches } | % { $_.Value }",
],
stdout=subprocess.PIPE,
)
elif platform.system() == "Darwin":
process = subprocess.Popen(
["strings", "-", path], stdout=subprocess.PIPE
)
else:
bootloader_version[chip] = "Not supported on host OS"
break
output, error = process.communicate()
strings_out = output.decode("utf-8", errors="ignore")
matches = re.finditer(regex, strings_out, re.MULTILINE)
try:
bootloader_version[chip] = next(matches).group(0)
except StopIteration:
bootloader_version[chip] = "Unknown"
else:
bootloader_version[chip] = "Bootloader image not found"
return bootloader_version
def get_espressif_toolchain_version():
"""
Get the version of different toolchains used by Espressif chips.
Args:
None.
Returns:
dict: A dictionary containing the toolchain version for each toolchain.
"""
toolchain_version = {}
toolchain_bins = [
"clang",
"gcc",
"xtensa-esp32-elf-gcc",
"xtensa-esp32s2-elf-gcc",
"xtensa-esp32s3-elf-gcc",
"riscv32-esp-elf-gcc",
"riscv64-unknown-elf-gcc",
]
for binary in toolchain_bins:
try:
version_output = subprocess.check_output(
[binary, "--version"], stderr=subprocess.STDOUT, universal_newlines=True
)
version_lines = version_output.split("\n")
version = version_lines[0].strip()
toolchain_version[binary] = version
except (subprocess.CalledProcessError, FileNotFoundError):
toolchain_version[binary] = "Not found"
return toolchain_version
# Main #
if __name__ == "__main__":
"""
Main function for the script. This function is called when the script is
executed directly. It parses the command line arguments and calls the
appropriate functions. It also prints the output generated to stdout.
Required arguments:
nuttx_path: The path to the NuttX source directory.
Optional arguments:
The command line arguments. The available arguments are:
-h, --help:
Show the help message and exit.
-m, --modules:
Get the list of installed Python modules.
-k, --packages:
Get the list of installed system packages.
-p, --path:
Get the system PATH environment variable.
-c, --config:
Get the NuttX compilation configurations used.
-f, --flags <CFLAGS> <CXXFLAGS> <LDFLAGS>:
Provide the NuttX compilation flags used.
--espressif <ESPTOOL_BINDIR>:
Get Espressif specific information.
Requires the path to the bootloader binary directory.
"""
# Generic arguments
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("nuttx_path", help="NuttX source directory path.")
parser.add_argument(
"-h",
"--help",
action="help",
default=argparse.SUPPRESS,
help="Show this help message and exit.",
)
parser.add_argument(
"-m",
"--modules",
action="store_true",
help="Get the list of installed Python modules.",
)
parser.add_argument(
"-k",
"--packages",
action="store_true",
help="Get the list of installed system packages.",
)
parser.add_argument(
"-p",
"--path",
action="store_true",
help="Get the system PATH environment variable.",
)
parser.add_argument(
"-c",
"--config",
action="store_true",
help="Get the NuttX compilation configurations used.",
)
parser.add_argument(
"-f",
"--flags",
nargs=3,
default=[],
metavar=("CFLAGS", "CXXFLAGS", "LDFLAGS"),
action=validate_flags_arg,
help="Provide the NuttX compilation and linker flags used.",
)
# Vendor specific arguments
parser.add_argument(
"--espressif",
nargs=1,
default=[],
metavar="ESPTOOL_BINDIR",
help="Get Espressif specific information. Requires the path to the bootloader binary directory.",
)
# Parse arguments
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
os.chdir(args.nuttx_path)
header = generate_header(args)
print(header)