summaryrefslogtreecommitdiff
path: root/xmake/modules/private/detect/find_platform.lua
blob: a94b5ade3882ec37c1519eea1d7bdcc8e68319fa (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
--!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        find_platform.lua
--

-- imports
import("core.project.config")
import("core.project.project")
import("detect.sdks.find_cross_toolchain")

-- find platform
function _find_plat(plat)
    plat = plat or config.get("plat")
    if not plat then
        if not plat then
            plat = project.get("defaultplat")
        end
        if not plat then
            plat = os.subhost()
        end
        if plat == "msys" then
            local msystem = os.getenv("MSYSTEM")
            if msystem and msystem:lower():find("mingw", 1, true) then
                plat = "mingw"
            end
        end
    end
    return plat
end

-- find architecture for cross platform
function _find_arch_from_cross()
    local cross  = config.get("cross")
    if not cross then
        local cross_toolchain = find_cross_toolchain(config.get("sdk"), {bindir = config.get("bin")})
        if cross_toolchain then
            cross = cross_toolchain.cross
        end
    end
    local arch = "none"
    if cross then
        if cross:find("aarch64", 1, true) then
            arch = "arm64"
        elseif cross:find("arm", 1, true) then
            arch = "arm"
        elseif cross:find("mips64", 1, true) then
            arch = "mips64"
        elseif cross:find("mips", 1, true) then
            arch = "mips"
        elseif cross:find("riscv64", 1, true) then
            arch = "riscv64"
        elseif cross:find("riscv", 1, true) then
            arch = "riscv"
        elseif cross:find("loong64", 1, true) then
            arch = "loong64"
        elseif cross:find("s390x", 1, true) then
            arch = "s390x"
        elseif cross:find("powerpc64", 1, true) then
            arch = "ppc64"
        elseif cross:find("powerpc", 1, true) then
            arch = "ppc"
        elseif cross:find("sh4", 1, true) then
            arch = "sh4"
        elseif cross:find("x86_64", 1, true) then
            arch = "x86_64"
        elseif cross:find("i386", 1, true) or cross:find("i686", 1, true) then
            arch = "i386"
        end
    end
    return arch
end

-- find architecture
function _find_arch(plat, arch)
    arch = arch or config.get("arch")
    if not arch then
        if not arch then
            arch = project.default_arch(plat)
        end
        if not arch then
            local appledev = config.get("appledev")
            if plat == "android" then
                arch = "armeabi-v7a"
            elseif plat == "iphoneos" or plat == "appletvos" or plat == "applexros" then
                arch = appledev == "simulator" and os.arch() or "arm64"
            elseif plat == "watchos" then
                arch = appledev == "simulator" and os.arch() or "armv7k"
            elseif plat == "wasm" then
                arch = "wasm32"
            elseif plat == "mingw" then
                local mingw_chost = nil
                if is_subhost("msys") then
                    mingw_chost = os.getenv("MINGW_CHOST")
                end
                if mingw_chost == "i686-w64-mingw32" then
                    arch = "i386"
                else
                    arch = "x86_64"
                end
            elseif plat == "harmony" then
                arch = "arm64-v8a"
            elseif plat == "cross" then
                arch = _find_arch_from_cross()
            else
                arch = os.subarch()
            end
        end
    end
    return arch
end

-- find default platform and architecture
--
-- @param   opt the argument options, e.g. {plat = "", arch = "", global = true}
--
-- @return  plat, arch
--
-- @code
--
-- find the default platform:
--   local result = find_platform()
--
-- find the default architecture from the given platform:
--   local result = find_platform({plat = "iphoneos"})
--
-- @endcode
--
function main(opt)

    -- find platform
    opt = opt or {}
    local plat = _find_plat(opt.plat)
    local will_set_plat = false
    if opt.global then
        if not opt.plat and not config.get("plat") then
            will_set_plat = true
            config.set("plat", plat)
        end
    end

    -- find architecture
    local arch = _find_arch(plat, opt.arch)
    local will_set_arch = false
    if opt.global then
        if not opt.arch and not config.get("arch") then
            will_set_arch = true
            config.set("arch", arch)
        end
    end
    if opt.global then
        if will_set_plat or will_set_arch then
            cprint("checking for platform ... ${color.success}%s (%s)", plat, arch)
        end
    end
    return plat, arch
end