-
Notifications
You must be signed in to change notification settings - Fork 25
/
configure.lua
502 lines (426 loc) · 11.8 KB
/
configure.lua
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
--[[@GROUP Configuration@END]]--
--[[@FUNCTION
TODO
@END]]--
function NewConfig(on_configured_callback)
local config = {}
config.OnConfigured = function(self)
return true
end
if on_configured_callback then config.OnConfigured = on_configured_callback end
config.options = {}
config.settings = NewSettings()
config.NewSettings = function(self)
local s = NewSettings()
for _,v in pairs(self.options) do
v:Apply(s)
end
return s
end
config.Add = function(self, o)
table.insert(self.options, o)
self[o.name] = o
end
config.Print = function(self)
for k,v in pairs(self.options) do
print(v:FormatDisplay())
end
end
config.Save = function(self, filename)
print("saved configuration to '"..filename.."'")
local file = io.open(filename, "w")
-- Define a little helper function to save options
local saver = {}
saver.file = file
saver.line = function(self, str)
self.file:write(str .. "\n")
end
saver.option = function(self, option, name)
local valuestr = "no"
if type(option[name]) == type(0) then
valuestr = option[name]
elseif type(option[name]) == type(true) then
valuestr = "false"
if option[name] then
valuestr = "true"
end
elseif type(option[name]) == type("") then
valuestr = "'"..option[name].."'"
else
error("option "..name.." have a value of type ".. type(option[name]).." that can't be saved")
end
self.file:write(option.name.."."..name.." = ".. valuestr.."\n")
end
-- Save all the options
for k,v in pairs(self.options) do
v:Save(saver)
end
file:close()
end
config.Load = function(self, filename)
local options_func = loadfile(filename)
local options_table = {}
if not options_func then
print("auto configuration")
self:Config(filename)
options_func = loadfile(filename)
end
if options_func then
-- Setup the options tables
for k,v in pairs(self.options) do
options_table[v.name] = {}
end
setfenv(options_func, options_table)
-- this is to make sure that we get nice error messages when
-- someone sets an option that isn't valid.
local mt = {}
mt.__index = function(t, key)
local v = rawget(t, key)
if v ~= nil then return v end
error("there is no configuration option named '" .. key .. "'")
end
setmetatable(options_table, mt)
-- Process the options
options_func()
-- Copy the options
for k,v in pairs(self.options) do
if options_table[v.name] then
for k2,v2 in pairs(options_table[v.name]) do
v[k2] = v2
end
v.auto_detected = false
end
end
else
print("error: no '"..filename.."' found")
print("")
print("run 'bam config' to generate")
print("run 'bam config help' for configuration options")
print("")
os.exit(1)
end
end
config.Config = function(self, filename)
print("")
print("configuration:")
if _bam_targets[1] == "print" then
self:Load(filename)
self:Print()
print("")
print("notes:")
self:OnConfigured()
print("")
else
self:Autodetect()
print("")
print("notes:")
if self:OnConfigured() then
self:Save(filename)
end
print("")
end
end
config.Autodetect = function(self)
for k,v in pairs(self.options) do
v:Check(self.settings)
print(v:FormatDisplay())
self[v.name] = v
end
end
config.PrintHelp = function(self)
print("options:")
for k,v in pairs(self.options) do
if v.PrintHelp then
v:PrintHelp()
end
end
end
config.Finalize = function(self, filename)
if _bam_targets[0] == "config" then
if _bam_targets[1] == "help" then
self:PrintHelp()
os.exit(0)
end
self:Config(filename)
os.exit(0)
end
self:Load(filename)
bam_update_globalstamp(filename)
end
return config
end
-- Helper functions --------------------------------------
function DefaultOptionDisplay(option)
if not option.value then return "no" end
if option.value == 1 or option.value == true then return "yes" end
return option.value
end
function IsNegativeTerm(s)
if s == "no" then return true end
if s == "false" then return true end
if s == "off" then return true end
if s == "disable" then return true end
if s == "0" then return true end
return false
end
function IsPositiveTerm(s)
if s == "yes" then return true end
if s == "true" then return true end
if s == "on" then return true end
if s == "enable" then return true end
if s == "1" then return true end
return false
end
function MakeOption(name, value, check, save, display, printhelp)
local o = {}
o.name = name
o.value = value
o.Check = check
o.Save = save
o.auto_detected = true
o.FormatDisplay = function(self)
local a = "SET"
if self.auto_detected then a = "AUTO" end
return string.format("%-5s %-20s %s", a, self.name, self:Display())
end
o.Display = display
o.PrintHelp = printhelp
if o.Display == nil then o.Display = DefaultOptionDisplay end
return o
end
-- Test Compile C --------------------------------------
function OptTestCompileC(name, source, compileoptions, desc)
local check = function(option, settings)
option.value = false
if ScriptArgs[option.name] then
if IsNegativeTerm(ScriptArgs[option.name]) then
option.value = false
elseif IsPositiveTerm(ScriptArgs[option.name]) then
option.value = true
else
error(ScriptArgs[option.name].." is not a valid value for option "..option.name)
end
option.auto_detected = false
else
if CTestCompile(settings, option.source, option.compileoptions) then
option.value = true
end
end
end
local save = function(option, output)
output:option(option, "value")
end
local printhelp = function(option)
print("\t"..option.name.."=on|off")
if option.desc then print("\t\t"..option.desc) end
end
local o = MakeOption(name, false, check, save, nil, printhelp)
o.desc = desc
o.source = source
o.compileoptions = compileoptions
return o
end
-- OptToggle --------------------------------------
function OptToggle(name, default_value, desc)
local check = function(option, settings)
if ScriptArgs[option.name] then
if IsNegativeTerm(ScriptArgs[option.name]) then
option.value = false
elseif IsPositiveTerm(ScriptArgs[option.name]) then
option.value = true
else
error(ScriptArgs[option.name].." is not a valid value for option "..option.name)
end
end
end
local save = function(option, output)
output:option(option, "value")
end
local printhelp = function(option)
print("\t"..option.name.."=on|off")
if option.desc then print("\t\t"..option.desc) end
end
local o = MakeOption(name, default_value, check, save, nil, printhelp)
o.desc = desc
return o
end
-- OptInteger --------------------------------------
function OptInteger(name, default_value, desc)
local check = function(option, settings)
if ScriptArgs[option.name] then
option.value = tonumber(ScriptArgs[option.name])
end
end
local save = function(option, output)
output:option(option, "value")
end
local printhelp = function(option)
print("\t"..option.name.."=N")
if option.desc then print("\t\t"..option.desc) end
end
local o = MakeOption(name, default_value, check, save, nil, printhelp)
o.desc = desc
return o
end
-- OptString --------------------------------------
function OptString(name, default_value, desc)
local check = function(option, settings)
if ScriptArgs[option.name] then
option.value = ScriptArgs[option.name]
end
end
local save = function(option, output)
output:option(option, "value")
end
local printhelp = function(option)
print("\t"..option.name.."=STRING")
if option.desc then print("\t\t"..option.desc) end
end
local o = MakeOption(name, default_value, check, save, nil, printhelp)
o.desc = desc
return o
end
-- Find Compiler --------------------------------------
--[[@FUNCTION
TODO
@END]]--
function OptCCompiler(name, default_driver, default_c, default_cxx, desc)
local check = function(option, settings)
if ScriptArgs[option.name] then
-- set compile driver
option.driver = ScriptArgs[option.name]
-- set c compiler
if ScriptArgs[option.name..".c"] then
option.c_compiler = ScriptArgs[option.name..".c"]
end
-- set c+= compiler
if ScriptArgs[option.name..".cxx"] then
option.cxx_compiler = ScriptArgs[option.name..".cxx"]
end
option.auto_detected = false
elseif option.driver then
-- no need todo anything if we have a driver
-- TODO: test if we can find the compiler
else
if ExecuteSilent("cl") == 0 then
option.driver = "cl"
elseif ExecuteSilent("g++ -v") == 0 then
option.driver = "gcc"
else
error("no c/c++ compiler found")
end
end
--setup_compiler(option.value)
end
local apply = function(option, settings)
if option.driver == "cl" then
SetDriversCL(settings)
elseif option.driver == "gcc" then
SetDriversGCC(settings)
elseif option.driver == "clang" then
SetDriversClang(settings)
else
error(option.driver.." is not a known c/c++ compile driver")
end
if option.c_compiler then settings.cc.c_compiler = option.c_compiler end
if option.cxx_compiler then settings.cc.cxx_compiler = option.cxx_compiler end
end
local save = function(option, output)
output:option(option, "driver")
output:option(option, "c_compiler")
output:option(option, "cxx_compiler")
end
local printhelp = function(option)
local a = ""
if option.desc then a = "for "..option.desc end
print("\t"..option.name.."=gcc|cl|clang")
print("\t\twhat c/c++ compile driver to use"..a)
print("\t"..option.name..".c=FILENAME")
print("\t\twhat c compiler executable to use"..a)
print("\t"..option.name..".cxx=FILENAME")
print("\t\twhat c++ compiler executable to use"..a)
end
local display = function(option)
local s = option.driver
if option.c_compiler then s = s .. " c="..option.c_compiler end
if option.cxx_compiler then s = s .. " cxx="..option.cxx_compiler end
return s
end
local o = MakeOption(name, nil, check, save, display, printhelp)
o.desc = desc
o.driver = false
o.c_compiler = false
o.cxx_compiler = false
if default_driver then o.driver = default_driver end
if default_c then o.c_compiler = default_c end
if default_cxx then o.cxx_compiler = default_cxx end
o.Apply = apply
return o
end
-- Option Library --------------------------------------
--[[@FUNCTION
TODO
@END]]--
function OptLibrary(name, header, desc)
local check = function(option, settings)
option.value = false
option.include_path = false
local function check_compile_include(filename, paths)
if CTestCompile(settings, "#include <" .. filename .. ">\nint main(){return 0;}", "") then
return ""
end
for k,v in pairs(paths) do
if CTestCompile(settings, "#include <" .. filename .. ">\nint main(){return 0;}", "-I"..v) then
return v
end
end
return false
end
if ScriptArgs[option.name] then
if IsNegativeTerm(ScriptArgs[option.name]) then
option.value = false
elseif ScriptArgs[option.name] == "system" then
option.value = true
else
option.value = true
option.include_path = ScriptArgs[option.name]
end
option.auto_detected = false
else
option.include_path = check_compile_include(option.header, {})
if option.include_path == false then
if option.required then
print(name.." library not found and is required")
error("required library not found")
end
else
option.value = true
option.include_path = false
end
end
end
local save = function(option, output)
output:option(option, "value")
output:option(option, "include_path")
end
local display = function(option)
if option.value then
if option.include_path then
return option.include_path
else
return "(in system path)"
end
else
return "not found"
end
end
local printhelp = function(option)
print("\t"..option.name.."=disable|system|PATH")
if option.desc then print("\t\t"..option.desc) end
end
local o = MakeOption(name, false, check, save, display, printhelp)
o.include_path = false
o.header = header
o.desc = desc
return o
end