diff options
| author | ruki <[email protected]> | 2022-05-18 22:25:35 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-18 13:09:42 +0800 |
| commit | 05b98d524b5b524683c6f59c1b59a0bab088b1dc (patch) | |
| tree | 1c79cbda6f96b9c707aa42f69aae9baa9f35d859 /xmake/modules | |
| parent | 3674a502ae48035813cac69525162d7a7f89ba28 (diff) | |
enable build cache
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/core/tools/cl.lua | 19 | ||||
| -rw-r--r-- | xmake/modules/core/tools/gcc.lua | 21 | ||||
| -rw-r--r-- | xmake/modules/private/cache/build_cache.lua | 24 | ||||
| -rw-r--r-- | xmake/modules/private/service/distcc_build/client.lua | 2 |
4 files changed, 59 insertions, 7 deletions
diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index e4d36f9e6..6a67d5621 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -26,6 +26,7 @@ import("core.project.project") import("core.language.language") import("private.tools.vstool") import("private.tools.cl.parse_include") +import("private.cache.build_cache") import("private.service.distcc_build.client", {alias = "distcc_build_client"}) import("utils.progress") @@ -509,6 +510,24 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) local program, argv = compargv(self, sourcefile, objectfile, compflags, table.join(opt, {rawargs = true})) return 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, _, _, 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) + cached = true + end + if not cached then + vstool.iorunv(program, winos.cmdargv(argv), {envs = self:runenvs()}) + if cachekey then + build_cache.put(cachekey, objectfile) + end + end + return outdata, errdata else local program, argv = compargv(self, sourcefile, objectfile, compflags, opt) return vstool.iorunv(program, argv, {envs = self:runenvs()}) 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 diff --git a/xmake/modules/private/cache/build_cache.lua b/xmake/modules/private/cache/build_cache.lua index 629c903a3..62ddeafa2 100644 --- a/xmake/modules/private/cache/build_cache.lua +++ b/xmake/modules/private/cache/build_cache.lua @@ -19,10 +19,11 @@ -- -- imports +import("core.base.hashset") import("core.project.config") -- is enabled? -function enabled() +function is_enabled() local build_cache = _g.build_cache if build_cache == nil then build_cache = config.get("ccache") or false @@ -31,6 +32,16 @@ function enabled() return build_cache or false end +-- is supported? +function is_supported(sourcekind) + local sourcekinds = _g.sourcekinds + if sourcekinds == nil then + sourcekinds = hashset.of("cc", "cxx", "mm", "mxx") + _g.sourcekinds = sourcekinds + end + return sourcekinds:has(sourcekind) +end + -- get cache key function cachekey(program, cppfile, cppflags, envs) local items = {program} @@ -38,9 +49,14 @@ function cachekey(program, cppfile, cppflags, envs) table.sort(items) table.insert(items, hash.sha256(cppfile)) if envs then - for k, v in pairs(table.orderpairs(envs)) do - table.insert(items, k) - table.insert(items, v) + local basename = path.basename(program) + if basename == "cl" then + for _, name in ipairs({"WindowsSDKVersion", "VCToolsVersion", "LIB"}) do + local val = envs[name] + if val then + table.insert(items, val) + end + end end end return (hash.uuid(table.concat(items, "")):gsub("-", "")):lower() diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index da0f17dd8..7e8f46b6f 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -237,7 +237,7 @@ function distcc_build_client:compile(program, argv, opt) -- get objectfile from the build cache first local cached = false local cachekey - if build_cache.enabled() then + 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 |
