From be399abceea8da01cbcd2e5391bc6aead52cfcfe Mon Sep 17 00:00:00 2001 From: Saikari Date: Thu, 29 Jan 2026 12:24:27 +0300 Subject: Add is_pefile function to check for PE file signatures on Windows --- xmake/core/base/os.lua | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'xmake/core/base/os.lua') diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 6c4c1fa48..351dbd68c 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -1151,23 +1151,39 @@ end function os.isexec(filepath) assert(filepath) - -- TODO - -- check permission + if os.host() == "windows" then + local exts = {".exe", ".cmd", ".bat", ".ps1", ".sh"} + if os.isfile(filepath) then + -- detect file extension first + local extension = path.extension(filepath):lower() + if extension then + if table.contains(exts, extension) then + return true + end + end - -- check executable program exist - if os.isfile(filepath) then - if os.host() == "windows" then - return true - else - return os._access(filepath, "x") + -- detect file header + if winos.is_pefile(filepath) then + return true + end + + -- detect shebang scripts + local file = io.open(filepath, "rb") + if file then + local header = file:read(2) + file:close() + if header == "#!" then + return true + end + end end - end - if os.host() == "windows" then - for _, suffix in ipairs({".exe", ".cmd", ".bat", ".ps1"}) do + for _, suffix in ipairs(exts) do if os.isfile(filepath .. suffix) then return true end end + elseif os.isfile(filepath) then + return os._access(filepath, "x") end return false end -- cgit v1.3.1