From f7ff810bcddbc5c892ba2c7073f995e33633f9ae Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Fri, 31 Jan 2025 19:00:41 +0900 Subject: [PATCH] Add hello world example --- hello_world/CMakeLists.txt | 53 ++++++++++++++++++++++++++++++++++++++ hello_world/dub.sdl | 7 +++++ hello_world/ldc2.conf | 14 ++++++++++ hello_world/prj.conf | 4 +++ hello_world/sample.yaml | 9 +++++++ hello_world/src/app.d | 10 +++++++ hello_world/src/main.c | 6 +++++ 7 files changed, 103 insertions(+) create mode 100644 hello_world/CMakeLists.txt create mode 100644 hello_world/dub.sdl create mode 100644 hello_world/ldc2.conf create mode 100644 hello_world/prj.conf create mode 100644 hello_world/sample.yaml create mode 100644 hello_world/src/app.d create mode 100644 hello_world/src/main.c diff --git a/hello_world/CMakeLists.txt b/hello_world/CMakeLists.txt new file mode 100644 index 0000000..0a66863 --- /dev/null +++ b/hello_world/CMakeLists.txt @@ -0,0 +1,53 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(ldc_hello_world) + +target_sources(app PRIVATE src/main.c) + +if(${ARCH} STREQUAL "posix" OR ${ARCH} STREQUAL "x86") + set(ldc_target i686-unknown-none-newlibeabi) +elseif(CONFIG_CPU_CORTEX_M) + if(CONFIG_CPU_CORTEX_M3) + set(ldc_target thumbv7em-unknown-none-newlibeabi) + else() + message(FATAL_ERROR "Unknown Cortex-M target") + endif() +elseif(CONFIG_RISCV) + if(CONFIG_RISCV_ISA_RV32I) + set(ldc_target riscv32-unknown-newlib-elf) + else() + message(FATAL_ERROR "Unsupported riscv ISA") + endif() +else() + message(FATAL_ERROR "ARCH ${ARCH} not supported") +endif() + +include(ExternalProject) +set(ldc_src_dir ${CMAKE_CURRENT_SOURCE_DIR}) +ExternalProject_Add( + ldc_project + PREFIX ${CMAKE_CURRENT_BINARY_DIR} + SOURCE_DIR ${ldc_src_dir} + BUILD_IN_SOURCE 1 + BUILD_ALWAYS 1 + CONFIGURE_COMMAND "" + BUILD_COMMAND + dub + build + -f + -b release + --compiler=ldc2 + --arch=${ldc_target} + INSTALL_COMMAND "" + BUILD_BYPRODUCTS ${ldc_src_dir}/lib/libhelloworld.a +) + +add_library(ldc_lib STATIC IMPORTED GLOBAL) +add_dependencies( + ldc_lib + ldc_project +) +set_target_properties(ldc_lib PROPERTIES IMPORTED_LOCATION ${ldc_src_dir}/lib/libhelloworld.a) +target_link_libraries(app PUBLIC ldc_lib) diff --git a/hello_world/dub.sdl b/hello_world/dub.sdl new file mode 100644 index 0000000..e48b99b --- /dev/null +++ b/hello_world/dub.sdl @@ -0,0 +1,7 @@ +name "hello_world" +authors "Hiroki Noda" +license "BSL-1.0" +targetType "staticLibrary" +targetPath "lib" +targetName "helloworld" +dependency "zephyr-core" path="../zephyr-core" diff --git a/hello_world/ldc2.conf b/hello_world/ldc2.conf new file mode 100644 index 0000000..f751629 --- /dev/null +++ b/hello_world/ldc2.conf @@ -0,0 +1,14 @@ +default: +{ + switches = [ + "-Os", + "--betterC", + "--float-abi=soft", + "--relocation-model=static", + "--defaultlib=" + ]; + post-switches = [ + "-I%%ldcbinarypath%%/../import", + ]; + lib-dirs = []; +} diff --git a/hello_world/prj.conf b/hello_world/prj.conf new file mode 100644 index 0000000..bbb9138 --- /dev/null +++ b/hello_world/prj.conf @@ -0,0 +1,4 @@ +CONFIG_NEWLIB_LIBC=y +CONFIG_POSIX_API=y +CONFIG_MAIN_STACK_SIZE=2048 +CONFIG_LINKER_ORPHAN_SECTION_PLACE=y diff --git a/hello_world/sample.yaml b/hello_world/sample.yaml new file mode 100644 index 0000000..6a8bdae --- /dev/null +++ b/hello_world/sample.yaml @@ -0,0 +1,9 @@ +sample: + description: hello world + name: helloworld +common: + build_only: true + platform_allow: + - qemu_cortex_m3 +tests: + app.default: {} diff --git a/hello_world/src/app.d b/hello_world/src/app.d new file mode 100644 index 0000000..60211a6 --- /dev/null +++ b/hello_world/src/app.d @@ -0,0 +1,10 @@ +import zephyr.core.stdc.stdio; + +extern(C): +nothrow: +@nogc: + +void d_main() +{ + printf("Hello, World!\n"); +} diff --git a/hello_world/src/main.c b/hello_world/src/main.c new file mode 100644 index 0000000..d6b24c6 --- /dev/null +++ b/hello_world/src/main.c @@ -0,0 +1,6 @@ +extern void d_main(void); + +int main(void) { + d_main(); + return 0; +}