Skip to content

Commit 14d4059

Browse files
yf13xiaoxiang781216
authored andcommitted
Zig: revise apps build support
This requires nuttx/pull/11548 to work. - Zig apps can use normal entrance "main()" now as we - added RENAMEMAIN for none BUILD_MODULE case. - enabled Zig program building in BUILD_MODULE case. Note the Zig main source need to have "fn main(" pattern as renaming is implemented via `sed`. Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
1 parent dbcbec3 commit 14d4059

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

Application.mk

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ ifneq ($(BUILD_MODULE),y)
9494
endif
9595

9696
ifneq ($(strip $(PROGNAME)),)
97-
PROGOBJ := $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ)
97+
PROGOBJ := $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ) $(MAINZIGOBJ)
9898
PROGLIST := $(addprefix $(BINDIR)$(DELIM),$(PROGNAME))
9999
REGLIST := $(addprefix $(BUILTIN_REGISTRY)$(DELIM),$(addsuffix .bdat,$(PROGNAME)))
100100

@@ -168,9 +168,9 @@ define ELFCOMPILERUST
168168
$(ECHO_END)
169169
endef
170170

171+
# Remove target suffix here since zig compiler add .o automatically
171172
define ELFCOMPILEZIG
172173
$(ECHO_BEGIN)"ZIG: $1 "
173-
# Remove target suffix here since zig compiler add .o automatically
174174
$(Q) $(ZIG) build-obj $(ZIGELFFLAGS) $($(strip $1)_ZIGELFFLAGS) --name $(basename $2) $1
175175
$(ECHO_END)
176176
endef
@@ -181,6 +181,13 @@ define ELFLD
181181
$(ECHO_END)
182182
endef
183183

184+
# rename "main()" in $1 to "xxx_main()" and save to $2
185+
define RENAMEMAIN
186+
$(ECHO_BEGIN)"Rename main() in $1 and save to $2"
187+
$(Q) ${shell cat $1 | sed -e "s/fn[ ]\+main/fn $(addsuffix _main,$(PROGNAME_$@))/" > $2}
188+
$(ECHO_END)
189+
endef
190+
184191
$(RAOBJS): %.s$(SUFFIX)$(OBJEXT): %.s
185192
$(if $(and $(CONFIG_BUILD_LOADABLE),$(AELFFLAGS)), \
186193
$(call ELFASSEMBLE, $<, $@), $(call ASSEMBLE, $<, $@))
@@ -235,9 +242,13 @@ $(MAINCOBJ): %.c$(SUFFIX)$(OBJEXT): %.c
235242
$(if $(and $(CONFIG_BUILD_LOADABLE),$(CELFFLAGS)), \
236243
$(call ELFCOMPILE, $<, $@), $(call COMPILE, $<, $@))
237244

238-
$(PROGLIST): $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ)
245+
$(MAINZIGOBJ): %$(ZIGEXT)$(SUFFIX)$(OBJEXT): %$(ZIGEXT)
246+
$(if $(and $(CONFIG_BUILD_LOADABLE),$(CELFFLAGS)), \
247+
$(call ELFCOMPILEZIG, $<, $@), $(call COMPILEZIG, $<, $@))
248+
249+
$(PROGLIST): $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ) $(MAINZIGOBJ)
239250
$(Q) mkdir -p $(BINDIR)
240-
$(call ELFLD,$(PROGOBJ_$@),$(call CONVERT_PATH,$@))
251+
$(call ELFLD, $(PROGOBJ_$@), $(call CONVERT_PATH,$@))
241252
$(Q) chmod +x $@
242253
ifneq ($(CONFIG_DEBUG_SYMBOLS),y)
243254
$(Q) $(STRIP) $@
@@ -265,8 +276,10 @@ $(MAINRUSTOBJ): %$(RUSTEXT)$(SUFFIX)$(OBJEXT): %$(RUSTEXT)
265276
$(call ELFCOMPILERUST, $<, $@), $(call COMPILERUST, $<, $@))
266277

267278
$(MAINZIGOBJ): %$(ZIGEXT)$(SUFFIX)$(OBJEXT): %$(ZIGEXT)
279+
$(Q) $(call RENAMEMAIN, $<, $(basename $<)_tmp.zig)
268280
$(if $(and $(CONFIG_BUILD_LOADABLE),$(CELFFLAGS)), \
269-
$(call ELFCOMPILEZIG, $<, $@), $(call COMPILEZIG, $<, $@))
281+
$(call ELFCOMPILEZIG, $(basename $<)_tmp.zig, $@), $(call COMPILEZIG, $(basename $<)_tmp.zig, $@))
282+
$(Q) rm -f $(basename $<)_tmp.zig
270283

271284
install::
272285
@:

examples/hello_zig/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ include $(APPDIR)/Make.defs
2222

2323
# Hello, Zig! Example
2424

25-
MAINSRC = hello_zig_main.zig
25+
MAINSRC = hello_zig.zig
2626

2727
# Hello, Zig! built-in application info
2828

examples/hello_zig/hello_zig.zig

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//***************************************************************************
2+
// examples/hello_zig/hello_zig.zig
3+
//
4+
// Licensed to the Apache Software Foundation (ASF) under one or more
5+
// contributor license agreements. See the NOTICE file distributed with
6+
// this work for additional information regarding copyright ownership. The
7+
// ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance with the
9+
// License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
// License for the specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
//***************************************************************************
20+
21+
//***************************************************************************
22+
// Included Files
23+
//***************************************************************************
24+
const std = @import("std");
25+
26+
//****************************************************************************
27+
//* Externs
28+
//****************************************************************************
29+
30+
pub extern fn printf(_format: [*:0]const u8) c_int;
31+
32+
//****************************************************************************
33+
//* hello_zig_main
34+
//****************************************************************************
35+
pub export fn main(_argc: c_int, _argv: [*]const [*]const u8) u8 {
36+
_ = _argc;
37+
_ = _argv;
38+
_ = printf("Hello, Zig!\n");
39+
return 0;
40+
}

0 commit comments

Comments
 (0)