Skip to content

Commit 98f683d

Browse files
ML-dev-cryptojerpelea
authored andcommitted
apps: Sanitize PROGNAME for _main symbol generation
Program names containing '-' (e.g. renaming hello to hello-world via PROGNAME) previously generated an invalid identifier <PROGNAME>_main when constructing internal entry-point symbols. This caused build failures for C/C++ applications due to invalid identifiers in compiler definitions and generated builtin_proto.h. Introduce PROGSYM, a sanitized copy of PROGNAME with '-' replaced by '_', and use it wherever an internal C/Zig identifier is constructed (the -Dmain= defines, the Zig RENAMEMAIN rule, and the REGISTER call's entry-point argument). Preserve the original PROGNAME for the registered NSH command name and builtin registry entry, where hyphens are valid and expected. The Zig RENAMEMAIN path is also updated to use PROGSYM for consistency. The current hello_zig and leds_zig examples do not exercise this path, since neither uses a literal 'fn main' entry point, but the change keeps symbol generation consistent for future Zig applications. Testing (WSL2 Ubuntu, x86_64, sim:nsh): - CONFIG_EXAMPLES_HELLO_PROGNAME="hello-world": clean build, 'hello-world' runs and prints 'Hello, World!!' - Reverted to default PROGNAME="hello": clean build, no regression - Confirmed by inspection that RENAMEMAIN's sed substitution does not fire on either current .zig source (generated _tmp.zig is byte-identical to the source) Fixes #19447 Signed-off-by: Ansh Rai <anshrai331@gmail.com>
1 parent 7469e88 commit 98f683d

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

Application.mk

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ ifneq ($(strip $(PROGNAME)),)
122122
NLIST := $(shell seq 1 $(words $(PROGNAME)))
123123
$(foreach i, $(NLIST), \
124124
$(eval PROGNAME_$(word $i,$(PROGOBJ)) := $(word $i,$(PROGNAME))) \
125+
$(eval PROGSYM_$(word $i,$(PROGOBJ)) := $(subst -,_,$(word $i,$(PROGNAME)))) \
125126
$(eval PROGOBJ_$(word $i,$(PROGLIST)) := $(word $i,$(PROGOBJ))) \
126127
$(eval PRIORITY_$(word $i,$(REGLIST)) := \
127128
$(if $(word $i,$(PRIORITY)),$(word $i,$(PRIORITY)),$(lastword $(PRIORITY)))) \
@@ -223,7 +224,7 @@ endef
223224
# rename "main()" in $1 to "xxx_main()" and save to $2
224225
define RENAMEMAIN
225226
$(ECHO_BEGIN)"Rename main() in $1 and save to $2"
226-
$(Q) ${shell cat $1 | sed -e "s/fn[ ]\+main/fn $(addsuffix _main,$(PROGNAME_$@))/" > $2}
227+
$(Q) ${shell cat $1 | sed -e "s/fn[ ]\+main/fn $(addsuffix _main,$(PROGSYM_$@))/" > $2}
227228
$(ECHO_END)
228229
endef
229230

@@ -319,14 +320,14 @@ install:: $(PROGLIST)
319320
else
320321

321322
$(MAINCXXOBJ): $(PREFIX)%$(CXXEXT)$(SUFFIX)$(OBJEXT): %$(CXXEXT)
322-
$(eval $<_CXXFLAGS += ${shell $(DEFINE) "$(CXX)" main=$(addsuffix _main,$(PROGNAME_$@))})
323-
$(eval $<_CXXELFFLAGS += ${shell $(DEFINE) "$(CXX)" main=$(addsuffix _main,$(PROGNAME_$@))})
323+
$(eval $<_CXXFLAGS += ${shell $(DEFINE) "$(CXX)" main=$(addsuffix _main,$(PROGSYM_$@))})
324+
$(eval $<_CXXELFFLAGS += ${shell $(DEFINE) "$(CXX)" main=$(addsuffix _main,$(PROGSYM_$@))})
324325
$(if $(and $(CONFIG_MODULES),$(MODCXXFLAGS)), \
325326
$(call ELFCOMPILEXX, $<, $@), $(call COMPILEXX, $<, $@))
326327

327328
$(MAINCOBJ): $(PREFIX)%.c$(SUFFIX)$(OBJEXT): %.c
328-
$(eval $<_CFLAGS += ${DEFINE_PREFIX}main=$(addsuffix _main,$(PROGNAME_$@)))
329-
$(eval $<_CELFFLAGS += ${DEFINE_PREFIX}main=$(addsuffix _main,$(PROGNAME_$@)))
329+
$(eval $<_CFLAGS += ${DEFINE_PREFIX}main=$(addsuffix _main,$(PROGSYM_$@)))
330+
$(eval $<_CELFFLAGS += ${DEFINE_PREFIX}main=$(addsuffix _main,$(PROGSYM_$@)))
330331
$(if $(and $(CONFIG_MODULES),$(MODCFLAGS)), \
331332
$(call ELFCOMPILE, $<, $@), $(call COMPILE, $<, $@))
332333

@@ -362,10 +363,11 @@ ifeq ($(DO_REGISTRATION),y)
362363

363364
$(REGLIST): $(DEPCONFIG) Makefile
364365
$(eval PROGNAME_$@ := $(basename $(notdir $@)))
366+
$(eval PROGSYM_$@ := $(subst -,_,$(PROGNAME_$@)))
365367
ifeq ($(CONFIG_SCHED_USER_IDENTITY),y)
366-
$(call REGISTER,$(PROGNAME_$@),$(PRIORITY_$@),$(STACKSIZE_$@),$(if $(BUILD_MODULE),,$(PROGNAME_$@)_main),$(UID_$@),$(GID_$@),$(MODE_$@))
368+
$(call REGISTER,$(PROGNAME_$@),$(PRIORITY_$@),$(STACKSIZE_$@),$(if $(BUILD_MODULE),,$(PROGSYM_$@)_main),$(UID_$@),$(GID_$@),$(MODE_$@))
367369
else
368-
$(call REGISTER,$(PROGNAME_$@),$(PRIORITY_$@),$(STACKSIZE_$@),$(if $(BUILD_MODULE),,$(PROGNAME_$@)_main))
370+
$(call REGISTER,$(PROGNAME_$@),$(PRIORITY_$@),$(STACKSIZE_$@),$(if $(BUILD_MODULE),,$(PROGSYM_$@)_main))
369371
endif
370372

371373
register:: $(REGLIST)

0 commit comments

Comments
 (0)