diff options
| author | ruki <[email protected]> | 2022-05-18 00:59:06 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-18 00:59:06 +0800 |
| commit | e73caa9e24f4c6ca5fa6d60e9c1623cf496bf1e8 (patch) | |
| tree | 6b058abb0bf5552e38d1cc54cc1056df25dca0cf | |
| parent | 11ba8e1356abcdd6efa9716c5e9dfd337764aeae (diff) | |
add build cache to distcc
| -rw-r--r-- | xmake/modules/private/cache/build_cache.lua | 42 | ||||
| -rw-r--r-- | xmake/modules/private/service/distcc_build/client.lua | 20 |
2 files changed, 59 insertions, 3 deletions
diff --git a/xmake/modules/private/cache/build_cache.lua b/xmake/modules/private/cache/build_cache.lua index 6537f691b..8f3c98bd3 100644 --- a/xmake/modules/private/cache/build_cache.lua +++ b/xmake/modules/private/cache/build_cache.lua @@ -24,9 +24,47 @@ import("core.project.config") -- is enabled? function enabled() local build_cache = _g.build_cache - if build_cache == nil and config.get("ccache") then - _g.build_cache = build_cache or false + if build_cache == nil then + build_cache = config.get("ccache") or false + _g.build_cache = build_cache end return build_cache or false end +-- get cache key +function cachekey(program, cppfile, cppflags, envs) + local items = {program} + table.insert(items, hash.sha256(cppfile)) + table.join2(items, cppflags) + if envs then + for k, v in pairs(table.orderpairs(envs)) do + table.insert(items, k) + table.insert(items, v) + end + end + return (hash.uuid(table.concat(items, "")):gsub("-", "")):lower() +end + +-- get cache root directory +function rootdir() + return path.join(config.buildir(), ".cache") +end + +-- clean cached files +function clean() + os.rm(rootdir()) +end + +-- get object file +function get(cachekey) + local objectfile_cached = path.join(rootdir(), cachekey:sub(1, 2):lower(), cachekey) + if os.isfile(objectfile_cached) then + return objectfile_cached + end +end + +-- put object file +function put(cachekey, objectfile) + local objectfile_cached = path.join(rootdir(), cachekey:sub(1, 2):lower(), cachekey) + os.cp(objectfile, objectfile_cached) +end diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index a814cdf6d..da0f17dd8 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -29,6 +29,7 @@ import("lib.detect.find_tool") import("private.service.client_config", {alias = "config"}) import("private.service.message") import("private.service.client") +import("private.cache.build_cache") import("private.service.distcc_build.client_session") import("private.service.stream", {alias = "socket_stream"}) @@ -233,8 +234,25 @@ function distcc_build_client:compile(program, argv, opt) 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.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 + -- do distcc compilation - session:compile(sourcefile, objectfile, cppfile, cppflags, opt) + if not cached then + session:compile(sourcefile, objectfile, cppfile, cppflags, opt) + if cachekey then + build_cache.put(cachekey, objectfile) + end + end -- close session self:_host_status_session_close(host, session) |
