Skip to content

Commit

Permalink
improve to select script
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Mar 5, 2024
1 parent a5e284b commit 53c7d75
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
6 changes: 3 additions & 3 deletions tests/modules/private/select_script/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function test_plat_arch(t)
t:require(_match_pattern("macosx|!i386", {plat = "macosx", arch = "x86_64"}))
t:require(_match_pattern("!macosx|!i386", {plat = "linux", arch = "x86_64"}))
t:require(_match_pattern("windows|!x86", {plat = "windows", arch = "x64"}))
-- t:require_not(_match_pattern("windows|!x86", {plat = "android", arch = "arm64-v8a"}))
t:require_not(_match_pattern("windows|!x86", {plat = "android", arch = "arm64-v8a"}))
t:require(_match_pattern("macosx|native", {plat = "macosx", arch = "x86_64", subarch = "x86_64"}))
t:require(_match_pattern("macosx|!native", {plat = "macosx", arch = "arm64", subarch = "x86_64"}))
t:require_not(_match_pattern("macosx|!native", {plat = "macosx", arch = "x86_64", subarch = "x86_64"}))
Expand Down Expand Up @@ -68,7 +68,7 @@ function test_subhost_subarch(t)
t:require(_match_pattern("@macosx|!i386", {subhost = "macosx", subarch = "x86_64"}))
t:require(_match_pattern("@!macosx|!i386", {subhost = "linux", subarch = "x86_64"}))
t:require(_match_pattern("@windows|!x86", {subhost = "windows", subarch = "x64"}))
-- t:require_not(_match_pattern("@windows|!x86", {subhost = "android", subarch = "arm64-v8a"}))
t:require_not(_match_pattern("@windows|!x86", {subhost = "android", subarch = "arm64-v8a"}))
t:require(_match_pattern("@macosx|native", {subhost = "macosx", subarch = "x86_64"}))
t:require(_match_pattern("@macosx|native", {subhost = "macosx", subarch = "arm64"}))
t:require_not(_match_pattern("@macosx|!native", {subhost = "macosx", subarch = "x86_64"}))
Expand Down Expand Up @@ -107,5 +107,5 @@ function test_plat_arch_subhost_subarch(t)
t:require(_match_pattern("!iphon*|x86_64@macosx|x86_64", {plat = "linux", subhost = "macosx", arch = "x86_64", subarch = "x86_64"}))
t:require(_match_pattern("iphon*|arm64@macosx|x86_64", {plat = "iphoneos", subhost = "macosx", arch = "arm64", subarch = "x86_64"}))
t:require_not(_match_pattern("iphon*|arm64@macosx|x86_64", {plat = "iphoneos", subhost = "linux", arch = "arm64", subarch = "x86_64"}))
-- t:require(_match_pattern("android|native@macosx|x86_64", {plat = "android", subhost = "macosx", arch = "x86_64", subarch = "x86_64"}))
t:require(_match_pattern("android|native@macosx|x86_64", {plat = "android", subhost = "macosx", arch = "x86_64", subarch = "x86_64"}))
end
48 changes: 32 additions & 16 deletions xmake/core/base/private/select_script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,44 @@ function _match_pattern(pattern, plat, arch, opt)
local excluded = opt.excluded
local subhost = opt.subhost or os.subhost()
local subarch = opt.subarch or os.subarch()
local splitinfo = pattern:split("|", {strict = true, plain = true})
local pattern_plat = splitinfo[1]
local pattern_arch = splitinfo[2]
if pattern_plat and #pattern_plat > 0 then
local matched = false
local is_excluded_pattern = pattern_plat:find('!', 1, true)
if excluded and is_excluded_pattern then
matched = not ('!' .. plat):match('^' .. pattern_plat .. '$')
elseif not is_excluded_pattern then
matched = plat:match('^' .. pattern_plat .. '$')
end
if not matched then
return false
end
end
if pattern_arch and #pattern_arch > 0 then
-- support native arch, e.g. macosx|native
-- @see https://github.com/xmake-io/xmake/issues/4657
pattern_arch = pattern_arch:gsub("native", subarch)

-- support native arch, e.g. macosx|native
-- @see https://github.com/xmake-io/xmake/issues/4657
if pattern:find("native", 1, true) then
local splitinfo = pattern:split("|")
local pattern_plat = splitinfo[1]
local pattern_arch = splitinfo[2]
if pattern_arch and pattern_plat:trim("!") == subhost then
pattern_arch = pattern_arch:gsub("native", subarch)
pattern = pattern_plat .. "|" .. pattern_arch
local matched = false
local is_excluded_pattern = pattern_arch:find('!', 1, true)
if excluded and is_excluded_pattern then
matched = not ('!' .. arch):match('^' .. pattern_arch .. '$')
elseif not is_excluded_pattern then
matched = arch:match('^' .. pattern_arch .. '$')
end
if not matched then
return false
end
end
local is_excluded_pattern = pattern:find('!', 1, true)
if excluded and is_excluded_pattern then
return not ('!' .. plat .. '|' .. arch):match('^' .. pattern .. '$') and
not (plat .. '|!' .. arch):match('^' .. pattern .. '$') and
not ('!' .. plat):match('^' .. pattern .. '$')
elseif not is_excluded_pattern then
return (plat .. '|' .. arch):match('^' .. pattern .. '$') or plat:match('^' .. pattern .. '$')
if not pattern_plat and not pattern_arch then
os.raise("invalid script pattern: %s", pattern)
end
return true
end


-- match patterns
function _match_patterns(patterns, plat, arch, opt)
for _, pattern in ipairs(patterns) do
Expand Down

0 comments on commit 53c7d75

Please sign in to comment.