forked from achal126/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
x.py
executable file
·139 lines (101 loc) · 4.16 KB
/
x.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
#!/usr/bin/env python3
import os, re, argparse, sys
from subprocess import Popen, check_call, PIPE, check_output, CalledProcessError
from shutil import copy2, copytree, rmtree
def sh(command, silent=False, cwd=None, shell=True, env=None):
if env is not None:
env = dict(**os.environ, **env)
if silent:
p = Popen(
command, shell=shell, stdout=PIPE, stderr=PIPE, cwd=cwd, env=env)
stdout, _ = p.communicate()
return stdout
else:
check_call(command, shell=shell, cwd=cwd, env=env)
def update_submodules():
print(" :: update hedera-sdk-c")
# Ensure the submodule is initialized
sh("git submodule update --init", silent=True)
# Fetch upstream changes
sh("git submodule foreach git fetch", silent=True)
# Reset to upstream
sh("git submodule foreach git reset --hard origin/HEAD", silent=True)
# Update include/
rmtree("include")
copytree("vendor/hedera-sdk-c/include", "include")
def get_default_target():
targets = sh("rustup target list", silent=True).decode()
m = re.search(r"(.*?)\s*\(default\)", targets)
return m[1]
def build(release=False):
default_target = get_default_target()
targets = [
"x86_64-apple-darwin", "x86_64-pc-windows-gnu",
"x86_64-unknown-linux-musl"
]
prefix = {
"x86_64-apple-darwin": "",
"x86_64-pc-windows-gnu": "x86_64-w64-mingw32-",
"x86_64-unknown-linux-musl": "x86_64-linux-musl-"
}
artifact = {
"x86_64-apple-darwin": "libhedera.a",
"x86_64-pc-windows-gnu": "hedera.lib",
"x86_64-unknown-linux-musl": "libhedera.a"
}
if release:
for target in targets:
print(f" :: build hedera-sdk-c for {target}")
if target != default_target:
sh(["rustup", "target", "add", target],
shell=False,
silent=True,
cwd="vendor/hedera-sdk-c")
if 'musl' in target:
cc = 'musl-gcc'
else:
cc = f"{prefix[target]}gcc"
profile = "--release" if release else ''
sh(f"cargo build --target {target} {profile}",
cwd="vendor/hedera-sdk-c",
env={
"CC": cc,
"CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER": cc,
"CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER": cc,
})
if target.endswith("-apple-darwin"):
sh(f"strip -Sx {artifact[target]}",
cwd=f"vendor/hedera-sdk-c/target/{target}/release", silent=True)
elif target.endswith("-musl"):
sh(f"strip --strip-unneeded -d -x {artifact[target]}",
cwd=f"vendor/hedera-sdk-c/target/{target}/release")
else:
sh(f"{prefix[target]}strip --strip-unneeded -d -x {artifact[target]}",
cwd=f"vendor/hedera-sdk-c/target/{target}/release")
copy2(f"vendor/hedera-sdk-c/target/{target}/release/{artifact[target]}", f"libs/{target}/")
else:
target = default_target
# For development; build only the _default_ target
print(f" :: build hedera-sdk-c for {target}")
sh(f"cargo build --target {target}", cwd="vendor/hedera-sdk-c")
# Copy _default_ lib over
copy2(f"vendor/hedera-sdk-c/target/{target}/debug/{artifact[target]}", f"libs/{target}/")
def commit():
sha = sh("git rev-parse --short HEAD", cwd="vendor/hedera-sdk-c", silent=True).decode().strip()
sh("git add ./vendor/hedera-sdk-c ./libs ./include")
try:
sh(f"git commit -m \"build libs/ and sync include/ from hedera-sdk-c#{sha}\"")
sh("git push")
except CalledProcessError:
# Commit likely failed because there was nothing to commit
pass
parser = argparse.ArgumentParser()
parser.add_argument(
"-R", "--release", help="build in release mode", action="store_true")
parser.add_argument(
"-C", "--commit", help="commit include/ and libs/", action="store_true")
argv = parser.parse_args(sys.argv[1:])
update_submodules()
build(release=argv.release)
if argv.commit and argv.release:
commit()