Skip to content

Commit e171c8a

Browse files
committed
Update stub scripts and output files.
1 parent e473214 commit e171c8a

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

analytics/generate_windows_stubs.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
7474
matches = function_pattern.finditer(header_content)
7575

7676
extern_declarations = []
77+
macro_definitions = []
7778
stub_functions = []
7879
pointer_initializations = []
7980
function_details_for_loader = []
@@ -90,9 +91,9 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
9091
if "void" in return_type:
9192
return_statement = " // No return value."
9293
elif "*" in return_type:
93-
return_statement = " return NULL;"
94+
return_statement = f' return ({return_type})(&g_stub_memory);'
9495
else: # bool, int64_t, etc.
95-
return_statement = " return 0;"
96+
return_statement = " return 1;"
9697

9798
stub_function = (
9899
f"// Stub for {function_name}\n"
@@ -105,6 +106,9 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
105106
declaration = f"extern {return_type} (*ptr_{function_name})({cleaned_params_for_decl});"
106107
extern_declarations.append(declaration)
107108

109+
macro = f'#define {function_name} ptr_{function_name}'
110+
macro_definitions.append(macro)
111+
108112
pointer_init = f"{return_type} (*ptr_{function_name})({cleaned_params_for_decl}) = &{stub_name};"
109113
pointer_initializations.append(pointer_init)
110114

@@ -119,7 +123,8 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
119123
f.write(f"// Generated from {os.path.basename(header_file_path)} by {os.path.basename(sys.argv[0])}\n\n")
120124
f.write(f"#ifndef {header_guard}\n")
121125
f.write(f"#define {header_guard}\n\n")
122-
126+
f.write("#include <stdbool.h> // needed for bool type in pure C\n\n")
127+
123128
f.write("// --- Copied from original header ---\n")
124129
f.write("\n".join(includes) + "\n\n")
125130
f.write("".join(typedefs))
@@ -131,6 +136,8 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
131136
f.write("// --- Function Pointer Declarations ---\n")
132137
f.write("// clang-format off\n")
133138
f.write("\n".join(extern_declarations))
139+
f.write("\n\n")
140+
f.write("\n".join(macro_definitions))
134141
f.write("\n// clang-format on\n")
135142
f.write("\n\n// --- Dynamic Loader Declaration for Windows ---\n")
136143
f.write("#if defined(_WIN32)\n")
@@ -155,6 +162,7 @@ def generate_function_pointers(header_file_path, output_h_path, output_c_path):
155162
f.write(f'#include "{INCLUDE_PREFIX}{os.path.basename(output_h_path)}"\n')
156163
f.write('#include <stddef.h>\n\n')
157164
f.write("// clang-format off\n\n")
165+
f.write("static void* g_stub_memory = NULL;\n\n")
158166
f.write("// --- Stub Function Definitions ---\n")
159167
f.write("\n\n".join(stub_functions))
160168
f.write("\n\n\n// --- Function Pointer Initializations ---\n")

analytics/src/windows/analytics_dynamic.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
// Generated from analytics.h by generate_windows_stubs.py
1616

1717
#include "analytics/src/windows/analytics_dynamic.h"
18-
1918
#include <stddef.h>
2019

2120
// clang-format off
2221

22+
static void* g_stub_memory = NULL;
23+
2324
// --- Stub Function Definitions ---
2425
// Stub for GoogleAnalytics_Item_Create
2526
static GoogleAnalytics_Item* Stub_GoogleAnalytics_Item_Create() {
26-
return NULL;
27+
return (GoogleAnalytics_Item*)(&g_stub_memory);
2728
}
2829

2930
// Stub for GoogleAnalytics_Item_InsertInt
@@ -54,7 +55,7 @@ static void Stub_GoogleAnalytics_Item_Destroy(GoogleAnalytics_Item* item) {
5455

5556
// Stub for GoogleAnalytics_ItemVector_Create
5657
static GoogleAnalytics_ItemVector* Stub_GoogleAnalytics_ItemVector_Create() {
57-
return NULL;
58+
return (GoogleAnalytics_ItemVector*)(&g_stub_memory);
5859
}
5960

6061
// Stub for GoogleAnalytics_ItemVector_InsertItem
@@ -69,7 +70,7 @@ static void Stub_GoogleAnalytics_ItemVector_Destroy(GoogleAnalytics_ItemVector*
6970

7071
// Stub for GoogleAnalytics_EventParameters_Create
7172
static GoogleAnalytics_EventParameters* Stub_GoogleAnalytics_EventParameters_Create() {
72-
return NULL;
73+
return (GoogleAnalytics_EventParameters*)(&g_stub_memory);
7374
}
7475

7576
// Stub for GoogleAnalytics_EventParameters_InsertInt

analytics/src/windows/analytics_dynamic.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef FIREBASE_ANALYTICS_SRC_WINDOWS_ANALYTICS_DYNAMIC_H_
1818
#define FIREBASE_ANALYTICS_SRC_WINDOWS_ANALYTICS_DYNAMIC_H_
1919

20+
#include <stdbool.h> // needed for bool type in pure C
21+
2022
// --- Copied from original header ---
2123
#include <stdint.h>
2224

@@ -88,19 +90,40 @@ extern void (*ptr_GoogleAnalytics_SetUserProperty)(const char* name, const char*
8890
extern void (*ptr_GoogleAnalytics_SetUserId)(const char* user_id);
8991
extern void (*ptr_GoogleAnalytics_ResetAnalyticsData)();
9092
extern void (*ptr_GoogleAnalytics_SetAnalyticsCollectionEnabled)(bool enabled);
93+
94+
#define GoogleAnalytics_Item_Create ptr_GoogleAnalytics_Item_Create
95+
#define GoogleAnalytics_Item_InsertInt ptr_GoogleAnalytics_Item_InsertInt
96+
#define GoogleAnalytics_Item_InsertDouble ptr_GoogleAnalytics_Item_InsertDouble
97+
#define GoogleAnalytics_Item_InsertString ptr_GoogleAnalytics_Item_InsertString
98+
#define GoogleAnalytics_Item_Destroy ptr_GoogleAnalytics_Item_Destroy
99+
#define GoogleAnalytics_ItemVector_Create ptr_GoogleAnalytics_ItemVector_Create
100+
#define GoogleAnalytics_ItemVector_InsertItem ptr_GoogleAnalytics_ItemVector_InsertItem
101+
#define GoogleAnalytics_ItemVector_Destroy ptr_GoogleAnalytics_ItemVector_Destroy
102+
#define GoogleAnalytics_EventParameters_Create ptr_GoogleAnalytics_EventParameters_Create
103+
#define GoogleAnalytics_EventParameters_InsertInt ptr_GoogleAnalytics_EventParameters_InsertInt
104+
#define GoogleAnalytics_EventParameters_InsertDouble ptr_GoogleAnalytics_EventParameters_InsertDouble
105+
#define GoogleAnalytics_EventParameters_InsertString ptr_GoogleAnalytics_EventParameters_InsertString
106+
#define GoogleAnalytics_EventParameters_InsertItemVector ptr_GoogleAnalytics_EventParameters_InsertItemVector
107+
#define GoogleAnalytics_EventParameters_Destroy ptr_GoogleAnalytics_EventParameters_Destroy
108+
#define GoogleAnalytics_LogEvent ptr_GoogleAnalytics_LogEvent
109+
#define GoogleAnalytics_SetUserProperty ptr_GoogleAnalytics_SetUserProperty
110+
#define GoogleAnalytics_SetUserId ptr_GoogleAnalytics_SetUserId
111+
#define GoogleAnalytics_ResetAnalyticsData ptr_GoogleAnalytics_ResetAnalyticsData
112+
#define GoogleAnalytics_SetAnalyticsCollectionEnabled ptr_GoogleAnalytics_SetAnalyticsCollectionEnabled
91113
// clang-format on
92114

115+
93116
// --- Dynamic Loader Declaration for Windows ---
94117
#if defined(_WIN32)
95-
#include <windows.h> // For HMODULE
96-
// Load Google Analytics functions from the given DLL handle into function
97-
// pointers. Returns the number of functions successfully loaded (out of 19).
118+
#include <windows.h> // For HMODULE
119+
// Load Google Analytics functions from the given DLL handle into function pointers.
120+
// Returns the number of functions successfully loaded (out of 19).
98121
int FirebaseAnalytics_LoadAnalyticsFunctions(HMODULE dll_handle);
99122

100123
// Reset all function pointers back to stubs.
101124
void FirebaseAnalytics_UnloadAnalyticsFunctions(void);
102125

103-
#endif // defined(_WIN32)
126+
#endif // defined(_WIN32)
104127

105128
#ifdef __cplusplus
106129
}

0 commit comments

Comments
 (0)