Skip to content

Commit

Permalink
Merge pull request #5553 from xmake-io/cosmocc
Browse files Browse the repository at this point in the history
improve cosmocc for windows
  • Loading branch information
waruqi authored Sep 2, 2024
2 parents 0d2bd15 + b7dbf58 commit f20c40b
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 10 deletions.
32 changes: 31 additions & 1 deletion xmake/core/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,36 @@ function os._run_exit_cbs(ok, errors)
end
end

-- get shell path, e.g. sh, bash
function os._get_shell_path(opt)
opt = opt or {}
local setenvs = opt.setenvs or opt.envs or {}
local addenvs = opt.addenvs or {}
local paths = {}
local p = setenvs.PATH
if type(p) == "string" then
p = path.splitenv(p)
end
if p then
table.join2(paths, p)
end
p = addenvs.PATH
if type(p) == "string" then
p = path.splitenv(p)
end
if p then
table.join2(paths, p)
end
for _, p in ipairs(paths) do
for _, name in ipairs({"sh", "bash"}) do
local filepath = path.join(p, name)
if os.isexec(filepath) then
return filepath
end
end
end
end

-- match files or directories
--
-- @param pattern the search pattern
Expand Down Expand Up @@ -808,7 +838,7 @@ function os.execv(program, argv, opt)
-- because `/bin/sh` is not real file path, maybe we need to convert it.
local host = os.host()
if host == "windows" then
filename = "sh"
filename = os._get_shell_path(opt) or "sh"
argv = table.join(shellfile, argv)
else
line = line:sub(3)
Expand Down
5 changes: 5 additions & 0 deletions xmake/core/project/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ function _instance:enable(enabled)
self:set("__enabled", enabled)
end

-- get environments
function _instance:envs()
return self:get("envs")
end

-- get the given rule
function _instance:rule(name)
return self:rules()[name]
Expand Down
2 changes: 1 addition & 1 deletion xmake/core/project/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ function _instance:pkgenvs()
end
end
for _, pkg in pkgs:orderkeys() do
local envs = pkg:get("envs")
local envs = pkg:envs()
if envs then
for name, values in table.orderpairs(envs) do
if type(values) == "table" then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ end

-- check program
function sandbox_lib_detect_find_program._check(program, opt)
opt = opt or {}
local findname = program
if os.subhost() == "windows" then
if not program:endswith(".exe") and not program:endswith(".cmd") and not program:endswith(".bat") then
if not opt.shell and not program:endswith(".exe") and not program:endswith(".cmd") and not program:endswith(".bat") then
findname = program .. ".exe"
end
elseif os.subhost() == "msys" and os.isfile(program) and os.filesize(program) < 256 then
Expand Down
1 change: 1 addition & 0 deletions xmake/modules/detect/tools/find_cosmoar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import("lib.detect.find_program")
function main(opt)
opt = opt or {}
opt.shell = true
opt.envs = opt.envs or {PATH = os.getenv("PATH")}
local program = find_program(opt.program or "cosmoar", opt)
if program and is_host("windows") then
program = program:gsub("\\", "/")
Expand Down
2 changes: 1 addition & 1 deletion xmake/modules/detect/tools/find_cosmocc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import("lib.detect.find_programver")
function main(opt)
opt = opt or {}
opt.shell = true
opt.envs = {PATH = os.getenv("PATH")}
opt.envs = opt.envs or {PATH = os.getenv("PATH")}
local program = find_program(opt.program or "cosmocc", opt)
if program and is_host("windows") then
program = program:gsub("\\", "/")
Expand Down
1 change: 1 addition & 0 deletions xmake/modules/detect/tools/find_cosmocxx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import("lib.detect.find_programver")
function main(opt)
opt = opt or {}
opt.shell = true
opt.envs = opt.envs or {PATH = os.getenv("PATH")}
local program = find_program(opt.program or "cosmoc++", opt)
if program and is_host("windows") then
program = program:gsub("\\", "/")
Expand Down
10 changes: 10 additions & 0 deletions xmake/toolchains/cosmocc/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function main(toolchain)
local bindir = toolchain:bindir()

-- find cross toolchain from external envirnoment
local envs
local cross_toolchain = find_cross_toolchain(sdkdir, {bindir = bindir})
if not cross_toolchain then
-- find it from packages
Expand All @@ -40,6 +41,11 @@ function main(toolchain)
if installdir and os.isdir(installdir) then
cross_toolchain = find_cross_toolchain(installdir)
if cross_toolchain then
-- we need to bind msys2 shell envirnoments for calling cosmocc,
-- @see https://github.com/xmake-io/xmake/issues/5552
if is_subhost("windows") then
envs = package:envs()
end
break
end
end
Expand All @@ -57,9 +63,13 @@ function main(toolchain)
toolchain:config_set("cross", cross_toolchain.cross)
toolchain:config_set("bindir", cross_toolchain.bindir)
toolchain:config_set("sdkdir", cross_toolchain.sdkdir)
if envs then
toolchain:config_set("envs", envs)
end
toolchain:configs_save()
else
raise("cosmocc toolchain not found!")
end
return cross_toolchain
end

8 changes: 8 additions & 0 deletions xmake/toolchains/cosmocc/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ toolchain("cosmocc")
toolchain:set("toolset", "ranlib", "aarch64-linux-cosmo-ranlib")
toolchain:set("toolset", "strip", "aarch64-linux-cosmo-strip")
end
-- @see https://github.com/xmake-io/xmake/issues/5552
local envs = toolchain:config("envs")
if envs then
for k, v in pairs(envs) do
toolchain:add("runenvs", k, v)
end
end
end)

2 changes: 1 addition & 1 deletion xmake/toolchains/emcc/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ toolchain("emcc")
toolchain:add("ldflags", "")
toolchain:add("shflags", "")
for _, package in ipairs(toolchain:packages()) do
local envs = package:get("envs")
local envs = package:envs()
if envs then
for _, name in ipairs({"EMSDK", "EMSDK_NODE", "EMSDK_PYTHON", "JAVA_HOME"}) do
local values = envs[name]
Expand Down
2 changes: 1 addition & 1 deletion xmake/toolchains/iverilog/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ toolchain("iverilog")
import("lib.detect.find_tool")
local paths = {}
for _, package in ipairs(toolchain:packages()) do
local envs = package:get("envs")
local envs = package:envs()
if envs then
table.join2(paths, envs.PATH)
end
Expand Down
2 changes: 1 addition & 1 deletion xmake/toolchains/nim/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ toolchain("nim")
import("lib.detect.find_tool")
local paths = {}
for _, package in ipairs(toolchain:packages()) do
local envs = package:get("envs")
local envs = package:envs()
if envs then
table.join2(paths, envs.PATH)
end
Expand Down
4 changes: 2 additions & 2 deletions xmake/toolchains/verilator/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ toolchain("verilator")
import("lib.detect.find_tool")
local paths = {}
for _, package in ipairs(toolchain:packages()) do
local envs = package:get("envs")
local envs = package:envs()
if envs then
table.join2(paths, envs.PATH)
end
Expand All @@ -46,7 +46,7 @@ toolchain("verilator")
on_load(function (toolchain)
if is_host("windows") then
for _, package in ipairs(toolchain:packages()) do
local envs = package:get("envs")
local envs = package:envs()
if envs then
local verilator_root = envs.VERILATOR_ROOT
if verilator_root then
Expand Down
2 changes: 1 addition & 1 deletion xmake/toolchains/zig/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ toolchain("zig")
import("lib.detect.find_tool")
local paths = {}
for _, package in ipairs(toolchain:packages()) do
local envs = package:get("envs")
local envs = package:envs()
if envs then
table.join2(paths, envs.PATH)
end
Expand Down

0 comments on commit f20c40b

Please sign in to comment.