@@ -10,14 +10,14 @@ extern crate syn;
1010extern crate proc_macro;
1111use proc_macro2:: Span ;
1212
13- use cpp_common:: { flags, kw, RustInvocation , FILE_HASH , LIB_NAME , MSVC_LIB_NAME , OUT_DIR , VERSION } ;
13+ use cpp_common:: { file_hash , flags, kw, out_dir , RustInvocation , LIB_NAME , MSVC_LIB_NAME , VERSION } ;
1414use std:: collections:: HashMap ;
1515use std:: iter:: FromIterator ;
16+ use std:: sync:: OnceLock ;
1617use syn:: parse:: Parser ;
1718use syn:: Ident ;
1819
1920use byteorder:: { BigEndian , ByteOrder , LittleEndian , ReadBytesExt } ;
20- use lazy_static:: lazy_static;
2121use quote:: { quote, quote_spanned} ;
2222use std:: fs:: File ;
2323use std:: io:: { self , BufReader , Read , Seek , SeekFrom } ;
@@ -33,8 +33,10 @@ impl MetaData {
3333 }
3434}
3535
36- lazy_static ! {
37- static ref METADATA : HashMap <u64 , Vec <MetaData >> = {
36+ static METADATA_LOCK : OnceLock < HashMap < u64 , Vec < MetaData > > > = OnceLock :: new ( ) ;
37+
38+ fn metadata ( ) -> & ' static HashMap < u64 , Vec < MetaData > > {
39+ METADATA_LOCK . get_or_init ( || {
3840 let file = match open_lib_file ( ) {
3941 Ok ( x) => x,
4042 Err ( e) => {
@@ -62,7 +64,7 @@ NOTE: Did you make sure to add the rust-cpp build script?
6264
6365I/O error while reading metadata from target library file."# ,
6466 )
65- } ;
67+ } )
6668}
6769
6870/// NOTE: This panics when it can produce a better error message
@@ -126,10 +128,10 @@ fn read_metadata_rest<E: ByteOrder>(
126128/// metadata. We check both `MSVC_LIB_NAME` and `LIB_NAME`, in case we are on
127129/// or are targeting Windows.
128130fn open_lib_file ( ) -> io:: Result < File > {
129- if let Ok ( file) = File :: open ( OUT_DIR . join ( MSVC_LIB_NAME ) ) {
131+ if let Ok ( file) = File :: open ( out_dir ( ) . join ( MSVC_LIB_NAME ) ) {
130132 Ok ( file)
131133 } else {
132- File :: open ( OUT_DIR . join ( LIB_NAME ) )
134+ File :: open ( out_dir ( ) . join ( LIB_NAME ) )
133135 }
134136}
135137
@@ -195,7 +197,7 @@ pub fn expand_internal(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
195197 } ;
196198
197199 // Get the size data compiled by the build macro
198- let size_data = match METADATA . get ( & closure. sig . name_hash ( ) ) {
200+ let size_data = match metadata ( ) . get ( & closure. sig . name_hash ( ) ) {
199201 Some ( x) => x,
200202 None => {
201203 #[ cfg( not( feature = "docs-only" ) ) ]
@@ -314,7 +316,7 @@ NOTE: They cannot be generated by macro expansion."#})
314316 let rust_invocations = find_all_rust_macro. parse2 ( input) . expect ( "rust! macro" ) ;
315317 let init_callbacks = if !rust_invocations. is_empty ( ) {
316318 let rust_cpp_callbacks =
317- Ident :: new ( & format ! ( "rust_cpp_callbacks{}" , * FILE_HASH ) , Span :: call_site ( ) ) ;
319+ Ident :: new ( & format ! ( "rust_cpp_callbacks{}" , file_hash ( ) ) , Span :: call_site ( ) ) ;
318320 let offset = ( flags >> 32 ) as isize ;
319321 let callbacks: Vec < Ident > = rust_invocations. iter ( ) . map ( |x| x. id . clone ( ) ) . collect ( ) ;
320322 quote ! {
@@ -370,7 +372,7 @@ pub fn expand_wrap_class(input: proc_macro::TokenStream) -> proc_macro::TokenStr
370372 let class_name = class. name . clone ( ) ;
371373
372374 // Get the size data compiled by the build macro
373- let size_data = match METADATA . get ( & hash) {
375+ let size_data = match metadata ( ) . get ( & hash) {
374376 Some ( x) => x,
375377 None => {
376378 #[ cfg( not( feature = "docs-only" ) ) ]
0 commit comments