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
|
--!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 language.lua
--
-- define module
local language = language or {}
local _instance = _instance or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
local utils = require("base/utils")
local table = require("base/table")
local interpreter = require("base/interpreter")
local sandbox = require("sandbox/sandbox")
local config = require("project/config")
local global = require("base/global")
-- new an instance
function _instance.new(name, info, rootdir)
local instance = table.inherit(_instance)
instance._NAME = name
instance._INFO = info
instance._ROOTDIR = rootdir
return instance
end
-- get the language configure
function _instance:get(name)
local info = self._INFO:info()
local value = info[name]
if value ~= nil then
return value
end
if self._g == nil and info.load ~= nil then
local ok, results = sandbox.load(info.load)
if not ok then
os.raise(results)
end
self._g = results
end
return self._g[name]
end
-- get the language menu
function _instance:menu()
return self._INFO:get("menu")
end
-- get the language name
function _instance:name()
return self._NAME
end
-- get the source extensions
function _instance:extensions()
if self._EXTENSIONS then
return self._EXTENSIONS
end
-- get extensions
local extensions = {}
for sourcekind, exts in pairs(self:sourcekinds()) do
for _, extension in ipairs(table.wrap(exts)) do
extensions[extension:lower()] = sourcekind
end
end
self._EXTENSIONS = extensions
return extensions
end
-- get the rules
function _instance:rules()
return self._INFO:get("rules")
end
-- get the source kinds
function _instance:sourcekinds()
return self._INFO:get("sourcekinds")
end
-- get the source flags
function _instance:sourceflags()
return self._INFO:get("sourceflags")
end
-- get the target kinds (targetkind => linkerkind)
--
-- e.g.
-- {binary = "ld", static = "ar", shared = "sh"}
--
function _instance:kinds()
return self._INFO:get("targetkinds")
end
-- get the target flags (targetkind => linkerflag)
--
-- e.g.
-- {binary = "ldflags", static = "arflags", shared = "shflags"}
--
function _instance:targetflags()
return self._INFO:get("targetflags")
end
-- get the mixing kinds for linker
--
-- e.g.
-- {"cc", "cxx"}
--
function _instance:mixingkinds()
return self._INFO:get("mixingkinds")
end
-- get the language kinds
function _instance:langkinds()
return self._INFO:get("langkinds")
end
-- get the name flags
function _instance:nameflags()
-- attempt to get it from cache first
if self._NAMEFLAGS then
return self._NAMEFLAGS
end
-- get nameflags
local results = {}
for targetkind, nameflags in pairs(table.wrap(self._INFO:get("nameflags"))) do
-- make tool info
local toolinfo = results[targetkind] or {}
for _, namedflag in ipairs(nameflags) do
-- split it by '.'
local splitinfo = namedflag:split('.', {plain = true})
assert(#splitinfo == 2)
-- get flag scope
local flagscope = splitinfo[1]
assert(flagscope)
-- get flag info
local flaginfo = splitinfo[2]:split(':')
-- get flag name
local flagname = flaginfo[1]
assert(flagname)
-- get check state
local checkstate = false
if #flaginfo == 2 and flaginfo[2] == "check" then
checkstate = true
end
-- insert this flag info
table.insert(toolinfo, {flagscope, flagname, checkstate})
end
-- save this tool info
results[targetkind] = toolinfo
end
-- cache this results
self._NAMEFLAGS = results
return results
end
-- the directory of language
function language._directory()
return path.join(os.programdir(), "languages")
end
-- the interpreter
function language._interpreter()
-- the interpreter has been initialized? return it directly
if language._INTERPRETER then
return language._INTERPRETER
end
-- init interpreter
local interp = interpreter.new()
assert(interp)
-- define apis
interp:api_define
{
values =
{
-- language.set_xxx
"language.set_mixingkinds"
-- language.add_xxx
, "language.add_rules"
}
, script =
{
-- language.on_xxx
"language.on_load"
, "language.on_check_main"
}
, dictionary =
{
-- language.set_xxx
"language.set_menu"
, "language.set_nameflags"
, "language.set_langkinds"
, "language.set_sourcekinds"
, "language.set_sourceflags"
, "language.set_targetkinds"
, "language.set_targetflags"
}
}
language._INTERPRETER = interp
return interp
end
-- load the language from the given name (c++, objc++, swift, golang, asm, ...)
function language.load(name)
-- load all languages
if not name then
if not language._LANGUAGES then
for _, name in ipairs(table.wrap(os.dirs(path.join(language._directory(), "*")))) do
local instance, errors = language.load(path.basename(name))
if not instance then
return nil, errors
end
end
end
return language._LANGUAGES
end
-- get it directly from cache dirst
language._LANGUAGES = language._LANGUAGES or {}
if language._LANGUAGES[name] then
return language._LANGUAGES[name]
end
-- find the language script path
local scriptpath = path.join(path.join(language._directory(), name), "xmake.lua")
if not os.isfile(scriptpath) then
return nil, string.format("the language %s not found!", name)
end
-- not exists?
if not scriptpath or not os.isfile(scriptpath) then
return nil, string.format("the language %s not found!", name)
end
-- get interpreter
local interp = language._interpreter()
-- load script
local ok, errors = interp:load(scriptpath)
if not ok then
return nil, errors
end
-- load language
local results, errors = interp:make("language", true, false)
if not results and os.isfile(scriptpath) then
return nil, errors
end
-- check the language name
if not results[name] then
return nil, string.format("the language %s not found!", name)
end
-- new an instance
local instance, errors = _instance.new(name, results[name], language._interpreter():rootdir())
if not instance then
return nil, errors
end
language._LANGUAGES[name] = instance
return instance
end
-- load the language from the given source kind: cc, cxx, mm, mxx, sc, gc, as ..
function language.load_sk(sourcekind)
-- load all languages
local languages, errors = language.load()
if not languages then
return nil, errors
end
-- make source kind as lower
sourcekind = sourcekind:lower()
-- get it directly from cache dirst
language._LANGUAGES_OF_SK = language._LANGUAGES_OF_SK or {}
if language._LANGUAGES_OF_SK[sourcekind] then
return language._LANGUAGES_OF_SK[sourcekind]
end
-- find language instance
local result = nil
for _, instance in pairs(languages) do
if instance:sourcekinds()[sourcekind] ~= nil then
result = instance
break
end
end
if not result then
return nil, string.format("unknown language sourcekind: %s", sourcekind)
end
language._LANGUAGES_OF_SK[sourcekind] = result
return result
end
-- load the language from the given source extension: .c, .cpp, .m, .mm, .swift, .go, .s ..
function language.load_ex(extension)
-- load all languages
local languages, errors = language.load()
if not languages then
return nil, errors
end
-- make source extension as lower
extension = extension:lower()
-- get it directly from cache dirst
language._LANGUAGES_OF_EX = language._LANGUAGES_OF_EX or {}
if language._LANGUAGES_OF_EX[extension] then
return language._LANGUAGES_OF_EX[extension]
end
-- find language instance
local result = nil
for _, instance in pairs(languages) do
if instance:extensions()[extension] ~= nil then
result = instance
break
end
end
if not result then
return nil, string.format("unknown language source extension: %s", extension)
end
language._LANGUAGES_OF_EX[extension] = result
return result
end
-- load the language apis
function language.apis()
local apis = language._APIS
if not apis then
local languages, errors = language.load()
if not languages then
os.raise(errors)
end
apis = {values = {}, groups = {}, paths = {}, custom = {}, dictionary = {}}
for name, instance in pairs(languages) do
local instance_apis = instance:get("apis")
if instance_apis then
table.join2(apis.values, table.wrap(instance_apis.values))
table.join2(apis.groups, table.wrap(instance_apis.groups))
table.join2(apis.paths, table.wrap(instance_apis.paths))
table.join2(apis.custom, table.wrap(instance_apis.custom))
table.join2(apis.dictionary, table.wrap(instance_apis.dictionary))
end
end
apis.values = table.unique(apis.values)
apis.groups = table.unique(apis.groups)
apis.paths = table.unique(apis.paths)
apis.custom = table.unique(apis.custom)
language._APIS = apis
end
return apis
end
-- get language source extensions
--
-- e.g.
--
-- {
-- [".c"] = cc
-- , [".cc"] = cxx
-- , [".cpp"] = cxx
-- , [".m"] = mm
-- , [".mm"] = mxx
-- , [".swift"] = sc
-- , [".go"] = gc
-- }
--
function language.extensions()
local extensions = language._EXTENSIONS
if not extensions then
local languages, errors = language.load()
if not languages then
os.raise(errors)
end
extensions = {}
for name, instance in pairs(languages) do
table.join2(extensions, instance:extensions())
end
language._EXTENSIONS = extensions
end
return extensions
end
-- get language source kinds
--
-- e.g.
--
-- {
-- cc = ".c"
-- , cxx = {".cc", ".cpp", ".cxx"}
-- , mm = ".m"
-- , mxx = ".mm"
-- , sc = ".swift"
-- , gc = ".go"
-- }
--
function language.sourcekinds()
local sourcekinds = language._SOURCEKINDS
if not sourcekinds then
local languages, errors = language.load()
if not languages then
os.raise(errors)
end
sourcekinds = {}
for name, instance in pairs(languages) do
table.join2(sourcekinds, instance:sourcekinds())
end
language._SOURCEKINDS = sourcekinds
end
return sourcekinds
end
-- get language source flags
--
-- e.g.
--
-- {
-- cc = {"cflags", "cxflags"}
-- , cxx = {"cxxflags", "cxflags"}
-- , ...
-- }
--
function language.sourceflags()
local sourceflags = language._SOURCEFLAGS
if not sourceflags then
local languages, errors = language.load()
if not languages then
os.raise(errors)
end
sourceflags = {}
for name, instance in pairs(languages) do
table.join2(sourceflags, instance:sourceflags())
end
language._SOURCEFLAGS = sourceflags
end
return sourceflags
end
-- get source kind of the source file name
function language.sourcekind_of(sourcefile)
-- get the source file extension
local extension = path.extension(sourcefile)
if not extension then
return nil, string.format("%s has not extension", sourcefile)
end
-- get extensions
local extensions = language.extensions()
-- get source kind from extension
local sourcekind = extensions[extension:lower()]
if not sourcekind then
return nil, string.format("%s is unknown extension", extension)
end
return sourcekind
end
-- get extension of the source kind
function language.extension_of(sourcekind)
local extension = table.wrap(language.sourcekinds()[sourcekind])[1]
if not extension then
return nil, string.format("%s is unknown source kind", sourcekind)
end
return extension
end
-- get linker infos(kind and flag) of the target kind and the source kinds
function language.linkerinfos_of(targetkind, sourcekinds)
-- load linkerinfos
local linkerinfos = language._LINKERINFOS
if not linkerinfos then
-- load all languages
local languages, errors = language.load()
if not languages then
return nil, errors
end
-- make linker infos
linkerinfos = {}
for name, instance in pairs(languages) do
for _, mixingkind in ipairs(table.wrap(instance:mixingkinds())) do
local targetflags = instance:targetflags()
for _targetkind, linkerkind in pairs(table.wrap(instance:kinds())) do
-- init linker info
linkerinfos[_targetkind] = linkerinfos[_targetkind] or {}
linkerinfos[_targetkind][linkerkind] = linkerinfos[_targetkind][linkerkind] or {}
local linkerinfo = linkerinfos[_targetkind][linkerkind]
-- sve linker info
local linkerflag = targetflags[_targetkind]
linkerinfo.linkerkind = linkerkind
if linkerflag then
linkerinfo.linkerflag = linkerflag
end
linkerinfo.mixingkinds = linkerinfo.mixingkinds or {}
linkerinfo.mixingkinds[mixingkind] = 1
linkerinfo.sourcecount = (linkerinfo.sourcecount or 0) + 1
end
end
end
language._LINKERINFOS = linkerinfos
end
-- find suitable linkers
local results = {}
for _, linkerinfo in pairs(table.wrap(linkerinfos[targetkind])) do
-- match all source kinds?
local count = 0
for _, sourcekind in ipairs(sourcekinds) do
count = count + (linkerinfo.mixingkinds[sourcekind] or 0)
end
if count == #sourcekinds then
table.insert(results, linkerinfo)
end
end
if #results > 0 then
-- sort it by most matches
table.sort(results, function(a, b) return a.sourcecount > b.sourcecount end)
return results
end
-- not suitable linker
return nil, string.format("no suitable linker for %s.{%s}", targetkind, table.concat(sourcekinds, ' '))
end
-- get language target kinds
--
-- e.g.
--
-- {
-- binary = {"ld", "gcld", "dcld"}
-- , static = {"ar", "gcar", "dcar"}
-- , shared = {"sh", "dcsh"}
-- }
--
function language.targetkinds()
local targetkinds = language._TARGETKINDS
if not targetkinds then
local languages, errors = language.load()
if not languages then
os.raise(errors)
end
targetkinds = {}
for name, instance in pairs(languages) do
for targetkind, linkerkind in pairs(table.wrap(instance:kinds())) do
targetkinds[targetkind] = targetkinds[targetkind] or {}
table.insert(targetkinds[targetkind], linkerkind)
end
end
for targetkind, linkerkinds in pairs(targetkinds) do
targetkinds[targetkind] = table.unique(linkerkinds)
end
language._TARGETKINDS = targetkinds
end
return targetkinds
end
-- get language kinds (langkind => sourcekind)
--
-- e.g.
--
-- {
-- c = "cc"
-- , cxx = "cxx"
-- , m = "mm"
-- , mxx = "mxx"
-- , swift = "sc"
-- , go = "gc"
-- , as = "as"
-- , rust = "rc"
-- , d = "dc"
-- }
--
function language.langkinds()
local langkinds = language._LANGKINDS
if not langkinds then
local languages, errors = language.load()
if not languages then
os.raise(errors)
end
langkinds = {}
for name, instance in pairs(languages) do
table.join2(langkinds, instance:langkinds())
end
language._LANGKINDS = langkinds
end
return langkinds
end
return language
|