diff options
| author | ruki <[email protected]> | 2023-10-07 10:21:33 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-10-07 10:21:33 +0800 |
| commit | ebb301cf17c2aa25b1f698ce2e5bd2eb753a9bc5 (patch) | |
| tree | ea5f1de7c1a6c79339e3c1bdf43ae7418588acf7 | |
| parent | c12127b37c2d967a928cb039187d1ea65d958803 (diff) | |
auto build targets for tests
| -rw-r--r-- | tests/actions/test/xmake.lua | 4 | ||||
| -rw-r--r-- | xmake/actions/build/build.lua | 26 | ||||
| -rw-r--r-- | xmake/actions/build/check.lua | 8 | ||||
| -rw-r--r-- | xmake/actions/build/main.lua | 96 | ||||
| -rw-r--r-- | xmake/actions/test/main.lua | 156 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 2 |
6 files changed, 128 insertions, 164 deletions
diff --git a/tests/actions/test/xmake.lua b/tests/actions/test/xmake.lua index f02f50c0c..fac51a018 100644 --- a/tests/actions/test/xmake.lua +++ b/tests/actions/test/xmake.lua @@ -7,8 +7,8 @@ for _, file in ipairs(os.files("src/test_*.cpp")) do set_default(false) add_files("src/" .. name .. ".cpp") add_tests(name) - add_tests(name .. "_arg", "foo", "bar") - add_tests(name .. "_pass_output", "foo", {pass_output = "hello foo"}) + add_tests(name .. "_args", {arguments = {"foo", "bar"}}) + add_tests(name .. "_pass_output", {arguments = "foo", pass_output = "hello foo"}) add_tests(name .. "_fail_output", {fail_output = {"hello .*", "hello xmake"}}) end diff --git a/xmake/actions/build/build.lua b/xmake/actions/build/build.lua index 5cea43185..bb0b61cc1 100644 --- a/xmake/actions/build/build.lua +++ b/xmake/actions/build/build.lua @@ -229,19 +229,21 @@ function _add_batchjobs_for_target_and_deps(batchjobs, rootjob, jobrefs, target) end -- get batch jobs, @note we need to export it for private.diagnosis.dump_buildjobs -function get_batchjobs(targetname, group_pattern) +function get_batchjobs(targetnames, group_pattern) -- get root targets local targets_root = {} - if targetname then - local target = project.target(targetname) - if target then - table.insert(targets_root, target) - if option.get("rebuild") then - target:data_set("rebuilt", true) - if not option.get("shallow") then - for _, dep in ipairs(target:orderdeps()) do - dep:data_set("rebuilt", true) + if targetnames then + for _, targetname in ipairs(table.wrap(targetnames)) do + local target = project.target(targetname) + if target then + table.insert(targets_root, target) + if option.get("rebuild") then + target:data_set("rebuilt", true) + if not option.get("shallow") then + for _, dep in ipairs(target:orderdeps()) do + dep:data_set("rebuilt", true) + end end end end @@ -280,7 +282,7 @@ function get_batchjobs(targetname, group_pattern) end -- the main entry -function main(targetname, group_pattern) +function main(targetnames, group_pattern) -- enable distcc? local distcc @@ -289,7 +291,7 @@ function main(targetname, group_pattern) end -- build all jobs - local batchjobs = get_batchjobs(targetname, group_pattern) + local batchjobs = get_batchjobs(targetnames, group_pattern) if batchjobs and batchjobs:size() > 0 then local curdir = os.curdir() runjobs("build", batchjobs, {on_exit = function (errors) diff --git a/xmake/actions/build/check.lua b/xmake/actions/build/check.lua index e26e4f0d0..f468acfb1 100644 --- a/xmake/actions/build/check.lua +++ b/xmake/actions/build/check.lua @@ -44,13 +44,15 @@ function _show(str, opt) end end -function main(targetname, opt) +function main(targetnames, opt) opt = opt or {} -- get targets local targets = {} - if targetname then - table.insert(targets, project.target(targetname)) + if targetnames then + for _, targetname in ipairs(table.wrap(targetnames)) do + table.insert(targets, project.target(targetname)) + end else for _, target in pairs(project.targets()) do if target:is_enabled() then diff --git a/xmake/actions/build/main.lua b/xmake/actions/build/main.lua index 58456ba70..43897578a 100644 --- a/xmake/actions/build/main.lua +++ b/xmake/actions/build/main.lua @@ -129,61 +129,26 @@ function _on_exit(ok, errors) end end --- main -function main() - - -- try building it using third-party buildsystem if xmake.lua not exists - if not os.isfile(project.rootfile()) and _try_build() then - return - end - - -- post statistics before locking project - statistics.post() - - -- do action for remote? - if remote_build_action.enabled() then - return remote_build_action() - end - - -- lock the whole project - project.lock() - - -- config it first - local targetname - local group_pattern = option.get("group") - if group_pattern then - group_pattern = "^" .. path.pattern(group_pattern) .. "$" - else - targetname = option.get("target") - end - task.run("config", {}, {disable_dump = true}) - - -- enter project directory - local oldir = os.cd(project.directory()) - - -- clean up temporary files once a day - cleaner.cleanup() +-- build targets +function build_targets(targetnames, opt) + opt = opt or {} -- register exit callbacks os.atexit(_on_exit) - local build_time + local group_pattern = opt.group_pattern try { function () - local time = os.mclock() -- do rules before building _do_project_rules("build_before") -- do build - _do_build(targetname, group_pattern) + _do_build(targetnames, group_pattern) -- do check - check_targets(targetname, {build = true}) - - -- get build time - build_time = os.mclock() - time + check_targets(targetnames, {build = true}) -- dump cache stats if option.get("diagnosis") then @@ -206,8 +171,9 @@ function main() raise(errors) elseif group_pattern then raise("build targets with group(%s) failed!", group_pattern) - elseif targetname then - raise("build target: %s failed!", targetname) + elseif targetnames then + targetnames = table.wrap(targetnames) + raise("build target: %s failed!", table.concat(targetnames, ", ")) else raise("build target failed!") end @@ -217,13 +183,53 @@ function main() -- do rules after building _do_project_rules("build_after") +end - -- unlock the whole project - project.unlock() +function main() + + -- try building it using third-party buildsystem if xmake.lua not exists + if not os.isfile(project.rootfile()) and _try_build() then + return + end + + -- post statistics before locking project + statistics.post() + + -- do action for remote? + if remote_build_action.enabled() then + return remote_build_action() + end + + -- lock the whole project + project.lock() + + -- config it first + local targetname + local group_pattern = option.get("group") + if group_pattern then + group_pattern = "^" .. path.pattern(group_pattern) .. "$" + else + targetname = option.get("target") + end + task.run("config", {}, {disable_dump = true}) + + -- enter project directory + local oldir = os.cd(project.directory()) + + -- clean up temporary files once a day + cleaner.cleanup() + + -- build targets + local build_time = os.mclock() + build_targets(targetname, {group_pattern = group_pattern}) + build_time = os.mclock() - build_time -- leave project directory os.cd(oldir) + -- unlock the whole project + project.unlock() + -- trace local str = "" if build_time then diff --git a/xmake/actions/test/main.lua b/xmake/actions/test/main.lua index 667ae2a0e..3c113d661 100644 --- a/xmake/actions/test/main.lua +++ b/xmake/actions/test/main.lua @@ -29,6 +29,7 @@ import("devel.debugger") import("async.runjobs") import("private.action.run.runenvs") import("private.service.remote_build.action", {alias = "remote_build_action"}) +import("actions.build.main", {rootdir = os.programdir(), alias = "build_action"}) -- run target function _do_run_target(target) @@ -89,20 +90,6 @@ function _add_target_pkgenvs(target, targets_added) end end --- find target names matching a specific name -function _find_matching_target_names(targetname) - targetname = targetname:lower() - local matching_targetnames = {} - for _, target in ipairs(project.ordertargets()) do - if target:name():lower():find(targetname, 1, true) then - table.insert(matching_targetnames, target:name()) - end - end - - table.sort(matching_targetnames) - return matching_targetnames -end - -- run the given target function _run(target) @@ -151,53 +138,10 @@ function _run(target) os.setenvs(oldenvs) end --- check targets -function _check_targets(targetname, group_pattern) - - -- get targets - local targets = {} - if targetname then - local target = project.target(targetname) - if not target then - -- check if the name is part of other target to help - local possible_targetnames = _find_matching_target_names(targetname) - local errors = targetname .. " is not a valid target name for this project" - if #possible_targetnames > 0 then - errors = errors .. "\nlist of valid target names close to your input:\n - " .. table.concat(possible_targetnames, '\n - ') - end - raise(errors) - end - - table.insert(targets, target) - else - for _, target in ipairs(project.ordertargets()) do - if target:is_binary() or target:script("run") then - local group = target:get("group") - if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then - table.insert(targets, target) - end - end - end - end - - -- filter and check targets with builtin-run script - local targetnames = {} - for _, target in ipairs(targets) do - if target:targetfile() and target:is_enabled() and not target:script("run") then - local targetfile = target:targetfile() - if targetfile and not os.isfile(targetfile) then - table.insert(targetnames, target:name()) - end - end - end - - -- there are targets that have not yet been built? - if #targetnames > 0 then - raise("please run `$xmake build [target]` to build the following targets first:\n -> " .. table.concat(targetnames, '\n -> ')) - end +-- run tests +function _run_tests(tests) end --- main function main() -- do action for remote? @@ -205,6 +149,9 @@ function main() return remote_build_action() end + -- lock the whole project + project.lock() + -- load config first config.load() @@ -213,61 +160,68 @@ function main() -- get tests local tests = {} + local group_pattern = option.get("group") + if group_pattern then + group_pattern = "^" .. path.pattern(group_pattern) .. "$" + end for _, target in ipairs(project.ordertargets()) do if target:is_binary() or target:script("run") then - for name, argv in pairs(target:get("tests")) do + for _, name in ipairs(target:get("tests")) do + local info = {target = target} local extra = target:extraconf("tests", name) - print(name, extra) - tests[name] = table.join({argv = table.wrap(argv)}, extra) + if extra then + table.join2(info, extra) + end + if not info.group then + info.group = target:get("group") + end + if not info.rundir then + info.rundir = target:rundir() + end + if not info.runenvs then + local addenvs, setenvs = runenvs.make(target) + local envs = runenvs.join(addenvs, setenvs) + info.runenvs = envs + end + + local group = info.group + if (not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then + tests[name] = info + end end - --[[ - local group = target:get("group") - if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then - table.insert(targets, target) - end]] end end --- print(tests) - - --[[ - -- check targets first - local targetname - local group_pattern = option.get("group") - if group_pattern then - group_pattern = "^" .. path.pattern(group_pattern) .. "$" - else - targetname = option.get("target") + local test_patterns = option.get("tests") + if test_patterns then + local tests_new = {} + for _, pattern in ipairs(test_patterns) do + pattern = "^" .. path.pattern(pattern) .. "$" + for name, info in pairs(tests) do + if name:match(pattern) then + tests_new[name] = info + end + end + end + tests = tests_new end - _check_targets(targetname, group_pattern) -- enter project directory local oldir = os.cd(project.directory()) - -- run the given target? - if targetname then - _run(project.target(targetname)) - else - local targets = {} - for _, target in ipairs(project.ordertargets()) do - if target:is_binary() or target:script("run") then - local group = target:get("group") - if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then - table.insert(targets, target) - end - end - end - local jobs = tonumber(option.get("jobs") or "1") - runjobs("run_targets", function (index) - local target = targets[index] - if target then - _run(target) - end - end, {total = #targets, - comax = jobs, - isolate = true}) + -- build targets with the given tests first + local targetnames = {} + for _, info in table.orderpairs(tests) do + table.insert(targetnames, info.target:name()) end + build_action.build_targets(targetnames) + + -- run tests + _run_tests(tests) -- leave project directory - os.cd(oldir)]] + os.cd(oldir) + + -- unlock the whole project + project.unlock() end diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index b5673a668..56b7cb7b8 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -2455,6 +2455,7 @@ function target.apis() , "target.add_languages" , "target.add_vectorexts" , "target.add_toolchains" + , "target.add_tests" } , keyvalues = { @@ -2467,7 +2468,6 @@ function target.apis() -- target.add_xxx , "target.add_values" , "target.add_runenvs" - , "target.add_tests" } , paths = { |
