mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
refactor(treewide): get rid of compile time build environment introspection
It's cursed and not very useful. Still a few uses of ctor left, but oh well.
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
use std::{fs::read_to_string, path::PathBuf};
|
||||
|
||||
use proc_macro::{Span, TokenStream};
|
||||
use quote::quote;
|
||||
use syn::{Error, ItemConst, Meta};
|
||||
|
||||
use crate::{Result, utils};
|
||||
|
||||
pub(super) fn manifest(item: ItemConst, args: &[Meta]) -> Result<TokenStream> {
|
||||
let member = utils::get_named_string(args, "crate");
|
||||
let path = manifest_path(member.as_deref())?;
|
||||
let manifest = read_to_string(&path).unwrap_or_default();
|
||||
let val = manifest.as_str();
|
||||
let name = item.ident;
|
||||
let ret = quote! {
|
||||
const #name: &'static str = #val;
|
||||
};
|
||||
|
||||
Ok(ret.into())
|
||||
}
|
||||
|
||||
#[allow(clippy::option_env_unwrap)]
|
||||
fn manifest_path(member: Option<&str>) -> Result<PathBuf> {
|
||||
let Some(path) = option_env!("CARGO_MANIFEST_DIR") else {
|
||||
return Err(Error::new(
|
||||
Span::call_site().into(),
|
||||
"missing CARGO_MANIFEST_DIR in environment",
|
||||
));
|
||||
};
|
||||
|
||||
let mut path: PathBuf = path.into();
|
||||
|
||||
// conduwuit/src/macros/ -> conduwuit/src/
|
||||
path.pop();
|
||||
|
||||
if let Some(member) = member {
|
||||
// conduwuit/$member/Cargo.toml
|
||||
path.push(member);
|
||||
} else {
|
||||
// conduwuit/src/ -> conduwuit/
|
||||
path.pop();
|
||||
}
|
||||
|
||||
path.push("Cargo.toml");
|
||||
|
||||
Ok(path)
|
||||
}
|
||||
+1
-11
@@ -1,15 +1,13 @@
|
||||
mod admin;
|
||||
mod cargo;
|
||||
mod config;
|
||||
mod debug;
|
||||
mod implement;
|
||||
mod refutable;
|
||||
mod rustc;
|
||||
mod utils;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{
|
||||
Error, Item, ItemConst, ItemEnum, ItemFn, ItemStruct, Meta,
|
||||
Error, Item, ItemEnum, ItemFn, ItemStruct, Meta,
|
||||
parse::{Parse, Parser},
|
||||
parse_macro_input,
|
||||
};
|
||||
@@ -26,19 +24,11 @@ pub fn admin_command_dispatch(args: TokenStream, input: TokenStream) -> TokenStr
|
||||
attribute_macro::<ItemEnum, _>(args, input, admin::command_dispatch)
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn cargo_manifest(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
attribute_macro::<ItemConst, _>(args, input, cargo::manifest)
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn recursion_depth(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
attribute_macro::<Item, _>(args, input, debug::recursion_depth)
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn rustc_flags_capture(args: TokenStream) -> TokenStream { rustc::flags_capture(args) }
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn refutable(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
attribute_macro::<ItemFn, _>(args, input, refutable::refutable)
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
|
||||
pub(super) fn flags_capture(args: TokenStream) -> TokenStream {
|
||||
let cargo_crate_name = std::env::var("CARGO_CRATE_NAME");
|
||||
let crate_name = match cargo_crate_name.as_ref() {
|
||||
| Err(_) => return args,
|
||||
| Ok(crate_name) => crate_name.trim_start_matches("conduwuit_"),
|
||||
};
|
||||
|
||||
let flag = std::env::args().collect::<Vec<_>>();
|
||||
let flag_len = flag.len();
|
||||
let ret = quote! {
|
||||
pub static RUSTC_FLAGS: [&str; #flag_len] = [#( #flag ),*];
|
||||
|
||||
#[ctor]
|
||||
fn _set_rustc_flags() {
|
||||
conduwuit_core::info::rustc::FLAGS.lock().expect("locked").insert(#crate_name, &RUSTC_FLAGS);
|
||||
}
|
||||
|
||||
// static strings have to be yanked on module unload
|
||||
#[dtor]
|
||||
fn _unset_rustc_flags() {
|
||||
conduwuit_core::info::rustc::FLAGS.lock().expect("locked").remove(#crate_name);
|
||||
}
|
||||
};
|
||||
|
||||
ret.into()
|
||||
}
|
||||
Reference in New Issue
Block a user