-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool.lua
More file actions
executable file
·332 lines (277 loc) · 9.29 KB
/
tool.lua
File metadata and controls
executable file
·332 lines (277 loc) · 9.29 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env lua
--[[
This file is a part of ThetaGP.
ThetaGP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ThetaGP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.
If not, see <https://www.gnu.org/licenses/>.
]]
-- =============================================================================
-- ThetaGP Build Script
-- =============================================================================
local HELP_TEXT = [[
Usage: lua tool.lua [command] [options]
Commands:
all Config + build + flash (default)
config Run CMake configuration
build Build the project
flash Flash firmware via OpenOCD
help Show this help message
Options:
--build-dir DIR Build directory (default: build)
--build-type TYPE Build type: Debug|Release (default: Debug)
--jobs N Parallel build jobs (default: auto)
--openocd-cfg FILE OpenOCD config (required for flash)
--target TARGET Board target (required, or set TARGET env)
]]
local function print_help()
print(HELP_TEXT)
os.exit(0)
end
-- =============================================================================
-- Utility Functions
-- =============================================================================
local function print_info(...)
local args = {}
for _, v in ipairs({ ... }) do
table.insert(args, tostring(v))
end
print("[INFO] " .. table.concat(args, " "))
end
local function print_warning(...)
local args = {}
for _, v in ipairs({ ... }) do
table.insert(args, tostring(v))
end
print("[WARNING] " .. table.concat(args, " "))
end
local function print_error(...)
local args = {}
for _, v in ipairs({ ... }) do
table.insert(args, tostring(v))
end
print("[ERROR] " .. table.concat(args, " "))
end
local function execute_command(cmd)
print_info("Executing:", cmd)
local ret = os.execute(cmd)
local success = (ret == true) or (ret == 0)
if not success then
print_error("Command failed with exit code:", ret)
return false
end
return true
end
local function file_exists(path)
local f = io.open(path, "r")
if f then
io.close(f)
return true
end
return false
end
-- =============================================================================
-- Argument Parsing
-- =============================================================================
local args = { ... }
local options = {
target = os.getenv("TARGET") or "",
build_type = "Debug",
build_dir = "",
jobs = nil,
openocd_cfg = "",
}
local commands = {}
local i = 1
while i <= #args do
local arg = args[i]
if arg == "--target" then
i = i + 1
options.target = args[i]
elseif arg == "--build-type" then
i = i + 1
options.build_type = args[i]
elseif arg == "--build-dir" then
i = i + 1
options.build_dir = args[i]
elseif arg == "--jobs" then
i = i + 1
options.jobs = tonumber(args[i])
elseif arg == "--openocd-cfg" then
i = i + 1
options.openocd_cfg = args[i]
elseif arg:sub(1, 1) ~= "-" then
table.insert(commands, arg)
else
print_error("Unknown option: " .. arg)
io.stderr:write(HELP_TEXT)
os.exit(1)
end
i = i + 1
end
if #commands == 0 then
commands = { "all" }
end
-- =============================================================================
-- Path Resolution
-- =============================================================================
local script_dir = debug.getinfo(1, "S").source:match("^@?(.*[/\\])")
local project_root = script_dir or "./"
-- Resolve relative paths
local function resolve_path(p)
if p == "" then
return project_root .. "build"
end
if p:sub(1, 1) == "/" then
return p
end
return project_root .. p
end
options.build_dir = resolve_path(options.build_dir)
if options.openocd_cfg ~= "" then
options.openocd_cfg = resolve_path(options.openocd_cfg)
end
-- =============================================================================
-- CMake Configuration
-- =============================================================================
local function step_config()
if options.target == "" then
print_error("TARGET is not defined. Set --target or export TARGET environment variable.")
return false
end
print_info("===========================================")
print_info("Step: Config")
print_info(" Target: " .. options.target)
print_info(" BuildType: " .. options.build_type)
print_info(" BuildDir: " .. options.build_dir)
print_info("===========================================")
os.execute("mkdir -p " .. options.build_dir)
local cmake_args = {
"-S", project_root,
"-B", options.build_dir,
"-DTARGET=" .. options.target,
"-DCMAKE_BUILD_TYPE=" .. options.build_type,
}
local cmd = "cmake " .. table.concat(cmake_args, " ")
return execute_command(cmd)
end
-- =============================================================================
-- Build
-- =============================================================================
local function step_build()
print_info("===========================================")
print_info("Step: Build")
print_info(" BuildDir: " .. options.build_dir)
if options.jobs then
print_info(" Jobs: " .. options.jobs)
end
print_info("===========================================")
local cmake_args = { "--build", options.build_dir }
if options.jobs then
table.insert(cmake_args, "--parallel")
table.insert(cmake_args, tostring(options.jobs))
end
local cmd = "cmake " .. table.concat(cmake_args, " ")
return execute_command(cmd)
end
-- =============================================================================
-- Flash via OpenOCD
-- =============================================================================
local function get_elf_path()
local handle = io.popen("find '" .. options.build_dir .. "' -maxdepth 1 -name '*.elf' -type f 2>/dev/null")
if not handle then
return nil
end
local result = handle:read("*l")
handle:close()
return result
end
local function step_flash()
if options.openocd_cfg == "" then
print_error("OpenOCD config not specified. Use --openocd-cfg.")
return false
end
print_info("===========================================")
print_info("Step: Flash")
print_info(" OpenOCD config: " .. options.openocd_cfg)
print_info("===========================================")
if not file_exists(options.openocd_cfg) then
print_error("OpenOCD config not found: " .. options.openocd_cfg)
return false
end
local elf = get_elf_path()
if not elf then
print_error("No ELF file found in build directory. Build the project first.")
return false
end
print_info(" ELF: " .. elf)
-- Check if openocd is available
local openocd_check = io.popen("which openocd 2>/dev/null")
if not openocd_check or not openocd_check:read("*l") then
print_error("openocd not found. Please install OpenOCD.")
if openocd_check then
openocd_check:close()
end
return false
end
if openocd_check then
openocd_check:close()
end
local cmd = string.format(
"openocd -f '%s' -c 'program \"%s\" verify' -c 'reset run' -c 'sleep 100' -c 'shutdown'",
options.openocd_cfg, elf
)
return execute_command(cmd)
end
-- =============================================================================
-- Main
-- =============================================================================
local function main()
local steps = {
config = step_config,
build = step_build,
flash = step_flash,
help = print_help,
}
local order = { "config", "build", "flash" }
local found_all = false
for _, cmd in ipairs(commands) do
if cmd == "all" then
found_all = true
break
end
end
if found_all then
for _, name in ipairs(order) do
if not steps[name]() then
print_error("'" .. name .. "' failed. Aborting.")
os.exit(1)
end
end
else
for _, cmd in ipairs(commands) do
if steps[cmd] then
if not steps[cmd]() then
print_error("'" .. cmd .. "' failed. Aborting.")
os.exit(1)
end
else
print_error("Unknown command: " .. cmd)
io.stderr:write(HELP_TEXT)
os.exit(1)
end
end
end
print_info("===========================================")
print_info("All steps completed successfully!")
print_info("===========================================")
os.exit(0)
end
main()