summaryrefslogtreecommitdiff
path: root/xmake/plugins/show/info/target.lua
blob: d5ddfbe7633a218a67bf50692f7c2de6eca2cfd4 (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
--!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        target.lua
--

-- imports
import("core.base.option")
import("core.base.hashset")
import("core.project.config")
import("core.language.language")
import("private.detect.check_targetname")

-- get source info string
function _get_sourceinfo_str(target, name, item, opt)
    opt = opt or {}
    local sourceinfo = target:sourceinfo(name, item)
    if sourceinfo then
        local tips = opt.tips
        if tips then
            tips = tips .. " -> "
        end
        return string.format(" ${dim}-> %s%s:%s${clear}", tips or "", sourceinfo.file or "", sourceinfo.line or -1)
    elseif opt.tips then
        return string.format(" ${dim}-> %s${clear}", opt.tips)
    end
    return ""
end

-- get values from target options
function _get_values_from_opts(target, name)
    local values = {}
    for _, opt_ in ipairs(target:orderopts()) do
        for _, value in ipairs(opt_:get(name)) do
            local tips = string.format("option(%s)", opt_:name())
            values[value] = _get_sourceinfo_str(opt_, name, value, {tips = tips})
        end
    end
    return values
end

-- get values from target packages
function _get_values_from_pkgs(target, name)
    local values = {}
    for _, pkg in ipairs(target:orderpkgs()) do
        local configinfo = target:pkgconfig(pkg:name())
        -- get values from package components
        -- e.g. `add_packages("sfml", {components = {"graphics", "window"}})`
        local selected_components = configinfo and configinfo.components or pkg:components_default()
        if selected_components and pkg:components() then
            local components_enabled = hashset.new()
            for _, comp in ipairs(table.wrap(selected_components)) do
                components_enabled:insert(comp)
                for _, dep in ipairs(table.wrap(pkg:component_orderdeps(comp))) do
                    components_enabled:insert(dep)
                end
            end
            components_enabled:insert("__base")
            -- if we can't find the values from the component, we need to fall back to __base to find them.
            -- it contains some common values of all components
            local components = table.wrap(pkg:components())
            for _, component_name in ipairs(table.join(pkg:components_orderlist(), "__base")) do
                if components_enabled:has(component_name) then
                    local info = components[component_name]
                    if info then
                        for _, value in ipairs(info[name]) do
                            values[value] = string.format(" -> package(%s)", pkg:fullname())
                        end
                    else
                        local components_str = table.concat(table.wrap(configinfo.components), ", ")
                        utils.warning("unknown component(%s) in add_packages(%s, {components = {%s}})", component_name, pkg:fullname(), components_str)
                    end
                end
            end
        -- get values instead of the builtin configs if exists extra package config
        -- e.g. `add_packages("xxx", {links = "xxx"})`
        elseif configinfo and configinfo[name] then
            for _, value in ipairs(configinfo[name]) do
                values[value] = _get_sourceinfo_str(target, "packages", pkg:name())
            end
        else
            -- get values from the builtin package configs
            for _, value in ipairs(pkg:get(name)) do
                values[value] = string.format(" -> package(%s)", pkg:fullname())
            end
        end
    end
    return values
end

-- get values from target dependencies
function _get_values_from_deps(target, name)
    local values = {}
    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 _, value in ipairs(dep:get(name, {interface = true})) do
                values[value] = string.format(" -> dep(%s)", dep:name())
            end
            local values_chunks = dep:get_from(name, "option::*", {interface = true})
            for _, values_chunk in ipairs(values_chunks) do
                for _, value in ipairs(values_chunk) do
                    values[value] = string.format(" -> dep(%s) -> options", dep:name())
                end
            end
            values_chunks = dep:get_from(name, "package::*", {interface = true})
            for _, values_chunk in ipairs(values_chunks) do
                for _, value in ipairs(values_chunk) do
                    values[value] = string.format(" -> dep(%s) -> packages", dep:name())
                end
            end
        end
    end
    return values
end

-- show target information
function _show_target(target)
    print("The information of target(%s):", target:name())
    cprint("    ${color.dump.string}at${clear}: %s", path.join(target:scriptdir(), "xmake.lua"))
    cprint("    ${color.dump.string}kind${clear}: %s", target:kind())
    local targetfile = target:targetfile()
    if targetfile then
        cprint("    ${color.dump.string}targetfile${clear}: %s", targetfile)
    end
    local deps = target:get("deps")
    if deps then
        cprint("    ${color.dump.string}deps${clear}:")
        for _, dep in ipairs(deps) do
            cprint("      ${color.dump.reference}->${clear} %s%s", dep, _get_sourceinfo_str(target, "deps", dep))
        end
    end
    local rules = target:get("rules")
    if rules then
        cprint("    ${color.dump.string}rules${clear}:")
        for _, value in ipairs(rules) do
            cprint("      ${color.dump.reference}->${clear} %s%s", value, _get_sourceinfo_str(target, "rules", value))
        end
    end
    local options = {}
    for _, opt in ipairs(target:get("options")) do
        if not opt:startswith("__") then
            table.insert(options, opt)
        end
    end
    if #options > 0 then
        cprint("    ${color.dump.string}options${clear}:")
        for _, value in ipairs(options) do
            cprint("      ${color.dump.reference}->${clear} %s%s", value, _get_sourceinfo_str(target, "options", value))
        end
    end
    local packages = target:get("packages")
    if packages then
        cprint("    ${color.dump.string}packages${clear}:")
        for _, value in ipairs(packages) do
            cprint("      ${color.dump.reference}->${clear} %s%s", value, _get_sourceinfo_str(target, "packages", value))
        end
    end
    for _, apiname in ipairs(table.join(language.apis().values, language.apis().paths)) do
        if apiname:startswith("target.") then
            local valuename = apiname:split('.add_', {plain = true})[2]
            if valuename then
                local results = {}
                local values = table.unique(table.wrap(target:get(valuename)))
                if #values > 0 then
                    for _, value in ipairs(values) do
                        table.insert(results, {value = value, sourceinfo = _get_sourceinfo_str(target, valuename, value)})
                    end
                end
                local values_from_opts = _get_values_from_opts(target, valuename)
                for value, sourceinfo in pairs(values_from_opts) do
                    table.insert(results, {value = value, sourceinfo = sourceinfo})
                end
                local values_from_pkgs = _get_values_from_pkgs(target, valuename)
                for value, sourceinfo in pairs(values_from_pkgs) do
                    table.insert(results, {value = value, sourceinfo = sourceinfo})
                end
                local values_from_deps = _get_values_from_deps(target, valuename)
                for value, sourceinfo in pairs(values_from_deps) do
                    table.insert(results, {value = value, sourceinfo = sourceinfo})
                end
                if #results > 0 then
                    cprint("    ${color.dump.string}%s${clear}:", valuename)
                    for _, result in ipairs(results) do
                        cprint("      ${color.dump.reference}->${clear} %s%s", result.value, result.sourceinfo)
                    end
                end
            end
        end
    end
    local files = target:get("files")
    if files then
        cprint("    ${color.dump.string}files${clear}:")
        for _, file in ipairs(files) do
            if not file:startswith("__remove_") then
                cprint("      ${color.dump.reference}->${clear} %s%s", file, _get_sourceinfo_str(target, "files", file))
            end
        end
    end
    local sourcekinds = hashset.new()
    for _, sourcebatch in pairs(target:sourcebatches()) do
        if sourcebatch.sourcekind then
            sourcekinds:insert(sourcebatch.sourcekind)
        end
    end
    for _, sourcekind in sourcekinds:keys() do
        local compinst = target:compiler(sourcekind)
        if compinst then
            cprint("    ${color.dump.string}compiler (%s)${clear}: %s", sourcekind, compinst:program())
            cprint("      ${color.dump.reference}->${clear} %s", os.args(compinst:compflags()))
        end
    end
    local linker = targetfile and target:linker()
    if linker then
        cprint("    ${color.dump.string}linker (%s)${clear}: %s", linker:kind(), linker:program())
        cprint("      ${color.dump.reference}->${clear} %s", os.args(linker:linkflags()))
    end
    for _, sourcekind in sourcekinds:keys() do
        local compinst = target:compiler(sourcekind)
        if compinst then
            cprint("    ${color.dump.string}compflags (%s)${clear}:", sourcekind)
            cprint("      ${color.dump.reference}->${clear} %s", os.args(compinst:compflags({target = target})))
        end
    end
    if linker then
        cprint("    ${color.dump.string}linkflags (%s)${clear}:", linker:kind())
        cprint("      ${color.dump.reference}->${clear} %s", os.args(linker:linkflags({target = target})))
    end
end

function main(name)

    -- get target
    config.load()
    local target = assert(check_targetname(name))

    -- show target information
    _show_target(target)
end