diff options
| author | ruki <[email protected]> | 2022-05-21 17:20:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-21 17:20:55 +0800 |
| commit | 4fcc245863137663031406f4bcde43adffc2dc92 (patch) | |
| tree | dfe2422511c616cf8937c78c53b2b9f520418357 | |
| parent | 75b7205643d9846f6233250e1189c41251821674 (diff) | |
cache file in distcc server
4 files changed, 50 insertions, 20 deletions
diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index 6a6486323..63c291e9b 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -251,7 +251,8 @@ function distcc_build_client:compile(program, argv, opt) if not cached then -- 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) + session:compile(cppinfo.sourcefile, cppinfo.objectfile, cppinfo.cppfile, cppinfo.cppflags, + table.join(opt, {cachekey = cachekey})) if cachekey then build_cache.put(cachekey, cppinfo.objectfile) end diff --git a/xmake/modules/private/service/distcc_build/client_session.lua b/xmake/modules/private/service/distcc_build/client_session.lua index 71d048214..4518b33b6 100644 --- a/xmake/modules/private/service/distcc_build/client_session.lua +++ b/xmake/modules/private/service/distcc_build/client_session.lua @@ -87,10 +87,11 @@ function client_session:compile(sourcefile, objectfile, cppfile, cppflags, opt) local toolkind = tool:kind() local plat = tool:plat() local arch = tool:arch() + local cachekey = opt.cachekey local toolchain = tool:toolchain():name() local stream = self:stream() if stream:send_msg(message.new_compile(self:id(), toolname, toolkind, plat, arch, toolchain, - cppflags, path.filename(sourcefile), {token = self:token()})) and + cppflags, path.filename(sourcefile), {token = self:token(), cachekey = cachekey})) and stream:send_file(cppfile, {compress = os.filesize(cppfile) > 4096}) and stream:flush() then local recv = stream:recv_file(objectfile) if recv ~= nil then diff --git a/xmake/modules/private/service/distcc_build/server_session.lua b/xmake/modules/private/service/distcc_build/server_session.lua index 8f4939c80..3680d9306 100644 --- a/xmake/modules/private/service/distcc_build/server_session.lua +++ b/xmake/modules/private/service/distcc_build/server_session.lua @@ -85,7 +85,8 @@ end -- do clean function server_session:clean() vprint("%s: clean files in %s ..", self, self:workdir()) - os.tryrm(self:workdir()) + os.tryrm(self:buildir()) + os.tryrm(self:cachedir()) vprint("%s: clean files ok", self) end @@ -95,32 +96,46 @@ function server_session:compile(respmsg) -- recv source file local body = respmsg:body() local stream = self:stream() + local cachekey = body.cachekey local sourcename = body.sourcename - local sourcedir = path.join(self:workdir(), (hash.uuid4():gsub("-", ""))) + local sourcedir = path.join(self:buildir(), (hash.uuid4():gsub("-", ""))) local sourcefile = path.join(sourcedir, sourcename) - local objectfile = sourcefile .. ".o" + local objectfile = (cachekey and path.join(self:cachedir(), cachekey:sub(1, 2), cachekey) or sourcefile) .. ".o" if not stream:recv_file(sourcefile) then raise("recv %s failed!", sourcename) end -- do compile local errors - local ok = try - { - function () - local flags = body.flags - local toolname = body.toolname - local compile = assert(self["_" .. toolname .. "_compile"], "%s: compiler(%s) is not supported!", self, toolname) - compile(self, toolname, flags, sourcefile, objectfile, body) - return os.isfile(objectfile) - end, - catch + local ok + if not (cachekey and os.isfile(objectfile)) then -- no cached object file? + ok = try { - function (errs) - errors = tostring(errs) - end + function () + local flags = body.flags + local toolname = body.toolname + local compile = assert(self["_" .. toolname .. "_compile"], "%s: compiler(%s) is not supported!", self, toolname) + local objectdir = path.directory(objectfile) + if not os.isdir(objectdir) then + os.mkdir(objectdir) + end + compile(self, toolname, flags, sourcefile, objectfile, body) + return os.isfile(objectfile) + end, + catch + { + function (errs) + errors = tostring(errs) + end + } } - } + if ok then + vprint("send compiled object file %s ..", objectfile) + end + else + vprint("send cached object file %s ..", objectfile) + ok = true + end -- send object file if ok then @@ -135,7 +150,9 @@ function server_session:compile(respmsg) -- remove files os.tryrm(sourcefile) - os.tryrm(objectfile) + if not cachekey then + os.tryrm(objectfile) + end return ok, errors end @@ -154,6 +171,16 @@ function server_session:workdir() return path.join(self:server():workdir(), "sessons", self:id()) end +-- get build directory +function server_session:buildir() + return path.join(self:workdir(), "build") +end + +-- get cache directory +function server_session:cachedir() + return path.join(self:workdir(), "cache") +end + -- is connected? function server_session:is_connected() return self:status().connected diff --git a/xmake/modules/private/service/message.lua b/xmake/modules/private/service/message.lua index b22a49bc2..772d697b4 100644 --- a/xmake/modules/private/service/message.lua +++ b/xmake/modules/private/service/message.lua @@ -240,6 +240,7 @@ function new_compile(session_id, toolname, toolkind, plat, arch, toolchain, flag token = opt.token, toolname = toolname, toolkind = toolkind, + cachekey = opt.cachekey, plat = plat, arch = arch, toolchain = toolchain, |
