-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.py
129 lines (112 loc) · 4.02 KB
/
build.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
# build script that creates aliases for libwally files
# properly structured in scr to work with Arduino build system
import os
# where all the files are
ROOT = "libwally-core"
# where aliases will be placed
TARGET = "src"
alias_blacklist = [
"wally_elements.h",
"elements.c",
"scrypt.c",
"test.c",
"run.c",
"bench.c",
]
def create_alias(in_path, out_path, fname):
# how many ../ we need
if fname in alias_blacklist:
return False
level = len([d for d in out_path.split("/") if len(d)>0 and d!="."])
prefix = "../"*level
with open(os.path.join(out_path,fname), "w") as f:
f.write("#include \"wally_config.h\"\n")
f.write("#include \"%s\"" % os.path.join(prefix, in_path, fname))
return True
global_whitelist = [
"secp256k1.h",
"secp256k1_preallocated.h",
"secp256k1_recovery.h",
"secp256k1_ecdh.h",
]
def global_import(in_path, out_path, fname):
if fname not in global_whitelist:
return False
with open(os.path.join(out_path, fname), "w") as f:
f.write("#include <%s>" % fname)
return True
recursive_ignore = [
"test",
]
rules = [
# (initial folder, dest folder, file extensions, content rule, recursive?)
# wally will be importing from here
("include", "include", ["h", "hpp"], create_alias, False),
# for library imports in IDE
("include", "", ["h", "hpp"], create_alias, False),
("src/secp256k1/include", "secp256k1/include", ["h"], global_import, False),
("src", "", ["c", "h", "inl"], create_alias, False),
("src/data/wordlists", "data/wordlists", ["c"], create_alias, False),
# ("src/ctaes", "ctaes", ["c", "h"], create_alias, False),
("src/ccan/ccan", "ccan/ccan", ["c", "h"], create_alias, True),
("src/ccan/ccan", "ccan", ["h"], create_alias, True),
]
WALLY_CONFIG = """
#ifndef LIBWALLYCORE_CONFIG_H
#define LIBWALLYCORE_CONFIG_H
#include "ccan_config.h"
#endif /*LIBWALLYCORE_CONFIG_H*/
"""
def patch():
# patches where it didn't work
with open(os.path.join(TARGET, "wally_config.h"), "w") as f:
f.write(WALLY_CONFIG)
# encodings and config file
with open(os.path.join(TARGET, "config.h"), "w") as f:
f.write("#include \"wally_config.h\"")
for fname in ["ccan/endian/endian.h", "ccan/ccan/endian/endian.h"]:
fname = os.path.join(TARGET, fname)
with open(fname, "r") as f:
content = f.read()
with open(fname, "w") as f:
f.write("#include \"wally_config.h\"\n")
f.write(content)
def main():
for in_path, out_path, extensions, fn, recursive in rules:
in_path = os.path.join(ROOT, in_path)
out_path = os.path.join(TARGET, out_path)
try:
os.makedirs(out_path)
print("Created:", out_path)
except:
print("Path already exists:", out_path)
if not recursive:
files = [f for f in os.listdir(in_path)
if f.split(".")[-1] in extensions]
for f in files:
if fn(in_path, out_path, f):
print("created", os.path.join(out_path, f))
else:
print("skip", os.path.join(out_path, f))
else:
for parent, dirs, files in os.walk(in_path):
if parent.split("/")[-1] in recursive_ignore:
continue
files = [f for f in files
if f.split(".")[-1] in extensions]
if len(files) == 0:
continue
out = parent.replace(in_path, out_path, 1)
try:
os.makedirs(out)
print("Created:", out)
except:
print("Path already exists:", out)
for f in files:
if fn(parent, out, f):
print("created", os.path.join(out, f))
else:
print("skip", os.path.join(out, f))
patch()
if __name__ == '__main__':
main()