-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch-headers.py
More file actions
81 lines (64 loc) · 2.53 KB
/
patch-headers.py
File metadata and controls
81 lines (64 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import re
import sys
import os
def patch_header(input_file, output_file):
# Read the input file
with open(input_file, 'r') as f:
content = f.read()
# Modify #include directives to include _patched before the extension
# Only modify if the file name starts with "v5_"
include_pattern = re.compile(r'(#include\s+")([^"]+)(")')
def include_replacer(match):
filename = match.group(2)
if not filename.startswith("v5_"):
return match.group(0)
base, ext = os.path.splitext(filename)
return f'{match.group(1)}{base}_patched{ext}{match.group(3)}'
content = include_pattern.sub(include_replacer, content)
# Regular expression to find function declarations/definitions
# This regex looks for closing parenthesis of function parameters followed by attributes and termination
pattern = re.compile(
r'\)(\s*)((?:__attribute__\s*\(\([^)]*\)\)\s*)*)(\s*)(;|\{)',
re.DOTALL
)
def replacer(match):
whitespace_after_paren = match.group(1)
existing_attrs = match.group(2)
whitespace_after_attrs = match.group(3)
ending = match.group(4)
# Check if 'pcs("aapcs")' is already present
if 'pcs("aapcs")' in existing_attrs:
return match.group(0)
new_attr = ' __attribute__((pcs("aapcs")))'
new_part = f'){whitespace_after_paren}{new_attr}'
# Add existing attributes if present
if existing_attrs:
new_part += f' {existing_attrs}'
# Add remaining whitespace and ending
new_part += f'{whitespace_after_attrs}{ending}'
return new_part
# Substitute all occurrences for function declarations/definitions
modified_content = pattern.sub(replacer, content)
# Write the output file
dir_name = os.path.dirname(output_file)
if dir_name != '':
os.makedirs(dir_name, exist_ok=True)
with open(output_file, 'w') as f:
f.write(modified_content)
def append_patched_to_filename(file_path: str) -> str:
base, ext = os.path.splitext(file_path)
return f"{base}_patched{ext}"
def main():
# get headers to patch
files = sys.argv[3:-1]
# get output directory
out_dir = sys.argv[-1]
# patch headers
for file in files:
name = os.path.join(out_dir, os.path.basename(file))
new_name = append_patched_to_filename(name)
patch_header(name, new_name)
# we're done
sys.exit(0)
if __name__ == "__main__":
main()