Skip to content

Commit f20c40b

Browse files
authored
Merge pull request #5553 from xmake-io/cosmocc
improve cosmocc for windows
2 parents 0d2bd15 + b7dbf58 commit f20c40b

File tree

14 files changed

+66
-10
lines changed

14 files changed

+66
-10
lines changed

xmake/core/base/os.lua

+31-1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,36 @@ function os._run_exit_cbs(ok, errors)
288288
end
289289
end
290290

291+
-- get shell path, e.g. sh, bash
292+
function os._get_shell_path(opt)
293+
opt = opt or {}
294+
local setenvs = opt.setenvs or opt.envs or {}
295+
local addenvs = opt.addenvs or {}
296+
local paths = {}
297+
local p = setenvs.PATH
298+
if type(p) == "string" then
299+
p = path.splitenv(p)
300+
end
301+
if p then
302+
table.join2(paths, p)
303+
end
304+
p = addenvs.PATH
305+
if type(p) == "string" then
306+
p = path.splitenv(p)
307+
end
308+
if p then
309+
table.join2(paths, p)
310+
end
311+
for _, p in ipairs(paths) do
312+
for _, name in ipairs({"sh", "bash"}) do
313+
local filepath = path.join(p, name)
314+
if os.isexec(filepath) then
315+
return filepath
316+
end
317+
end
318+
end
319+
end
320+
291321
-- match files or directories
292322
--
293323
-- @param pattern the search pattern
@@ -808,7 +838,7 @@ function os.execv(program, argv, opt)
808838
-- because `/bin/sh` is not real file path, maybe we need to convert it.
809839
local host = os.host()
810840
if host == "windows" then
811-
filename = "sh"
841+
filename = os._get_shell_path(opt) or "sh"
812842
argv = table.join(shellfile, argv)
813843
else
814844
line = line:sub(3)

xmake/core/project/package.lua

+5
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ function _instance:enable(enabled)
271271
self:set("__enabled", enabled)
272272
end
273273

274+
-- get environments
275+
function _instance:envs()
276+
return self:get("envs")
277+
end
278+
274279
-- get the given rule
275280
function _instance:rule(name)
276281
return self:rules()[name]

xmake/core/project/target.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ function _instance:pkgenvs()
13351335
end
13361336
end
13371337
for _, pkg in pkgs:orderkeys() do
1338-
local envs = pkg:get("envs")
1338+
local envs = pkg:envs()
13391339
if envs then
13401340
for name, values in table.orderpairs(envs) do
13411341
if type(values) == "table" then

xmake/core/sandbox/modules/import/lib/detect/find_program.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ end
7474

7575
-- check program
7676
function sandbox_lib_detect_find_program._check(program, opt)
77+
opt = opt or {}
7778
local findname = program
7879
if os.subhost() == "windows" then
79-
if not program:endswith(".exe") and not program:endswith(".cmd") and not program:endswith(".bat") then
80+
if not opt.shell and not program:endswith(".exe") and not program:endswith(".cmd") and not program:endswith(".bat") then
8081
findname = program .. ".exe"
8182
end
8283
elseif os.subhost() == "msys" and os.isfile(program) and os.filesize(program) < 256 then

xmake/modules/detect/tools/find_cosmoar.lua

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import("lib.detect.find_program")
3636
function main(opt)
3737
opt = opt or {}
3838
opt.shell = true
39+
opt.envs = opt.envs or {PATH = os.getenv("PATH")}
3940
local program = find_program(opt.program or "cosmoar", opt)
4041
if program and is_host("windows") then
4142
program = program:gsub("\\", "/")

xmake/modules/detect/tools/find_cosmocc.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import("lib.detect.find_programver")
3737
function main(opt)
3838
opt = opt or {}
3939
opt.shell = true
40-
opt.envs = {PATH = os.getenv("PATH")}
40+
opt.envs = opt.envs or {PATH = os.getenv("PATH")}
4141
local program = find_program(opt.program or "cosmocc", opt)
4242
if program and is_host("windows") then
4343
program = program:gsub("\\", "/")

xmake/modules/detect/tools/find_cosmocxx.lua

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import("lib.detect.find_programver")
3737
function main(opt)
3838
opt = opt or {}
3939
opt.shell = true
40+
opt.envs = opt.envs or {PATH = os.getenv("PATH")}
4041
local program = find_program(opt.program or "cosmoc++", opt)
4142
if program and is_host("windows") then
4243
program = program:gsub("\\", "/")

xmake/toolchains/cosmocc/check.lua

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function main(toolchain)
3232
local bindir = toolchain:bindir()
3333

3434
-- find cross toolchain from external envirnoment
35+
local envs
3536
local cross_toolchain = find_cross_toolchain(sdkdir, {bindir = bindir})
3637
if not cross_toolchain then
3738
-- find it from packages
@@ -40,6 +41,11 @@ function main(toolchain)
4041
if installdir and os.isdir(installdir) then
4142
cross_toolchain = find_cross_toolchain(installdir)
4243
if cross_toolchain then
44+
-- we need to bind msys2 shell envirnoments for calling cosmocc,
45+
-- @see https://github.com/xmake-io/xmake/issues/5552
46+
if is_subhost("windows") then
47+
envs = package:envs()
48+
end
4349
break
4450
end
4551
end
@@ -57,9 +63,13 @@ function main(toolchain)
5763
toolchain:config_set("cross", cross_toolchain.cross)
5864
toolchain:config_set("bindir", cross_toolchain.bindir)
5965
toolchain:config_set("sdkdir", cross_toolchain.sdkdir)
66+
if envs then
67+
toolchain:config_set("envs", envs)
68+
end
6069
toolchain:configs_save()
6170
else
6271
raise("cosmocc toolchain not found!")
6372
end
6473
return cross_toolchain
6574
end
75+

xmake/toolchains/cosmocc/xmake.lua

+8
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ toolchain("cosmocc")
4545
toolchain:set("toolset", "ranlib", "aarch64-linux-cosmo-ranlib")
4646
toolchain:set("toolset", "strip", "aarch64-linux-cosmo-strip")
4747
end
48+
-- @see https://github.com/xmake-io/xmake/issues/5552
49+
local envs = toolchain:config("envs")
50+
if envs then
51+
for k, v in pairs(envs) do
52+
toolchain:add("runenvs", k, v)
53+
end
54+
end
4855
end)
56+

xmake/toolchains/emcc/xmake.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ toolchain("emcc")
5858
toolchain:add("ldflags", "")
5959
toolchain:add("shflags", "")
6060
for _, package in ipairs(toolchain:packages()) do
61-
local envs = package:get("envs")
61+
local envs = package:envs()
6262
if envs then
6363
for _, name in ipairs({"EMSDK", "EMSDK_NODE", "EMSDK_PYTHON", "JAVA_HOME"}) do
6464
local values = envs[name]

xmake/toolchains/iverilog/xmake.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ toolchain("iverilog")
2828
import("lib.detect.find_tool")
2929
local paths = {}
3030
for _, package in ipairs(toolchain:packages()) do
31-
local envs = package:get("envs")
31+
local envs = package:envs()
3232
if envs then
3333
table.join2(paths, envs.PATH)
3434
end

xmake/toolchains/nim/xmake.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ toolchain("nim")
2626
import("lib.detect.find_tool")
2727
local paths = {}
2828
for _, package in ipairs(toolchain:packages()) do
29-
local envs = package:get("envs")
29+
local envs = package:envs()
3030
if envs then
3131
table.join2(paths, envs.PATH)
3232
end

xmake/toolchains/verilator/xmake.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ toolchain("verilator")
2626
import("lib.detect.find_tool")
2727
local paths = {}
2828
for _, package in ipairs(toolchain:packages()) do
29-
local envs = package:get("envs")
29+
local envs = package:envs()
3030
if envs then
3131
table.join2(paths, envs.PATH)
3232
end
@@ -46,7 +46,7 @@ toolchain("verilator")
4646
on_load(function (toolchain)
4747
if is_host("windows") then
4848
for _, package in ipairs(toolchain:packages()) do
49-
local envs = package:get("envs")
49+
local envs = package:envs()
5050
if envs then
5151
local verilator_root = envs.VERILATOR_ROOT
5252
if verilator_root then

xmake/toolchains/zig/xmake.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ toolchain("zig")
3030
import("lib.detect.find_tool")
3131
local paths = {}
3232
for _, package in ipairs(toolchain:packages()) do
33-
local envs = package:get("envs")
33+
local envs = package:envs()
3434
if envs then
3535
table.join2(paths, envs.PATH)
3636
end

0 commit comments

Comments
 (0)