Skip to content

Commit 8ef5065

Browse files
committed
examples/elf: extend nxpkg validation coverage
Extend the examples/elf ROMFS path so nxpkg validation can use generated package fixtures instead of hand-managed metadata. Generate index.json, bad-index.json, pkgtest.nsh, and pkgfail.nsh from the built hello ELF, recording the correct target arch/compat metadata and SHA-256 digest for the valid fixture. Keep mismatched- target and missing-artifact cases alongside the valid fixture so nxpkg target selection and failure handling can be exercised from the same example tree. Group the paired fixture outputs in one make step so parallel builds do not re-enter the generator independently. This keeps the fixture metadata tied to the actual built hello artifact instead of relying on manually maintained hashes or ad hoc shell setup, making follow-up review and later test reruns more predictable. There is no intended loader or board-behavior change; this is limited to fixture generation inside examples/elf. Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
1 parent 75ee4d9 commit 8ef5065

4 files changed

Lines changed: 611 additions & 3 deletions

File tree

examples/elf/main/CMakeLists.txt

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,109 @@
1919
# ##############################################################################
2020

2121
if(CONFIG_EXAMPLES_ELF)
22-
nuttx_add_application(NAME elf SRCS elf_main.c)
22+
set(ELF_SYMTAB ${CMAKE_CURRENT_BINARY_DIR}/test_symtab.c)
23+
set(ELF_ROMFS_IMG ${CMAKE_CURRENT_BINARY_DIR}/elf_romfs.img)
24+
set(ELF_ROMFS_SRC ${CMAKE_CURRENT_BINARY_DIR}/elf_romfs.c)
25+
26+
set(ELFNAME_BASE hello)
27+
28+
set(ELF_BINARY_FILES)
29+
set(ELF_BINARY_TARGETS)
30+
foreach(ELFNAME IN LISTS ELFNAME_BASE)
31+
list(APPEND ELF_BINARY_FILES ${CMAKE_BINARY_DIR}/bin/${ELFNAME})
32+
33+
if(CMAKE_C_ELF_COMPILER)
34+
list(APPEND ELF_BINARY_TARGETS ${ELFNAME})
35+
else()
36+
list(APPEND ELF_BINARY_TARGETS ELF_${ELFNAME})
37+
endif()
38+
endforeach()
39+
40+
add_custom_command(
41+
OUTPUT ${ELF_SYMTAB}
42+
COMMAND ${NUTTX_APPS_DIR}/tools/mksymtab.sh ${ELF_BINARY_FILES} g_elf >
43+
${ELF_SYMTAB}
44+
DEPENDS ${ELF_BINARY_TARGETS})
45+
46+
set_source_files_properties(${ELF_SYMTAB} PROPERTIES GENERATED TRUE)
47+
48+
set(ELF_GENERATED_OUTPUTS ${ELF_SYMTAB})
49+
set(ELF_SRCS elf_main.c ${ELF_SYMTAB})
50+
51+
if(CONFIG_EXAMPLES_ELF_ROMFS)
52+
set(ELF_ROMFS_DEPS ${ELF_BINARY_TARGETS})
53+
54+
if(CONFIG_SYSTEM_NXPKG)
55+
set(ELF_INDEX_JSON ${CMAKE_BINARY_DIR}/bin/index.json)
56+
set(ELF_BAD_INDEX_JSON ${CMAKE_BINARY_DIR}/bin/bad-index.json)
57+
set(ELF_PKGTEST ${CMAKE_BINARY_DIR}/bin/pkgtest.nsh)
58+
set(ELF_PKGFAIL ${CMAKE_BINARY_DIR}/bin/pkgfail.nsh)
59+
set(ELF_HELLO_BIN ${CMAKE_BINARY_DIR}/bin/hello)
60+
set(ELF_FIXTURE_HOST_DIR ${CMAKE_CURRENT_BINARY_DIR}/host)
61+
set(ELF_FIXTURE_HOST_SRC ${CMAKE_CURRENT_SOURCE_DIR}/host/CMakeLists.txt)
62+
set(ELF_FIXTURE_TOOL
63+
${ELF_FIXTURE_HOST_DIR}/mk_pkg_fixture${CMAKE_EXECUTABLE_SUFFIX})
64+
set(ELF_HELLO_TARGET hello)
65+
66+
if(NOT CMAKE_C_ELF_COMPILER)
67+
set(ELF_HELLO_TARGET ELF_hello)
68+
endif()
69+
70+
add_custom_command(
71+
OUTPUT ${ELF_FIXTURE_TOOL}
72+
COMMAND ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/host -B
73+
${ELF_FIXTURE_HOST_DIR}
74+
COMMAND ${CMAKE_COMMAND} --build ${ELF_FIXTURE_HOST_DIR} --target
75+
mk_pkg_fixture
76+
DEPENDS ${ELF_FIXTURE_HOST_SRC}
77+
${CMAKE_CURRENT_SOURCE_DIR}/mk_pkg_fixture.c
78+
VERBATIM)
79+
80+
add_custom_command(
81+
OUTPUT ${ELF_INDEX_JSON} ${ELF_BAD_INDEX_JSON} ${ELF_PKGTEST}
82+
${ELF_PKGFAIL}
83+
COMMAND
84+
${ELF_FIXTURE_TOOL} ${ELF_HELLO_BIN} ${ELF_INDEX_JSON}
85+
${ELF_BAD_INDEX_JSON} ${ELF_PKGTEST} ${ELF_PKGFAIL} ${CONFIG_ARCH}
86+
${CONFIG_ARCH_BOARD}
87+
DEPENDS ${ELF_HELLO_TARGET} ${ELF_FIXTURE_TOOL}
88+
VERBATIM)
89+
90+
list(APPEND ELF_ROMFS_DEPS ${ELF_INDEX_JSON} ${ELF_BAD_INDEX_JSON}
91+
${ELF_PKGTEST} ${ELF_PKGFAIL})
92+
endif()
93+
94+
add_custom_command(
95+
OUTPUT ${ELF_ROMFS_IMG}
96+
COMMAND genromfs -f ${ELF_ROMFS_IMG} -d ${CMAKE_BINARY_DIR}/bin
97+
DEPENDS ${ELF_ROMFS_DEPS}
98+
VERBATIM)
99+
100+
get_filename_component(ELF_ROMFS_IMG_NAME ${ELF_ROMFS_IMG} NAME)
101+
102+
# xxd -i derives the generated array/length symbol names from the exact path
103+
# it is given - elf_main.c declares them as plain
104+
# "elf_romfs_img"/"elf_romfs_img_len", so xxd must be run against just the
105+
# bare filename (via WORKING_DIRECTORY) rather than the absolute
106+
# ${ELF_ROMFS_IMG} path, or every non-alphanumeric character in that path
107+
# (e.g. every "/" in the build directory) gets folded into the symbol name
108+
# instead, leaving the names elf_main.c expects undefined at link time.
109+
110+
add_custom_command(
111+
OUTPUT ${ELF_ROMFS_SRC}
112+
COMMAND xxd -i ${ELF_ROMFS_IMG_NAME} > ${ELF_ROMFS_SRC}
113+
DEPENDS ${ELF_ROMFS_IMG}
114+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
115+
116+
set_source_files_properties(${ELF_ROMFS_SRC} PROPERTIES GENERATED TRUE)
117+
118+
list(APPEND ELF_GENERATED_OUTPUTS ${ELF_ROMFS_SRC})
119+
list(APPEND ELF_SRCS ${ELF_ROMFS_SRC})
120+
endif()
121+
122+
add_custom_target(elf_artifacts DEPENDS ${ELF_GENERATED_OUTPUTS})
123+
124+
nuttx_add_application(NAME elf SRCS ${ELF_SRCS} DEPENDS elf_artifacts)
23125

24126
# TODO: tests
25127

examples/elf/main/Makefile

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,34 @@ ifeq ($(CONFIG_EXAMPLES_ELF_ROMFS),y)
6969
ROMFSIMG = elf_romfs.img
7070
ROMFSSRC = elf_romfs.c
7171
ROMFSOBJ = $(ROMFSSRC:.c=$(OBJEXT))
72+
ifdef CONFIG_SYSTEM_NXPKG
73+
HOSTOBJSEXT ?= hobj
74+
PKGINDEX = $(BINDIR)/index.json
75+
PKGBADINDEX = $(BINDIR)/bad-index.json
76+
PKGTEST = $(BINDIR)/pkgtest.nsh
77+
PKGFAIL = $(BINDIR)/pkgfail.nsh
78+
PKG_FIXTURE_GEN = mk_pkg_fixture$(HOSTEXEEXT)
79+
PKG_FIXTURE_SRCS = mk_pkg_fixture.c
80+
PKG_FIXTURE_OBJS = $(PKG_FIXTURE_SRCS:.c=.$(HOSTOBJSEXT))
81+
PKG_FIXTURE_OUTPUTS = $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL)
82+
PKG_FIXTURE_INPUTS = $(BINDIR)/hello
83+
PKG_FIXTURE_ARGS = \
84+
$(BINDIR)/hello $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL) \
85+
$(CONFIG_ARCH) $(CONFIG_ARCH_BOARD)
86+
87+
$(PKG_FIXTURE_OBJS): %.$(HOSTOBJSEXT): %.c
88+
@echo "CC: $<"
89+
$(Q) $(HOSTCC) -c $(HOSTCFLAGS) $< -o $@
90+
91+
$(PKG_FIXTURE_GEN): $(PKG_FIXTURE_OBJS)
92+
$(Q) $(HOSTCC) $(HOSTLDFLAGS) $(PKG_FIXTURE_OBJS) -o $@
93+
94+
$(PKG_FIXTURE_OUTPUTS) &: $(PKG_FIXTURE_INPUTS) $(PKG_FIXTURE_GEN)
95+
$(Q) ./$(PKG_FIXTURE_GEN) \
96+
$(PKG_FIXTURE_ARGS)
97+
endif
7298

73-
$(ROMFSIMG):
99+
$(ROMFSIMG): $(PKG_FIXTURE_OUTPUTS)
74100
$(Q) genromfs -d $(BINDIR) -f $@
75101

76102
$(ROMFSSRC): $(ROMFSIMG)
@@ -106,7 +132,7 @@ postinstall:: $(ROMFSOBJ) $(SYMTABOBJ) $(FSIMG_OBJ)
106132
$(call ARLOCK, $(call CONVERT_PATH,$(BIN)), $^)
107133

108134
distclean::
109-
$(Q) $(call DELFILE, $(SYMTABSRC) $(SYMTABOBJ) $(ROMFSSRC) $(ROMFSIMG) $(ROMFSOBJ) $(MODLUE_NAME))
135+
$(Q) $(call DELFILE, $(SYMTABSRC) $(SYMTABOBJ) $(ROMFSSRC) $(ROMFSIMG) $(ROMFSOBJ) $(MODLUE_NAME) $(PKG_FIXTURE_GEN) $(PKG_FIXTURE_OBJS))
110136

111137

112138
include $(APPDIR)/Application.mk
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ##############################################################################
2+
# apps/examples/elf/main/host/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
cmake_minimum_required(VERSION 3.16)
24+
project(elf_fixture_host LANGUAGES C)
25+
26+
add_executable(mk_pkg_fixture ../mk_pkg_fixture.c)

0 commit comments

Comments
 (0)