diff options
Diffstat (limited to 'xmake')
| -rw-r--r-- | xmake/actions/build/kinds/object.lua | 17 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 32 | ||||
| -rw-r--r-- | xmake/modules/core/tools/gcc.lua | 33 |
3 files changed, 71 insertions, 11 deletions
diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index 045045041..2b801aeb0 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -27,6 +27,7 @@ import("core.base.option") import("core.tool.compiler") import("core.tool.extractor") import("core.project.config") +import("core.language.language") import("detect.tools.find_ccache") -- build the object from the *.[o|obj] source file @@ -101,10 +102,7 @@ function _build_object(target, buildinfo, index, sourcebatch, ccache) table.insert(depfiles, sourcefile) -- add precompiled header to the dependent files - local precompiled_header = target:get("precompiled_header") - if precompiled_header then - table.insert(depfiles, precompiled_header) - end + table.join2(depfiles, target:pcsourcefile()) -- check the dependent files are modified? local modified = false @@ -232,7 +230,7 @@ end function _build_precompiled_header(target, buildinfo) -- get the precompiled header - local precompiled_header = target:get("precompiled_header") + local precompiled_header, precompiled_source = target:pcsourcefile() if not precompiled_header then return end @@ -244,7 +242,14 @@ function _build_precompiled_header(target, buildinfo) local sourcefile = precompiled_header local objectfile = target:pcheaderfile() local incdepfile = objectfile .. ".d" - local sourcekind = sourcekinds[path.extension(precompiled_header)] or "cc" + + -- init sourcekind + local sourcekind = nil + if precompiled_source then + sourcekind = language.sourcekind_of(precompiled_source) + else + sourcekind = sourcekinds[path.extension(precompiled_header)] or "cc" + end -- init source batch local sourcebatch = {sourcekind = sourcekind, sourcefiles = {sourcefile}, objectfiles = {objectfile}, incdepfiles = {incdepfile}} diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index ee033a892..7999ee2de 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -711,11 +711,41 @@ function target:configheader(outputdir) return configheader, outputheader end +-- get the precompiled source file (.[h|hpp], .[c|cpp]) +function target:pcsourcefile() + + -- get it from the cache first + if self._PCSOURCEFILE then + return unpack(self._PCSOURCEFILE) + end + + -- get the precompiled header and source file + local headerfile = nil + local sourcefile = nil + for _, file in ipairs(table.wrap(self:get("precompiled_header"))) do + local extension = path.extension(file) + if not headerfile and extension:startswith(".h") then + headerfile = file + else + sourcefile = file + end + if headerfile and sourcefile then + break + end + end + + -- save to cache + self._PCSOURCEFILE = {headerfile, sourcefile} + + -- ok? + return headerfile, sourcefile +end + -- get the precompiled header file (xxx.h.pch) function target:pcheaderfile() -- get the precompiled header file in the object directory - local precompiled_header = self:get("precompiled_header") + local precompiled_header = self:pcsourcefile() if precompiled_header then -- get it from the cache first diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 4f53cc19c..254f3301a 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -252,10 +252,13 @@ end -- make the precompiled header flag function nf_precompiled_header(self, headerfile, target) - if self:name() == "clang" then - return "-include " .. headerfile .. " -include-pch " .. target:pcheaderfile() - else - return "-include " .. headerfile + local extension = path.extension(headerfile) + if extension == ".h" or extension == ".hpp" then + if self:name() == "clang" then + return "-include " .. headerfile .. " -include-pch " .. target:pcheaderfile() + else + return "-include " .. headerfile + end end end @@ -280,6 +283,15 @@ function _include_deps(self, sourcefile, flags) -- the temporary file local tmpfile = os.tmpfile() + -- uses pchflags for precompiled header + if _g._PCHFLAGS then + local key = sourcefile .. tostring(flags) + local pchflags = _g._PCHFLAGS[key] + if pchflags then + flags = pchflags + end + end + -- generate it os.runv(self:program(), table.join("-c", "-E", "-MM", flags or {}, "-o", tmpfile, sourcefile)) @@ -304,6 +316,10 @@ end -- make the complie arguments list for the precompiled header function _compargv1_pch(self, headerfile, objectfile, flags) + -- init key and cache + local key = headerfile .. tostring(flags) + _g._PCHFLAGS = _g._PCHFLAGS or {} + -- remove "-include xxx.h" and "-include-pch xxx.pch" local pchflags = {} local include = false @@ -316,6 +332,15 @@ function _compargv1_pch(self, headerfile, objectfile, flags) end end + -- compile header.h as c++? + if self:kind() == "cxx" and headerfile:endswith(".h") then + table.insert(pchflags, "-x") + table.insert(pchflags, "c++-header") + end + + -- save pchflags to cache + _g._PCHFLAGS[key] = pchflags + -- make complie arguments list return self:program(), table.join("-c", pchflags, "-o", objectfile, headerfile) end |
