summaryrefslogtreecommitdiff
path: root/xmake/core/tool/builder.lua
blob: 3433813c990b6e213f46f0635655267ac0fa8984 (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
--!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, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        builder.lua
--

-- define module
local builder = builder or {}

-- load modules
local io       = require("base/io")
local path     = require("base/path")
local utils    = require("base/utils")
local table    = require("base/table")
local string   = require("base/string")
local option   = require("base/option")
local tool     = require("tool/tool")
local config   = require("project/config")
local sandbox  = require("sandbox/sandbox")
local language = require("language/language")
local platform = require("platform/platform")

-- get the tool of builder
function builder:_tool()
    return self._TOOL
end

-- get the name flags
function builder:_nameflags()
    return self._NAMEFLAGS
end

-- get the target kind
function builder:_targetkind()
    return self._TARGETKIND
end

-- map gcc flag to the given builder flag
function builder:_mapflag(flag, flagkind, mapflags, auto_ignore_flags)

    -- attempt to map it directly
    local flag_mapped = mapflags[flag]
    if flag_mapped then
        return flag_mapped
    end

    -- find and replace it using pattern
    for k, v in pairs(mapflags) do
        local flag_mapped, count = flag:gsub("^" .. k .. "$", function (w) return v end)
        if flag_mapped and count ~= 0 then
            return #flag_mapped ~= 0 and flag_mapped
        end
    end

    -- has this flag?
    if auto_ignore_flags == false or self:has_flags(flag, flagkind) then
        return flag
    else
        utils.warning("add_%s(\"%s\") is ignored, please pass `{force = true}` or call `set_policy(\"check.auto_ignore_flags\", false)` if you want to set it.", flagkind, flag)
    end
end

-- map gcc flags to the given builder flags
function builder:_mapflags(flags, flagkind, target)

    local results = {}
    local mapflags = self:get("mapflags")
    local auto_map_flags = target and target.policy and target:policy("check.auto_map_flags")
    local auto_ignore_flags = target and target.policy and target:policy("check.auto_ignore_flags")
    flags = table.wrap(flags)
    if mapflags and (auto_map_flags ~= false) then
        for _, flag in pairs(flags) do
            local flag_mapped = self:_mapflag(flag, flagkind, mapflags, auto_ignore_flags)
            if flag_mapped then
                table.insert(results, flag_mapped)
            end
        end
    else
        for _, flag in pairs(flags) do
            if auto_ignore_flags == false or self:has_flags(flag, flagkind) then
                table.insert(results, flag)
            else
                utils.warning("add_%s(\"%s\") is ignored, please pass `{force = true}` or call `set_policy(\"check.auto_ignore_flags\", false)` if you want to set it.", flagkind, flag)
            end
        end
    end
    return results
end

-- get the flag kinds
function builder:_flagkinds()
    return self._FLAGKINDS
end

-- inherit flags (only for public/interface) from target deps
--
-- e.g.
-- add_cflags("", {public = true})
-- add_cflags("", {interface = true})
--
function builder:_inherit_flags_from_targetdeps(flags, target)
    local orderdeps = target:orderdeps()
    local total = #orderdeps
    for idx, _ in ipairs(orderdeps) do
        local dep = orderdeps[total + 1 - idx]
        local depinherit = target:extraconf("deps", dep:name(), "inherit")
        if depinherit == nil or depinherit then
            for _, flagkind in ipairs(self:_flagkinds()) do
                self:_add_flags_from_flagkind(flags, dep, flagkind, {interface = true})
            end
        end
    end
end

-- add flags from the flagkind
function builder:_add_flags_from_flagkind(flags, target, flagkind, opt)
    local targetflags = target:get(flagkind, opt)
    local extraconf   = target:extraconf(flagkind)
    if extraconf then
        for _, flag in ipairs(table.wrap(targetflags)) do
            -- force to add flags?
            local flagconf = extraconf[flag]
            if flagconf and flagconf.force then
                table.join2(flags, flag)
            else
                table.join2(flags, self:_mapflags(flag, flagkind, target))
            end
        end
    else
        table.join2(flags, self:_mapflags(targetflags, flagkind, target))
    end
end

-- add flags from the configure
function builder:_add_flags_from_config(flags)
    for _, flagkind in ipairs(self:_flagkinds()) do
        local values = config.get(flagkind)
        if values then
            table.join2(flags, os.argv(values))
        end
    end
end

-- add flags from the option
function builder:_add_flags_from_option(flags, opt)
    for _, flagkind in ipairs(self:_flagkinds()) do
        self:_add_flags_from_flagkind(flags, opt, flagkind)
    end
end

-- add flags from the package
function builder:_add_flags_from_package(flags, pkg, target)
    for _, flagkind in ipairs(self:_flagkinds()) do
        table.join2(flags, self:_mapflags(pkg:get(flagkind), flagkind, target))
    end
end

-- add flags from the target
function builder:_add_flags_from_target(flags, target)

    -- no target?
    if not target then
        return
    end

    -- init cache
    self._TARGETFLAGS = self._TARGETFLAGS or {}
    local cache = self._TARGETFLAGS

    -- get flags from cache first
    local key = target:cachekey()
    local targetflags = cache[key]
    if not targetflags then

        -- add flags from language
        targetflags = {}
        self:_add_flags_from_language(targetflags, target)

        -- add flags for the target
        if target:type() == "target" then

            -- add flags from options
            for _, opt in ipairs(target:orderopts()) do
                self:_add_flags_from_option(targetflags, opt)
            end

            -- add flags from packages
            for _, pkg in ipairs(target:orderpkgs()) do
                self:_add_flags_from_package(targetflags, pkg, target)
            end

            -- inherit flags (public/interface) from all dependent targets
            self:_inherit_flags_from_targetdeps(targetflags, target)
        end

        -- add the target flags
        for _, flagkind in ipairs(self:_flagkinds()) do
            self:_add_flags_from_flagkind(targetflags, target, flagkind)
        end

        -- cache it
        cache[key] = targetflags
    end

    -- add flags
    table.join2(flags, targetflags)
end

-- add flags from the argument option
function builder:_add_flags_from_argument(flags, target, args)

    -- add flags from the flag kinds (cxflags, ..)
    for _, flagkind in ipairs(self:_flagkinds()) do

        -- add auto mapping flags
        table.join2(flags, self:_mapflags(args[flagkind], flagkind, target))

        -- add original flags
        local original_flags = (args.force or {})[flagkind]
        if original_flags then
            table.join2(flags, original_flags)
        end
    end

    -- add flags (named) from the language
    if target then
        local key = target:type()
        self:_add_flags_from_language(flags, target, {
            [key] = function (name) return args[name] end,
            toolchain = function (name) return platform.toolconfig(name) end})
    else
        self:_add_flags_from_language(flags, nil, {
            target = function (name) return args[name] end,
            toolchain = function (name) return platform.toolconfig(name) end})
    end
end

-- add flags from the language
function builder:_add_flags_from_language(flags, target, getters)

    -- init getters
    --
    -- e.g.
    --
    -- target.linkdirs => flags = getters("target")("linkdirs")
    --
    local getters = getters or
    {
        config      =   function (name)
                            local values = config.get(name)
                            if values and name:endswith("dirs") then
                                values = path.splitenv(values)
                            end
                            return values
                        end
    ,   toolchain   =   function (name)
                            if target and target:type() == "target" then
                                return target:toolconfig(name)
                            else
                                return platform.toolconfig(name)
                            end
                        end
    ,   target      =   function (name)

                            -- only for target
                            local results = {}
                            if target:type() == "target" then

                                -- get flagvalues (public or interface) of all dependent targets
                                table.join2(results, target:get_from_deps(name, {interface = true}))

                                -- get flagvalues of target with given flagname
                                table.join2(results, target:get(name))
                            end
                            return results
                        end
    ,   option      =   function (name)

                            -- is target? get flagvalues of the attached options and packages
                            local results = {}
                            if target:type() == "target" then
                                table.join2(results, target:get_from_opts(name))
                                table.join2(results, target:get_from_pkgs(name))

                            -- is option? get flagvalues of option with given flagname
                            elseif target:type() == "option" then
                                table.join2(results, target:get(name))
                            end
                            return results
                        end
    }

    -- get name flags for builder
    for _, flaginfo in ipairs(self:_nameflags()) do

        -- get flag info
        local flagscope     = flaginfo[1]
        local flagname      = flaginfo[2]
        local checkstate    = flaginfo[3]

        -- get getter
        local getter = getters[flagscope]
        if getter then

            -- get api name of tool
            --
            -- ignore "nf_" and "_if_ok" (deprecated)
            --
            -- e.g.
            --
            -- defines => define
            -- defines_if_ok => define
            -- ...
            --
            local apiname  = flagname:gsub("^nf_", ""):gsub("_if_ok$", "")

            -- use multiple values mapper if be defined in tool module
            local multival = false
            if apiname:endswith("s") then
                if self:_tool()["nf_" .. apiname] then
                    multival = true
                else
                    apiname = apiname:sub(1, #apiname - 1)
                end
            end

            -- map named flags to real flags
            local mapper = self:_tool()["nf_" .. apiname]
            if mapper then
                if multival then
                    local results = mapper(self:_tool(), table.wrap(getter(flagname)), target, self:_targetkind())
                    for _, flag in ipairs(table.wrap(results)) do
                        if flag and flag ~= "" and (not checkstate or self:has_flags(flag)) then
                            table.join2(flags, flag)
                        end
                    end
                else
                    for _, flagvalue in ipairs(table.wrap(getter(flagname))) do
                        local flag = mapper(self:_tool(), flagvalue, target, self:_targetkind())
                        if flag and flag ~= "" and (not checkstate or self:has_flags(flag)) then
                            table.join2(flags, flag)
                        end
                    end
                end
            end
        end
    end
end

-- preprocess flags
function builder:_preprocess_flags(flags)

    -- remove repeat first and split flags group, e.g. "-I /xxx" => {"-I", "/xxx"}
    local unique = {}
    local results = {}
    for _, flag in ipairs(flags) do
        flag = flag:trim()
        if #flag > 0 and not unique[flag] then
            if flag:find(" ", 1, true) then
                table.join2(results, os.argv(flag, {splitonly = true}))
            else
                table.insert(results, flag)
            end
            unique[flag] = true
        end
    end
    return results
end

-- get tool name
function builder:name()
    return self:_tool():name()
end

-- get tool kind
function builder:kind()
    return self:_tool():kind()
end

-- get tool program
function builder:program()
    return self:_tool():program()
end

-- get toolchain of this tool
function builder:toolchain()
    return self:_tool():toolchain()
end

-- get the run environments
function builder:runenvs()
    return self:_tool():runenvs()
end

-- get properties of the tool
function builder:get(name)
    return self:_tool():get(name)
end

-- has flags?
function builder:has_flags(flags, flagkind)
    return self:_tool():has_flags(flags, flagkind)
end

-- map flags from name and values, e.g. linkdirs, links, defines
function builder:map_flags(name, values, opt)
    local flags  = {}
    local mapper = self:_tool()["nf_" .. name]
    if mapper then
        opt = opt or {}
        for _, value in ipairs(table.wrap(values)) do
            local flag = mapper(self:_tool(), value, opt.target, opt.targetkind)
            if flag and flag ~= "" and (not opt.check or self:has_flags(flag)) then
                table.join2(flags, flag)
            end
        end
    end
    if #flags > 0 then
        return flags
    end
end

-- get the format of the given target kind
function builder:format(targetkind)
    local formats = self:get("formats")
    if formats then
        return formats[targetkind]
    end
end

-- return module
return builder