Skip to main content

directcpp_macro/
lib.rs

1mod mangle;
2mod parse;
3mod util;
4mod tests;
5mod buildcode;
6
7use crate::buildcode::FFIBuilder;
8use std::collections::HashSet;
9use proc_macro::{TokenStream as TS0, TokenTree};
10use std::env;
11use regex::Regex;
12use syn;
13
14
15extern "C" {
16	fn enable_msvc_debug_c();
17}
18
19#[proc_macro_attribute]
20pub fn bridge(args: TS0, input: TS0) -> TS0 {
21	let mut flags = HashSet::new();
22	for tt in args.into_iter() {
23		if let TokenTree::Ident(val) = tt {
24			flags.insert(val.to_string());
25		}
26	}
27	let mut bb = FFIBuilder::new(! flags.contains("goon") );
28	match bb.build_bridge_code(input.into()) {
29		Ok(code) => code.into(),
30		Err(e) => syn::Error::new(proc_macro2::Span::call_site(), e).to_compile_error().into()
31	}
32}
33
34#[proc_macro_attribute]
35pub fn enable_msvc_debug(args: TS0, _input: TS0) -> TS0
36{
37	let enable_ = if let Some(TokenTree::Ident(val)) = args.into_iter().next() {
38		val.to_string().parse::<i32>().unwrap_or(-1)
39	} else {
40		-1
41	};
42	let is_debug = match enable_ {
43		0 => false,
44		1 => true,
45		_ => {
46			match env::var("OUT_DIR") {
47				Ok(profile) => Regex::new(r"[\\/]target[\\/]debug[\\/]").unwrap().is_match(&profile),
48				Err(_) => false,
49			}
50		}
51	};
52	if is_debug {
53		unsafe{ enable_msvc_debug_c(); }
54	}
55	TS0::new()
56}