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

Fix procedural macros #260

Merged
merged 3 commits into from
Jul 31, 2021
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
11 changes: 8 additions & 3 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ The following steps allow you to build a simple UEFI app.
#![feature(abi_efiapi)]
use uefi::prelude::*;

extern crate rlibc;

#[entry]
fn efi_main(handle: Handle, mut system_table: SystemTable<Boot>) -> Status;
fn main(handle: Handle, mut system_table: SystemTable<Boot>) -> Status;
```
You will also want to add a dependency to the [`rlibc`](https://docs.rs/rlibc/) crate,
to avoid linking errors.
Note that Rust EFI target requires the entry function to be exported with an `efi_main` symbol,
the `#[entry]` macro takes care of that, so the function name is irrelevant.

You will also want to add a dependency to the [`rlibc`](https://docs.rs/rlibc/) crate and
explicitly link it with `extern crate rlibc;` line to avoid linking errors.

- Build using a `nightly` version of the compiler and activate the
[`build-std`](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std)
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#[cfg(feature = "exts")]
extern crate alloc as alloc_api;

// allow referring to self as ::uefi for macros to work universally (from this crate and from others)
// see https://github.com/rust-lang/rust/issues/54647
extern crate self as uefi;

#[macro_use]
pub mod data_types;
pub use self::data_types::{unsafe_guid, Identify};
Expand Down
19 changes: 11 additions & 8 deletions uefi-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
extern crate proc_macro;

use proc_macro::TokenStream;

use proc_macro2::Span;
use quote::{quote, TokenStreamExt};
use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, DeriveInput, Generics, Ident, ItemFn, ItemType, LitStr};
Expand Down Expand Up @@ -87,10 +89,10 @@ pub fn unsafe_guid(args: TokenStream, input: TokenStream) -> TokenStream {
let ident = type_definition.ident.clone();
let (impl_generics, ty_generics, where_clause) = type_definition.generics.split_for_impl();
result.append_all(quote! {
unsafe impl #impl_generics crate::Identify for #ident #ty_generics #where_clause {
unsafe impl #impl_generics ::uefi::Identify for #ident #ty_generics #where_clause {
#[doc(hidden)]
#[allow(clippy::unreadable_literal)]
const GUID : crate::Guid = crate::Guid::from_values(
const GUID: ::uefi::Guid = ::uefi::Guid::from_values(
#time_low,
#time_mid,
#time_high_and_version,
Expand All @@ -113,7 +115,7 @@ pub fn derive_protocol(item: TokenStream) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();
let result = quote! {
// Mark this as a `Protocol` implementation
impl #impl_generics crate::proto::Protocol for #ident #ty_generics #where_clause {}
impl #impl_generics ::uefi::proto::Protocol for #ident #ty_generics #where_clause {}

// Most UEFI functions expect to be called on the bootstrap processor.
impl #impl_generics !Send for #ident #ty_generics #where_clause {}
Expand All @@ -134,14 +136,15 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
panic!("This attribute accepts no arguments");
}

let f = parse_macro_input!(input as ItemFn);
let mut f = parse_macro_input!(input as ItemFn);

let entry_fn_ident = &f.sig.ident;
// force the exported symbol to be 'efi_main'
f.sig.ident = Ident::new("efi_main", Span::call_site());

let result = quote!(
static _UEFI_ENTRY_POINT_TYPE_CHECK: extern "efiapi" fn(uefi::Handle, uefi::table::SystemTable<uefi::table::Boot>) -> uefi::Status = #entry_fn_ident;
let result = quote! {
static _UEFI_ENTRY_POINT_TYPE_CHECK: extern "efiapi" fn(uefi::Handle, uefi::table::SystemTable<uefi::table::Boot>) -> uefi::Status = efi_main;
#[no_mangle]
pub extern "efiapi" #f
);
};
result.into()
}