From 05b98d524b5b524683c6f59c1b59a0bab088b1dc Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 18 May 2022 22:25:35 +0800 Subject: enable build cache --- xmake/modules/core/tools/gcc.lua | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 291e2b47e..7b198a217 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -28,6 +28,7 @@ import("core.project.project") import("core.language.language") import("private.tools.ccache") import("utils.progress") +import("private.cache.build_cache") import("private.service.distcc_build.client", {alias = "distcc_build_client"}) -- init it @@ -507,9 +508,25 @@ function compile(self, sourcefile, objectfile, dependinfo, flags) -- do compile local program, argv = compargv(self, sourcefile, objectfile, compflags) if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then - return distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), preprocess = _preprocess, tool = self}) + 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 _, _, _, _, cppfile, cppflags = _preprocess(program, argv, opt) + local cached = false + 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) + cached = true + end + if not cached then + os.iorunv(program, argv, {envs = self:runenvs()}) + if cachekey then + build_cache.put(cachekey, objectfile) + end + end else - return os.iorunv(program, argv, {envs = self:runenvs()}) + os.iorunv(program, argv, {envs = self:runenvs()}) end end, catch -- cgit v1.3.1 From f4a709d728f700689c56dc7cd0f66a9bed76e83a Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 18 May 2022 22:26:12 +0800 Subject: improve cache prefix --- xmake/modules/core/tools/gcc.lua | 3 +-- xmake/modules/private/action/build/object.lua | 9 +++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 7b198a217..710b6ef5c 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -26,7 +26,6 @@ import("core.base.global") import("core.project.config") import("core.project.project") import("core.language.language") -import("private.tools.ccache") import("utils.progress") import("private.cache.build_cache") import("private.service.distcc_build.client", {alias = "distcc_build_client"}) @@ -473,7 +472,7 @@ function compargv(self, sourcefile, objectfile, flags) if (extension:startswith(".h") or extension == ".inl") then return _compargv_pch(self, sourcefile, objectfile, flags) end - return ccache.cmdargv(self:program(), table.join("-c", flags, "-o", objectfile, sourcefile)) + return self:program(), table.join("-c", flags, "-o", objectfile, sourcefile) end -- compile the source file diff --git a/xmake/modules/private/action/build/object.lua b/xmake/modules/private/action/build/object.lua index 487be763a..b8816f446 100644 --- a/xmake/modules/private/action/build/object.lua +++ b/xmake/modules/private/action/build/object.lua @@ -23,7 +23,7 @@ import("core.base.option") import("core.theme.theme") import("core.tool.compiler") import("core.project.depend") -import("private.tools.ccache") +import("private.cache.build_cache") import("private.async.runjobs") import("utils.progress") import("private.service.distcc_build.client", {alias = "distcc_build_client"}) @@ -61,11 +61,12 @@ function _do_build_file(target, sourcefile, opt) -- exists ccache or distcc? local prefix = "" - if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then - prefix = "distcc " - elseif ccache.exists() then + if build_cache.is_enabled() and build_cache.is_supported(sourcekind) then prefix = "ccache " end + if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then + prefix = prefix .. "distcc " + end -- trace progress info if not opt.quiet then -- cgit v1.3.1 From 17db46f7afecc8dce915b4440ed1461b19325974 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 19 May 2022 00:06:06 +0800 Subject: fix cache for pch --- xmake/modules/core/tools/cl.lua | 6 +++--- xmake/modules/core/tools/gcc.lua | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index 6a67d5621..b768c1fce 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -512,19 +512,19 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) 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 outdata, errdata, _, _, cppfile, cppflags = _preprocess(program, argv, {envs = self:runenvs(), target = opt.target}) + local outdata, errdata, _, objectfile_real, cppfile, cppflags = _preprocess(program, argv, {envs = self:runenvs(), target = opt.target}) local cached = false 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) + os.cp(objectfile_cached, objectfile_real) cached = true end if not cached then vstool.iorunv(program, winos.cmdargv(argv), {envs = self:runenvs()}) if cachekey then - build_cache.put(cachekey, objectfile) + build_cache.put(cachekey, objectfile_real) end end return outdata, errdata diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 710b6ef5c..744b2cfd6 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -509,19 +509,19 @@ function compile(self, sourcefile, objectfile, dependinfo, flags) if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then 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 _, _, _, _, cppfile, cppflags = _preprocess(program, argv, opt) + local _, _, _, objectfile_real, cppfile, cppflags = _preprocess(program, argv, opt) local cached = false 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) + os.cp(objectfile_cached, objectfile_real) cached = true end if not cached then os.iorunv(program, argv, {envs = self:runenvs()}) if cachekey then - build_cache.put(cachekey, objectfile) + build_cache.put(cachekey, objectfile_real) end end else -- cgit v1.3.1 From 05eff25304b1a440b481fc71ec2dcb082be98e4f Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 19 May 2022 00:29:15 +0800 Subject: fix preprocessor --- xmake/modules/core/tools/cl.lua | 40 ++++++++++---------- xmake/modules/core/tools/gcc.lua | 43 +++++++++++----------- .../private/service/distcc_build/client.lua | 37 ++++++++++--------- 3 files changed, 61 insertions(+), 59 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index b768c1fce..148957029 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -442,7 +442,7 @@ function _preprocess(program, argv, opt) assert(objectfile and sourcefile, "%s: iorunv(%s): invalid arguments!", self, program) -- do preprocess - local cppfile = objectfile .. ".p" + local cppfile = path.join(path.directory(objectfile), path.basename(objectfile) .. path.extension(sourcefile)) local cppfiledir = path.directory(cppfile) if not os.isdir(cppfiledir) then os.mkdir(cppfiledir) @@ -450,8 +450,8 @@ function _preprocess(program, argv, opt) table.insert(cppflags, "-P") table.insert(cppflags, "-Fi" .. cppfile) table.insert(cppflags, sourcefile) - local outdata, errdata = vstool.iorunv(program, winos.cmdargv(cppflags), opt) - return outdata, errdata, sourcefile, objectfile, cppfile, flags + local ok = try{ function() vstool.runv(program, winos.cmdargv(cppflags), opt); return true end} + return ok, sourcefile, objectfile, cppfile, flags end -- make the compile arguments list @@ -506,29 +506,31 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) 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})) - return distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), + 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 outdata, errdata, _, objectfile_real, cppfile, cppflags = _preprocess(program, argv, {envs = self:runenvs(), target = opt.target}) - local cached = false - 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) - cached = true - end - if not cached then - vstool.iorunv(program, winos.cmdargv(argv), {envs = self:runenvs()}) - if cachekey then - build_cache.put(cachekey, objectfile_real) + 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 - return outdata, errdata - else + end + if not preprocessed then local program, argv = compargv(self, sourcefile, objectfile, compflags, opt) return vstool.iorunv(program, argv, {envs = self:runenvs()}) end diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 744b2cfd6..6a6f5fadd 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -358,11 +358,7 @@ end -- link the target file function link(self, objectfiles, targetkind, targetfile, flags) - - -- ensure the target directory os.mkdir(path.directory(targetfile)) - - -- link it local program, argv = linkargv(self, objectfiles, targetkind, targetfile, flags) os.runv(program, argv, {envs = self:runenvs()}) end @@ -425,7 +421,7 @@ function _preprocess(program, argv, opt) assert(objectfile and sourcefile, "%s: iorunv(%s): invalid arguments!", self, program) -- do preprocess - local cppfile = objectfile .. ".p" + local cppfile = path.join(path.directory(objectfile), path.basename(objectfile) .. path.extension(sourcefile)) local cppfiledir = path.directory(cppfile) if not os.isdir(cppfiledir) then os.mkdir(cppfiledir) @@ -434,8 +430,8 @@ function _preprocess(program, argv, opt) table.insert(cppflags, "-o") table.insert(cppflags, cppfile) table.insert(cppflags, sourcefile) - local outdata, errdata = os.iorunv(program, cppflags, opt) - return outdata, errdata, sourcefile, objectfile, cppfile, flags + local ok = try {function () os.runv(program, cppflags, opt); return true end} + return ok, sourcefile, objectfile, cppfile, flags end -- make the compile arguments list for the precompiled header @@ -505,26 +501,29 @@ 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 - distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), preprocess = _preprocess, tool = self}) + 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 _, _, _, objectfile_real, cppfile, cppflags = _preprocess(program, argv, opt) - local cached = false - 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) - cached = true - end - if not cached then - os.iorunv(program, argv, {envs = self:runenvs()}) - if cachekey then - build_cache.put(cachekey, objectfile_real) + 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 - else + end + if not preprocessed then os.iorunv(program, argv, {envs = self:runenvs()}) end end, diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index 7e8f46b6f..e4ade5c13 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -232,25 +232,26 @@ function distcc_build_client:compile(program, argv, opt) -- do preprocess opt = opt or {} local preprocess = assert(opt.preprocess, "preprocessor not found!") - local outdata, errdata, sourcefile, objectfile, cppfile, cppflags = preprocess(program, argv, opt) - - -- 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) - local objectfile_cached = build_cache.get(cachekey) - if objectfile_cached then - os.cp(objectfile_cached, objectfile) - cached = true + local ok, sourcefile, objectfile, cppfile, cppflags = preprocess(program, argv, opt) + if ok 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) + local objectfile_cached = build_cache.get(cachekey) + if objectfile_cached then + os.cp(objectfile_cached, objectfile) + cached = true + end end - end - -- do distcc compilation - if not cached then - session:compile(sourcefile, objectfile, cppfile, cppflags, opt) - if cachekey then - build_cache.put(cachekey, objectfile) + -- do distcc compilation + if not cached then + session:compile(sourcefile, objectfile, cppfile, cppflags, opt) + if cachekey then + build_cache.put(cachekey, objectfile) + end end end @@ -259,7 +260,7 @@ function distcc_build_client:compile(program, argv, opt) -- unlock this host self:_host_status_unlock(host) - return outdata, errdata + return ok end -- get the status -- cgit v1.3.1 From d3b2139aacaf4efba9f47e16f36d6086f9e5544b Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 19 May 2022 00:40:53 +0800 Subject: ignore pch for preprocess --- xmake/modules/core/tools/cl.lua | 7 ++++++- xmake/modules/core/tools/gcc.lua | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index 148957029..4966d81da 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -423,7 +423,7 @@ function _preprocess(program, argv, opt) -- get compiler flags if flag == "-showIncludes" or flag == "/showIncludes" or - flag:startswith("-I") or flag:startswith("/I") or + (flag:startswith("-I") and #flag > 2) or (flag:startswith("/I") and #flag > 2) or flag:startswith("-Yu") or flag:startswith("/Yu") or flag:startswith("-FI") or flag:startswith("/FI") or flag:startswith("-Fp") or flag:startswith("/Fp") or @@ -441,6 +441,11 @@ function _preprocess(program, argv, opt) local sourcefile = argv[#argv] assert(objectfile and sourcefile, "%s: iorunv(%s): invalid arguments!", self, program) + -- is precompiled header? + if objectfile:endswith(".pch") then + return false + end + -- do preprocess local cppfile = path.join(path.directory(objectfile), path.basename(objectfile) .. path.extension(sourcefile)) local cppfiledir = path.directory(cppfile) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 6a6f5fadd..5c1a3f132 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -401,7 +401,7 @@ function _preprocess(program, argv, opt) table.insert(cppflags, flag) -- get compiler flags - if flag == "-MMD" or flag:startswith("-I") or flag:startswith("--sysroot=") then + if flag == "-MMD" or (flag:startswith("-I") and #flag > 2) or flag:startswith("--sysroot=") then skipped = 1 elseif flag == "-MF" or flag == "-I" or flag == "-isystem" or flag == "-include" or flag == "-include-pch" or @@ -420,6 +420,11 @@ function _preprocess(program, argv, opt) local sourcefile = argv[#argv] assert(objectfile and sourcefile, "%s: iorunv(%s): invalid arguments!", self, program) + -- is precompiled header? + if objectfile:endswith(".gch") or objectfile:endswith(".pch") then + return false + end + -- do preprocess local cppfile = path.join(path.directory(objectfile), path.basename(objectfile) .. path.extension(sourcefile)) local cppfiledir = path.directory(cppfile) -- cgit v1.3.1 From 0bb56c9a4efc55c6f478b49340607cb651656159 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 19 May 2022 00:51:20 +0800 Subject: improve preprocess --- xmake/modules/core/tools/cl.lua | 70 ++++++++++++---------- xmake/modules/core/tools/gcc.lua | 63 ++++++++++--------- .../private/service/distcc_build/client.lua | 14 ++--- 3 files changed, 81 insertions(+), 66 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') 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 -- cgit v1.3.1 From 95d7f5df2e1aa01cca69e74f0c4dadad8d37f4d0 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 19 May 2022 00:58:00 +0800 Subject: add todo --- xmake/modules/core/tools/gcc.lua | 1 + xmake/modules/private/cache/build_cache.lua | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 9645dad79..67667149a 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -431,6 +431,7 @@ function _preprocess(program, argv, opt) if not os.isdir(cppfiledir) then os.mkdir(cppfiledir) end + -- TODO try -fdirectives-only table.insert(cppflags, "-E") table.insert(cppflags, "-o") table.insert(cppflags, cppfile) diff --git a/xmake/modules/private/cache/build_cache.lua b/xmake/modules/private/cache/build_cache.lua index 440dfb9a2..f31270ecf 100644 --- a/xmake/modules/private/cache/build_cache.lua +++ b/xmake/modules/private/cache/build_cache.lua @@ -49,7 +49,7 @@ function cachekey(program, cppfile, cppflags, envs) table.insert(items, cppflag) end table.sort(items) - table.insert(items, hash.sha1(cppfile)) + table.insert(items, hash.sha1(cppfile)) -- TODO use blake3 if envs then local basename = path.basename(program) if basename == "cl" then -- cgit v1.3.1 From f70ee30b489fef9436425fbd30d72ffa5f955be1 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 19 May 2022 21:02:19 +0800 Subject: improve preprocess --- xmake/modules/core/tools/gcc.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 67667149a..72d3dc784 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -431,11 +431,21 @@ function _preprocess(program, argv, opt) if not os.isdir(cppfiledir) then os.mkdir(cppfiledir) end - -- TODO try -fdirectives-only table.insert(cppflags, "-E") + -- it will be faster for preprocessing + -- when preprocessing, handle directives, but do not expand macros. + table.insert(cppflags, "-fdirectives-only") table.insert(cppflags, "-o") table.insert(cppflags, cppfile) table.insert(cppflags, sourcefile) + + -- we need mark as it when compiling the preprocessed source file + -- it will indicate to the preprocessor that the input file has already been preprocessed. + table.insert(flags, "-fpreprocessed") + -- with -fpreprocessed, predefinition of command line and most builtin macros is disabled. + table.insert(flags, "-fdirectives-only") + + -- do preprocess return try {function () local outdata, errdata = os.iorunv(program, cppflags, opt) return {outdata = outdata, errdata = errdata, sourcefile = sourcefile, objectfile = objectfile, cppfile = cppfile, cppflags = flags} -- cgit v1.3.1 From 757251a0d8a993ceafe0a70eae723fdf44d27e9c Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 19 May 2022 22:15:14 +0800 Subject: fix fallback --- xmake/modules/core/tools/cl.lua | 2 +- xmake/modules/core/tools/gcc.lua | 32 +++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index 1d13a51bd..7c803ebeb 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -470,7 +470,7 @@ function _compile(self, sourcefile, objectfile, compflags, opt) 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}) + 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()) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 72d3dc784..2c1e7f79b 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -425,6 +425,17 @@ function _preprocess(program, argv, opt) return false end + -- enable "-fdirectives-only"? + local tool = opt.tool + local fdirectives_only = _g.fdirectives_only + local is_gcc = false + if tool and (tool:name() == "gcc" or tool:name() == "gxx") then + is_gcc = true + end + if fdirectives_only ~= false and is_gcc then + fdirectives_only = true + end + -- do preprocess local cppfile = path.join(path.directory(objectfile), path.basename(objectfile) .. path.extension(sourcefile)) local cppfiledir = path.directory(cppfile) @@ -434,22 +445,32 @@ function _preprocess(program, argv, opt) table.insert(cppflags, "-E") -- it will be faster for preprocessing -- when preprocessing, handle directives, but do not expand macros. - table.insert(cppflags, "-fdirectives-only") + if fdirectives_only then + table.insert(cppflags, "-fdirectives-only") + end table.insert(cppflags, "-o") table.insert(cppflags, cppfile) table.insert(cppflags, sourcefile) -- we need mark as it when compiling the preprocessed source file -- it will indicate to the preprocessor that the input file has already been preprocessed. - table.insert(flags, "-fpreprocessed") + if is_gcc then + table.insert(flags, "-fpreprocessed") + end -- with -fpreprocessed, predefinition of command line and most builtin macros is disabled. - table.insert(flags, "-fdirectives-only") + if fdirectives_only then + table.insert(flags, "-fdirectives-only") + end -- do preprocess - return try {function () + local cppinfo = try {function () local outdata, errdata = os.iorunv(program, cppflags, opt) return {outdata = outdata, errdata = errdata, sourcefile = sourcefile, objectfile = objectfile, cppfile = cppfile, cppflags = flags} end} + if not cppinfo then + _g.fdirectives_only = false + end + return cppinfo end -- do compile @@ -459,7 +480,8 @@ function _compile(self, sourcefile, objectfile, compflags, opt) 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) + local t = os.mclock() + cppinfo = _preprocess(program, argv, {envs = self:runenvs(), tool = self}) if cppinfo then local cachekey cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) -- cgit v1.3.1 From 3024730a94c340390213c79dc6d810ece1e28500 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 19 May 2022 23:20:40 +0800 Subject: remove logs --- xmake/modules/core/tools/cl.lua | 11 +---------- xmake/modules/core/tools/gcc.lua | 3 +-- 2 files changed, 2 insertions(+), 12 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index 41fbcd92b..dfa73cebd 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -470,22 +470,13 @@ function _compile(self, sourcefile, objectfile, compflags, opt) 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 dt = os.mclock() cppinfo = _preprocess(program, argv, {envs = self:runenvs(), target = opt.target}) - print("preprocess", os.mclock() - dt) if cppinfo then - local cachekey - dt = os.mclock() - cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) - print("cachekey", os.mclock() - dt) - dt = os.mclock() + local 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) - print("get", os.mclock() - dt) else - print("put") 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) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 2c1e7f79b..8a6cc7b84 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -483,8 +483,7 @@ function _compile(self, sourcefile, objectfile, compflags, opt) local t = os.mclock() cppinfo = _preprocess(program, argv, {envs = self:runenvs(), tool = self}) if cppinfo then - local cachekey - cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) + local 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) -- cgit v1.3.1 From c3fd2dd9110aa41cd321d5e73823f57f2d25ff8c Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 20 May 2022 21:02:30 +0800 Subject: build small file in local --- xmake/modules/core/tools/cl.lua | 10 ++++++++++ xmake/modules/core/tools/gcc.lua | 12 +++++++++++- xmake/modules/private/service/distcc_build/client.lua | 14 ++++++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index ca66ede29..a3a3dc5aa 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -464,10 +464,20 @@ end -- do compile function _compile(self, sourcefile, objectfile, compflags, opt) local cppinfo + local build_in_local 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}) + if cppinfo and build_in_local then + vstool.iorunv(program, winos.cmdargv(table.join(cppinfo.cppflags, "-Fo" .. cppinfo.objectfile, cppinfo.cppfile)), {envs = self:runenvs()}) + if build_cache.is_enabled() and build_cache.is_supported(self:kind()) then + local cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) + if cachekey then + build_cache.put(cachekey, cppinfo.objectfile) + end + end + end 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})) cppinfo = _preprocess(program, argv, {envs = self:runenvs(), target = opt.target}) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 8a6cc7b84..16c650768 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -476,9 +476,19 @@ end -- do compile function _compile(self, sourcefile, objectfile, compflags, opt) local cppinfo + local build_in_local 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}) + cppinfo, build_in_local = distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), preprocess = _preprocess, tool = self}) + if cppinfo and build_in_local then + os.iorunv(program, table.join(cppinfo.cppflags, "-o", cppinfo.objectfile, cppinfo.cppfile), {envs = self:runenvs()}) + if build_cache.is_enabled() and build_cache.is_supported(self:kind()) then + local cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) + if cachekey then + build_cache.put(cachekey, cppinfo.objectfile) + end + end + end elseif build_cache.is_enabled() and build_cache.is_supported(self:kind()) then local t = os.mclock() cppinfo = _preprocess(program, argv, {envs = self:runenvs(), tool = self}) diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index 80b92ab63..3730ea9a9 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -233,6 +233,7 @@ function distcc_build_client:compile(program, argv, opt) opt = opt or {} local preprocess = assert(opt.preprocess, "preprocessor not found!") local cppinfo = preprocess(program, argv, opt) + local build_in_local = false if cppinfo then -- get objectfile from the build cache first local cached = false @@ -248,9 +249,14 @@ function distcc_build_client:compile(program, argv, opt) -- do distcc compilation if not cached then - session:compile(cppinfo.sourcefile, cppinfo.objectfile, cppinfo.cppfile, cppinfo.cppflags, opt) - if cachekey then - build_cache.put(cachekey, cppinfo.objectfile) + -- we just compile the large preprocessed file in remote + if os.filesize(cppinfo.cppfile) > 4096 then + session:compile(cppinfo.sourcefile, cppinfo.objectfile, cppinfo.cppfile, cppinfo.cppflags, opt) + if cachekey then + build_cache.put(cachekey, cppinfo.objectfile) + end + else + build_in_local = true end end end @@ -260,7 +266,7 @@ function distcc_build_client:compile(program, argv, opt) -- unlock this host self:_host_status_unlock(host) - return cppinfo + return cppinfo, build_in_local end -- get the status -- cgit v1.3.1 From 6fad9d40962d1c137d65394e99ea84acba5fe5cc Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 20 May 2022 21:05:11 +0800 Subject: ignore remote pdb --- xmake/modules/core/tools/cl.lua | 10 ++++++++-- xmake/modules/core/tools/gcc.lua | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index a3a3dc5aa..ab17b2aa3 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -404,6 +404,7 @@ function _preprocess(program, argv, opt) local cppflags = {} local skipped = 0 local objectfile + local pdbfile for _, flag in ipairs(argv) do if flag:startswith("-Fo") or flag:startswith("/Fo") then objectfile = flag:sub(4) @@ -431,6 +432,9 @@ function _preprocess(program, argv, opt) skipped = 1 elseif flag == "-I" or flag == "-sourceDependencies" or flag == "/sourceDependencies" then skipped = 2 + elseif opt.remote and flag:startswith("-Fd") or flag:startswith("/Fd") then + skipped = 1 + pdbfile = flag:sub(4) --TODO handle remote pdb end if skipped > 0 then skipped = skipped - 1 @@ -457,7 +461,9 @@ function _preprocess(program, argv, opt) table.insert(cppflags, sourcefile) 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} + return {outdata = outdata, errdata = errdata, + sourcefile = sourcefile, objectfile = objectfile, cppfile = cppfile, cppflags = flags, + pdbfile = pdbfile} end} end @@ -468,7 +474,7 @@ function _compile(self, sourcefile, objectfile, compflags, opt) 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}) + preprocess = _preprocess, tool = self, target = opt.target, remote = true}) if cppinfo and build_in_local then vstool.iorunv(program, winos.cmdargv(table.join(cppinfo.cppflags, "-Fo" .. cppinfo.objectfile, cppinfo.cppfile)), {envs = self:runenvs()}) if build_cache.is_enabled() and build_cache.is_supported(self:kind()) then diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 16c650768..af25a2d02 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -479,7 +479,8 @@ function _compile(self, sourcefile, objectfile, compflags, opt) local build_in_local local program, argv = compargv(self, sourcefile, objectfile, compflags) if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then - cppinfo, build_in_local = distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), preprocess = _preprocess, tool = self}) + cppinfo, build_in_local = distcc_build_client.singleton():compile(program, argv, + {envs = self:runenvs(), preprocess = _preprocess, tool = self, remote = true}) if cppinfo and build_in_local then os.iorunv(program, table.join(cppinfo.cppflags, "-o", cppinfo.objectfile, cppinfo.cppfile), {envs = self:runenvs()}) if build_cache.is_enabled() and build_cache.is_supported(self:kind()) then -- cgit v1.3.1 From c710b189a6501578c29f1c882cdf711663950f62 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 20 May 2022 22:24:41 +0800 Subject: improve compile and build cache --- xmake/modules/core/tools/cl.lua | 33 +++++--------------- xmake/modules/core/tools/gcc.lua | 36 ++++++---------------- xmake/modules/private/cache/build_cache.lua | 25 +++++++++++++++ .../private/service/distcc_build/client.lua | 16 +++++++++- 4 files changed, 57 insertions(+), 53 deletions(-) (limited to 'xmake/modules/core/tools/gcc.lua') diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index ab17b2aa3..12bb9d445 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -467,39 +467,22 @@ function _preprocess(program, argv, opt) end} end +-- compile preprocessed file +function _compile_preprocessed_file(program, cppinfo, opt) + vstool.iorunv(program, winos.cmdargv(table.join(cppinfo.cppflags, "-Fo" .. cppinfo.objectfile, cppinfo.cppfile)), opt) +end + -- do compile function _compile(self, sourcefile, objectfile, compflags, opt) local cppinfo - local build_in_local 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, remote = true}) - if cppinfo and build_in_local then - vstool.iorunv(program, winos.cmdargv(table.join(cppinfo.cppflags, "-Fo" .. cppinfo.objectfile, cppinfo.cppfile)), {envs = self:runenvs()}) - if build_cache.is_enabled() and build_cache.is_supported(self:kind()) then - local cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) - if cachekey then - build_cache.put(cachekey, cppinfo.objectfile) - end - end - end + preprocess = _preprocess, compile = _compile_preprocessed_file, target = opt.target, remote = true, tool = self}) 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})) - cppinfo = _preprocess(program, argv, {envs = self:runenvs(), target = opt.target}) - if cppinfo then - local 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 + cppinfo = build_cache.build(program, argv, {envs = self:runenvs(), + preprocess = _preprocess, compile = _compile_preprocessed_file, target = opt.target}) end if cppinfo then return cppinfo.outdata, cppinfo.errdata diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index af25a2d02..b4ea28ba2 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -473,39 +473,21 @@ function _preprocess(program, argv, opt) return cppinfo end +-- compile preprocessed file +function _compile_preprocessed_file(program, cppinfo, opt) + os.iorunv(program, table.join(cppinfo.cppflags, "-o", cppinfo.objectfile, cppinfo.cppfile), opt) +end + -- do compile function _compile(self, sourcefile, objectfile, compflags, opt) local cppinfo - local build_in_local local program, argv = compargv(self, sourcefile, objectfile, compflags) if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then - cppinfo, build_in_local = distcc_build_client.singleton():compile(program, argv, - {envs = self:runenvs(), preprocess = _preprocess, tool = self, remote = true}) - if cppinfo and build_in_local then - os.iorunv(program, table.join(cppinfo.cppflags, "-o", cppinfo.objectfile, cppinfo.cppfile), {envs = self:runenvs()}) - if build_cache.is_enabled() and build_cache.is_supported(self:kind()) then - local cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, self:runenvs()) - if cachekey then - build_cache.put(cachekey, cppinfo.objectfile) - end - end - end + cppinfo = distcc_build_client.singleton():compile(program, argv, + {envs = self:runenvs(), preprocess = _preprocess, compile = _compile_preprocessed_file, remote = true, tool = self}) elseif build_cache.is_enabled() and build_cache.is_supported(self:kind()) then - local t = os.mclock() - cppinfo = _preprocess(program, argv, {envs = self:runenvs(), tool = self}) - if cppinfo then - local 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 + cppinfo = build_cache.build(program, argv, + {envs = self:runenvs(), preprocess = _preprocess, compile = _compile_preprocessed_file}) end if cppinfo then return cppinfo.outdata, cppinfo.errdata diff --git a/xmake/modules/private/cache/build_cache.lua b/xmake/modules/private/cache/build_cache.lua index 8ab9cb03a..16655a632 100644 --- a/xmake/modules/private/cache/build_cache.lua +++ b/xmake/modules/private/cache/build_cache.lua @@ -100,3 +100,28 @@ function put(cachekey, objectfile) local objectfile_cached = path.join(rootdir(), cachekey:sub(1, 2):lower(), cachekey) os.cp(objectfile, objectfile_cached) end + +-- build with cache +function build(program, argv, opt) + + -- do preprocess + opt = opt or {} + local preprocess = assert(opt.preprocess, "preprocessor not found!") + local compile = assert(opt.compile, "compiler not found!") + local cppinfo = preprocess(program, argv, opt) + if cppinfo then + local cachekey = cachekey(program, cppinfo.cppfile, cppinfo.cppflags, opt.envs) + local objectfile_cached = get(cachekey) + if objectfile_cached then + os.cp(objectfile_cached, cppinfo.objectfile) + else + -- do compile + compile(program, cppinfo, opt) + if cachekey then + put(cachekey, cppinfo.objectfile) + end + end + os.rm(cppinfo.cppfile) + end + return cppinfo +end diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index 3730ea9a9..6a6486323 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -266,7 +266,21 @@ function distcc_build_client:compile(program, argv, opt) -- unlock this host self:_host_status_unlock(host) - return cppinfo, build_in_local + + -- build in local + if build_in_local then + if cppinfo and build_in_local then + local compile = assert(opt.compile, "compiler not found!") + compile(program, cppinfo, opt) + if build_cache.is_enabled() then + local cachekey = build_cache.cachekey(program, cppinfo.cppfile, cppinfo.cppflags, opt.envs) + if cachekey then + build_cache.put(cachekey, cppinfo.objectfile) + end + end + end + end + return cppinfo end -- get the status -- cgit v1.3.1