55import re
66import sys
77
8- HEADER_GUARD_PREFIX = "FIREBASE_ANALYTICS_ "
8+ HEADER_GUARD_PREFIX = "FIREBASE_ANALYTICS_SRC_WINDOWS_ "
99INCLUDE_PATH = "src/windows"
1010INCLUDE_PREFIX = "analytics/" + INCLUDE_PATH
1111
1212def generate_function_pointers (header_file_path , output_h_path , output_c_path ):
1313 """
14- Parses a C header file to generate a header with extern function pointer
15- declarations and a source file with stub functions, initialized pointers ,
16- and a dynamic loading function for Windows.
14+ Parses a C header file to generate a self-contained header with typedefs,
15+ extern function pointer declarations, and a source file with stub functions,
16+ initialized pointers, and a dynamic loading function for Windows.
1717
1818 Args:
1919 header_file_path (str): The path to the input C header file.
@@ -28,11 +28,19 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
2828 print (f"Error: Header file not found at '{ header_file_path } '" )
2929 return
3030
31+ # --- Extract necessary definitions from the original header ---
32+
33+ # Find all standard includes (e.g., <stdint.h>)
34+ includes = re .findall (r"#include\s+<.*?>" , header_content )
35+
36+ # Find all typedefs, including their documentation comments
37+ typedefs = re .findall (r"/\*\*(?:[\s\S]*?)\*/\s*typedef[\s\S]*?;\s*" , header_content )
38+
39+ # --- Extract function prototypes ---
3140 function_pattern = re .compile (
3241 r"ANALYTICS_API\s+([\w\s\*]+?)\s+(\w+)\s*\((.*?)\);" ,
3342 re .DOTALL
3443 )
35-
3644 matches = function_pattern .finditer (header_content )
3745
3846 extern_declarations = []
@@ -45,18 +53,15 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
4553 function_name = match .group (2 ).strip ()
4654 params_str = match .group (3 ).strip ()
4755
48- # Clean up newlines and extra spaces for declarations
4956 cleaned_params_for_decl = re .sub (r'\s+' , ' ' , params_str ) if params_str else ""
50-
51- # --- Prepare for Stub and Pointer Initialization ---
5257 stub_name = f"Stub_{ function_name } "
5358
54- # Generate the return statement for the stub
59+ # Generate return statement for the stub
5560 if "void" in return_type :
5661 return_statement = " // No return value."
5762 elif "*" in return_type :
5863 return_statement = " return NULL;"
59- else :
64+ else : # bool, int64_t, etc.
6065 return_statement = " return 0;"
6166
6267 stub_function = (
@@ -67,26 +72,29 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
6772 )
6873 stub_functions .append (stub_function )
6974
70- # Create the extern declaration for the header file
7175 declaration = f"extern { return_type } (*ptr_{ function_name } )({ cleaned_params_for_decl } );"
7276 extern_declarations .append (declaration )
7377
74- # Create the initialized pointer definition for the source file
7578 pointer_init = f"{ return_type } (*ptr_{ function_name } )({ cleaned_params_for_decl } ) = &{ stub_name } ;"
7679 pointer_initializations .append (pointer_init )
7780
78- # Collect details for the dynamic loader function
7981 function_details_for_loader .append ((function_name , return_type , cleaned_params_for_decl ))
8082
8183 print (f"Found { len (pointer_initializations )} functions. Generating output files..." )
8284
83- # --- Write the Header File (.h) ---
84- header_guard = HEADER_GUARD_PREFIX + f" { os .path .basename (output_h_path ).upper ().replace ('.' , '_' )} _"
85+ # --- Write the self-contained Header File (.h) ---
86+ header_guard = f" { HEADER_GUARD_PREFIX } { os .path .basename (output_h_path ).upper ().replace ('.' , '_' )} _"
8587 with open (output_h_path , 'w' , encoding = 'utf-8' ) as f :
86- f .write (f"// Generated from { os .path .basename (header_file_path )} \n \n " )
88+ f .write (f"// Generated from { os .path .basename (header_file_path )} \n " )
89+ f .write (f"// This is a self-contained header file.\n \n " )
8790 f .write (f"#ifndef { header_guard } \n " )
8891 f .write (f"#define { header_guard } \n \n " )
89- f .write (f'#include "{ INCLUDE_PREFIX } { os .path .basename (header_file_path )} "\n \n ' )
92+
93+ f .write ("// --- Copied from original header ---\n " )
94+ f .write ("\n " .join (includes ) + "\n \n " )
95+ f .write ("" .join (typedefs ))
96+ f .write ("// --- End of copied section ---\n \n " )
97+
9098 f .write ("#ifdef __cplusplus\n " )
9199 f .write ('extern "C" {\n ' )
92100 f .write ("#endif\n \n " )
@@ -108,7 +116,7 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
108116 with open (output_c_path , 'w' , encoding = 'utf-8' ) as f :
109117 f .write (f"// Generated from { os .path .basename (header_file_path )} \n \n " )
110118 f .write (f'#include "{ INCLUDE_PREFIX } { os .path .basename (output_h_path )} "\n ' )
111- f .write ('#include <stddef.h>\n \n ' )
119+ f .write ('#include <stddef.h> // For NULL \n \n ' )
112120 f .write ("// --- Stub Function Definitions ---\n " )
113121 f .write ("\n \n " .join (stub_functions ))
114122 f .write ("\n \n \n // --- Function Pointer Initializations ---\n " )
0 commit comments