Skip to content
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

Update HelloWorldRustDxe to permit compiling for unit tests #341

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MsCorePkg/HelloWorldRustDxe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
[[bin]]
name = "HelloWorldRustDxe"
path = "src/main.rs"
test = false

[dependencies]
r-efi = {workspace=true}
Expand Down
66 changes: 43 additions & 23 deletions MsCorePkg/HelloWorldRustDxe/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,59 @@
//!
//! SPDX-License-Identifier: BSD-2-Clause-Patent
//!

#![no_std]
#![no_main]
#![allow(non_snake_case)]
// UEFI requires "no_main" and a panic handler. To facilitate unit tests, gate "no_main", UEFI entry point, and panic
// handler behind target_os configuration.
#![cfg_attr(target_os = "uefi", no_main)]
#[cfg(target_os = "uefi")]
mod uefi_entry {
extern crate alloc;

use alloc::vec;
use core::panic::PanicInfo;
use r_efi::efi::Status;
use rust_advanced_logger_dxe::{debugln, init_debug, DEBUG_INFO};

extern crate alloc;
#[no_mangle]
pub extern "efiapi" fn efi_main(
_image_handle: *const core::ffi::c_void,
_system_table: *const r_efi::system::SystemTable,
) -> u64 {
rust_boot_services_allocator_dxe::GLOBAL_ALLOCATOR.init(unsafe { (*_system_table).boot_services });
init_debug(unsafe { (*_system_table).boot_services });

use alloc::vec;
use core::panic::PanicInfo;
use r_efi::efi::Status;
use rust_advanced_logger_dxe::{debugln, init_debug, DEBUG_INFO};
debugln!(DEBUG_INFO, "Hello, World. This is Rust in UEFI.");

#[no_mangle]
pub extern "efiapi" fn efi_main(
_image_handle: *const core::ffi::c_void,
_system_table: *const r_efi::system::SystemTable,
) -> u64 {
rust_boot_services_allocator_dxe::GLOBAL_ALLOCATOR.init(unsafe { (*_system_table).boot_services });
init_debug(unsafe { (*_system_table).boot_services });
debugln!(DEBUG_INFO, "file: {:} line: {:} as hex: {:x}", file!(), line!(), line!());

debugln!(DEBUG_INFO, "Hello, World. This is Rust in UEFI.");
let mut foo = vec!["asdf", "xyzpdq", "abcdefg", "thxyzb"];

debugln!(DEBUG_INFO, "file: {:} line: {:} as hex: {:x}", file!(), line!(), line!());
debugln!(DEBUG_INFO, "Unsorted vec: {:?}", foo);
foo.sort();
debugln!(DEBUG_INFO, "Sorted vec: {:?}", foo);

let mut foo = vec!["asdf", "xyzpdq", "abcdefg", "thxyzb"];
Status::SUCCESS.as_usize() as u64
}

debugln!(DEBUG_INFO, "Unsorted vec: {:?}", foo);
foo.sort();
debugln!(DEBUG_INFO, "Sorted vec: {:?}", foo);
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
}

Status::SUCCESS.as_usize() as u64
// For non-UEFI targets (e.g. compiling for unit test or clippy), supply a "main" function.
#[cfg(not(target_os = "uefi"))]
fn main() {
//do nothing.
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
#[cfg(test)]
mod test {

#[test]
fn sample_test() {
assert_eq!(1 + 1, 2);
}
}