1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under both the MIT license found in the
 * LICENSE-MIT file in the root directory of this source tree and the Apache
 * License, Version 2.0 found in the LICENSE-APACHE file in the root directory
 * of this source tree.
 */

#![deny(warnings, clippy::all, broken_intra_doc_links)]
// See https://github.com/rust-lang/rust/pull/60562
// #![deny(missing_docs)]

//! This crate offers a workaround for [issue](https://github.com/rust-lang/rfcs/issues/752).
//! The gist of it is that `include!` proc macro will include the content of
//! lib.rs file stored inside OUT_DIR, presumably generated by cargo's build
//! script.
//!
//! # Example
//!
//! ```
//! ::codegen_includer_proc_macro::include!();
//! fn main() {
//!     helloWorld(); // This was included from $OUT_DIR/lib.rs
//! }
//! ```

extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use std::env;
use std::path::Path;

/// See crate's documentation
#[proc_macro]
pub fn include(_: TokenStream) -> TokenStream {
    let path_to_include = Path::new(&env::var("OUT_DIR").unwrap())
        .join("lib.rs")
        .to_str()
        .unwrap()
        .to_string();

    let result = quote! {
        #[path = #path_to_include]
        #[allow(unused_attributes)]
        mod codegen_included;
        pub use codegen_included::*;
    };
    result.into()
}