5
5
import re
6
6
import sys
7
7
8
- HEADER_GUARD_PREFIX = "FIREBASE_ANALYTICS_ "
8
+ HEADER_GUARD_PREFIX = "FIREBASE_ANALYTICS_SRC_WINDOWS_ "
9
9
INCLUDE_PATH = "src/windows"
10
10
INCLUDE_PREFIX = "analytics/" + INCLUDE_PATH
11
11
12
12
def generate_function_pointers (header_file_path , output_h_path , output_c_path ):
13
13
"""
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.
17
17
18
18
Args:
19
19
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):
28
28
print (f"Error: Header file not found at '{ header_file_path } '" )
29
29
return
30
30
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 ---
31
40
function_pattern = re .compile (
32
41
r"ANALYTICS_API\s+([\w\s\*]+?)\s+(\w+)\s*\((.*?)\);" ,
33
42
re .DOTALL
34
43
)
35
-
36
44
matches = function_pattern .finditer (header_content )
37
45
38
46
extern_declarations = []
@@ -45,18 +53,15 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
45
53
function_name = match .group (2 ).strip ()
46
54
params_str = match .group (3 ).strip ()
47
55
48
- # Clean up newlines and extra spaces for declarations
49
56
cleaned_params_for_decl = re .sub (r'\s+' , ' ' , params_str ) if params_str else ""
50
-
51
- # --- Prepare for Stub and Pointer Initialization ---
52
57
stub_name = f"Stub_{ function_name } "
53
58
54
- # Generate the return statement for the stub
59
+ # Generate return statement for the stub
55
60
if "void" in return_type :
56
61
return_statement = " // No return value."
57
62
elif "*" in return_type :
58
63
return_statement = " return NULL;"
59
- else :
64
+ else : # bool, int64_t, etc.
60
65
return_statement = " return 0;"
61
66
62
67
stub_function = (
@@ -67,26 +72,29 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
67
72
)
68
73
stub_functions .append (stub_function )
69
74
70
- # Create the extern declaration for the header file
71
75
declaration = f"extern { return_type } (*ptr_{ function_name } )({ cleaned_params_for_decl } );"
72
76
extern_declarations .append (declaration )
73
77
74
- # Create the initialized pointer definition for the source file
75
78
pointer_init = f"{ return_type } (*ptr_{ function_name } )({ cleaned_params_for_decl } ) = &{ stub_name } ;"
76
79
pointer_initializations .append (pointer_init )
77
80
78
- # Collect details for the dynamic loader function
79
81
function_details_for_loader .append ((function_name , return_type , cleaned_params_for_decl ))
80
82
81
83
print (f"Found { len (pointer_initializations )} functions. Generating output files..." )
82
84
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 ('.' , '_' )} _"
85
87
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 " )
87
90
f .write (f"#ifndef { header_guard } \n " )
88
91
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
+
90
98
f .write ("#ifdef __cplusplus\n " )
91
99
f .write ('extern "C" {\n ' )
92
100
f .write ("#endif\n \n " )
@@ -108,7 +116,7 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
108
116
with open (output_c_path , 'w' , encoding = 'utf-8' ) as f :
109
117
f .write (f"// Generated from { os .path .basename (header_file_path )} \n \n " )
110
118
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 ' )
112
120
f .write ("// --- Stub Function Definitions ---\n " )
113
121
f .write ("\n \n " .join (stub_functions ))
114
122
f .write ("\n \n \n // --- Function Pointer Initializations ---\n " )
0 commit comments