summaryrefslogtreecommitdiff
path: root/xmake/core
diff options
context:
space:
mode:
authorSaikari <[email protected]>2026-01-29 12:24:27 +0300
committerSaikari <[email protected]>2026-01-29 12:24:27 +0300
commitbe399abceea8da01cbcd2e5391bc6aead52cfcfe (patch)
treefd2592b035938ea622a720add6165dc1baddb124 /xmake/core
parent38a8b6998f5ef826725ed565b46d50f097840ed0 (diff)
Add is_pefile function to check for PE file signatures on Windows
Diffstat (limited to 'xmake/core')
-rw-r--r--xmake/core/base/os.lua38
1 files changed, 27 insertions, 11 deletions
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