summaryrefslogtreecommitdiff
path: root/xmake/modules/cli/amalgamate.lua
blob: 547ca54d167e272110a46cf46380eba7c1007bd1 (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
--!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        amalgamate.lua
--

-- imports
import("core.base.option")
import("core.base.graph")
import("core.project.config")
import("core.project.task")
import("core.project.project")
import("private.detect.check_targetname")

-- the options
local options =
{
    {'u', "uniqueid",  "kv", nil, "Set the unique id."       },
    {'o', "outputdir", "kv", nil, "Set the output directory."},
    {nil, "target",    "v",  nil, "The target name."         }
}

-- get include files
function _get_include_files(target, filepath)
    local includes = {}
    local sourcecode = io.readfile(filepath)
    sourcecode = sourcecode:gsub("/%*.-%*/", "")
    sourcecode = sourcecode:gsub("//.-\n", "\n")
    sourcecode:gsub("#include%s+\"(.-)\"", function (include)
        table.insert(includes, include)
    end)
    includes = table.unique(includes)

    local includefiles = {}
    local filedir = path.directory(filepath)
    local includedirs = table.join(filedir, target:get("includedirs"))
    for _, include in ipairs(includes) do
        local result
        for _, includedir in ipairs(includedirs) do
            local includefile = path.join(includedir, include)
            if os.isfile(includefile) then
                includefile = path.normalize(path.absolute(includefile, os.projectdir()))
                result = includefile
                break
            end
        end
        if result then
            table.insert(includefiles, result)
        else
            wprint("#include \"%s\" not found in %s", include, filepath)
        end
    end
    return includefiles
end

-- generate include graph
function _generate_include_graph(target, inputpaths, gh, marked)
    for _, inputpath in ipairs(inputpaths) do
        if not marked[inputpath] then
            marked[inputpath] = true
            local includefiles = _get_include_files(target, inputpath)
            for _, includefile in ipairs(includefiles) do
                gh:add_edge(inputpath, includefile)
            end
            if includefiles and #includefiles > 0 then
                _generate_include_graph(target, includefiles, gh, marked)
            end
        end
    end
end

-- generate file
function _generate_file(target, inputpaths, outputpath, uniqueid)

    -- generate include graph
    local gh = graph.new(true)
    for idx, inputpath in ipairs(inputpaths) do
        inputpath = path.normalize(path.absolute(inputpath, os.projectdir()))
        inputpaths[idx] = inputpath
        gh:add_edge("__root__", inputpath)
    end
    _generate_include_graph(target, inputpaths, gh, {})

    -- sort file paths and remove root path
    local filepaths = gh:topo_sort()
    table.remove(filepaths, 1)

    -- generate amalgamate file
    local outputfile = io.open(outputpath, "w")
    for _, filepath in irpairs(filepaths) do
        cprint("  ${color.dump.reference}+${clear} %s", filepath)
        if uniqueid then
            outputfile:print("#define %s %s", uniqueid, "unity_" .. hash.uuid():split("-", {plain = true})[1])
        end
        outputfile:write(io.readfile(filepath))
        if uniqueid then
            outputfile:print("#undef %s", uniqueid)
        end
    end
    outputfile:close()
    cprint("${bright}%s generated!", outputpath)
end

-- generate code
function _generate_amalgamate_code(target, opt)

    -- only for library/binary
    if not target:is_library() and not target:is_binary() then
        return
    end

    -- generate source code
    local outputdir = opt.outputdir
    local uniqueid = opt.uniqueid
    for _, sourcebatch in pairs(target:sourcebatches()) do
        local rulename = sourcebatch.rulename
        if rulename == "c.build" or rulename == "c++.build" then
            local outputpath = path.join(outputdir, target:name() .. (sourcekind == "cxx" and ".cpp" or ".c"))
            _generate_file(target, sourcebatch.sourcefiles, outputpath, uniqueid)
        end
    end

    -- generate header file
    local srcheaders = target:headerfiles(includedir)
    if srcheaders and #srcheaders > 0 then
        local outputpath = path.join(outputdir, target:name() .. ".h")
        _generate_file(target, srcheaders, outputpath, uniqueid)
    end
end

-- generate amalgamate code
--
-- https://github.com/xmake-io/xmake/issues/1438
--
function main(...)

    -- parse arguments
    local argv = table.pack(...)
    local args = option.parse(argv, options, "Generate amalgamate code.",
                                             "",
                                             "Usage: xmake l cli.amalgamate [options]")

    -- config first
    task.run("config")

    -- generate amalgamate code
    args.outputdir = args.outputdir or config.builddir()
    if args.target then
        local target = assert(check_targetname(args.target))
        _generate_amalgamate_code(target, args)
    else
        for _, target in ipairs(project.ordertargets()) do
            _generate_amalgamate_code(target, args)
        end
    end
end