summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaikari <[email protected]>2026-02-02 14:36:27 +0300
committerSaikari <[email protected]>2026-02-02 14:36:27 +0300
commit7294865c853930c4a50009cf3be1f8d1b302198d (patch)
tree84d9b30ed51e2153e4088672cb8398b7a93982bd
parent93d38be02f934c234dd6e62f5fd6cfd25d4d59c4 (diff)
refactor: simplify ape program file handling in test cases and remove unused functions
-rw-r--r--core/src/xmake/engine.c24
-rw-r--r--tests/modules/stdin/test.lua39
2 files changed, 27 insertions, 36 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index df5ae06be..7a76da7a1 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -874,6 +874,30 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t *engine, tb_char_t **arg
ssize_t size = readlink(XM_PROC_SELF_FILE, path, (size_t)maxn);
if (size > 0 && size < maxn) {
path[size] = '\0';
+#if defined(TB_CONFIG_OS_LINUX)
+ // fix /usr/bin/ape for cosmocc
+ if (size > 3 && !tb_strcmp(path + size - 3, "ape")) {
+ FILE* fp = fopen("/proc/self/cmdline", "rb");
+ if (fp) {
+ tb_char_t line[TB_PATH_MAXN * 2];
+ if (fread(line, 1, sizeof(line), fp) > 0) {
+ tb_char_t* p = line;
+ // if argv[0] is /usr/bin/ape, we use argv[1]
+ if (tb_strstr(p, "ape")) {
+ p += tb_strlen(p) + 1;
+ }
+ // get absolute path
+ if (p < line + sizeof(line) && *p) {
+ tb_char_t buf[TB_PATH_MAXN];
+ if (tb_path_absolute(p, buf, sizeof(buf))) {
+ tb_strlcpy(path, buf, maxn);
+ }
+ }
+ }
+ fclose(fp);
+ }
+ }
+#endif
ok = tb_true;
}
#elif defined(TB_CONFIG_OS_BSD) && defined(KERN_PROC_PATHNAME)
diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua
index af2954517..d92b0f1f8 100644
--- a/tests/modules/stdin/test.lua
+++ b/tests/modules/stdin/test.lua
@@ -1,31 +1,5 @@
import("core.base.binutils")
-function resolve_path(p)
- if path.is_absolute(p) then return p end
- local root = path.join(os.scriptdir(), "../../..")
- local p_root = path.join(root, p)
- if os.isfile(p_root) then return path.absolute(p_root) end
- if os.isfile(p) then return path.absolute(p) end
- return p
-end
-
-function fix_ape_programfile(xmake)
- if is_host("linux") and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then
- local content = io.readfile("/proc/self/cmdline", {encoding = "binary"})
- if content then
- local args = {}
- for arg in content:gmatch("[^\0]+") do
- table.insert(args, arg)
- end
- if #args >= 2 and (path.unix(args[1]) == xmake or path.filename(path.unix(args[1])) == path.filename(xmake)) then
- xmake = path.unix(args[2])
- xmake = resolve_path(xmake)
- end
- end
- end
- return xmake
-end
-
function test_shell(t, name, cmd, expect)
local outfile = os.tmpfile()
local errfile = os.tmpfile()
@@ -60,17 +34,10 @@ end
function main(t)
local xmake = path.translate(os.programfile())
- -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux
- xmake = fix_ape_programfile(xmake)
-
local is_ape = binutils.format(xmake) == "ape"
-
local run_stdin = string.format('"%s" l --stdin', xmake)
- if not is_host("windows") then
- run_stdin = string.format('env "%s" l --stdin', xmake)
- end
- -- Fix pwsh and cosmocc "exec format error" for MacOS
- if is_ape and not is_host("windows") then
+ -- Fix pwsh and cosmocc "exec format error" for MacOS
+ if is_ape and not is_host("windows") then
run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake)
end
@@ -78,7 +45,7 @@ function main(t)
if is_host("windows") then
-- Test cmd
test_shell(t, "cmd_single", string.format("cmd /c echo print 'hello_cmd' | %s l --stdin", xmake), "hello_cmd")
- test_shell(t, "cmd_calc", string.format('cmd /c echo local f = 1+1; print^(f^) | %s l --stdin', xmake), "2")
+ test_shell(t, "cmd_calc", string.format("cmd /c echo local f = 1+1; print^(f^) | %s l --stdin", xmake), "2")
test_shell(
t,
"cmd_multi_lines",