Skip to content

Commit

Permalink
Zig & D examples clean & refactor
Browse files Browse the repository at this point in the history
dlang: use importC to get NuttX include
zig: leds_zig added
  • Loading branch information
kassane committed Aug 25, 2024
1 parent 4e3f77d commit 5e6eee2
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 94 deletions.
8 changes: 7 additions & 1 deletion examples/hello_d/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ MAINSRC = hello_d_main.d

# hello_d built-in application info

DFLAGS += -oq -betterC
# no garbage collection
DFLAGS += -betterC
# better informations
DFLAGS += -vtls -verrors=context
# like #ifdef/#define NuttX_ImportC
# importC: need $(CC) to compile C file and -P-I to import C includes
DFLAGS += -P-I$(TOPDIR)/include --d-version=NuttX_ImportC --gcc=$(CC)
PROGNAME = $(CONFIG_EXAMPLES_HELLO_D_PROGNAME)
PRIORITY = $(CONFIG_EXAMPLES_HELLO_D_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_HELLO_D_STACKSIZE)
Expand Down
130 changes: 84 additions & 46 deletions examples/hello_d/hello_d_main.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,79 +26,116 @@ module examples.hello_d_main;
// import core.stdc.stdio : printf;
// import core.stdc.stdlib : malloc, free;

///
pragma(printf)
extern (C) int printf(scope const(char)* fmt, scope...) @nogc nothrow @trusted;
///
extern (C) void* malloc(size_t size) @nogc nothrow @trusted;
///
extern (C) void free(void* ptr) @nogc nothrow @trusted;
version (D_BetterC) // no DRT
{
/// if betterC and LDC, disable moduleinfo and typeinfo
version (LDC)
{
pragma(LDC_no_moduleinfo);
pragma(LDC_no_typeinfo);
}
}

/// use --d-version=NuttX_ImportC to define
version (NuttX_ImportC)
{
/// D compiler will not import C++ symbols
/// so we need to import them manually
/// @nogc, @trusted, @safe, nothrow - not allowed
/// https://issues.dlang.org/show_bug.cgi?id=23812
import nuttx_std : malloc, free, printf; // by default: @system (unsafe)
}
else
{
/// Correctly FFI-import C/C++ symbols (@safe compatibility)
///
pragma(printf)
extern (C) int printf(scope const(char)* fmt, scope...) @nogc nothrow @trusted;
///
extern (C) void* malloc(size_t size) @nogc nothrow @trusted;
///
extern (C) void free(scope void* ptr) @nogc nothrow @trusted;
}

//***************************************************************************
// Private module content
// Private module content (default is public)
//***************************************************************************
private:

// based heloxx class layout
extern (C++,class)
struct DHelloWorld
{
@nogc nothrow:

@disable this();
@disable this(this);
this(int secret) @safe pure
{
mSecret = secret;
debug printf("Constructor\n");
}

~this() @safe pure
/// use --d-version=NuttX_ImportC to define
version (NuttX_ImportC)
{
debug printf("Destructor\n");
}

bool HelloWorld() @safe
{
debug printf("HelloWorld: mSecret=%d\n", mSecret);
this(int secret)
{
mSecret = secret;
printf("Constructor\n");
}

if (mSecret != 42)
~this()
{
printf("DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n");
return false;
printf("Destructor\n");
}
else

bool HelloWorld()
{
printf("DHelloWorld.HelloWorld: Hello, World!!\n");
return true;
printf("HelloWorld: mSecret=%d\n", mSecret);

if (mSecret != 42)
{
printf("DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n");
return false;
}
else
{
printf("DHelloWorld.HelloWorld: Hello, World!!\n");
return true;
}
}
}
else
{
this(int secret) @safe nothrow @nogc
{
mSecret = secret;
printf("Constructor\n");
}

private int mSecret;
}

//***************************************************************************
// Private Data
//***************************************************************************
~this() @safe nothrow @nogc
{
printf("Destructor\n");
}

// Define a statically constructed DHellowWorld instance if D static
// initializers are supported by the platform
// --d-version=D_Initialize
version (D_Initialize)
{
static DHelloWorld g_HelloWorld;
bool HelloWorld() @safe nothrow @nogc
{
printf("HelloWorld: mSecret=%d\n", mSecret);

if (mSecret != 42)
{
printf("DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n");
return false;
}
else
{
printf("DHelloWorld.HelloWorld: Hello, World!!\n");
return true;
}
}
}
private int mSecret = 0;
}

//***************************************************************************
// Public Functions
//***************************************************************************

/****************************************************************************
* Name: hello_d_main
****************************************************************************/
// betterC need main function no-mangle.
extern (C)
int hello_d_main(int argc, char*[] argv) nothrow @nogc
extern (C) int hello_d_main(int argc, char*[] argv)
{
version (LDC)
{
Expand All @@ -118,5 +155,6 @@ int hello_d_main(int argc, char*[] argv) nothrow @nogc

printf("hello_d_main: Saying hello from the instance constructed on the stack\n");
HelloWorld.HelloWorld();

return 0;
}
39 changes: 39 additions & 0 deletions examples/hello_d/nuttx_std.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/****************************************************************************
* apps/examples/hello_d/nuttx_std.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
****************************************************************************/

/****************************************************************************
* This is imported to d source using importC
* https://dlang.org/spec/importc.html
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* hello_d_main
****************************************************************************/
2 changes: 1 addition & 1 deletion examples/hello_zig/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ include $(APPDIR)/Make.defs
MAINSRC = hello_zig.zig

# Hello, Zig! built-in application info

ZIGFLAGS += -lc -I$(TOPDIR)/include
PROGNAME = $(CONFIG_EXAMPLES_HELLO_ZIG_PROGNAME)
PRIORITY = $(CONFIG_EXAMPLES_HELLO_ZIG_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_HELLO_ZIG_STACKSIZE)
Expand Down
30 changes: 24 additions & 6 deletions examples/hello_zig/hello_zig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,35 @@
const std = @import("std");

//****************************************************************************
//* Externs
//* C API - need libc linking (freestanding libc is stubs only)
//****************************************************************************
// NuttX namespace
const NuttX = struct {
pub const c = @cImport({
@cInclude("nuttx/config.h");
@cInclude("stdio.h");
});
pub fn print(comptime fmt: [*:0]const u8, args: anytype) void {
_ = printf(std.fmt.comptimePrint(std.mem.span(fmt), args));
}
};

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

// typedef alias
const printf = NuttX.c.printf;
//****************************************************************************
//* hello_zig_main
//****************************************************************************
pub export fn main(_argc: c_int, _argv: [*]const [*]const u8) u8 {
_ = _argc;
_ = _argv;
_ = printf("Hello, Zig!\n");
comptime {
// rename to hello_zig_main entry point for nuttx
@export(hello_zig, .{
.name = "hello_zig_main",
.linkage = .weak,
});
}

fn hello_zig(_: c_int, _: ?[*]const [*]const u8) callconv(.C) c_int {
NuttX.print("[{s}]: Hello, Zig!\n", .{NuttX.c.CONFIG_ARCH_BOARD});
return 0;
}
40 changes: 0 additions & 40 deletions examples/hello_zig/hello_zig_main.zig

This file was deleted.

30 changes: 30 additions & 0 deletions examples/leds_zig/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config EXAMPLES_LEDS_ZIG
tristate "\"LEDs Zig\" example"
default n
depends on USERLED
---help---
Enable the \"LEDs Zig\" example

if EXAMPLES_LEDS_ZIG

config EXAMPLES_LEDS_ZIG_PROGNAME
string "Program name"
default "leds_zig"
---help---
This is the name of the program that will be used when the
program is installed.

config EXAMPLES_LEDS_ZIG_PRIORITY
int "LEDs Zig task priority"
default 100

config EXAMPLES_LEDS_ZIG_STACKSIZE
int "LEDs Zig stack size"
default DEFAULT_TASK_STACKSIZE

endif
23 changes: 23 additions & 0 deletions examples/leds_zig/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
############################################################################
# apps/examples/leds_zig/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_EXAMPLES_LEDS_ZIG),)
CONFIGURED_APPS += $(APPDIR)/examples/leds_zig
endif
Loading

0 comments on commit 5e6eee2

Please sign in to comment.