Skip to content

Commit 89dd0c2

Browse files
kassanexiaoxiang781216
authored andcommitted
Zig & D examples clean & refactor
dlang: use importC to get NuttX include zig: leds_zig added
1 parent c91a181 commit 89dd0c2

File tree

10 files changed

+453
-94
lines changed

10 files changed

+453
-94
lines changed

examples/hello_d/Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ MAINSRC = hello_d_main.d
2626

2727
# hello_d built-in application info
2828

29-
DFLAGS += -oq -betterC
29+
# no garbage collection
30+
DFLAGS += -betterC
31+
# better informations
32+
DFLAGS += -vtls -verrors=context
33+
# like #ifdef/#define NuttX_ImportC
34+
# importC: need $(CC) to compile C file and -P-I to import C includes
35+
DFLAGS += -P-I$(TOPDIR)/include --d-version=NuttX_ImportC --gcc=$(CC)
3036
PROGNAME = $(CONFIG_EXAMPLES_HELLO_D_PROGNAME)
3137
PRIORITY = $(CONFIG_EXAMPLES_HELLO_D_PRIORITY)
3238
STACKSIZE = $(CONFIG_EXAMPLES_HELLO_D_STACKSIZE)

examples/hello_d/hello_d_main.d

+84-46
Original file line numberDiff line numberDiff line change
@@ -26,79 +26,116 @@ module examples.hello_d_main;
2626
// import core.stdc.stdio : printf;
2727
// import core.stdc.stdlib : malloc, free;
2828

29-
///
30-
pragma(printf)
31-
extern (C) int printf(scope const(char)* fmt, scope...) @nogc nothrow @trusted;
32-
///
33-
extern (C) void* malloc(size_t size) @nogc nothrow @trusted;
34-
///
35-
extern (C) void free(void* ptr) @nogc nothrow @trusted;
29+
version (D_BetterC) // no DRT
30+
{
31+
/// if betterC and LDC, disable moduleinfo and typeinfo
32+
version (LDC)
33+
{
34+
pragma(LDC_no_moduleinfo);
35+
pragma(LDC_no_typeinfo);
36+
}
37+
}
38+
39+
/// use --d-version=NuttX_ImportC to define
40+
version (NuttX_ImportC)
41+
{
42+
/// D compiler will not import C++ symbols
43+
/// so we need to import them manually
44+
/// @nogc, @trusted, @safe, nothrow - not allowed
45+
/// https://issues.dlang.org/show_bug.cgi?id=23812
46+
import nuttx_std : malloc, free, printf; // by default: @system (unsafe)
47+
}
48+
else
49+
{
50+
/// Correctly FFI-import C/C++ symbols (@safe compatibility)
51+
///
52+
pragma(printf)
53+
extern (C) int printf(scope const(char)* fmt, scope...) @nogc nothrow @trusted;
54+
///
55+
extern (C) void* malloc(size_t size) @nogc nothrow @trusted;
56+
///
57+
extern (C) void free(scope void* ptr) @nogc nothrow @trusted;
58+
}
3659

3760
//***************************************************************************
38-
// Private module content
61+
// Private module content (default is public)
3962
//***************************************************************************
4063
private:
4164

4265
// based heloxx class layout
4366
extern (C++,class)
4467
struct DHelloWorld
4568
{
46-
@nogc nothrow:
47-
4869
@disable this();
4970
@disable this(this);
50-
this(int secret) @safe pure
51-
{
52-
mSecret = secret;
53-
debug printf("Constructor\n");
54-
}
5571

56-
~this() @safe pure
72+
/// use --d-version=NuttX_ImportC to define
73+
version (NuttX_ImportC)
5774
{
58-
debug printf("Destructor\n");
59-
}
60-
61-
bool HelloWorld() @safe
62-
{
63-
debug printf("HelloWorld: mSecret=%d\n", mSecret);
75+
this(int secret)
76+
{
77+
mSecret = secret;
78+
printf("Constructor\n");
79+
}
6480

65-
if (mSecret != 42)
81+
~this()
6682
{
67-
printf("DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n");
68-
return false;
83+
printf("Destructor\n");
6984
}
70-
else
85+
86+
bool HelloWorld()
7187
{
72-
printf("DHelloWorld.HelloWorld: Hello, World!!\n");
73-
return true;
88+
printf("HelloWorld: mSecret=%d\n", mSecret);
89+
90+
if (mSecret != 42)
91+
{
92+
printf("DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n");
93+
return false;
94+
}
95+
else
96+
{
97+
printf("DHelloWorld.HelloWorld: Hello, World!!\n");
98+
return true;
99+
}
74100
}
75101
}
102+
else
103+
{
104+
this(int secret) @safe nothrow @nogc
105+
{
106+
mSecret = secret;
107+
printf("Constructor\n");
108+
}
76109

77-
private int mSecret;
78-
}
79-
80-
//***************************************************************************
81-
// Private Data
82-
//***************************************************************************
110+
~this() @safe nothrow @nogc
111+
{
112+
printf("Destructor\n");
113+
}
83114

84-
// Define a statically constructed DHellowWorld instance if D static
85-
// initializers are supported by the platform
86-
// --d-version=D_Initialize
87-
version (D_Initialize)
88-
{
89-
static DHelloWorld g_HelloWorld;
115+
bool HelloWorld() @safe nothrow @nogc
116+
{
117+
printf("HelloWorld: mSecret=%d\n", mSecret);
118+
119+
if (mSecret != 42)
120+
{
121+
printf("DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n");
122+
return false;
123+
}
124+
else
125+
{
126+
printf("DHelloWorld.HelloWorld: Hello, World!!\n");
127+
return true;
128+
}
129+
}
130+
}
131+
private int mSecret = 0;
90132
}
91133

92-
//***************************************************************************
93-
// Public Functions
94-
//***************************************************************************
95-
96134
/****************************************************************************
97135
* Name: hello_d_main
98136
****************************************************************************/
99137
// betterC need main function no-mangle.
100-
extern (C)
101-
int hello_d_main(int argc, char*[] argv) nothrow @nogc
138+
extern (C) int hello_d_main(int argc, char*[] argv)
102139
{
103140
version (LDC)
104141
{
@@ -118,5 +155,6 @@ int hello_d_main(int argc, char*[] argv) nothrow @nogc
118155

119156
printf("hello_d_main: Saying hello from the instance constructed on the stack\n");
120157
HelloWorld.HelloWorld();
158+
121159
return 0;
122160
}

examples/hello_d/nuttx_std.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/****************************************************************************
2+
* apps/examples/hello_d/nuttx_std.c
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+
25+
/****************************************************************************
26+
* This is imported to d source using importC
27+
* https://dlang.org/spec/importc.html
28+
****************************************************************************/
29+
#include <nuttx/config.h>
30+
#include <stdio.h>
31+
#include <stdlib.h>
32+
33+
/****************************************************************************
34+
* Public Functions
35+
****************************************************************************/
36+
37+
/****************************************************************************
38+
* hello_d_main
39+
****************************************************************************/

examples/hello_zig/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ include $(APPDIR)/Make.defs
2525
MAINSRC = hello_zig.zig
2626

2727
# Hello, Zig! built-in application info
28-
28+
ZIGFLAGS += -lc -I$(TOPDIR)/include
2929
PROGNAME = $(CONFIG_EXAMPLES_HELLO_ZIG_PROGNAME)
3030
PRIORITY = $(CONFIG_EXAMPLES_HELLO_ZIG_PRIORITY)
3131
STACKSIZE = $(CONFIG_EXAMPLES_HELLO_ZIG_STACKSIZE)

examples/hello_zig/hello_zig.zig

+24-6
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,35 @@
2424
const std = @import("std");
2525

2626
//****************************************************************************
27-
//* Externs
27+
//* C API - need libc linking (freestanding libc is stubs only)
2828
//****************************************************************************
29+
// NuttX namespace
30+
const NuttX = struct {
31+
pub const c = @cImport({
32+
@cInclude("nuttx/config.h");
33+
@cInclude("stdio.h");
34+
});
35+
pub fn print(comptime fmt: [*:0]const u8, args: anytype) void {
36+
_ = printf(std.fmt.comptimePrint(std.mem.span(fmt), args));
37+
}
38+
};
2939

30-
pub extern fn printf(_format: [*:0]const u8) c_int;
40+
// or (optional) const c = std.c; // from std library (non-full libc)
3141

42+
// typedef alias
43+
const printf = NuttX.c.printf;
3244
//****************************************************************************
3345
//* hello_zig_main
3446
//****************************************************************************
35-
pub export fn main(_argc: c_int, _argv: [*]const [*]const u8) u8 {
36-
_ = _argc;
37-
_ = _argv;
38-
_ = printf("Hello, Zig!\n");
47+
comptime {
48+
// rename to hello_zig_main entry point for nuttx
49+
@export(hello_zig, .{
50+
.name = "hello_zig_main",
51+
.linkage = .weak,
52+
});
53+
}
54+
55+
fn hello_zig(_: c_int, _: ?[*]const [*]const u8) callconv(.C) c_int {
56+
NuttX.print("[{s}]: Hello, Zig!\n", .{NuttX.c.CONFIG_ARCH_BOARD});
3957
return 0;
4058
}

examples/hello_zig/hello_zig_main.zig

-40
This file was deleted.

examples/leds_zig/Kconfig

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_LEDS_ZIG
7+
tristate "\"LEDs Zig\" example"
8+
default n
9+
depends on USERLED
10+
---help---
11+
Enable the \"LEDs Zig\" example
12+
13+
if EXAMPLES_LEDS_ZIG
14+
15+
config EXAMPLES_LEDS_ZIG_PROGNAME
16+
string "Program name"
17+
default "leds_zig"
18+
---help---
19+
This is the name of the program that will be used when the
20+
program is installed.
21+
22+
config EXAMPLES_LEDS_ZIG_PRIORITY
23+
int "LEDs Zig task priority"
24+
default 100
25+
26+
config EXAMPLES_LEDS_ZIG_STACKSIZE
27+
int "LEDs Zig stack size"
28+
default DEFAULT_TASK_STACKSIZE
29+
30+
endif

examples/leds_zig/Make.defs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/examples/leds_zig/Make.defs
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+
ifneq ($(CONFIG_EXAMPLES_LEDS_ZIG),)
22+
CONFIGURED_APPS += $(APPDIR)/examples/leds_zig
23+
endif

0 commit comments

Comments
 (0)