-
Notifications
You must be signed in to change notification settings - Fork 12
/
csibe.py
executable file
·232 lines (178 loc) · 6.58 KB
/
csibe.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python
import argparse
import os
import subprocess
import sys
import textwrap
class CSiBEBuilder(object):
def __init__(self, csibe_path, build_path, toolchain_name, projects, flags):
self.csibe_dir = csibe_path
self.build_dir = build_path
self.toolchain_name = toolchain_name
self.projects = projects
self.cflags = []
self.cxxflags = []
self.rustcflags = []
if flags['cflags']:
self.cflags.extend(flags['cflags'])
if flags['cxxflags']:
self.cxxflags.extend(flags['cxxflags'])
if flags['rustcflags']:
self.rustcflags.extend(flags['rustcflags'])
if flags['globalflags']:
self.cflags.extend(flags['globalflags'])
self.cxxflags.extend(flags['globalflags'])
self.rustcflags.extend(flags['globalflags'])
self.toolchain_build_dir = os.path.join(
self.build_dir,
self.toolchain_name)
self.toolchain_files_dir = os.path.join(
self.csibe_dir,
"toolchain-files")
if self.toolchain_name == "native":
self.toolchain_file_path = None
else:
self.toolchain_file_path = os.path.join(
self.toolchain_files_dir,
"{}.cmake".format(toolchain_name))
self.cmake_toolchain_options = ""
if self.toolchain_file_path:
self.cmake_toolchain_options = "-DCMAKE_TOOLCHAIN_FILE={}".format(self.toolchain_file_path)
if self.projects:
os.environ['CSiBE_SUBPROJECTS'] = " ".join(self.projects);
if self.cflags:
os.environ['CSiBE_CFLAGS'] = " ".join(self.cflags);
if self.cxxflags:
os.environ['CSiBE_CXXFLAGS'] = " ".join(self.cxxflags);
if self.rustcflags:
os.environ['CSiBE_RUSTCFLAGS'] = " ".join(self.rustcflags);
def run_cmake(self):
return subprocess.call(
["cmake",
self.cmake_toolchain_options,
self.csibe_dir,
"-B{}".format(self.toolchain_build_dir)])
def run_make(self, jobs):
return subprocess.call(
["make",
"-C{}".format(self.toolchain_build_dir),
"-j{}".format(jobs)])
def run_make_size(self):
return subprocess.call(
["make",
"-C{}".format(self.toolchain_build_dir),
"size"])
def submodule_init_and_update(repository_path):
init_return_value = subprocess.call(
["git",
"-C",
repository_path,
"submodule",
"init"])
if init_return_value:
sys.stdout.write("Warning: Failed to execute git submodule init.")
return
update_return_value = subprocess.call(
["git",
"-C",
repository_path,
"submodule",
"update"])
if update_return_value:
sys.stdout.write("Warning: Failed to execute git submodule update.")
return
if __name__ == "__main__":
toolchains = ["native",
"clang-cortex-m0",
"clang-cortex-m4",
"gcc-cortex-m0",
"gcc-cortex-m4"]
projects = []
for item in os.listdir('src'):
projects.append(item)
helpProjects = "\navailable project names:\n\t" + "\n\t".join(projects)
helpToolchains = "\n\navailable toolchain files:\n\t" + "\n\t".join(toolchains)
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent(helpProjects + helpToolchains))
parser.add_argument(
"-j",
"--jobs",
type=int,
default=1,
help="number of jobs for make")
parser.add_argument(
"--toolchain",
choices=toolchains,
default="native",
help="Toolchain to be used by CMake. Possible values are " + ", ".join(toolchains),
metavar="")
parser.add_argument(
"--build-all",
action="store_true",
help="build every target")
parser.add_argument(
"--cmake-only",
action="store_true",
help="run CMake only")
parser.add_argument(
"--build-dir",
default="build",
help="directory name for build files")
parser.add_argument(
"--cflags",
action='append',
help="compiler flags for C files")
parser.add_argument(
"--cxxflags",
action='append',
help="compiler flags for CXX files")
parser.add_argument(
"--rustcflags",
action='append',
help="compiler flags for Rust files")
parser.add_argument(
"--globalflags",
action='append',
help="compiler flags for CXX files")
parser.add_argument(
"option",
nargs="*",
help="can be project names, toolchain files, or compiler flags")
args, global_flags = parser.parse_known_args()
if args.globalflags:
global_flags.append(args.globalflags)
csibe_path = os.path.dirname(os.path.realpath(__file__))
submodule_init_and_update(csibe_path)
# Target selection
targets_to_build = []
for opt in args.option:
if opt in toolchains:
targets_to_build.append(opt)
if not targets_to_build:
if args.build_all:
targets_to_build = toolchains
else:
targets_to_build = [args.toolchain]
# Project selection
projects_to_build = []
for opt in args.option:
if opt in projects:
projects_to_build.append(opt)
for target in targets_to_build:
builder = CSiBEBuilder(csibe_path, args.build_dir, target, projects_to_build,
{'cflags' : args.cflags,
'cxxflags' : args.cxxflags,
'rustcflags' : args.rustcflags,
'globalflags' : global_flags})
cmake_return_value = builder.run_cmake()
if cmake_return_value:
sys.exit(cmake_return_value)
if args.cmake_only:
continue
make_return_value = builder.run_make(args.jobs)
if make_return_value:
sys.exit(make_return_value)
make_size_return_value = builder.run_make_size()
if make_size_return_value:
sys.exit(make_size_return_value)