summaryrefslogtreecommitdiff
path: root/xmake/modules/private/action/require/impl/utils/requirekey.lua
blob: 5fd228ecf5dcb67284f1f2b17b54081d9454958e (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
--!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        requirekey.lua
--

-- imports
import("core.base.hashset")
import("core.package.package", {alias = "core_package"})

-- get require key from requireinfo
function main(requireinfo, opt)
    opt = opt or {}
    local key = ""
    if opt.name then
        key = key .. "/" .. opt.name
    end
    if opt.plat then
        key = key .. "/" .. opt.plat
    end
    if opt.arch then
        key = key .. "/" .. opt.arch
    end
    if opt.kind then
        key = key .. "/" .. opt.kind
    end
    if opt.version then
        key = key .. "/" .. opt.version
    end
    if requireinfo.label then
        key = key .. "/" .. requireinfo.label
    end
    if requireinfo.host then
        if is_subhost(core_package.targetplat()) and os.subarch() == core_package.targetarch() then
            -- we need to pass plat/arch to avoid repeat installation
            -- @see https://github.com/xmake-io/xmake/issues/1579
        else
            key = key .. "/host"
        end
    end
    if requireinfo.system then
        key = key .. "/system"
    end
    -- @see https://github.com/xmake-io/xmake/issues/4934
    if requireinfo.private then
        key = key .. "/private"
    end
    if key:startswith("/") then
        key = key:sub(2)
    end
    local configs = requireinfo.configs
    if configs then
        local configs_order = {}
        for k, v in pairs(configs) do
            if type(v) == "table" then
                v = string.serialize(v, {strip = true, indent = false, orderkeys = true})
            end
            table.insert(configs_order, k .. "=" .. tostring(v))
        end
        table.sort(configs_order)
        key = key .. ":" .. string.serialize(configs_order, true)
    end
    if opt.hash then
        if key == "" then
            key = "_" -- we need to generate a fixed hash value
        end
        return hash.strhash32(key)
    else
        return key
    end
end