diff options
| author | ruki <[email protected]> | 2022-05-19 00:51:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-19 00:51:20 +0800 |
| commit | 0bb56c9a4efc55c6f478b49340607cb651656159 (patch) | |
| tree | a1d984d0f496db60a19a52712821b991e4321ab2 | |
| parent | d3b2139aacaf4efba9f47e16f36d6086f9e5544b (diff) | |
improve preprocess
| -rw-r--r-- | xmake/modules/core/tools/cl.lua | 70 | ||||
| -rw-r--r-- | xmake/modules/core/tools/gcc.lua | 63 | ||||
| -rw-r--r-- | xmake/modules/private/service/distcc_build/client.lua | 14 |
3 files changed, 81 insertions, 66 deletions
diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index 4966d81da..1d13a51bd 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -455,8 +455,43 @@ function _preprocess(program, argv, opt) table.insert(cppflags, "-P") table.insert(cppflags, "-Fi" .. cppfile) table.insert(cppflags, sourcefile) - local ok = try{ function() vstool.runv(program, winos.cmdargv(cppflags), opt); return true end} - return ok, sourcefile, objectfile, cppfile, flags + return try{ function() + local outdata, errdata = vstool.iorunv(program, winos.cmdargv(cppflags), opt) + return {outdata = outdata, errdata = errdata, sourcefile = sourcefile, objectfile = objectfile, cppfile = cppfile, cppflags = flags} + end} +end + +-- do compile +function _compile(self, sourcefile, objectfile, compflags, opt) + local cppinfo + if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then + local program, argv = compargv(self, sourcefile, objectfile, compflags, table.join(opt, {rawargs = true})) + cppinfo = distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), + preprocess = _preprocess, tool = self, target = opt.target}) + elseif build_cache.is_enabled() and build_cache.is_supported(self:kind()) then + local program, argv = compargv(self, sourcefile, objectfile, compflags, table.join(opt, {rawargs = true})) + local cppinfo = _preprocess(program, argv, {envs = self:runenvs(), target = opt.target}) + if cppinfo then + local cachekey + cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) + local objectfile_cached = build_cache.get(cachekey) + if objectfile_cached then + os.cp(objectfile_cached, cppinfo.objectfile) + else + vstool.iorunv(program, winos.cmdargv(table.join(cppinfo.cppflags, "-Fo" .. cppinfo.objectfile, cppinfo.cppfile)), {envs = self:runenvs()}) + if cachekey then + build_cache.put(cachekey, cppinfo.objectfile) + end + end + os.rm(cppinfo.cppfile) + end + end + if cppinfo then + return cppinfo.outdata, cppinfo.errdata + else + local program, argv = compargv(self, sourcefile, objectfile, compflags, opt) + return vstool.iorunv(program, argv, {envs = self:runenvs()}) + end end -- make the compile arguments list @@ -510,35 +545,8 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) end end - -- use vstool to compile and enable vs_unicode_output @see https://github.com/xmake-io/xmake/issues/528 - local preprocessed = false - if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then - local program, argv = compargv(self, sourcefile, objectfile, compflags, table.join(opt, {rawargs = true})) - preprocessed = distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), - preprocess = _preprocess, tool = self, target = opt.target}) - elseif build_cache.is_enabled() and build_cache.is_supported(self:kind()) then - local program, argv = compargv(self, sourcefile, objectfile, compflags, table.join(opt, {rawargs = true})) - local ok, _, objectfile_real, cppfile, cppflags = _preprocess(program, argv, {envs = self:runenvs(), target = opt.target}) - if ok then - local cachekey - cachekey = build_cache.cachekey(program, cppfile, cppflags, self:runenvs()) - local objectfile_cached = build_cache.get(cachekey) - if objectfile_cached then - os.cp(objectfile_cached, objectfile_real) - else - vstool.iorunv(program, winos.cmdargv(table.join(cppflags, "-Fo" .. objectfile_real, cppfile)), {envs = self:runenvs()}) - if cachekey then - build_cache.put(cachekey, objectfile_real) - end - end - preprocessed = true - os.rm(cppfile) - end - end - if not preprocessed then - local program, argv = compargv(self, sourcefile, objectfile, compflags, opt) - return vstool.iorunv(program, argv, {envs = self:runenvs()}) - end + -- do compile + return _compile(self, sourcefile, objectfile, compflags, opt) end, catch { diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 5c1a3f132..9645dad79 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -435,8 +435,40 @@ function _preprocess(program, argv, opt) table.insert(cppflags, "-o") table.insert(cppflags, cppfile) table.insert(cppflags, sourcefile) - local ok = try {function () os.runv(program, cppflags, opt); return true end} - return ok, sourcefile, objectfile, cppfile, flags + return try {function () + local outdata, errdata = os.iorunv(program, cppflags, opt) + return {outdata = outdata, errdata = errdata, sourcefile = sourcefile, objectfile = objectfile, cppfile = cppfile, cppflags = flags} + end} +end + +-- do compile +function _compile(self, sourcefile, objectfile, compflags, opt) + local cppinfo + local program, argv = compargv(self, sourcefile, objectfile, compflags) + if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then + cppinfo = distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), preprocess = _preprocess, tool = self}) + elseif build_cache.is_enabled() and build_cache.is_supported(self:kind()) then + local cppinfo = _preprocess(program, argv, opt) + if cppinfo then + local cachekey + cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) + local objectfile_cached = build_cache.get(cachekey) + if objectfile_cached then + os.cp(objectfile_cached, cppinfo.objectfile) + else + os.iorunv(program, table.join(cppinfo.cppflags, "-o", cppinfo.objectfile, cppinfo.cppfile), {envs = self:runenvs()}) + if cachekey then + build_cache.put(cachekey, cppinfo.objectfile) + end + end + os.rm(cppinfo.cppfile) + end + end + if cppinfo then + return cppinfo.outdata, cppinfo.errdata + else + return os.iorunv(program, argv, {envs = self:runenvs()}) + end end -- make the compile arguments list for the precompiled header @@ -506,31 +538,7 @@ function compile(self, sourcefile, objectfile, dependinfo, flags) end -- do compile - local preprocessed = false - local program, argv = compargv(self, sourcefile, objectfile, compflags) - if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then - preprocessed = distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), preprocess = _preprocess, tool = self}) - elseif build_cache.is_enabled() and build_cache.is_supported(self:kind()) then - local ok, _, objectfile_real, cppfile, cppflags = _preprocess(program, argv, opt) - if ok then - local cachekey - cachekey = build_cache.cachekey(program, cppfile, cppflags, self:runenvs()) - local objectfile_cached = build_cache.get(cachekey) - if objectfile_cached then - os.cp(objectfile_cached, objectfile_real) - else - os.iorunv(program, table.join(cppflags, "-o", objectfile_real, cppfile), {envs = self:runenvs()}) - if cachekey then - build_cache.put(cachekey, objectfile_real) - end - end - preprocessed = true - os.rm(cppfile) - end - end - if not preprocessed then - os.iorunv(program, argv, {envs = self:runenvs()}) - end + return _compile(self, sourcefile, objectfile, compflags, opt) end, catch { @@ -564,7 +572,6 @@ function compile(self, sourcefile, objectfile, dependinfo, flags) results = results .. "\n ${yellow}> in ${bright}" .. sourcefile end raise(results) - end }, finally diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index e4ade5c13..80b92ab63 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -232,25 +232,25 @@ function distcc_build_client:compile(program, argv, opt) -- do preprocess opt = opt or {} local preprocess = assert(opt.preprocess, "preprocessor not found!") - local ok, sourcefile, objectfile, cppfile, cppflags = preprocess(program, argv, opt) - if ok then + local cppinfo = preprocess(program, argv, opt) + if cppinfo then -- get objectfile from the build cache first local cached = false local cachekey if build_cache.is_enabled() then - cachekey = build_cache.cachekey(program, cppfile, cppflags, opt.envs) + cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, opt.envs) local objectfile_cached = build_cache.get(cachekey) if objectfile_cached then - os.cp(objectfile_cached, objectfile) + os.cp(objectfile_cached, cppinfo.objectfile) cached = true end end -- do distcc compilation if not cached then - session:compile(sourcefile, objectfile, cppfile, cppflags, opt) + session:compile(cppinfo.sourcefile, cppinfo.objectfile, cppinfo.cppfile, cppinfo.cppflags, opt) if cachekey then - build_cache.put(cachekey, objectfile) + build_cache.put(cachekey, cppinfo.objectfile) end end end @@ -260,7 +260,7 @@ function distcc_build_client:compile(program, argv, opt) -- unlock this host self:_host_status_unlock(host) - return ok + return cppinfo end -- get the status |
