diff options
| author | ruki <[email protected]> | 2018-04-18 22:37:43 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-04-18 09:46:23 +0800 |
| commit | 383f44f85f2fd209a65d3bc58b28558ce5028051 (patch) | |
| tree | 71d42a9f065960e13cd67c04165d42adfa89a761 | |
| parent | 00004a01cd1ed305cbe7f1eaf7b50519464d0b32 (diff) | |
add before_build_file and after_build_file
| -rw-r--r-- | tests/apis/rules/xmake.lua | 6 | ||||
| -rw-r--r-- | xmake/actions/build/kinds/object.lua | 77 | ||||
| -rw-r--r-- | xmake/core/project/rule.lua | 4 | ||||
| -rw-r--r-- | xmake/rules/qt/xmake.lua | 9 |
4 files changed, 61 insertions, 35 deletions
diff --git a/tests/apis/rules/xmake.lua b/tests/apis/rules/xmake.lua index b1211e4bd..25640965c 100644 --- a/tests/apis/rules/xmake.lua +++ b/tests/apis/rules/xmake.lua @@ -22,6 +22,9 @@ rule("man") -- define rule: c code rule("c code") add_imports("core.tool.compiler") + before_build_file(function (target, sourcefile) + print("before_build_file: ", sourcefile) + end) on_build_file(function (target, sourcefile) local objectfile_o = os.tmpfile() .. ".o" local sourcefile_c = os.tmpfile() .. ".c" @@ -29,6 +32,9 @@ rule("c code") compiler.compile(sourcefile_c, objectfile_o) table.insert(target:objectfiles(), objectfile_o) end) + after_build_file(function (target, sourcefile) + print("after_build_file: ", sourcefile) + end) -- define rule: stub3 rule("stub3") diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index 14d30f5bb..37707a0ae 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -295,7 +295,7 @@ function _build_pcheaderfiles(target, buildinfo) end -- build source files with the custom rule -function _build_files_with_rule(target, buildinfo, sourcebatch, jobs) +function _build_files_with_rule(target, buildinfo, sourcebatch, jobs, suffix) -- the rule name local rulename = sourcebatch.rulename @@ -305,42 +305,48 @@ function _build_files_with_rule(target, buildinfo, sourcebatch, jobs) assert(ruleinst, "unknown rule: %s", rulename) -- on_build_files? - local on_build_files = ruleinst:script("build_files") + local on_build_files = ruleinst:script("build_files" .. (suffix and ("_" .. suffix) or "")) if on_build_files then on_build_files(target, sourcebatch.sourcefiles) else -- get the build file script - local on_build_file = ruleinst:script("build_file") - assert(on_build_file, "rule(%s): on_build_file() script not found!", rulename) + local on_build_file = ruleinst:script("build_file" .. (suffix and ("_" .. suffix) or "")) + if on_build_file then - -- run build jobs for each source file - local curdir = os.curdir() - process.runjobs(function (index) + -- run build jobs for each source file + local curdir = os.curdir() + process.runjobs(function (index) - -- force to set the current directory first because the other jobs maybe changed it - os.cd(curdir) + -- force to set the current directory first because the other jobs maybe changed it + os.cd(curdir) - -- calculate percent - local percent = ((buildinfo.targetindex + (_g.sourceindex + index - 1) / _g.sourcecount) * 100 / buildinfo.targetcount) + -- calculate percent + local percent = ((buildinfo.targetindex + (_g.sourceindex + index - 1) / _g.sourcecount) * 100 / buildinfo.targetcount) + if suffix then + percent = ((buildinfo.targetindex + (suffix == "before" and _g.sourceindex or _g.sourcecount) / _g.sourcecount) * 100 / buildinfo.targetcount) + end - -- the source file - local sourcefile = sourcebatch.sourcefiles[index] + -- the source file + local sourcefile = sourcebatch.sourcefiles[index] - -- trace percent info - if option.get("verbose") then - cprint("${green}[%02d%%]:${dim} compiling.%s %s", percent, rulename, sourcefile) - else - cprint("${green}[%02d%%]:${clear} compiling.%s %s", percent, rulename, sourcefile) - end + -- trace percent info + if option.get("verbose") then + cprint("${green}[%02d%%]:${dim} compiling.%s %s", percent, rulename, sourcefile) + else + cprint("${green}[%02d%%]:${clear} compiling.%s %s", percent, rulename, sourcefile) + end - -- do build file - on_build_file(target, sourcefile) + -- do build file + on_build_file(target, sourcefile) - end, #sourcebatch.sourcefiles, jobs) + end, #sourcebatch.sourcefiles, jobs) + end end -- update object index - _g.sourceindex = _g.sourceindex + #sourcebatch.sourcefiles + if not suffix then + _g.sourceindex = _g.sourceindex + #sourcebatch.sourcefiles + end end -- build objects for the given target @@ -360,27 +366,34 @@ function build(target, buildinfo) end -- build precompiled headers - _build_pcheaderfiles(target, buildinfo) + _build_pcheaderfiles(target, buildinfo) + + -- build source batches with custom rules before building other sources + for sourcekind, sourcebatch in pairs(target:sourcebatches()) do + if sourcebatch.rulename then + _build_files_with_rule(target, buildinfo, sourcebatch, jobs, "before") + end + end -- build source batches for sourcekind, sourcebatch in pairs(target:sourcebatches()) do - -- compile source files using the custom rule + -- compile source files with custom rule if sourcebatch.rulename then - - -- build source files with the custom rule _build_files_with_rule(target, buildinfo, sourcebatch, jobs) - -- compile source files to single object at once elseif type(sourcebatch.objectfiles) == "string" then - - -- build single object _build_single_object(target, buildinfo, sourcekind, sourcebatch, jobs, ccache) else - - -- build each objects _build_each_objects(target, buildinfo, sourcekind, sourcebatch, jobs, ccache) end end + + -- build source batches with custom rules after building other sources + for sourcekind, sourcebatch in pairs(target:sourcebatches()) do + if sourcebatch.rulename then + _build_files_with_rule(target, buildinfo, sourcebatch, jobs, "after") + end + end end diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua index 9d27f22a0..6d35bc36a 100644 --- a/xmake/core/project/rule.lua +++ b/xmake/core/project/rule.lua @@ -172,6 +172,8 @@ function rule.apis() -- rule.before_xxx , "rule.before_run" , "rule.before_build" + , "rule.before_build_file" + , "rule.before_build_files" , "rule.before_clean" , "rule.before_package" , "rule.before_install" @@ -179,6 +181,8 @@ function rule.apis() -- rule.after_xxx , "rule.after_run" , "rule.after_build" + , "rule.after_build_file" + , "rule.after_build_files" , "rule.after_clean" , "rule.after_package" , "rule.after_install" diff --git a/xmake/rules/qt/xmake.lua b/xmake/rules/qt/xmake.lua index 245c45bbf..d27259d03 100644 --- a/xmake/rules/qt/xmake.lua +++ b/xmake/rules/qt/xmake.lua @@ -86,8 +86,8 @@ rule("qt.ui") target:data_set("qt.uic", uic) end) - -- on build file - on_build_file(function (target, sourcefile_ui) + -- before build file + before_build_file(function (target, sourcefile_ui) -- imports import("core.project.config") @@ -103,7 +103,10 @@ rule("qt.ui") end -- compile ui - os.vrunv(ui, {sourcefile_ui, "-o", headerfile_ui}) + os.vrunv(uic, {sourcefile_ui, "-o", headerfile_ui}) + + -- add includedirs + target:add("includedirs", path.absolute(headerfile_dir, os.projectdir())) -- add clean files target:data_add("qt.cleanfiles", headerfile_ui) |
