summaryrefslogtreecommitdiff
path: root/xmake/rules/utils/bin2c/utils.lua
blob: 405c2ed62551deb84e220b346f1d29557445ca75 (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
--!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        utils.lua
--

-- generate header file from binary file
--
-- @param target        the target
-- @param batchcmds     the batch commands
-- @param binaryfile    the binary file path
-- @param opt           the options
--                       - progress: the progress callback
--                       - linewidth: the line width for output (optional)
--                       - nozeroend: disable null terminator (deprecated, for backward compatibility only)
--                       - zeroend: enable null terminator (default: true, bin2c enables zeroend by default for compatibility)
--                       - rulename: the rule name for getting extra config (default: utils.bin2c)
--                       - headerfile: the output header file path (optional, auto-generated if not provided)
--                       - headerdir: the header directory (optional, auto-generated if not provided)
--
function generate_headerfile(target, batchcmds, binaryfile, opt)
    opt = opt or {}
    local rulename = opt.rulename or "utils.bin2c"
    local progress = opt.progress

    -- get header directory and file
    local headerdir = opt.headerdir
    local headerfile = opt.headerfile
    if not headerfile then
        if not headerdir then
            headerdir = path.join(target:autogendir(), "rules", "utils", "bin2c")
        end
        headerfile = path.join(headerdir, path.filename(binaryfile) .. ".h")
    else
        headerdir = headerdir or path.directory(headerfile)
    end

    -- add includedirs
    target:add("includedirs", headerdir)

    -- add commands
    if progress then
        batchcmds:show_progress(progress, "${color.build.object}generating.bin2c %s", binaryfile)
    end
    batchcmds:mkdir(headerdir)

    -- build argv
    local argv = {"-i", path(binaryfile), "-o", path(headerfile)}

    -- get linewidth
    local linewidth = opt.linewidth or target:extraconf("rules", rulename, "linewidth")
    if linewidth then
        table.insert(argv, "-w")
        table.insert(argv, tostring(linewidth))
    end

    -- get nozeroend/zeroend (check file-level config first, then rule-level config, then opt)
    local fileconfig = target:fileconfig(binaryfile)
    local nozeroend = nil
    if fileconfig then
        if fileconfig.nozeroend ~= nil then
            nozeroend = fileconfig.nozeroend
        elseif fileconfig.zeroend ~= nil then
            nozeroend = not fileconfig.zeroend
        end
    end
    if nozeroend == nil then
        nozeroend = target:extraconf("rules", rulename, "nozeroend") or false
    end
    -- also support zeroend in opt (inverse of nozeroend)
    if opt.zeroend ~= nil then
        nozeroend = not opt.zeroend
    elseif opt.nozeroend ~= nil then
        nozeroend = opt.nozeroend
    end
    if nozeroend then
        table.insert(argv, "--nozeroend")
    end

    batchcmds:vlua("cli.binutils.bin2c", argv)

    return headerfile
end