Skip to content

Commit

Permalink
Deduplicate target groups
Browse files Browse the repository at this point in the history
This makes `systems::elf` and `systems::mach_o` the sources of truth for
available targets that use these executable formats.
  • Loading branch information
nvzqz committed Nov 21, 2023
1 parent 39dc3d7 commit a173613
Showing 1 changed file with 37 additions and 43 deletions.
80 changes: 37 additions & 43 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,65 +26,59 @@ impl Macro<'_> {
}
}

/// Lists of comma-separated `#[cfg]` parameters.
mod systems {
use super::*;

pub fn elf() -> proc_macro2::TokenStream {
quote! {
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "haiku",
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"
}
}

pub fn mach_o() -> proc_macro2::TokenStream {
quote! {
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "watchos"
}
}
}

/// Attributes applied to a `static` containing a pointer to a function to run
/// before `main`.
fn pre_main_attrs() -> proc_macro2::TokenStream {
let elf = systems::elf();
let mach_o = systems::mach_o();

quote! {
#[used]
#[cfg_attr(windows, link_section = ".CRT$XCU")]
#[cfg_attr(
// ELF
any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "haiku",
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
),
link_section = ".init_array",
)]
#[cfg_attr(
// Mach-O
any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
),
link_section = "__DATA,__mod_init_func,mod_init_funcs",
)]
#[cfg_attr(any(#elf), link_section = ".init_array")]
#[cfg_attr(any(#mach_o), link_section = "__DATA,__mod_init_func,mod_init_funcs")]
}
}

fn unsupported_error(
std_crate: &proc_macro2::TokenStream,
attr_name: &str,
) -> proc_macro2::TokenStream {
let elf = systems::elf();
let mach_o = systems::mach_o();

let error = format!("Unsupported target OS for `#[divan::{attr_name}]`");

quote! {
#[cfg(not(any(
windows,
// ELF
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "haiku",
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
// Mach-O
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
)))]
#[cfg(not(any(windows, #elf, #mach_o)))]
#std_crate::compile_error!(#error);
}
}
Expand Down

0 comments on commit a173613

Please sign in to comment.