forked from pocketpy/pocketpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamalgamate.py
163 lines (132 loc) · 3.91 KB
/
amalgamate.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import re
import shutil
import os
import sys
import time
from typing import List, Dict
assert os.system("python prebuild.py") == 0
ROOT = 'include/pocketpy'
PUBLIC_HEADERS = ['config.h', 'export.h', 'linalg.h', 'pocketpy.h']
COPYRIGHT = '''/*
* Copyright (c) 2024 blueloveTH
* Distributed Under The MIT License
* https://github.com/pocketpy/pocketpy
*/
'''
def read_file(path):
with open(path, 'rt', encoding='utf-8') as f:
return f.read()
def write_file(path, content):
with open(path, 'wt', encoding='utf-8', newline='\n') as f:
f.write(content)
if os.path.exists('amalgamated'):
shutil.rmtree('amalgamated')
time.sleep(0.5)
os.mkdir('amalgamated')
class Header:
path: str
content: str # header source
dependencies: List[str]
def __init__(self, path: str):
self.path = path
self.dependencies = []
self.content = read_file(f'{ROOT}/{path}')
# process raw content and get dependencies
self.content = self.content.replace('#pragma once', '')
def _replace(m):
path = m.group(1)
if path.startswith('xmacros/'):
return read_file(f'{ROOT}/{path}') + '\n'
if path in PUBLIC_HEADERS:
return '' # remove include
if path != self.path:
self.dependencies.append(path)
return '' # remove include
self.content = re.sub(
r'#include\s+"pocketpy/(.+)"\s*',
_replace,
self.content
)
def __repr__(self):
return f'Header({self.path!r}, dependencies={self.dependencies})'
def text(self):
return f'// {self.path}\n{self.content}\n'
headers: Dict[str, Header] = {}
for entry in os.listdir(ROOT):
if os.path.isdir(f'{ROOT}/{entry}'):
if entry == 'xmacros' or entry in PUBLIC_HEADERS:
continue
files = os.listdir(f'{ROOT}/{entry}')
for file in sorted(files):
assert file.endswith('.h')
if entry in PUBLIC_HEADERS:
continue
headers[f'{entry}/{file}'] = Header(f'{entry}/{file}')
def merge_c_files():
c_files = [COPYRIGHT, '\n', '#include "pocketpy.h"', '\n']
# merge internal headers
internal_h = []
while True:
for h in headers.values():
if not h.dependencies:
break
else:
if headers:
print(headers)
raise RuntimeError("Circular dependencies detected")
break
# print(h.path)
internal_h.append(h.text())
del headers[h.path]
for h2 in headers.values():
h2.dependencies = [d for d in h2.dependencies if d != h.path]
c_files.extend(internal_h)
def _replace(m):
path = m.group(1)
if path.startswith('xmacros/'):
return read_file(f'{ROOT}/{path}') + '\n'
return '' # remove include
for root, _, files in os.walk('src/'):
for file in files:
if file.endswith('.c'):
path = os.path.join(root, file)
c_files.append(f'// {path}\n')
content = read_file(path)
content = re.sub(
r'#include\s+"pocketpy/(.+)"\s*',
_replace,
content,
)
c_files.append(content)
c_files.append('\n')
return ''.join(c_files)
def merge_h_files():
h_files = [COPYRIGHT, '#pragma once']
def _replace(m):
path = m.group(1)
if path.startswith('xmacros/'):
return read_file(f'{ROOT}/{path}') + '\n'
return '' # remove include
for path in PUBLIC_HEADERS:
content = read_file(f'{ROOT}/{path}')
content = content.replace('#pragma once', '')
content = re.sub(
r'#include\s+"pocketpy/(.+)"\s*',
_replace,
content,
)
h_files.append(content)
return '\n'.join(h_files)
write_file('amalgamated/pocketpy.c', merge_c_files())
write_file('amalgamated/pocketpy.h', merge_h_files())
shutil.copy("src2/main.c", "amalgamated/main.c")
if sys.platform in ['linux', 'darwin']:
ok = os.system("clang -o main amalgamated/pocketpy.c amalgamated/main.c -O1 --std=c11 -lm")
if ok == 0:
print("Test build success!")
print("amalgamated/pocketpy.h")
# def sync(path):
# shutil.copy("amalgamated/pocketpy.h", os.path.join(path, "pocketpy.h"))
# with open(os.path.join(path, "pocketpy.cpp"), "wt", encoding='utf-8', newline='\n') as f:
# f.write("#include \"pocketpy.h\"\n")
# sync("plugins/macos/pocketpy")