Skip to content

Support native simulator. #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zhang-wenchao opened this issue Nov 19, 2024 · 7 comments
Open

Support native simulator. #31

zhang-wenchao opened this issue Nov 19, 2024 · 7 comments

Comments

@zhang-wenchao
Copy link

https://docs.zephyrproject.org/latest/boards/native/native_sim/doc/index.html

@zhang-wenchao
Copy link
Author

Rust llvm target x86_64-unknown-none,I actually tested it and it works.

@zhang-wenchao
Copy link
Author

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 582e14a..26bf03d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -47,7 +47,8 @@ function(_rust_map_target)
message(FATAL_ERROR "Rust: Unsupported riscv ISA")
endif()
else()
-    message(FATAL_ERROR "Rust: Add support for other target")
+      set(RUST_TARGET "x86_64-unknown-none" PARENT_SCOPE)
+#    message(FATAL_ERROR "Rust: Add support for other target")
endif()
endfunction()

diff --git a/Kconfig b/Kconfig
index 9a3ed88..4c44422 100644
--- a/Kconfig
+++ b/Kconfig
@@ -8,7 +8,7 @@ menu "Rust Language Support"
config RUST_SUPPORTED
bool
default y if (CPU_CORTEX_M || \
-               (RISCV && !RISCV_ISA_RV32E && !RISCV_ISA_RV128I))
+               (RISCV && !RISCV_ISA_RV32E && !RISCV_ISA_RV128I) || ARCH_POSIX)
help
Selected for platforms that have support for Rust.

west build -p always -b native_sim/native/64

I'm trying to figure out how to support and Rust target.

@d3zd3z
Copy link
Collaborator

d3zd3z commented Feb 26, 2025

Any idea how well the various tests work?

@zhang-wenchao
Copy link
Author

native_sim/native/64 with x86_64-unknown-none No problem.

native_sim with i686-unknown-linux-gnu or i686-unknown-linux-musl there is a compilation error.

FAILED: zephyr/CMakeFiles/native_runner_executable zephyr/zephyr.exe /home/life/Develop/job/my/embedded/ohw-nano/build/zephyr/CMakeFiles/native_runner_executable /home/life/Develop/job/my/embedded/ohw-nano/build/zephyr/zephyr.exe 
cd /home/life/Develop/job/my/embedded/ohw-nano/build/zephyr && /usr/bin/make -f /home/life/Develop/platform/zephyr-project/zephyr/scripts/native_simulator/Makefile all --warn-undefined-variables -r NSI_CONFIG_FILE=/home/life/Develop/job/my/embedded/ohw-nano/build/zephyr/NSI/nsi_config
/usr/bin/ld: /home/life/Develop/job/my/embedded/ohw-nano/build/zephyr/NSI/home/life/Develop/job/my/embedded/ohw-nano/build/zephyr/zephyr.elf.loc_cpusw.o:(.data.DW.ref.rust_eh_personality[DW.ref.rust_eh_personality]+0x0): undefined reference to `rust_eh_personality'
collect2: error: ld returned 1 exit status

I hacked it and it works fine.

#[no_mangle]
extern "C" fn rust_eh_personality() {}

there are no other problems, but I don't know how to integrate it better. So there is no pr.

@zhang-wenchao
Copy link
Author

zhang-wenchao commented Feb 28, 2025

I also briefly tested the esp series chips and there were no problems.

The esp chip involves the esp toolchain, and the addition of the native simulator does not involve anything else.

In fact, supporting esp chips only requires a small modification after enabling the esp rust toolchain.

config RUST_SUPPORTED
        bool
        default y if ((CPU_CORTEX_M || \
                                (RISCV && !RISCV_ISA_RV32E && !RISCV_ISA_RV128I) || SOC_SERIES_ESP32S3) && \
                                !TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
        help
          Selected for platforms that have support for Rust.

elseif(CONFIG_SOC_ESP32S3)
  set(RUST_TARGET "xtensa-esp32s3-none-elf" PARENT_SCOPE)


cargo +esp build -Z build-std=core,alloc

@d3zd3z
Copy link
Collaborator

d3zd3z commented Mar 14, 2025

Also, ARCH_POSIX doesn't imply x86. But, we should at least be able to narrow it down a bit, perhaps x86-64, i686 (x86-32), and arm64? That would cover most Linux and Mac machines.

I'll be real curious if the embassy executor I've added will work. As long as k_thread_suspend and k_thread_resume work, as well as timers, this should be fine.

@kamathba
Copy link

kamathba commented Apr 9, 2025

x86_64-unknown-linux-gnu also works, so long as you conditionally don't overwrite the panic handler in the zephyr crate and change samples to use std. e.g.

-#![no_std]
+#![cfg_attr(not(CONFIG_BOARD_NATIVE_SIM), no_std)]

Part of me thinks targets with std support might be better for native_sim since I think you'd still be able to develop no_std crates / applications, but allow yourself to make simulation backends and stuff using std, but maybe it feels a little messy.

This issue was vaguely worded but I think to some level though it is done/landed, yes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants