summaryrefslogtreecommitdiff
path: root/xmake/rules/wdk/sign/sign.lua
blob: 8acdc5311cb59a29e4cfb4a44c54e963a63080cc (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
--!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        sign.lua
--

-- imports
import("core.base.option")
import("core.base.global")
import("core.project.config")

-- get tool
function _get_tool(target, name)

    -- init cache
    _g.tools = _g.tools or {}

    -- get it from the cache
    local tool = _g.tools[name]
    if not tool then

        -- get wdk
        local wdk = target:data("wdk")

        -- get arch
        local arch = assert(config.arch(), "arch not found!")

        -- get tool
        tool = path.join(wdk.bindir, arch, name .. ".exe")
        if not os.isexec(tool) then
            tool = path.join(wdk.bindir, wdk.sdkver, arch, name .. ".exe")
        end
        assert(os.isexec(tool), name .. " not found!")
        _g.tools[name] = tool
    end
    return tool
end

-- do sign
function main(target, filepath, mode)

    -- get signtool
    local signtool = _get_tool(target, "signtool")

    -- init arguments
    local argv = {"sign", "/v"}
    local timestamp = target:values("wdk.sign.timestamp")
    if timestamp then
        table.insert(argv, "/t")
        table.insert(argv, timestamp)
    end
    local company = target:values("wdk.sign.company")
    if company then
        table.insert(argv, "/n")
        table.insert(argv, company)
    end
    local certfile = target:values("wdk.sign.certfile")
    if certfile then
        table.insert(argv, "/ac")
        table.insert(argv, certfile)
    end
    local thumbprint = target:values("wdk.sign.thumbprint")
    if thumbprint then
        table.insert(argv, "/sha1")
        table.insert(argv, thumbprint)
    end
    local store = target:values("wdk.sign.store")
    if store then
        table.insert(argv, "/a")
        table.insert(argv, "/s")
        table.insert(argv, store)
    end
    local digest_algorithm = target:values("wdk.sign.digest_algorithm")
    if digest_algorithm then
        table.insert(argv, "/fd")
        table.insert(argv, digest_algorithm)
    end

    -- uses the default test certificate
    if mode == "test" and (not certfile and not thumbprint and not store) then
        table.insert(argv, "/a")
        table.insert(argv, "/n")
        table.insert(argv, "xmake.io(test)")
        table.insert(argv, "/s")
        table.insert(argv, "PrivateCertStore")
    end

    -- add target file
    table.insert(argv, filepath)

    -- do sign
    os.vrunv(signtool, argv)
end