diff options
| author | ruki <[email protected]> | 2017-08-01 13:04:10 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-08-01 13:04:10 +0800 |
| commit | 3e475513ba214e80ef524df95d5c315418f2a798 (patch) | |
| tree | 9a60f7066e5df9b8149739690bd955dbd1c9d318 | |
| parent | c87c400fcbb581c1590694bf6ee05d73d5d3e984 (diff) | |
add load compiler and linker api
| -rw-r--r-- | docs/manual.md | 6 | ||||
| -rw-r--r-- | docs/zh/manual.md | 6 | ||||
| -rw-r--r-- | xmake/actions/build/kinds/binary.lua | 24 | ||||
| -rw-r--r-- | xmake/actions/build/kinds/object.lua | 28 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/tool/compiler.lua | 13 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/tool/linker.lua | 13 |
7 files changed, 66 insertions, 26 deletions
diff --git a/docs/manual.md b/docs/manual.md index fc154c222..d9b1daa6d 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -5197,12 +5197,10 @@ compiler.compile("xxx.c", "xxx.o") 直接获取[compiler.compile](#compiler-compile)中执行的命令行字符串,相当于: ```lua -local cmdstr = compiler.compcmd("xxx.c", "xxx.o", {incdepfile = incdepfile, target = target}) +local cmdstr = compiler.compcmd("xxx.c", "xxx.o", {target = target}) ``` -注:后面`{incdepfile = incdepfile, target = target}`扩展参数部分是可选的,如果传递了target对象,那么生成的编译命令,会加上这个target配置对应的链接选项。 - -如果传递了incdepfile,那么还会生成头文件依赖列表文件`xxx.d` +注:后面`{target = target}`扩展参数部分是可选的,如果传递了target对象,那么生成的编译命令,会加上这个target配置对应的链接选项。 并且还可以自己传递各种配置,例如: diff --git a/docs/zh/manual.md b/docs/zh/manual.md index 1271e09ce..a35280d3c 100644 --- a/docs/zh/manual.md +++ b/docs/zh/manual.md @@ -5216,12 +5216,10 @@ compiler.compile("xxx.c", "xxx.o") 直接获取[compiler.compile](#compiler-compile)中执行的命令行字符串,相当于: ```lua -local cmdstr = compiler.compcmd("xxx.c", "xxx.o", {incdepfile = incdepfile, target = target}) +local cmdstr = compiler.compcmd("xxx.c", "xxx.o", {target = target}) ``` -注:后面`{incdepfile = incdepfile, target = target}`扩展参数部分是可选的,如果传递了target对象,那么生成的编译命令,会加上这个target配置对应的链接选项。 - -如果传递了incdepfile,那么还会生成头文件依赖列表文件`xxx.d` +注:后面`{target = target}`扩展参数部分是可选的,如果传递了target对象,那么生成的编译命令,会加上这个target配置对应的链接选项。 并且还可以自己传递各种配置,例如: diff --git a/xmake/actions/build/kinds/binary.lua b/xmake/actions/build/kinds/binary.lua index 31db2faed..0ad520585 100644 --- a/xmake/actions/build/kinds/binary.lua +++ b/xmake/actions/build/kinds/binary.lua @@ -29,7 +29,7 @@ import("core.tool.compiler") import("object") -- is modified? -function _is_modified(target, depfile, buildinfo) +function _is_modified(target, depfile, buildinfo, linker_instance) -- this target and it's deps are not modified? local modified = buildinfo.rebuild or buildinfo.modified[target:name()] @@ -50,8 +50,13 @@ function _is_modified(target, depfile, buildinfo) depinfo = io.load(depfile) or {} end - -- the command has been modified? - return target:linkcmd(objectfiles) ~= depinfo.command + -- the program has been modified? + if linker_instance:program() ~= depinfo.program then + return true + end + + -- the flags has been modified? + return os.args(linker_instance:linkflags({target = target})) ~= os.args(depinfo.flags) end -- build target from sources @@ -60,9 +65,12 @@ function _build_from_objects(target, buildinfo) -- build objects object.build(target, buildinfo) + -- load linker instance + local linker_instance = linker.load(target:targetkind(), target:sourcekinds()) + -- this target and it's deps are not modified? local depfile = target:depfile() - local modified = _is_modified(target, depfile, buildinfo) + local modified = _is_modified(target, depfile, buildinfo, linker_instance) if not modified then return end @@ -96,14 +104,14 @@ function _build_from_objects(target, buildinfo) -- trace verbose info if verbose then - print(target:linkcmd(objectfiles)) + print(linker_instance:linkcmd(objectfiles, targetfile, {target = target})) end -- link it - linker.link(target:get("kind"), target:sourcekinds(), objectfiles, targetfile, {target = target}) + linker_instance:link(objectfiles, targetfile, {target = target}) - -- save command to the dependent file - io.save(depfile, {command = target:linkcmd(objectfiles)}) + -- save program and flags to the dependent file + io.save(depfile, {program = linker_instance:program(), flags = linker_instance:linkflags({target = target})}) end -- build target from sources diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index 53df96bdd..bad46ae01 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -31,7 +31,7 @@ import("core.language.language") import("detect.tools.find_ccache") -- is modified? -function _is_modified(target, sourcefile, objectfile, depinfo, sourcekind, buildinfo) +function _is_modified(target, sourcefile, objectfile, depinfo, buildinfo, compiler_instance) -- rebuild? if buildinfo.rebuild then @@ -71,8 +71,13 @@ function _is_modified(target, sourcefile, objectfile, depinfo, sourcekind, build end end - -- the command has been modified? - return compiler.compcmd(sourcefile, objectfile, {target = target, sourcekind = sourcekind}) ~= depinfo.command + -- the program has been modified? + if compiler_instance:program() ~= depinfo.program then + return true + end + + -- the flags has been modified? + return os.args(compiler_instance:compflags({target = target})) ~= os.args(depinfo.flags) end -- build the object from the *.[o|obj] source file @@ -144,9 +149,12 @@ function _build_object(target, buildinfo, index, sourcebatch, ccache) if not buildinfo.rebuild and os.isfile(incdepfile) then depinfo = io.load(incdepfile) or {} end + + -- load compiler instance + local compiler_instance = compiler.load(sourcekind) -- is modified? - local modified = _is_modified(target, sourcefile, objectfile, depinfo, sourcekind, buildinfo) + local modified = _is_modified(target, sourcefile, objectfile, depinfo, buildinfo, compiler_instance) if not modified then return end @@ -165,19 +173,21 @@ function _build_object(target, buildinfo, index, sourcebatch, ccache) end -- trace verbose info - local command = compiler.compcmd(sourcefile, objectfile, {target = target, sourcekind = sourcekind}) if verbose then - print(command) + print(compiler_instance:compcmd(sourcefile, objectfile, {target = target})) end -- complie it - compiler.compile(sourcefile, objectfile, {depinfo = depinfo, target = target, sourcekind = sourcekind}) + compiler_instance:compile(sourcefile, objectfile, {depinfo = depinfo, target = target}) -- save sources to the dependent info depinfo.sources = {sourcefile, target:pcsourcefile()} - -- save command to the dependent info - depinfo.command = command + -- save program to the dependent info + depinfo.program = compiler_instance:program() + + -- save flags to the dependent info + depinfo.flags = compiler_instance:compflags({target = target}) -- save the dependent info io.save(incdepfile, depinfo) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 798feb4e2..ad01c67f1 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -161,7 +161,7 @@ function os.args(argv) -- make it local args = nil - for _, arg in ipairs(argv) do + for _, arg in ipairs(table.wrap(argv)) do arg = arg:trim() if #arg > 0 then arg = arg:gsub("([\"\\])", "\\%1") diff --git a/xmake/core/sandbox/modules/import/core/tool/compiler.lua b/xmake/core/sandbox/modules/import/core/tool/compiler.lua index 4566a50ba..35adf8413 100644 --- a/xmake/core/sandbox/modules/import/core/tool/compiler.lua +++ b/xmake/core/sandbox/modules/import/core/tool/compiler.lua @@ -34,6 +34,19 @@ local assert = require("sandbox/modules/assert") local import = require("sandbox/modules/import") local sandbox = require("sandbox/sandbox") +-- load the compiler from the given source kind +function sandbox_core_tool_compiler.load(sourcekind) + + -- get the compiler instance + local instance, errors = compiler.load(sourcekind) + if not instance then + raise(errors) + end + + -- ok + return instance +end + -- get the build mode of compiler function sandbox_core_tool_compiler.buildmode(sourcekind, name) diff --git a/xmake/core/sandbox/modules/import/core/tool/linker.lua b/xmake/core/sandbox/modules/import/core/tool/linker.lua index 33ded2ce2..2181594ac 100644 --- a/xmake/core/sandbox/modules/import/core/tool/linker.lua +++ b/xmake/core/sandbox/modules/import/core/tool/linker.lua @@ -30,6 +30,19 @@ local platform = require("platform/platform") local linker = require("tool/linker") local raise = require("sandbox/modules/raise") +-- load the linker from the given target kind +function sandbox_core_tool_linker.load(targetkind, sourcekinds) + + -- get the linker instance + local instance, errors = linker.load(targetkind, sourcekinds) + if not instance then + raise(errors) + end + + -- ok + return instance +end + -- make command for linking target file function sandbox_core_tool_linker.linkcmd(targetkind, sourcekinds, objectfiles, targetfile, opt) return os.args(table.join(sandbox_core_tool_linker.linkargv(targetkind, sourcekinds, objectfiles, targetfile, opt))) |
