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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file scopeinfo.lua
--
-- define module
local scopeinfo = scopeinfo or {}
local _instance = _instance or {}
-- load modules
local io = require("base/io")
local os = require("base/os")
local path = require("base/path")
local table = require("base/table")
local utils = require("base/utils")
local deprecated = require("base/deprecated")
-- new an instance
function _instance.new(kind, info, opt)
opt = opt or {}
local instance = table.inherit(_instance)
instance._KIND = kind or "root"
instance._INFO = info
instance._INTERPRETER = opt.interpreter
instance._DEDUPLICATE = opt.deduplicate
instance._ENABLE_FILTER = opt.enable_filter
return instance
end
-- get apis
function _instance:_apis()
local interp = self:interpreter()
if interp then
return interp:api_definitions()
end
end
-- get the given api type
function _instance:_api_type(name)
local apis = self:_apis()
if apis then
return apis[self:kind() .. '.' .. name]
end
end
-- handle the api values
function _instance:_api_handle(name, values)
local interp = self:interpreter()
if interp then
-- remove repeat first for each slice with deleted item (__remove_xxx)
if self._DEDUPLICATE and not table.is_dictionary(values) then
local policy = interp:deduplication_policy(name)
if policy ~= false then
local unique_func = policy == "toleft" and table.reverse_unique or table.unique
values = unique_func(values, function (v) return type(v) == "string" and v:startswith("__remove_") end)
end
end
-- filter values
if self._ENABLE_FILTER then
values = interp:_filter(values)
end
end
-- unwrap it if be only one
return table.unwrap(values)
end
-- save api source info, e.g. call api() in sourcefile:linenumber
function _instance:_api_save_sourceinfo_to_scope(scope, apiname, values)
-- save api source info, e.g. call api() in sourcefile:linenumber
local sourceinfo = debug.getinfo(5, "Sl")
if sourceinfo then
scope["__sourceinfo_" .. apiname] = scope["__sourceinfo_" .. apiname] or {}
local sourcescope = scope["__sourceinfo_" .. apiname]
for _, value in ipairs(values) do
if type(value) == "string" then
sourcescope[value] = {file = sourceinfo.short_src or sourceinfo.source, line = sourceinfo.currentline}
end
end
end
end
-- set the api values to the scope info
function _instance:_api_set_values(name, ...)
-- get the scope info
local scope = self._INFO
-- get extra config
local values = {...}
local extra_config = values[#values]
if table.is_dictionary(extra_config) then
table.remove(values)
else
extra_config = nil
end
-- @note we need to mark table value as meta object to avoid wrap/unwrap
-- if these values cannot be expanded, especially when there is only one value
--
-- e.g. target:set("shflags", {"-Wl,-exported_symbols_list", exportfile}, {force = true, expand = false})
if extra_config and extra_config.expand == false then
for _, value in ipairs(values) do
table.wrap_lock(value)
end
else
-- expand values
values = table.join(table.unpack(values))
end
-- handle values
local handled_values = self:_api_handle(name, values)
-- save values
if type(handled_values) == "table" and table.empty(handled_values) then
-- set("xx", nil)? remove it
scope[name] = nil
else
scope[name] = handled_values
end
-- save extra config
if extra_config then
scope["__extra_" .. name] = scope["__extra_" .. name] or {}
local extrascope = scope["__extra_" .. name]
for _, value in ipairs(values) do
extrascope[value] = extra_config
end
end
end
-- add the api values to the scope info
function _instance:_api_add_values(name, ...)
-- get the scope info
local scope = self._INFO
-- get extra config
local values = {...}
local extra_config = values[#values]
if table.is_dictionary(extra_config) then
table.remove(values)
else
extra_config = nil
end
-- @note we need to mark table value as meta object to avoid wrap/unwrap
-- if these values cannot be expanded, especially when there is only one value
--
-- e.g. target:add("shflags", {"-Wl,-exported_symbols_list", exportfile}, {force = true, expand = false})
if extra_config and extra_config.expand == false then
for _, value in ipairs(values) do
table.wrap_lock(value)
end
else
-- expand values
values = table.join(table.unpack(values))
end
-- save values
scope[name] = self:_api_handle(name, table.join2(table.wrap(scope[name]), values))
-- save extra config
if extra_config then
scope["__extra_" .. name] = scope["__extra_" .. name] or {}
local extrascope = scope["__extra_" .. name]
for _, value in ipairs(values) do
extrascope[value] = extra_config
end
end
end
-- set the api groups to the scope info
function _instance:_api_set_groups(name, ...)
-- get the scope info
local scope = self._INFO
-- get extra config
local values = {...}
local extra_config = values[#values]
if table.is_dictionary(extra_config) then
table.remove(values)
else
extra_config = nil
end
-- expand values
values = table.join(table.unpack(values))
-- save values
table.wrap_lock(values)
scope[name] = values
scope[name] = self:_api_handle(name, scope[name])
-- save extra config
if extra_config then
scope["__extra_" .. name] = scope["__extra_" .. name] or {}
local extrascope = scope["__extra_" .. name]
local key = table.concat(values, "_")
extrascope[key] = extra_config
end
end
-- add the api groups to the scope info
function _instance:_api_add_groups(name, ...)
-- get the scope info
local scope = self._INFO
-- get extra config
local values = {...}
local extra_config = values[#values]
if table.is_dictionary(extra_config) then
table.remove(values)
else
extra_config = nil
end
-- expand values
values = table.join(table.unpack(values))
-- save values
--
-- @note maybe scope[name] has been unwrapped, we need wrap it first
-- https://github.com/xmake-io/xmake/issues/4428
scope[name] = table.wrap(scope[name])
table.wrap_lock(values)
table.insert(scope[name], values)
scope[name] = self:_api_handle(name, scope[name])
-- save extra config
if extra_config then
scope["__extra_" .. name] = scope["__extra_" .. name] or {}
local extrascope = scope["__extra_" .. name]
local key = table.concat(values, "_")
extrascope[key] = extra_config
end
end
-- set the api key-values to the scope info
function _instance:_api_set_keyvalues(name, key, ...)
-- get the scope info
local scope = self._INFO
-- get extra config
local values = {...}
local extra_config = values[#values]
if table.is_dictionary(extra_config) then
table.remove(values)
else
extra_config = nil
end
-- save values to "name"
scope[name] = scope[name] or {}
scope[name][key] = self:_api_handle(name, values)
-- save values to "name.key"
local name_key = name .. "." .. key
scope[name_key] = scope[name][key]
-- fix override attributes
scope["__override_" .. name] = false
scope["__override_" .. name_key] = true
-- save extra config
if extra_config then
scope["__extra_" .. name_key] = scope["__extra_" .. name_key] or {}
local extrascope = scope["__extra_" .. name_key]
for _, value in ipairs(values) do
extrascope[value] = extra_config
end
end
end
-- add the api key-values to the scope info
function _instance:_api_add_keyvalues(name, key, ...)
-- get the scope info
local scope = self._INFO
-- get extra config
local values = {...}
local extra_config = values[#values]
if table.is_dictionary(extra_config) then
table.remove(values)
else
extra_config = nil
end
-- save values to "name"
scope[name] = scope[name] or {}
if scope[name][key] == nil then
scope[name][key] = self:_api_handle(name, values)
else
scope[name][key] = self:_api_handle(name, table.join2(table.wrap(scope[name][key]), values))
end
-- save values to "name.key"
local name_key = name .. "." .. key
scope[name_key] = scope[name][key]
-- save extra config
if extra_config then
scope["__extra_" .. name_key] = scope["__extra_" .. name_key] or {}
local extrascope = scope["__extra_" .. name_key]
for _, value in ipairs(values) do
extrascope[value] = extra_config
end
end
end
-- set the api dictionary to the scope info
function _instance:_api_set_dictionary(name, dict_or_key, value, extra_config)
-- get the scope info
local scope = self._INFO
-- check
if type(dict_or_key) == "table" then
local dict = {}
for k, v in pairs(dict_or_key) do
dict[k] = self:_api_handle(name, v)
end
scope[name] = dict
elseif type(dict_or_key) == "string" and value ~= nil then
scope[name] = {[dict_or_key] = self:_api_handle(name, value)}
-- save extra config
if extra_config and table.is_dictionary(extra_config) then
scope["__extra_" .. name] = scope["__extra_" .. name] or {}
local extrascope = scope["__extra_" .. name]
extrascope[dict_or_key] = extra_config
end
else
-- error
os.raise("%s:set(%s, ...): invalid value type!", self:kind(), name, type(dict))
end
end
-- add the api dictionary to the scope info
function _instance:_api_add_dictionary(name, dict_or_key, value, extra_config)
local scope = self._INFO
scope[name] = scope[name] or {}
if type(dict_or_key) == "table" then
local dict = {}
for k, v in pairs(dict_or_key) do
dict[k] = self:_api_handle(name, v)
end
table.join2(scope[name], dict)
elseif type(dict_or_key) == "string" and value ~= nil then
scope[name][dict_or_key] = self:_api_handle(name, value)
-- save extra config
if extra_config and table.is_dictionary(extra_config) then
scope["__extra_" .. name] = scope["__extra_" .. name] or {}
local extrascope = scope["__extra_" .. name]
extrascope[dict_or_key] = extra_config
end
else
-- error
os.raise("%s:add(%s, ...): invalid value type!", self:kind(), name, type(dict))
end
end
-- set the api paths to the scope info
function _instance:_api_set_paths(name, ...)
-- get the scope info
local scope = self._INFO
-- get interpreter
local interp = self:interpreter()
-- get extra config
local values = {...}
local extra_config = values[#values]
if table.is_dictionary(extra_config) then
table.remove(values)
else
extra_config = nil
end
-- expand values
values = table.join(table.unpack(values))
-- translate paths
local paths = interp:_api_translate_paths(values, "set_" .. name, 5)
-- save values
scope[name] = self:_api_handle(name, paths)
-- save extra config
if extra_config then
scope["__extra_" .. name] = scope["__extra_" .. name] or {}
local extrascope = scope["__extra_" .. name]
for _, value in ipairs(paths) do
extrascope[value] = extra_config
end
end
-- save api source info, e.g. call api() in sourcefile:linenumber
self:_api_save_sourceinfo_to_scope(scope, name, paths)
end
-- add the api paths to the scope info
function _instance:_api_add_paths(name, ...)
-- get the scope info
local scope = self._INFO
-- get interpreter
local interp = self:interpreter()
-- get extra config
local values = {...}
local extra_config = values[#values]
if table.is_dictionary(extra_config) then
table.remove(values)
else
extra_config = nil
end
-- expand values
values = table.join(table.unpack(values))
-- translate paths
local paths = interp:_api_translate_paths(values, "add_" .. name, 5)
-- save values
scope[name] = self:_api_handle(name, table.join2(table.wrap(scope[name]), paths))
-- save extra config
if extra_config then
scope["__extra_" .. name] = scope["__extra_" .. name] or {}
local extrascope = scope["__extra_" .. name]
for _, value in ipairs(paths) do
extrascope[value] = extra_config
end
end
-- save api source info, e.g. call api() in sourcefile:linenumber
self:_api_save_sourceinfo_to_scope(scope, name, paths)
end
-- remove the api paths to the scope info (deprecated)
function _instance:_api_del_paths(name, ...)
-- get the scope info
local scope = self._INFO
-- get interpreter
local interp = self:interpreter()
-- expand values
values = table.join(...)
-- it has been marked as deprecated
deprecated.add("remove_" .. name .. "(%s)", "del_" .. name .. "(%s)", table.concat(values, ", "), table.concat(values, ", "))
-- translate paths
local paths = interp:_api_translate_paths(values, "del_" .. name, 5)
-- mark these paths as deleted
local paths_deleted = {}
for _, pathname in ipairs(paths) do
table.insert(paths_deleted, "__remove_" .. pathname)
end
-- save values
scope[name] = self:_api_handle(name, table.join2(table.wrap(scope[name]), paths_deleted))
-- save api source info, e.g. call api() in sourcefile:linenumber
self:_api_save_sourceinfo_to_scope(scope, name, paths)
end
-- remove the api paths to the scope info
function _instance:_api_remove_paths(name, ...)
-- get the scope info
local scope = self._INFO
-- get interpreter
local interp = self:interpreter()
-- expand values
values = table.join(...)
-- translate paths
local paths = interp:_api_translate_paths(values, "remove_" .. name, 5)
-- mark these paths as removed
local paths_removed = {}
for _, pathname in ipairs(paths) do
table.insert(paths_removed, "__remove_" .. pathname)
end
-- save values
scope[name] = self:_api_handle(name, table.join2(table.wrap(scope[name]), paths_removed))
-- save api source info, e.g. call api() in sourcefile:linenumber
self:_api_save_sourceinfo_to_scope(scope, name, paths)
end
-- get the scope kind
function _instance:kind()
return self._KIND
end
-- is root scope?
function _instance:is_root()
return self:kind() == "root"
end
-- get all scope info
function _instance:info()
return self._INFO
end
-- get interpreter
function _instance:interpreter()
return self._INTERPRETER
end
-- get the scope info from the given name
function _instance:get(name)
return self._INFO[name]
end
-- set the value to the scope info
function _instance:set(name, value)
self._INFO[name] = value
end
-- set the api values to the scope info
function _instance:apival_set(name, ...)
if type(name) == "string" then
local api_type = self:_api_type("set_" .. name)
if api_type then
local set_xxx = self["_api_set_" .. api_type]
if set_xxx then
set_xxx(self, name, ...)
else
os.raise("unknown apitype(%s) for %s:set(%s, ...)", api_type, self:kind(), name)
end
else
-- unknown api values? only set values
self:_api_set_values(name, ...)
end
-- set array, e.g. set({{links = ..}, {links = ..}})
elseif table.is_array(name) then
local array = name
for _, dict in ipairs(array) do
for k, v in pairs(dict) do
self:apival_set(k, table.unpack(table.wrap(v)))
end
end
-- set dictionary, e.g. set({links = ..})
elseif table.is_dictionary(name) then
local dict = name
for k, v in pairs(dict) do
self:apival_set(k, table.unpack(table.wrap(v)))
end
elseif name ~= nil then
os.raise("unknown type(%s) for %s:set(%s, ...)", type(name), self:kind(), name)
end
end
-- add the api values to the scope info
function _instance:apival_add(name, ...)
if type(name) == "string" then
local api_type = self:_api_type("add_" .. name)
if api_type then
local add_xxx = self["_api_add_" .. api_type]
if add_xxx then
add_xxx(self, name, ...)
else
os.raise("unknown apitype(%s) for %s:add(%s, ...)", api_type, self:kind(), name)
end
else
-- unknown api values? only add values
self:_api_add_values(name, ...)
end
-- add array, e.g. add({{links = ..}, {links = ..}})
elseif table.is_array(name) then
local array = name
for _, dict in ipairs(array) do
for k, v in pairs(dict) do
self:apival_add(k, table.unpack(table.wrap(v)))
end
end
-- add dictionary, e.g. add({links = ..})
elseif table.is_dictionary(name) then
local dict = name
for k, v in pairs(dict) do
self:apival_add(k, table.unpack(table.wrap(v)))
end
elseif name ~= nil then
os.raise("unknown type(%s) for %s:add(%s, ...)", type(name), self:kind(), name)
end
end
-- remove the api values to the scope info (deprecated)
function _instance:apival_del(name, ...)
if type(name) == "string" then
local api_type = self:_api_type("del_" .. name)
if api_type then
local del_xxx = self["_api_remove_" .. api_type]
if del_xxx then
del_xxx(self, name, ...)
else
os.raise("unknown apitype(%s) for %s:del(%s, ...)", api_type, self:kind(), name)
end
else
os.raise("unknown api(%s) for %s:del(%s, ...)", name, self:kind(), name)
end
elseif name ~= nil then
-- TODO
os.raise("cannot support to remove a dictionary!")
end
end
-- remove the api values to the scope info
function _instance:apival_remove(name, ...)
if type(name) == "string" then
local api_type = self:_api_type("remove_" .. name)
if api_type then
local remove_xxx = self["_api_remove_" .. api_type]
if remove_xxx then
remove_xxx(self, name, ...)
else
os.raise("unknown apitype(%s) for %s:remove(%s, ...)", api_type, self:kind(), name)
end
else
os.raise("unknown api(%s) for %s:remove(%s, ...)", name, self:kind(), name)
end
elseif name ~= nil then
-- TODO
os.raise("cannot support to remove a dictionary!")
end
end
-- get the extra configuration
--
-- e.g.
--
-- add_includedirs("inc", {public = true})
--
-- function (target)
-- _instance:extraconf("includedirs", "inc", "public") -> true
-- _instance:extraconf("includedirs", "inc") -> {public = true}
-- _instance:extraconf("includedirs") -> {inc = {public = true}}
-- end
--
function _instance:extraconf(name, item, key)
-- get extra configurations
local extraconfs = self._EXTRACONFS
if not extraconfs then
extraconfs = {}
self._EXTRACONFS = extraconfs
end
-- get configuration
local extraconf = extraconfs[name]
if not extraconf then
extraconf = self:get("__extra_" .. name)
extraconfs[name] = extraconf
end
-- get configuration value
local value = extraconf
if item then
value = extraconf and extraconf[item] or nil
if value == nil and extraconf and type(item) == "table" then
value = extraconf[table.concat(item, "_")]
end
if value and key then
value = value[key]
end
end
return value
end
-- set the extra configuration
--
-- e.g.
--
-- add_includedirs("inc", {public = true})
--
-- function (target)
-- _instance:extraconf_set("includedirs", "inc", "public", true)
-- _instance:extraconf_set("includedirs", "inc", {public = true})
-- _instance:extraconf_set("includedirs", {inc = {public = true}})
-- end
--
function _instance:extraconf_set(name, item, key, value)
if key ~= nil then
local extraconf = self:get("__extra_" .. name) or {}
if value ~= nil then
extraconf[item] = extraconf[item] or {}
extraconf[item][key] = value
else
extraconf[item] = key
end
self:set("__extra_" .. name, extraconf)
else
self:set("__extra_" .. name, item)
end
end
-- get configuration source information of the given api item
--
-- e.g.
-- self:get("defines", "TEST")
-- - add_defines("TEST")
-- - src/xmake.lua:10
--
function _instance:sourceinfo(name, item)
return (self:get("__sourceinfo_" .. name) or {})[item]
end
-- clone a new instance from the current
function _instance:clone()
return _instance.new(self:kind(), table.clone(self:info(), 3), { -- @note we need do deep clone
interpreter = self:interpreter(),
deduplicate = self._DEDUPLICATE,
enable_filter = self._ENABLE_FILTER})
end
-- new a scope instance
function scopeinfo.new(...)
return _instance.new(...)
end
-- return module
return scopeinfo
|