feat: add no-std common runtime

Add an alloc-backed common library with direct platform I/O.

Build a host attribute macro for async entry points and keep runtime
boilerplate outside application code. Compile the library, macro, and
example directly with rustc through CMake.

Co-Authored-By: luna (openrouter/openai/gpt-5.6-luna): runtime work
This commit is contained in:
2026-09-05 04:48:21 +02:00
parent 54051d4068
commit 812278a22f
8 changed files with 412 additions and 161 deletions
+55 -22
View File
@@ -1,16 +1,17 @@
cmake_minimum_required(VERSION 3.20)
project(no_core_write LANGUAGES NONE)
project(rslibc LANGUAGES NONE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_program(RUSTC rustc REQUIRED)
set(RUST_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/main.rs")
set(RUST_BINARY "${CMAKE_CURRENT_BINARY_DIR}/no_core_write${CMAKE_EXECUTABLE_SUFFIX}")
set(RUST_MACRO_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/macros/src/lib.rs")
set(RUST_MACRO "${CMAKE_CURRENT_BINARY_DIR}/rslibc_macros${CMAKE_SHARED_LIBRARY_SUFFIX}")
set(RUST_LIBRARY_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/lib.rs")
set(RUST_RUNTIME_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/runtime/main.rs")
set(RUST_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/librslibc.rlib")
set(RUST_BINARY "${CMAKE_CURRENT_BINARY_DIR}/rslibc_example${CMAKE_EXECUTABLE_SUFFIX}")
# no_core is an unstable rustc feature. RUSTC_BOOTSTRAP lets this deliberately
# minimal example be built with the installed compiler without Cargo or core.
set(RUSTC_ENV "RUSTC_BOOTSTRAP=1")
set(RUSTC_FLAGS -C panic=abort)
set(RUSTC_FLAGS --edition=2021 -C panic=abort)
# CMAKE_BUILD_TYPE does not automatically affect a custom rustc command.
# Pass the corresponding optimization/debug-info settings explicitly.
@@ -22,31 +23,63 @@ else()
list(APPEND RUSTC_FLAGS -C opt-level=0 -C debuginfo=2)
endif()
# A no_core binary does not get Rust's normal startup/linker setup. Link the
# small platform system/runtime library needed by the stdout implementation.
# A no_std binary does not get Rust's normal startup or allocator setup.
# Use the OS heap and dynamically link only the small platform libraries
# needed by this runtime.
if (WIN32)
# Use Win32 directly instead of the C runtime; this keeps the executable
# small while still writing to the console's stdout handle.
set(RUSTC_LINK_ARGS
-l kernel32
-l vcruntime
-C
"link-args=/NODEFAULTLIB /ENTRY:mainCRTStartup /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF"
"link-args=/ENTRY:mainCRTStartup /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF"
)
elseif (UNIX)
set(RUSTC_LINK_ARGS -C "link-args=-lc")
endif()
add_custom_command(
OUTPUT "${RUST_BINARY}"
COMMAND "${CMAKE_COMMAND}" -E env "${RUSTC_ENV}" "${RUSTC}"
"${RUST_SOURCE}"
--crate-name no_core_write
--crate-type bin
-o "${RUST_BINARY}"
${RUSTC_FLAGS}
${RUSTC_LINK_ARGS}
DEPENDS "${RUST_SOURCE}"
OUTPUT "${RUST_MACRO}"
COMMAND "${RUSTC}"
"${RUST_MACRO_SOURCE}"
--crate-name rslibc_macros
--crate-type proc-macro
-o "${RUST_MACRO}"
--edition=2021
-C opt-level=3
DEPENDS "${RUST_MACRO_SOURCE}"
VERBATIM
)
add_custom_target(no_core_write ALL DEPENDS "${RUST_BINARY}")
add_custom_command(
OUTPUT "${RUST_LIBRARY}"
COMMAND "${RUSTC}"
"${RUST_LIBRARY_SOURCE}"
--crate-name rslibc
--crate-type rlib
-o "${RUST_LIBRARY}"
${RUSTC_FLAGS}
DEPENDS
"${RUST_LIBRARY_SOURCE}"
"${CMAKE_CURRENT_SOURCE_DIR}/src/io.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/src/runtime.rs"
VERBATIM
)
add_custom_command(
OUTPUT "${RUST_BINARY}"
COMMAND "${RUSTC}"
"${RUST_RUNTIME_SOURCE}"
--crate-name rslibc_example
--crate-type bin
--extern "rslibc=${RUST_LIBRARY}"
--extern "rslibc_macros=${RUST_MACRO}"
-o "${RUST_BINARY}"
${RUSTC_FLAGS}
${RUSTC_LINK_ARGS}
DEPENDS "${RUST_RUNTIME_SOURCE}" "${RUST_LIBRARY}" "${RUST_MACRO}"
VERBATIM
)
add_custom_target(rslibc_macros DEPENDS "${RUST_MACRO}")
add_custom_target(rslibc_library DEPENDS "${RUST_LIBRARY}")
add_custom_target(rslibc_example ALL DEPENDS "${RUST_BINARY}")