summaryrefslogtreecommitdiff
path: root/xmake/core/language/language.lua
blob: b1f8fcc245ca9f4a85089a634836faff73376076 (plain)
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
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements.  See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership.  The ASF licenses this file
-- to you 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 - 2018, TBOOX Open Source Group.
--
-- @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)

    -- new an instance
    local instance = table.inherit(_instance)

    -- init instance
    instance._NAME      = name
    instance._INFO      = info
    instance._ROOTDIR   = rootdir

    -- ok
    return instance
end

-- get the language configure
function _instance:get(name)

    -- the info
    local info = self._INFO

    -- get if from info first
    local value = info[name]
    if value ~= nil then
        return value 
    end

    -- load _g 
    if self._g == nil and info.load ~= nil then

        -- load it
        local ok, results = sandbox.load(info.load)
        if not ok then
            os.raise(results)
        end

        -- save _g
        self._g = results
    end

    -- get it from _g 
    return self._g[name]
end

-- get the language menu
function _instance:menu()
    return self._INFO.menu
end

-- get the language name
function _instance:name()
    return self._NAME
end

-- get the source extensions
function _instance:extensions()

    -- attempt to get it from cache
    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

    -- cache it
    self._EXTENSIONS = extensions

    -- get it
    return extensions
end

-- get the source kinds
function _instance:sourcekinds()
    return self._INFO.sourcekinds
end

-- get the source flags
function _instance:sourceflags()
    return self._INFO.sourceflags
end

-- get the target kinds (targetkind => linkerkind)
--
-- .e.g
-- {binary = "ld", static = "ar", shared = "sh"}
--
function _instance:targetkinds()
    return self._INFO.targetkinds
end

-- get the target flags (targetkind => linkerflag)
--
-- .e.g
-- {binary = "ldflags", static = "arflags", shared = "shflags"}
--
function _instance:targetflags()

    -- get it
    return self._INFO.targetflags
end

-- get the mixing kinds for linker
--
-- .e.g
-- {"cc", "cxx"}
--
function _instance:mixingkinds()

    -- get it
    return self._INFO.mixingkinds
end

-- get the language kinds
function _instance:langkinds()
    return self._INFO.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.nameflags)) do

        -- make tool info
        local toolinfo = results[targetkind] or {}
        for _, namedflag in ipairs(nameflags) do

            -- split it by '.'
            local splitinfo = namedflag:split('.')
            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

    -- ok?
    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"
        }
    ,   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"
        }
    }

    -- save interpreter
    language._INTERPRETER = interp

    -- ok?
    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
        for _, name in ipairs(table.wrap(os.match(path.join(language._directory(), "*"), true))) do
            local instance, errors = language.load(path.basename(name))
            if not instance then
                return nil, errors
            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

    -- load language
    local results, errors = language._interpreter():load(scriptpath, "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

    -- save instance to the cache
    language._LANGUAGES[name] = instance

    -- ok
    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

    -- not found?
    if not result then
        return nil, string.format("unknown language sourcekind: %s", sourcekind)
    end

    -- cache this language
    language._LANGUAGES_OF_SK[sourcekind] = result

    -- ok
    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

    -- not found?
    if not result then
        return nil, string.format("unknown language source extension: %s", extension)
    end

    -- cache this language
    language._LANGUAGES_OF_EX[extension] = result

    -- ok
    return result
end


-- load the language apis
function language.apis()

    -- load all languages
    local languages, errors = language.load()
    if not languages then
        os.raise(errors)
    end

    -- merge apis for each language
    local apis = {values = {}, pathes = {}, 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.pathes,     table.wrap(instance_apis.pathes))
            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.pathes = table.unique(apis.pathes)
    apis.custom = table.unique(apis.custom)

    -- ok
    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()

    -- attempt to get it from cache
    if language._EXTENSIONS then
        return language._EXTENSIONS
    end

    -- load all languages
    local languages, errors = language.load()
    if not languages then
        os.raise(errors)
    end

    -- merge all for each language
    local extensions = {}
    for name, instance in pairs(languages) do
        table.join2(extensions, instance:extensions())
    end

    -- cache it
    language._EXTENSIONS = extensions

    -- ok
    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()

    -- attempt to get it from cache
    if language._SOURCEKINDS then
        return language._SOURCEKINDS
    end

    -- load all languages
    local languages, errors = language.load()
    if not languages then
        os.raise(errors)
    end

    -- merge all for each language
    local sourcekinds = {}
    for name, instance in pairs(languages) do
        table.join2(sourcekinds, instance:sourcekinds())
    end

    -- cache it
    language._SOURCEKINDS = sourcekinds

    -- ok
    return sourcekinds
end

-- get language source flags
--
-- .e.g
--
-- {
--      cc  = {"cflags", "cxflags"}
-- ,    cxx = {"cxxflags", "cxflags"}
-- ,    ...
-- }
--
function language.sourceflags()

    -- attempt to get it from cache
    if language._SOURCEFLAGS then
        return language._SOURCEFLAGS
    end

    -- load all languages
    local languages, errors = language.load()
    if not languages then
        os.raise(errors)
    end

    -- merge all for each language
    local sourceflags = {}
    for name, instance in pairs(languages) do
        table.join2(sourceflags, instance:sourceflags())
    end

    -- cache it
    language._SOURCEFLAGS = sourceflags

    -- ok
    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

    -- ok
    return sourcekind
end

-- get extension of the source kind
function language.extension_of(sourcekind)

    -- get extension
    local extension = table.wrap(language.sourcekinds()[sourcekind])[1] 
    if not extension then
        return nil, string.format("%s is unknown source kind", sourcekind)
    end

    -- ok
    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:targetkinds())) 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

        -- cache it
        language._LINKERINFOS = linkerinfos
    end

    -- find suitable linkers
    local results = {}
    for _, linkerinfo in pairs(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", "gc-ld", "dc-ld"}
-- ,    static = {"ar", "gc-ar", "dc-ar"}
-- ,    shared = {"sh", "dc-sh"}
-- }
--
function language.targetkinds()

    -- attempt to get it from cache
    if language._TARGETKINDS then
        return language._TARGETKINDS
    end

    -- load all languages
    local languages, errors = language.load()
    if not languages then
        os.raise(errors)
    end

    -- merge all for each language
    local targetkinds = {}
    for name, instance in pairs(languages) do
        for targetkind, linkerkind in pairs(table.wrap(instance:targetkinds())) 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

    -- cache it
    language._TARGETKINDS = targetkinds

    -- ok
    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()

    -- attempt to get it from cache
    if language._LANGKINDS then
        return language._LANGKINDS
    end

    -- load all languages
    local languages, errors = language.load()
    if not languages then
        os.raise(errors)
    end

    -- merge all for each language
    local langkinds = {}
    for name, instance in pairs(languages) do
        table.join2(langkinds, instance:langkinds())
    end

    -- cache it
    language._LANGKINDS = langkinds

    -- ok
    return langkinds
end

-- return module
return language