forked from pocketpy/pocketpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amalgamate.py
103 lines (83 loc) · 2.71 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
import os
os.system("python3 prebuild.py")
with open("include/pocketpy/opcodes.h", "rt", encoding='utf-8') as f:
OPCODES_TEXT = '\n' + f.read() + '\n'
pipeline = [
["config.h", "export.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"],
["obj.h", "dict.h", "codeobject.h", "frame.h"],
["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"],
["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "random.h", "re.h", "linalg.h", "easing.h", "io.h"],
["pocketpy.h", "pocketpy_c.h"]
]
copied = set()
text = ""
import re
import shutil
import os
import sys
import time
if os.path.exists("amalgamated"):
shutil.rmtree("amalgamated")
time.sleep(0.5)
os.mkdir("amalgamated")
def remove_copied_include(text):
text = text.replace("#pragma once", "")
def _replace(m):
key = m.group(1)
if key.startswith("pocketpy/"):
key = key[9:]
if key in ["user_config.h", "box2dw.hpp", "cJSONw.hpp"]:
return m.group(0)
if key == "opcodes.h":
return OPCODES_TEXT
assert key in copied, f"include {key} not found"
return ""
text = re.sub(
r'#include\s+"(.+)"\s*',
_replace,
text
)
return text
for seq in pipeline:
for j in seq:
print(j)
with open("include/pocketpy/"+j, "rt", encoding='utf-8') as f:
text += remove_copied_include(f.read()) + '\n'
copied.add(j)
j = j.replace(".h", ".cpp")
if os.path.exists("src/"+j):
with open("src/"+j, "rt", encoding='utf-8') as f:
text += remove_copied_include(f.read()) + '\n'
copied.add(j)
with open("amalgamated/pocketpy.h", "wt", encoding='utf-8') as f:
final_text = \
r'''/*
* Copyright (c) 2023 blueloveTH
* Distributed Under The MIT License
* https://github.com/blueloveTH/pocketpy
*/
#ifndef POCKETPY_H
#define POCKETPY_H
''' + text + '\n#endif // POCKETPY_H'
f.write(final_text)
shutil.copy("src2/main.cpp", "amalgamated/main.cpp")
with open("amalgamated/main.cpp", "rt", encoding='utf-8') as f:
text = f.read()
text = text.replace('#include "pocketpy/pocketpy.h"', '#include "pocketpy.h"')
with open("amalgamated/main.cpp", "wt", encoding='utf-8') as f:
f.write(text)
if sys.platform in ['linux', 'darwin']:
ok = os.system("clang++ -o main amalgamated/main.cpp -O1 --std=c++17 -stdlib=libc++")
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') as f:
f.write("#include \"pocketpy.h\"\n")
sync("plugins/flutter/src")
sync("plugins/macos/pocketpy")
# unity plugin
unity_ios_root = 'plugins/unity/PocketPyUnityPlugin/Assets/PocketPython/Plugins/iOS'
if os.path.exists(unity_ios_root):
sync(unity_ios_root)