-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup_toolchains.py
173 lines (139 loc) · 5.11 KB
/
setup_toolchains.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
164
165
166
167
168
169
170
171
172
173
import argparse
import os
_AVAILABLE_TOOLCHAINS = [
{
'host_arch': 'x86_64',
'host_os': 'linux',
'target': 'arm-none-eabi',
'version': '12.3.rel1',
'sha256': '12a2815644318ebcceaf84beabb665d0924b6e79e21048452c5331a56332b309',
},
{
'host_arch': 'arm64',
'host_os': 'darwin',
'target': 'arm-none-eabi',
'version': '12.3.rel1',
'sha256': '3b2eee0bdf71c1bbeb3c3b7424fbf7bd9d5c3f0f5a3a4a78159c9e3ad219e7bd',
},
]
# GCC cpu options: https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html#index-mcpu-2
_ALL_CPU = [
'cortex-m0',
'cortex-m0plus',
'cortex-m1',
'cortex-m3',
'cortex-m4',
'cortex-m7',
'cortex-m7+nofp.dp',
'cortex-a8',
'cortex-a9',
]
_ALL_TOOLS = [
'ar',
'as',
'cpp',
'gcc',
'gcov',
'gdb',
'ld',
'nm',
'objcopy',
'objdump',
'readelf',
'strip',
]
def create_wrappers(toolchain_dir, root_dir):
for toolchain in _AVAILABLE_TOOLCHAINS:
host_os_name = '' if toolchain['host_os'] == 'linux' else toolchain['host_os'] + "-"
name = ('arm-gnu-toolchain-' +
f'{toolchain["version"]}-{host_os_name}{toolchain["host_arch"]}-{toolchain["target"]}')
toolchain['name'] = name
target_os = 'linux' if 'linux' in toolchain['target'] else 'none'
toolchain['target_os'] = target_os
wrapper_dir = os.path.join(toolchain_dir, 'tool_wrappers', toolchain['host_arch'],
toolchain['host_os'], toolchain['target'], toolchain['version'])
try:
os.makedirs(wrapper_dir)
except FileExistsError:
pass
# Populate empty BUILD files.
wrapper_dirs = os.path.relpath(wrapper_dir, toolchain_dir).split(os.path.sep)
for i in range(1, len(wrapper_dirs)):
with open(os.path.join(toolchain_dir, *wrapper_dirs[:-i], 'BUILD'), 'w') as f:
pass
toolchain['wrapper_dir'] = os.path.relpath(wrapper_dir, root_dir)
toolchain['wrapper_paths'] = {}
build_file = '''\
filegroup(
name = "wrappers",
srcs = glob(["*"]),
visibility = ["//visibility:public"],
)
'''
for tool in _ALL_TOOLS:
tool_name = f'{toolchain["target"]}-{tool}'
wrapper_path = os.path.join(wrapper_dir, tool_name)
toolchain['wrapper_paths'][tool] = os.path.relpath(wrapper_path, toolchain_dir)
build_file += f'''\
sh_binary(
name = "{tool}",
srcs = [":{tool_name}"],
data = ["@{name}//:all_files"],
visibility = ["//visibility:public"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
'''
with open(wrapper_path, 'w') as f:
f.write(f'''\
#!/bin/bash
TOOL_PATH={name}/bin/{tool_name}
# First check if we can find the executable directly and then revert to runfiles.
# This is necessary because when used in the toolchain, the runfiles library is not available.
if [[ -f external/${{TOOL_PATH}} ]]; then
exec external/${{TOOL_PATH}} $@
fi
# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${{RUNFILES_DIR:-/dev/null}}/$f" 2>/dev/null || \\
source "$(grep -sm1 "^$f " "${{RUNFILES_MANIFEST_FILE:-/dev/null}}" | cut -f2- -d' ')" 2>/dev/null || \\
source "$0.runfiles/$f" 2>/dev/null || \\
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \\
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \\
{{ echo>&2 "ERROR: cannot find $f"; exit 1; }}; f=; set -e
# --- end runfiles.bash initialization v3 ---
exec $(rlocation ${{TOOL_PATH}}) $@\n')
''')
os.chmod(wrapper_path, 0o777)
build_path = os.path.join(wrapper_dir, 'BUILD')
with open(build_path, 'w') as f:
f.write(build_file[:-2])
def write_toolchain_info(filename):
with open(filename, 'w') as f:
f.write(f'AVAILABLE_TOOLCHAINS = {_AVAILABLE_TOOLCHAINS}\n')
f.write(f'ALL_CPU = {_ALL_CPU}\n')
def write_test_script(filename):
with open(filename, 'w') as f:
f.write('#!/bin/bash\n')
f.write('set -e\n')
f.write('set -o xtrace\n\n')
f.write('bazel clean\n')
for toolchain in _AVAILABLE_TOOLCHAINS:
for cpu in _ALL_CPU:
platform = f'//platforms:{cpu}-{toolchain["target_os"]}-{toolchain["version"]}'
f.write(f'bazel build -s --verbose_failures --platforms={platform} //test:test_cpp\n')
f.write(f'bazel build -s --verbose_failures --platforms={platform} //test:test_c\n')
f.write('bazel run --verbose_failures //test:objdump -- --help\n')
f.write(
'bazel build -s --verbose_failures --platforms=//platforms:cortex-m0plus-none-12.3.rel1 //test:objdump_output\n'
)
os.chmod(filename, 0o777)
def main():
parser = argparse.ArgumentParser(description='Generate wrapper scripts for Bazel toolchains.')
args = parser.parse_args()
root_dir = os.path.dirname(os.path.realpath(__file__))
create_wrappers(os.path.join(root_dir, 'toolchains'), root_dir)
write_toolchain_info(os.path.join(root_dir, 'toolchains/toolchain_info.bzl'))
write_test_script(os.path.join(root_dir, 'test_build_all.sh'))
if __name__ == '__main__':
main()