Skip to content

Commit c785e32

Browse files
centurysysjerpelea
authored andcommitted
add "hello_nim" example application written by Nim.
Signed-off-by: Takeyoshi Kikuchi <[email protected]>
1 parent a594bbd commit c785e32

File tree

7 files changed

+289
-0
lines changed

7 files changed

+289
-0
lines changed

config.nims

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
############################################################################
2+
# apps/config.nims
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+
import std/os
22+
import std/strutils
23+
24+
switch "os", "nuttx"
25+
switch "mm", "orc"
26+
27+
switch "arm.nuttx.gcc.exe", "arm-none-eabi-gcc"
28+
switch "arm64.nuttx.gcc.exe", "aarch64-none-elf-gcc"
29+
switch "riscv32.nuttx.gcc.exe", "riscv64-unknown-elf-gcc"
30+
switch "amd64.nuttx.gcc.exe", "x86_64-linux-gnu-gcc"
31+
32+
switch "nimcache", ".nimcache"
33+
switch "d", "useStdLib"
34+
switch "d", "useMalloc"
35+
switch "d", "nimAllocPagesViaMalloc"
36+
switch "d", "noSignalHandler"
37+
switch "threads", "off"
38+
switch "noMain", "on"
39+
switch "compileOnly", "on"
40+
switch "noLinking", "on"
41+
# TODO: need OpenSSL-mbedTLS wrapper library.
42+
#switch "d", "ssl"
43+
#swich "dynlibOverride", "ssl"
44+
45+
type
46+
OptFlag = enum
47+
oNone
48+
oSize
49+
DotConfig = object
50+
arch: string
51+
opt: OptFlag
52+
debugSymbols: bool
53+
ramSize: int
54+
isSim: bool
55+
56+
proc killoBytes(bytes: int): int =
57+
result = (bytes / 1024).int
58+
59+
proc read_config(cfg: string): DotConfig =
60+
for line in cfg.readFile.splitLines:
61+
if not line.startsWith("CONFIG_"):
62+
continue
63+
let keyval = line.replace("CONFIG_", "").split("=")
64+
if keyval.len != 2:
65+
continue
66+
case keyval[0]
67+
of "ARCH":
68+
let arch = keyval[1].strip(chars = {'"'})
69+
case arch
70+
of "arm", "arm64":
71+
result.arch = arch
72+
of "riscv":
73+
result.arch = "riscv32"
74+
of "sim":
75+
if defined(amd64):
76+
result.arch = "amd64"
77+
elif defined(aarch64):
78+
result.arch = "arm64"
79+
result.isSim = true
80+
of "DEBUG_NOOPT":
81+
result.opt = oNone
82+
of "DEBUG_FULLOPT":
83+
result.opt = oSize
84+
of "DEBUG_SYMBOLS":
85+
result.debugSymbols = true
86+
of "RAM_SIZE":
87+
result.ramSize = keyval[1].parseInt
88+
echo "* arch: " & result.arch
89+
echo "* opt: " & $result.opt
90+
echo "* debug: " & $result.debugSymbols
91+
echo "* ramSize: " & $result.ramSize
92+
93+
func bool2onoff(b: bool): string =
94+
result = if b: "on" else: "off"
95+
96+
proc setup_cfg(cfg: DotConfig) =
97+
switch("cpu", cfg.arch)
98+
if cfg.opt == oSize:
99+
switch("define", "release")
100+
switch("define", "danger")
101+
switch("opt", "size")
102+
if cfg.debugSymbols:
103+
# use native debugger (gdb)
104+
switch("debugger", "native")
105+
let debug_onoff = cfg.debugSymbols.bool2onoff
106+
switch("lineDir", debug_onoff)
107+
switch("stackTrace", debug_onoff)
108+
switch("lineTrace", debug_onoff)
109+
if not cfg.isSim:
110+
# Adjust the page size for Nim's GC allocator.
111+
let ramKilloBytes = cfg.ramSize.killoBytes
112+
if ramKilloBytes < 32:
113+
switch("define", "nimPage256")
114+
elif ramKilloBytes < 512:
115+
switch("define", "nimPage512")
116+
elif ramKilloBytes < 2048:
117+
switch("define", "nimPage1k")
118+
if ramKilloBytes < 512:
119+
# Sets MemAlign to 4 bytes which reduces the memory alignment
120+
# to better match some embedded devices.
121+
switch("define", "nimMemAlignTiny")
122+
123+
124+
let topdir = getEnv("TOPDIR")
125+
let cfg = read_config(topdir & "/.config")
126+
cfg.setup_cfg()

examples/hello_nim/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.nimcache/
2+
.nimc

examples/hello_nim/Kconfig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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_HELLO_NIM
7+
tristate "\"Hello, World!\" example (Nim)"
8+
default n
9+
---help---
10+
Enable the \"Hello, World!\" example
11+
12+
if EXAMPLES_HELLO_NIM
13+
14+
config EXAMPLES_HELLO_NIM_PROGNAME
15+
string "Program name"
16+
default "hello_nim"
17+
---help---
18+
This is the name of the program that will be used when the NSH ELF
19+
program is installed.
20+
21+
config EXAMPLES_HELLO_NIM_PRIORITY
22+
int "Hello task priority"
23+
default 100
24+
25+
config EXAMPLES_HELLO_NIM_STACKSIZE
26+
int "Hello stack size"
27+
default DEFAULT_TASK_STACKSIZE
28+
29+
endif

examples/hello_nim/Make.defs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/examples/hello_nim/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_HELLO_NIM),)
22+
CONFIGURED_APPS += $(APPDIR)/examples/hello_nim
23+
endif

examples/hello_nim/Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
############################################################################
2+
# apps/examples/hello_nim/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+
include $(APPDIR)/Make.defs
22+
23+
# Hello, World! (Nim) built-in application info
24+
25+
PROGNAME = $(CONFIG_EXAMPLES_HELLO_NIM_PROGNAME)
26+
PRIORITY = $(CONFIG_EXAMPLES_HELLO_NIM_PRIORITY)
27+
STACKSIZE = $(CONFIG_EXAMPLES_HELLO_NIM_STACKSIZE)
28+
MODULE = $(CONFIG_EXAMPLES_HELLO_NIM)
29+
30+
# Hello, World! Example
31+
32+
NIMPATH = $(shell choosenim show path)
33+
CFLAGS += -I $(NIMPATH)/lib -I ./.nimcache
34+
CSRCS += $(wildcard .nimcache/*.c)
35+
36+
MAINSRC = hello_nim_main.c
37+
38+
.nimc: hello_nim_async.nim
39+
nim c --header $<
40+
touch .nimc
41+
42+
$(MAINSRC): .nimc
43+
44+
clean::
45+
$(call DELDIR, .nimcache)
46+
$(call DELFILE, .nimc)
47+
48+
all:: .nimc $(MAINSRC)
49+
50+
include $(APPDIR)/Application.mk
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import std/asyncdispatch
2+
import std/strformat
3+
4+
proc task(id: int): Future[void] {.async.} =
5+
for loop in 0..2:
6+
echo &"Hello from task {id}! loops: {loop}"
7+
if loop < 2:
8+
await sleepAsync(1000)
9+
10+
proc launch() {.async.} =
11+
for id in 1..2:
12+
asyncCheck task(id)
13+
await sleepAsync(200)
14+
await task(3)
15+
16+
proc hello_nim() {.exportc, cdecl.} =
17+
waitFor launch()
18+
GC_runOrc()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/****************************************************************************
2+
* apps/examples/hello_nim/hello_nim_main.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+
#include <nuttx/config.h>
26+
#include "hello_nim_async.h"
27+
28+
/****************************************************************************
29+
* Public Functions
30+
****************************************************************************/
31+
32+
/****************************************************************************
33+
* hello_main
34+
****************************************************************************/
35+
36+
int main(int argc, FAR char *argv[])
37+
{
38+
NimMain();
39+
hello_nim();
40+
return 0;
41+
}

0 commit comments

Comments
 (0)