Skip to content

Commit 336b54c

Browse files
committed
Update script and generated files.
1 parent 615622a commit 336b54c

File tree

4 files changed

+76
-354
lines changed

4 files changed

+76
-354
lines changed

analytics/generate_windows_stubs.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import re
66
import sys
77

8-
HEADER_GUARD_PREFIX = "FIREBASE_ANALYTICS_"
8+
HEADER_GUARD_PREFIX = "FIREBASE_ANALYTICS_SRC_WINDOWS_"
99
INCLUDE_PATH = "src/windows"
1010
INCLUDE_PREFIX = "analytics/" + INCLUDE_PATH
1111

1212
def 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")

analytics/src/windows/analytics_dynamic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Generated from analytics_windows.h
22

33
#include "analytics/src/windowsanalytics_dynamic.h"
4-
#include <stddef.h>
4+
#include <stddef.h> // For NULL
55

66
// --- Stub Function Definitions ---
77
// Stub for GoogleAnalytics_Item_Create

analytics/src/windows/analytics_dynamic.h

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
11
// Generated from analytics_windows.h
2+
// This is a self-contained header file.
23

3-
#ifndef FIREBASE_ANALYTICS_ANALYTICS_DYNAMIC_H_
4-
#define FIREBASE_ANALYTICS_ANALYTICS_DYNAMIC_H_
4+
#ifndef FIREBASE_ANALYTICS_SRC_WINDOWS_ANALYTICS_DYNAMIC_H_
5+
#define FIREBASE_ANALYTICS_SRC_WINDOWS_ANALYTICS_DYNAMIC_H_
56

6-
#include "analytics/src/windowsanalytics_windows.h"
7+
// --- Copied from original header ---
8+
#include <stdint.h>
9+
10+
/**
11+
* @brief Opaque type for an item.
12+
*
13+
* This type is an opaque object that represents an item in an item vector.
14+
*
15+
* The caller is responsible for creating the item using the
16+
* GoogleAnalytics_Item_Create() function, and destroying it using the
17+
* GoogleAnalytics_Item_Destroy() function, unless it has been added to an
18+
* item vector, in which case it will be destroyed at that time.
19+
*/
20+
typedef struct GoogleAnalytics_Item_Opaque GoogleAnalytics_Item;
21+
22+
/**
23+
* @brief Opaque type for an item vector.
24+
*
25+
* This type is an opaque object that represents a list of items. It is
26+
* used to pass item vectors to the
27+
* GoogleAnalytics_EventParameters_InsertItemVector() function.
28+
*
29+
* The caller is responsible for creating the item vector using the
30+
* GoogleAnalytics_ItemVector_Create() function, and destroying it using the
31+
* GoogleAnalytics_ItemVector_Destroy() function, unless it has been added
32+
* to an event parameter map, in which case it will be destroyed at that time.
33+
*/
34+
typedef struct GoogleAnalytics_ItemVector_Opaque GoogleAnalytics_ItemVector;
35+
36+
/**
37+
* @brief Opaque type for an event parameter map.
38+
*
39+
* This type is an opaque object that represents a dictionary of event
40+
* parameters. It is used to pass event parameters to the
41+
* GoogleAnalytics_LogEvent() function.
42+
*
43+
* The caller is responsible for creating the event parameter map using the
44+
* GoogleAnalytics_EventParameters_Create() function, and destroying it using
45+
* the GoogleAnalytics_EventParameters_Destroy() function, unless it has been
46+
* logged, in which case it will be destroyed automatically.
47+
*/
48+
typedef struct GoogleAnalytics_EventParameters_Opaque
49+
GoogleAnalytics_EventParameters;
50+
51+
// --- End of copied section ---
752

853
#ifdef __cplusplus
954
extern "C" {
@@ -40,4 +85,4 @@ void LoadAnalyticsFunctions(HMODULE dll_handle);
4085
}
4186
#endif
4287

43-
#endif // FIREBASE_ANALYTICS_ANALYTICS_DYNAMIC_H_
88+
#endif // FIREBASE_ANALYTICS_SRC_WINDOWS_ANALYTICS_DYNAMIC_H_

0 commit comments

Comments
 (0)