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
|
--!The Automatic Cross-platform Build Tool
--
-- XMake is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--
-- XMake 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 Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with XMake;
-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
--
-- Copyright (C) 2009 - 2015, ruki All rights reserved.
--
-- @author ruki
-- @file config.lua
--
-- define module: config
local config = config or {}
-- load modules
local io = require("base/io")
local os = require("base/os")
local utils = require("base/utils")
local option = require("base/option")
local global = require("base/global")
-- make configure for the current target
function config._make(configs)
-- check
assert(configs and configs.plat and configs.arch and configs._PLATS)
-- the options
local options = xmake._OPTIONS
assert(options)
-- the configs for platform
local configs_plat = configs._PLATS[configs.plat]
assert(configs_plat)
-- the configs for architecture
local configs_arch = configs_plat[configs.arch]
assert(configs_arch)
-- init current target configure
local current = {plat = configs.plat, arch = configs.arch, __rebuild = configs.__rebuild}
-- get configs from the global configure first
if global._CURRENT then
for k, v in pairs(global._CURRENT) do
current[k] = v
end
end
-- get configs from all targets
for k, v in pairs(configs_arch) do
if type(k) == "string" and not k:find("^_%u+") then
current[k] = v
end
end
-- get configs from the current target
if configs_arch._TARGETS and current.target ~= "all" then
-- get the target config
local target_config = configs_arch._TARGETS[current.target]
if target_config then
-- merge it
for k, v in pairs(target_config) do
current[k] = v
end
end
end
-- ok?
return current
end
-- get the configure file
function config._file()
-- get it
return config.directory() .. "/xmake.conf"
end
-- need configure?
function config._need(name)
return name and
name ~= "plat" and
name ~= "arch" and
name ~= "target" and
name ~= "file" and
name ~= "project" and
name ~= "verbose" and
name ~= "clean"
end
-- get the current target scope
function config._target()
-- the options
local options = xmake._OPTIONS
assert(options)
-- check
local configs = config._CONFIGS
assert(configs and configs.plat and configs.arch)
-- the target name
local name = options.target or options._DEFAULTS.target
assert(name and type(name) == "string")
-- init the configs for platform
configs._PLATS = configs._PLATS or {}
configs._PLATS[configs.plat] = configs._PLATS[configs.plat] or {}
local configs_plat = configs._PLATS[configs.plat]
-- init the configs for architecture
configs_plat[configs.arch] = configs_plat[configs.arch] or {}
local configs_arch = configs_plat[configs.arch]
-- init targets
configs_arch._TARGETS = configs_arch._TARGETS or {}
if name ~= "all" then
configs_arch._TARGETS[name] = configs_arch._TARGETS[name] or {}
end
-- for all targets?
if name == "all" then
return configs_arch
elseif configs_arch._TARGETS then
-- get it
return configs_arch._TARGETS[name]
end
end
-- get the given configure from the current
function config.get(name)
-- the configure has been not loaded
if not config._CURRENT then return end
-- the value
local value = config._CURRENT[name]
if type(value) == "string" and value == "auto" then
value = nil
end
-- get it
return value
end
-- set the given configure to the current
function config.set(name, value)
-- check
assert(config._CURRENT and config._CONFIGS and name)
-- set platform or rebuild?
if name == "plat" or name == "__rebuild" then
config._CONFIGS[name] = value
config._CURRENT[name] = value
-- set architecture?
elseif name == "arch" then
-- the configs
local configs = config._CONFIGS
if configs.arch == "auto" then
-- the configs for platform
local configs_plat = configs._PLATS[configs.plat]
assert(configs_plat)
-- update configs of the auto architecture
configs_plat[value] = configs_plat["auto"]
configs_plat["auto"] = nil
end
config._CONFIGS.arch = value
config._CURRENT.arch = value
-- set others
else
-- get the current target
local target = config._target()
assert(target)
-- set it to the current target configure for saving to file
target[name] = value
-- set it to the current configure
config._CURRENT[name] = value
end
end
-- get the configure directory
function config.directory()
-- the directory
local dir = xmake._PROJECT_DIR .. "/.xmake"
-- create it directly first if not exists
if not os.isdir(dir) then
assert(os.mkdir(dir))
end
-- get it
return dir
end
-- save xmake.conf
function config.save()
-- the configs
local configs = config._CONFIGS
assert(configs)
-- save to the configure file
return io.save(config._file(), configs)
end
function config.load()
-- the options
local options = xmake._OPTIONS
assert(options)
-- check
assert(option._MENU)
assert(option._MENU.config)
-- the target name
local name = options.target or options._DEFAULTS.target
if not name then
return "no given target name!"
end
-- does not clean the cached configure?
if not options.clean then
-- load and execute the xmake.xconf
local filepath = config._file()
if os.isfile(filepath) then
-- load the configure file
local configs, errors = io.load(filepath)
-- exists local configures?
if configs then
-- save configs
config._CONFIGS = configs
-- make the current target configs
local current = config._make(configs)
-- clear configs and mark as "rebuild" and "reconfig" if the host has been changed
if current and current.host ~= xmake._HOST then
-- clear configs and mark as "rebuild"
config._CONFIGS = { __rebuild = true }
-- mark as "reconfig" if the current action is not "config"
if options._ACTION ~= "config" then
config._RECONFIG = true
end
end
-- clear configs and mark as "rebuild" if the plat has been changed
if current and current.plat and options.plat and current.plat ~= options.plat then
-- clear architecture and mark as "rebuild"
configs.arch = nil
configs.__rebuild = true
end
-- mark as "rebuild" if the arch has been changed
if current and current.arch and options.arch and current.arch ~= options.arch then
-- mark as "rebuild"
configs.__rebuild = true
end
-- mark as "rebuild" if the mode has been changed
if current and current.mode and options.mode and current.mode ~= options.mode then
-- mark as "rebuild"
configs.__rebuild = true
end
elseif errors then
-- error
utils.error(errors)
end
end
end
-- init configs if not exists
if not config._CONFIGS then
-- clear configs and mark as "rebuild"
config._CONFIGS = { __rebuild = true }
-- mark as "reconfig" if the current action is not "config"
if options._ACTION ~= "config" then
config._RECONFIG = true
end
end
-- the configs
local configs = config._CONFIGS
-- mark as "rebuild" if clean the cached configure
if options.clean then
configs.__rebuild = true
end
-- init the platform
configs.plat = options.plat or configs.plat or options._DEFAULTS.plat or option.defaults("config").plat
assert(configs.plat)
-- init the architecture
configs.arch = options.arch or configs.arch or options._DEFAULTS.arch or option.defaults("config").arch
assert(configs.arch)
-- get the current target scope
local target = config._target()
assert(target and type(target) == "table")
-- merge xmake._OPTIONS to target
if options._ACTION == "config" then
for k, v in pairs(options) do
-- check
assert(type(k) == "string")
-- skip some options
if not k:startswith("_") and config._need(k) then
-- save the option to the target
target[k] = v
end
end
end
-- merge the default configure options to target
local defaults = nil
if config._RECONFIG then defaults = option.defaults("config")
elseif options._ACTION == "config" then defaults = options._DEFAULTS
end
if defaults then
for k, v in pairs(defaults) do
-- check
assert(type(k) == "string")
-- skip some options
if config._need(k) then
-- save the default option to the target
if nil == target[k] then
target[k] = v
end
end
end
end
-- make the current configure
config._CURRENT = config._make(configs)
end
-- clear up and remove all auto values
function config.clearup()
-- clear up the current configure
local current = config._CURRENT
if current then
for k, v in pairs(current) do
if type(v) == "string" and v == "auto" then
current[k] = nil
end
end
end
-- clear up the current target configure
local target = config._target()
if target then
for k, v in pairs(target) do
if type(v) == "string" and v == "auto" then
target[k] = nil
end
end
end
end
-- reload configure
function config.reload()
-- clear the old configure
config._CURRENT = nil
config._CONFIGS = nil
-- load it
return config.load()
end
-- dump the current configure
function config.dump()
-- check
assert(config._CURRENT)
-- dump
utils.dump(config._CURRENT, "__%w*", "configure")
end
-- return module: config
return config
|