Skip to content

Commit 17406df

Browse files
authored
Add Windows no_std project example (#63)
1 parent e493017 commit 17406df

File tree

10 files changed

+150
-2
lines changed

10 files changed

+150
-2
lines changed

.github/workflows/build.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,21 @@ jobs:
3030
with:
3131
toolchain: ${{ matrix.toolchain }}
3232
- name: Build
33-
working-directory: no_std
33+
working-directory: no_std/nix
34+
run: cargo build --release --all-features
35+
no_std_win:
36+
name: no_std-win
37+
strategy:
38+
matrix:
39+
toolchain: [ stable, nightly ]
40+
runs-on: windows-latest
41+
steps:
42+
- uses: actions/checkout@v4
43+
- uses: dtolnay/rust-toolchain@master
44+
with:
45+
toolchain: ${{ matrix.toolchain }}
46+
- name: Build
47+
working-directory: no_std/win
3448
run: cargo build --release --all-features
3549
no_main_win:
3650
name: no_main-win

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ On macOS, the final stripped binary is reduced to 8KB.
265265

266266
![Minimum Rust: 1.30](https://img.shields.io/badge/Minimum%20Rust%20Version-1.30-brightgreen.svg)
267267

268-
> Example project is located in the [`no_std`](no_std) folder.
268+
> Example projects are located in the [`no_std`](no_std) folder.
269269
270270
Up until this point, our application was using the Rust standard library, `libstd`. `libstd`
271271
provides many convenient, well tested cross-platform APIs and data types. But if a user wants
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

no_std/win/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

no_std/win/Cargo.lock

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

no_std/win/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "no_std_win"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
windows-sys = { version = "0.52.0", features = [
8+
"Win32_Foundation",
9+
"Win32_System_Threading", # for `ExitProcess`
10+
"Win32_System_Console", # for `WriteConsoleA` etc.
11+
] }
12+
13+
[profile.dev]
14+
panic = "abort"
15+
16+
[profile.release]
17+
opt-level = "z" # Optimize for size.
18+
lto = true # Enable Link Time Optimization
19+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
20+
panic = "abort" # Abort on panic
21+
strip = true # Automatically strip symbols from the binary.

no_std/win/src/main.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![no_main]
2+
#![no_std]
3+
#![windows_subsystem = "console"]
4+
5+
use core::ffi::c_void;
6+
use core::panic::PanicInfo;
7+
8+
use windows_sys::Win32::System::Console::GetStdHandle;
9+
use windows_sys::Win32::System::Console::WriteConsoleA;
10+
use windows_sys::Win32::System::Console::STD_OUTPUT_HANDLE;
11+
use windows_sys::Win32::System::Threading::ExitProcess;
12+
13+
#[panic_handler]
14+
fn panic(_: &PanicInfo<'_>) -> ! {
15+
unsafe {
16+
ExitProcess(1);
17+
}
18+
}
19+
20+
#[allow(non_snake_case)]
21+
#[no_mangle]
22+
fn mainCRTStartup() -> ! {
23+
let message = "Hello, world!\n";
24+
unsafe {
25+
let console = GetStdHandle(STD_OUTPUT_HANDLE);
26+
WriteConsoleA(
27+
console,
28+
message.as_ptr().cast::<c_void>(),
29+
message.len() as u32,
30+
core::ptr::null_mut(),
31+
core::ptr::null(),
32+
);
33+
34+
ExitProcess(0)
35+
}
36+
}

0 commit comments

Comments
 (0)