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
172
173
|
--!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_mingw.lua
--
-- imports
import("lib.detect.find_path")
import("core.base.option")
import("core.base.global")
import("core.project.config")
import("core.cache.detectcache")
import("detect.sdks.find_cross_toolchain")
-- find mingw directory
function _find_mingwdir(sdkdir, msystem)
if not sdkdir then
if is_host("macosx", "linux") and os.isdir("/opt/llvm-mingw") then
sdkdir = "/opt/llvm-mingw"
elseif is_host("macosx") and os.isdir("/usr/local/opt/mingw-w64") then
-- for macOS Intel
sdkdir = "/usr/local/opt/mingw-w64"
elseif is_host("macosx") and os.isdir("/opt/homebrew/opt/mingw-w64") then
-- for Apple Silicon
sdkdir = "/opt/homebrew/opt/mingw-w64"
elseif is_host("linux") then
sdkdir = "/usr"
else
local mingw_prefix = is_subhost("msys") and os.getenv("MINGW_PREFIX") or os.getenv("LLVM_MINGW_DIR") or os.getenv("LLVM_MINGW_ROOT")
if mingw_prefix and os.isdir(mingw_prefix) then
sdkdir = mingw_prefix
end
end
-- attempt to get it from $PATH
-- @see https://github.com/xmake-io/xmake/issues/977
if not sdkdir then
local pathenv = os.getenv("PATH")
if pathenv then
local buildhash_pattern = string.rep('%x', 32)
local match_pattern = "[\\/]packages[\\/]%w[\\/].*mingw.*[\\/][^\\/]+[\\/]" .. buildhash_pattern .. "[\\/]bin"
for _, p in ipairs(path.splitenv(pathenv)) do
if (p:find(match_pattern) or p:find(string.ipattern("mingw[%w%-%_%+]*[\\/]bin"))) and
path.filename(p) == "bin" and os.isdir(p) then
sdkdir = path.directory(p)
break
end
end
end
end
end
if is_subhost("msys") and sdkdir and msystem and not sdkdir:find(msystem, 1, true) then
sdkdir = path.unix(path.join(path.directory(sdkdir), msystem))
end
-- attempt to find mingw directory from the qt sdk
local qt = config.get("qt")
if not sdkdir and qt then
sdkdir = find_path("bin", path.join(qt, "Tools", "mingw*_" .. (is_arch("x86_64") and "64" or "32")))
end
-- get mingw directory
if sdkdir and os.isdir(sdkdir) then
return sdkdir
end
end
-- find the mingw toolchain
function _find_mingw(sdkdir, opt)
opt = opt or {}
local bindir = opt.bindir
local cross = opt.cross
local msystem = opt.msystem
local arch = opt.arch or config.get("arch") or os.arch()
-- find mingw root directory
sdkdir = _find_mingwdir(sdkdir, msystem)
if not sdkdir then
return
end
-- select cross on macOS, e.g x86_64-w64-mingw32- or i686-w64-mingw32-
if not cross then
if arch == "i386" or arch == "x86" or arch == "i686" then
cross = "i686-w64-mingw32-"
elseif arch == "arm64" or arch == "aarch64" then
cross = "aarch64-w64-mingw32-" -- for llvm-mingw
elseif arch:startswith("arm") then
cross = "armv7-w64-mingw32-" -- for llvm-mingw
else
cross = "x86_64-w64-mingw32-"
end
end
-- find cross toolchain
local toolchain = find_cross_toolchain(sdkdir or bindir, {bindir = bindir, cross = cross})
if not toolchain then -- fallback, e.g. gcc.exe without cross
toolchain = find_cross_toolchain(sdkdir or bindir, {bindir = bindir})
end
if toolchain then
return {sdkdir = toolchain.sdkdir, bindir = toolchain.bindir, cross = toolchain.cross, msystem = msystem}
end
end
-- find mingw toolchains
--
-- @param sdkdir the mingw directory
-- @param opt the argument options
-- e.g. {verbose = true, force = false, bindir = .., cross = ..., arch = ...}
--
-- @return the mingw toolchains. e.g. {sdkdir = .., bindir = .., cross = ..}
--
-- @code
--
-- local toolchain = find_mingw("/xxx/android-mingw-r10e")
-- local toolchain = find_mingw("/xxx/android-mingw-r10e", {force = true, verbose = true})
--
-- @endcode
--
-- find MinGW SDK
--
-- @param sdkdir the MinGW SDK directory (optional)
-- @param opt the options, e.g. {verbose = true, force = false}
-- @return the SDK info table {sdkdir, bindir, cross, ...}
--
function main(sdkdir, opt)
opt = opt or {}
-- attempt to load cache first
local key = "detect.sdks.find_mingw"
local cacheinfo = detectcache:get(key) or {}
if not opt.force and cacheinfo.mingw and cacheinfo.mingw.sdkdir and os.isdir(cacheinfo.mingw.sdkdir)
and cacheinfo.mingw.msystem == opt.msystem then
return cacheinfo.mingw
end
-- find mingw
local mingw = _find_mingw(sdkdir or config.get("mingw") or global.get("mingw") or config.get("sdk"), {
bindir = opt.bindir or config.get("bin"),
cross = opt.cross or config.get("cross"),
msystem = opt.msystem,
arch = opt.arch
})
if mingw and mingw.sdkdir then
config.set("mingw", mingw.sdkdir, {force = true, readonly = true})
if opt.verbose or option.get("verbose") then
cprint("checking for Mingw SDK ... ${color.success}%s (%s)", mingw.sdkdir, mingw.cross)
end
else
if opt.verbose or option.get("verbose") then
cprint("checking for Mingw SDK ... ${color.nothing}${text.nothing}")
end
end
-- save to cache
cacheinfo.mingw = mingw or false
detectcache:set(key, cacheinfo)
return mingw
end
|