summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-10-01 21:14:20 +0800
committerGitHub <[email protected]>2023-10-01 21:14:20 +0800
commit4ea0573047db301f1569c9d6b0d117f92433db49 (patch)
treef495cd7b7286c3d81f5a209515b662da4f0c5002
parent15e5f277191e8a088998d0f797dd1f44b5491e17 (diff)
parent85a605758c47eaa92b0a22b028cc4896eceeaf3f (diff)
Merge pull request #4253 from A2va/orderkeys-sort-function
Sort keys by callback function
-rw-r--r--tests/modules/table/test.lua15
-rw-r--r--xmake/core/base/table.lua9
-rw-r--r--xmake/core/project/target.lua3
-rw-r--r--xmake/plugins/project/cmake/cmakelists.lua9
-rw-r--r--xmake/plugins/project/ninja/build_ninja.lua3
-rw-r--r--xmake/plugins/project/vstudio/impl/vs201x.lua6
6 files changed, 34 insertions, 11 deletions
diff --git a/tests/modules/table/test.lua b/tests/modules/table/test.lua
index 007782cf4..2b586e7a4 100644
--- a/tests/modules/table/test.lua
+++ b/tests/modules/table/test.lua
@@ -29,3 +29,18 @@ function test_unwrap(t)
local a = table.wrap_lock({1})
t:are_equal(table.unwrap(a), a)
end
+
+function test_orderkeys(t)
+ -- sort by modulo 2 then from the smallest to largest
+ local f = function(a, b)
+ if a % 2 == 0 and b % 2 ~= 0 then
+ return true
+ elseif b % 2 == 0 and a % 2 ~= 0 then
+ return false
+ end
+ return a < b
+ end
+
+ t:are_equal(table.orderkeys({[2] = 2, [1] = 1, [4] = 4, [3] = 3}, f), {2, 4, 1, 3})
+ t:are_equal(table.orderkeys({[1] = 1, [2] = 2, [3] = 3, [4] = 4}), {1, 2 , 3, 4})
+end
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua
index 84caf1bfa..a8050f0a3 100644
--- a/xmake/core/base/table.lua
+++ b/xmake/core/base/table.lua
@@ -428,9 +428,10 @@ function table.keys(tbl)
end
-- get order keys of a table
-function table.orderkeys(tbl)
+function table.orderkeys(tbl, callback)
+ local callback = type(callback) == "function" and callback or nil
local keys = table.keys(tbl)
- table.sort(keys)
+ table.sort(keys, callback)
return keys
end
@@ -439,11 +440,11 @@ end
-- for k, v in table.orderpairs(t) do
-- TODO
-- end
-function table.orderpairs(t)
+function table.orderpairs(t, callback)
if type(t) ~= "table" then
t = t ~= nil and {t} or {}
end
- local orderkeys = table.orderkeys(t)
+ local orderkeys = table.orderkeys(t, callback)
local i = 1
return function (t, k)
k = orderkeys[i]
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index b53291358..61501648f 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -1889,7 +1889,8 @@ function _instance:sourcekinds()
local sourcekinds = self._SOURCEKINDS
if not sourcekinds then
sourcekinds = {}
- for _, sourcebatch in table.orderpairs(self:sourcebatches()) do
+ local sourcebatches = self:sourcebatches()
+ for _, sourcebatch in table.orderpairs(sourcebatches) do
local sourcekind = sourcebatch.sourcekind
if sourcekind then
table.insert(sourcekinds, sourcekind)
diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua
index 483ffb7b8..7dcabd2a4 100644
--- a/xmake/plugins/project/cmake/cmakelists.lua
+++ b/xmake/plugins/project/cmake/cmakelists.lua
@@ -381,7 +381,8 @@ end
function _add_target_sources(cmakelists, target, outputdir)
local has_cuda = false
cmakelists:print("target_sources(%s PRIVATE", target:name())
- for _, sourcebatch in table.orderpairs(target:sourcebatches()) do
+ local sourcebatches = target:sourcebatches()
+ for _, sourcebatch in table.orderpairs(sourcebatches) do
if _sourcebatch_is_built(sourcebatch) then
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
cmakelists:print(" " .. _get_relative_unix_path(sourcefile, outputdir))
@@ -612,7 +613,8 @@ function _add_target_compile_options(cmakelists, target, outputdir)
end
-- add cflags/cxxflags for the specific source files
- for _, sourcebatch in table.orderpairs(target:sourcebatches()) do
+ local sourcebatches = target:sourcebatches()
+ for _, sourcebatch in table.orderpairs(sourcebatches) do
if _sourcebatch_is_built(sourcebatch) then
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
_add_target_sourcefiles_flags(cmakelists, target, sourcefile, "cxxflags", outputdir)
@@ -835,7 +837,8 @@ function _add_target_link_libraries(cmakelists, target, outputdir)
-- add other object files, maybe from custom rules
local objectfiles_set = hashset.new()
- for _, sourcebatch in table.orderpairs(target:sourcebatches()) do
+ local sourcebatches = target:sourcebatches()
+ for _, sourcebatch in table.orderpairs(sourcebatches) do
if _sourcebatch_is_built(sourcebatch) then
for _, objectfile in ipairs(sourcebatch.objectfiles) do
objectfiles_set:insert(objectfile)
diff --git a/xmake/plugins/project/ninja/build_ninja.lua b/xmake/plugins/project/ninja/build_ninja.lua
index 360bb075c..2e55faf05 100644
--- a/xmake/plugins/project/ninja/build_ninja.lua
+++ b/xmake/plugins/project/ninja/build_ninja.lua
@@ -366,7 +366,8 @@ function _add_build_for_target(ninjafile, target, outputdir)
ninjafile:print("")
-- build target objects
- for _, sourcebatch in table.orderpairs(target:sourcebatches()) do
+ local sourcebatches = target:sourcebatches()
+ for _, sourcebatch in table.orderpairs(sourcebatches) do
if _sourcebatch_is_built(sourcebatch) then
_add_build_for_objects(ninjafile, target, sourcebatch, outputdir)
end
diff --git a/xmake/plugins/project/vstudio/impl/vs201x.lua b/xmake/plugins/project/vstudio/impl/vs201x.lua
index e2389b8ba..3b6f145f4 100644
--- a/xmake/plugins/project/vstudio/impl/vs201x.lua
+++ b/xmake/plugins/project/vstudio/impl/vs201x.lua
@@ -213,7 +213,8 @@ function _make_custom_commands(target, vcxprojdir)
local commands = {}
_make_custom_commands_for_target(commands, target, vcxprojdir, "before")
_make_custom_commands_for_target(commands, target, vcxprojdir)
- for _, sourcebatch in table.orderpairs(target:sourcebatches()) do
+ local sourcebatches = target:sourcebatches()
+ for _, sourcebatch in table.orderpairs(sourcebatches) do
local rulename = sourcebatch.rulename
local sourcekind = sourcebatch.sourcekind
if rulename ~= "c.build" and rulename ~= "c++.build" and rulename ~= "asm.build" and rulename ~= "cuda.build" and sourcekind ~= "mrc" then
@@ -281,7 +282,8 @@ function _make_targetinfo(mode, arch, target, vcxprojdir)
local firstcompflags = nil
targetinfo.compflags = {}
targetinfo.compargvs = {}
- for _, sourcebatch in table.orderpairs(target:sourcebatches()) do
+ local sourcebatches = target:sourcebatches()
+ for _, sourcebatch in table.orderpairs(sourcebatches) do
local sourcekind = sourcebatch.sourcekind
local rulename = sourcebatch.rulename
if sourcekind then