Skip to content

Commit ee7e45d

Browse files
cynthiajoanCynthia Jiang
andauthored
Add extra swig post process for analytics static properties (#201)
Co-authored-by: Cynthia Jiang <[email protected]>
1 parent 1e23d8e commit ee7e45d

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

cmake/firebase_swig.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ macro(firebase_swig_add_library name)
187187
set(import_module_override "")
188188
endif()
189189

190+
set(static_k_removal_override "")
191+
if("${name}" STREQUAL "firebase_analytics_swig") # hack for now, need to find better way to do special cases
192+
set(static_k_removal_override "--static_k_removal=True")
193+
endif()
194+
190195
add_custom_command(
191196
OUTPUT ${UNITY_SWIG_CS_FIX_FILE}
192197
DEPENDS ${UNITY_SWIG_CS_GEN_FILE}
@@ -198,6 +203,7 @@ macro(firebase_swig_add_library name)
198203
--out_file=\"${UNITY_SWIG_CS_FIX_FILE}\"
199204
--namespace=${UNITY_SWIG_MODULE_OUT}
200205
${import_module_override}
206+
${static_k_removal_override}
201207
)
202208

203209
set(UNITY_SWIG_CPP_GEN_FILE ${swig_generated_file_fullname})

cmake/swig_fix.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
'Clear out in file once out file is written')
4848
flags.DEFINE_string('import_module', None,
4949
'Change DllImport module to this value')
50+
flags.DEFINE_boolean('static_k_removal', False,
51+
'Whether to remove public static attributes k initial')
5052

5153

5254
def get_dll_import_transformation():
@@ -63,6 +65,20 @@ def get_dll_import_transformation():
6365
]
6466

6567

68+
def get_static_k_removal_transformation():
69+
"""Gets the transforms to remove k initial for public static attribute
70+
71+
Returns:
72+
list of static k removal transformations.
73+
"""
74+
if not FLAGS.static_k_removal:
75+
return []
76+
77+
return [
78+
swig_post_process.StaticFunctionKInitRemoval()
79+
]
80+
81+
6682
class NamespaceCMethodsCMake(swig_post_process.SWIGPostProcessingInterface):
6783
"""Add a module namespace prefix to all generated C methods."""
6884

@@ -146,7 +162,8 @@ def get_transformations(namespace):
146162
swig_post_process.RenameAsyncMethods(),
147163
swig_post_process.RenameArgsFromSnakeToCamelCase(),
148164
swig_post_process.AddPInvokeAttribute(),
149-
] + get_dll_import_transformation(),
165+
] + get_dll_import_transformation()
166+
+ get_static_k_removal_transformation(),
150167
}
151168

152169

@@ -166,7 +183,7 @@ def main(argv): # pylint: disable=unused-argument
166183
logging.warning('Empty input file "%s"', FLAGS.in_file)
167184
return 0
168185

169-
new_contents = swig_post_process.apply_post_process(contents, '', processes)
186+
new_contents = swig_post_process.apply_post_process(contents, FLAGS.in_file, processes)
170187

171188
with open(FLAGS.out_file, 'w') as out_file:
172189
out_file.write(new_contents)

swig_post_process.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,40 @@ def __call__(self, file_str, filename, iteration):
479479
output.append(line)
480480
return '\n'.join(output)
481481

482+
class StaticFunctionKInitRemoval(SWIGPostProcessingInterface):
483+
"""Change static function or property that has k in the name initial.
482484
485+
Any function or property after "public static" and named as kXYZ will
486+
be renamed to XYZ.
487+
"""
488+
489+
def __init__(self):
490+
"""Initialize the instance."""
491+
self.static_function_regex = re.compile(
492+
r'public static ([^ =]*) (k[^ =]*)')
493+
494+
def __call__(self, file_str, filename, iteration):
495+
"""Change static function or property that has k in the name initial.
496+
497+
Args:
498+
file_str: This is a string containing the file contents to be processed.
499+
filename: Unused.
500+
iteration: Unused.
501+
502+
Returns:
503+
Returns the modified file_str.
504+
"""
505+
output = []
506+
for line in file_str.splitlines():
507+
match = self.static_function_regex.search(line)
508+
if match:
509+
function_name = match.groups()[1]
510+
line = ''.join([line[:match.end(1)+1],
511+
function_name.replace('k', ''),
512+
line[match.end(2):]])
513+
output.append(line)
514+
return '\n'.join(output)
515+
483516
class DynamicToReinterpretCast(SWIGPostProcessingInterface):
484517
"""Changes the use of dynamic_cast in SWIG generated code to reinterpret_cast.
485518

0 commit comments

Comments
 (0)