From 9ed7e84644239886a26669551bac72af24b98a1c Mon Sep 17 00:00:00 2001 From: Aleksey Khoroshilov Date: Tue, 18 Jan 2022 23:36:01 +0700 Subject: [PATCH] MacOS, iOS: Replace dynamic comments in mig-generated files with stable ones. Mig-generated files contain mig identifiers, which include timestamp and mig build info. To improve build determinism and goma cachehits we can replace these lines with something stable. Bug: crashpad:390 Change-Id: Iedb2f6e64428612899587c2ac4d488baf439961f Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/3394052 Reviewed-by: Mark Mentovai Commit-Queue: Mark Mentovai --- util/mach/mig_fix.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/util/mach/mig_fix.py b/util/mach/mig_fix.py index 037746fa..6e287713 100755 --- a/util/mach/mig_fix.py +++ b/util/mach/mig_fix.py @@ -23,6 +23,33 @@ import sys from mig_gen import MigInterface +def _make_generated_comments_deterministic(contents): + """Replaces generated code comments with determenistic ones. + + This is what is generated by mig (only in .c files): + /* + * IDENTIFICATION: + * stub generated Mon Jan 17 15:28:03 2022 + * with a MiG generated by bootstrap_cmds-122 + * OPTIONS: + */ + + We look for two specific lines and replace them like so: + /* + * IDENTIFICATION: + * stub generated + * with a MiG generated by + * OPTIONS: + */ + """ + + return re.sub(r'^( \* (?:stub generated|with a MiG generated by) ).+$', + r'\1', + contents, + count=2, + flags=re.MULTILINE) + + def _fix_user_implementation(implementation, fixed_implementation, header, fixed_header): """Rewrites a MIG-generated user implementation (.c) file. @@ -33,7 +60,8 @@ def _fix_user_implementation(implementation, fixed_implementation, header, unused in the user implementation file, and this will trigger a -Wunused-local-typedefs warning in gcc unless removed or marked with the “unused” attribute. Also changes header references to point to the new - header filename, if changed. + header filename, if changed. Replaces generated code comments with + deterministic ones. If |fixed_implementation| is None, overwrites the original; otherwise, puts the result in the file at |fixed_implementation|. @@ -50,6 +78,9 @@ def _fix_user_implementation(implementation, fixed_implementation, header, '#include "%s"' % os.path.basename(header), '#include "%s"' % os.path.basename(fixed_header)) + # Replace generated code comments with determenistic ones. + contents = _make_generated_comments_deterministic(contents) + if fixed_implementation is None: file.seek(0) file.truncate() @@ -71,6 +102,7 @@ def _fix_server_implementation(implementation, fixed_implementation, header, added to a header file, so that other files that include that header file will have access to these declarations from a compilation perspective. Also changes header references to point to the new header filename, if changed. + Replaces generated code comments with deterministic ones. If |fixed_implementation| is None or not provided, overwrites the original; otherwise, puts the result in the file at |fixed_implementation|. @@ -117,6 +149,9 @@ extern '#include "%s"' % os.path.basename(header), '#include "%s"' % os.path.basename(fixed_header)) + # Replace generated code comments with determenistic ones. + contents = _make_generated_comments_deterministic(contents) + if fixed_implementation is None: file.seek(0) file.truncate()