-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgen_release_header.py
executable file
·61 lines (44 loc) · 1.9 KB
/
gen_release_header.py
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
#!/usr/bin/env python3
import re
from pathlib import Path
import sys
import os
version = os.popen("git describe --tags").read().strip()
visited_includes = set()
include_dir = Path(sys.argv[1])
main_header = Path(sys.argv[2])
# store content rather than emit directly
content_lines = []
system_headers = set()
insert_system_headers_line = None
def process(base_filepath):
global version, visited_includes, include_dir
global content_lines, system_headers, insert_system_headers_line
if base_filepath not in visited_includes:
visited_includes.add(base_filepath)
with open(base_filepath) as core:
for line in core.readlines():
m = re.match(r"\s*#include\s*<([a-zA-Z0-9_/. ]+)>", line)
if m:
# capture first include as location for system include list
if not insert_system_headers_line:
insert_system_headers_line = len(content_lines)
sub_filepath = Path(m.group(1))
full_path = include_dir / sub_filepath
if full_path.exists():
# recurse into a cib header
process(full_path)
else:
# otherwise keep the system header for later
system_headers.add(line)
elif line.startswith("#pragma once"):
pass
else:
line = re.sub("\.\.~~VERSION~~\.\.", version, line)
content_lines.append(line)
process(include_dir / main_header)
# write out the content, when we get to the line number the location to emit
# system headers, write those out before proceeding with the remaining content
print("".join(content_lines[:insert_system_headers_line]))
print("".join(sorted(system_headers)))
print("".join(content_lines[insert_system_headers_line:]))