1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#![no_std]
#![allow(internal_features)]
#![feature(lang_items)]
#![feature(core_intrinsics)]

#[cfg(feature = "alloc")]
extern crate alloc;

// Ensure that origin is linked in.
extern crate origin;

// <https://doc.rust-lang.org/nomicon/panic-handler.html>
#[panic_handler]
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
    eprintln!("{}", panic);
    core::intrinsics::abort();
}

// <https://docs.rust-embedded.org/embedonomicon/smallest-no-std.html#eh_personality>
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

// <https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html>
#[global_allocator]
static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc;

// Origin calls this.
#[no_mangle]
extern "C" fn main(argc: i32, argv: *mut *mut u8, envp: *mut *mut u8) -> i32 {
    #[cfg(feature = "std")]
    unsafe {
        crate::init::sanitize_stdio_fds();
        crate::init::store_args(argc, argv, envp);
    }
    unsafe {
        crate::init::reset_sigpipe();
        crate::stack_overflow::init();
    }

    // Call the function expanded by the macro in the user's module to call the
    // user's `main` function.
    extern "C" {
        fn origin_studio_no_problem();
    }
    unsafe {
        origin_studio_no_problem();
    }

    rustix::process::EXIT_SUCCESS
}

#[cfg(not(feature = "alloc"))]
#[macro_export]
macro_rules! no_problem {
    () => {
        extern crate compiler_builtins;

        #[doc(hidden)]
        #[no_mangle]
        extern "C" fn origin_studio_no_problem() {
            // Call the user's `main` function.
            main()
        }
    };
}

#[cfg(all(feature = "alloc", not(feature = "std")))]
#[macro_export]
macro_rules! no_problem {
    () => {
        extern crate alloc;
        extern crate compiler_builtins;

        #[doc(hidden)]
        #[no_mangle]
        extern "C" fn origin_studio_no_problem() {
            // Call the user's `main` function.
            main()
        }
    };
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! no_problem {
    () => {
        extern crate alloc;
        extern crate compiler_builtins;

        #[doc(hidden)]
        #[no_mangle]
        extern "C" fn origin_studio_no_problem() {
            // Call the user's `main` function.
            main()
        }

        // Provide a prelude.
        use ::alloc::{format, vec};
        use $crate::std::{self, prelude::rust_2021::*};
        use $crate::{eprint, eprintln, print, println};
    };
}

mod init;
mod stack_overflow;

// Provide a std-like API.
#[cfg(feature = "std")]
pub mod std;