diff options
| author | ruki <[email protected]> | 2022-05-14 23:58:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-14 23:58:38 +0800 |
| commit | 9a10308a41d9e1b03e0b5249058e839d6920aa91 (patch) | |
| tree | 172ee5e9f0856cca5e6d581304d254cb41a68fc5 | |
| parent | a413ba4233bf4887171c59add0cdc70394315f40 (diff) | |
send compile msg
5 files changed, 107 insertions, 12 deletions
diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index 89963d9d8..c9377f8b8 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -320,7 +320,7 @@ function distcc_build_client:_host_status_session(host_status) local session = host_status.sessions[running] if not session then local sock = assert(socket.connect(host_status.addr, host_status.port), "%s: server unreachable!", self) - session = client_session(self, host_status.session_id, sock) + session = client_session(self, host_status.session_id, host_status.token, sock) host_status.sessions[running] = session end return session diff --git a/xmake/modules/private/service/distcc_build/client_session.lua b/xmake/modules/private/service/distcc_build/client_session.lua index dba51db22..bcd9d04de 100644 --- a/xmake/modules/private/service/distcc_build/client_session.lua +++ b/xmake/modules/private/service/distcc_build/client_session.lua @@ -34,8 +34,9 @@ import("private.service.stream", {alias = "socket_stream"}) local client_session = client_session or object() -- init client session -function client_session:init(client, session_id, sock) +function client_session:init(client, session_id, token, sock) self._ID = session_id + self._TOKEN = token self._STREAM = socket_stream(sock) self._CLIENT = client end @@ -45,6 +46,11 @@ function client_session:id() return self._ID end +-- get token +function client_session:token() + return self._TOKEN +end + -- get client function client_session:client() return self._CLIENT @@ -65,10 +71,66 @@ end -- run compilation job for gcc function client_session:_gcc_iorunv(program, argv, opt) - -- TODO, do distcc compilation - local outdata, errdata = os.iorunv(program, argv, opt) - return outdata, errdata + -- get flags and source file + local flags = {} + local cppflags = {} + local skipped = 0 + for _, flag in ipairs(argv) do + if flag == "-o" then + break + end + + -- get preprocessor flags + table.insert(cppflags, flag) + + -- get compiler flags + if flag == "-MMD" or flag:startswith("-I") then + skipped = 1 + elseif flag == "-MF" or flag == "-I" or flag == "-isystem" then + skipped = 2 + elseif flag:endswith("xcrun") then + skipped = 4 + end + if skipped > 0 then + skipped = skipped - 1 + else + table.insert(flags, flag) + end + end + local objectfile = argv[#argv - 1] + local sourcefile = argv[#argv] + assert(objectfile and sourcefile, "%s: iorunv(%s): invalid arguments!", self, program) + + -- do preprocess + local cppfile = objectfile:gsub("%.o$", ".p") + local cppfiledir = path.directory(cppfile) + if not os.isdir(cppfiledir) then + os.mkdir(cppfiledir) + end + table.insert(cppflags, "-E") + table.insert(cppflags, "-o") + table.insert(cppflags, cppfile) + table.insert(cppflags, sourcefile) + os.runv(program, cppflags, opt) + + -- do compile + local ok = false + local errors + local stream = self:stream() + if stream:send_msg(message.new_compile(self:id(), opt.toolname, flags, path.filename(sourcefile), {token = self:token()})) and + stream:send_file(cppfile, {compress = os.filesize(cppfile) > 4096}) and stream:flush() then + local msg = stream:recv_msg() + if msg then + if msg:success() and stream:recv_file(objectfile) then + ok = true + else + errors = msg:errors() + end + end + end + os.tryrm(cppfile) + assert(ok, errors or "unknown errors!") end -- run compilation job for g++ @@ -95,8 +157,8 @@ function client_session:__tostring() return string.format("<session %s>", self:id()) end -function main(client, session_id, job_id, sock) +function main(client, session_id, token, sock) local instance = client_session() - instance:init(client, session_id, job_id, sock) + instance:init(client, session_id, token, sock) return instance end diff --git a/xmake/modules/private/service/distcc_build/server.lua b/xmake/modules/private/service/distcc_build/server.lua index 807d2e763..c67fecef7 100644 --- a/xmake/modules/private/service/distcc_build/server.lua +++ b/xmake/modules/private/service/distcc_build/server.lua @@ -86,7 +86,9 @@ function distcc_build_server:_on_handle(stream, msg) self._SESSIONS[session_id] = nil else assert(session:is_connected(), "session has not been connected!") - -- TODO + if msg:is_compile() then + session:compile(respmsg) + end end return true end, diff --git a/xmake/modules/private/service/distcc_build/server_session.lua b/xmake/modules/private/service/distcc_build/server_session.lua index 5b7316021..912e807fa 100644 --- a/xmake/modules/private/service/distcc_build/server_session.lua +++ b/xmake/modules/private/service/distcc_build/server_session.lua @@ -79,6 +79,24 @@ function server_session:close() self:status_save() end +-- do compile +function server_session:compile(respmsg) + + -- recv source file + local stream = self:stream() + local sourcename = respmsg.sourcename + local sourcedir = path.join(self:workdir(), (hash.uuid4():gsub("-", ""))) + local sourcefile = path.join(sourcedir, sourcename) + if not stream:recv_file(sourcefile) then + raise("recv %s failed!", sourcename) + end + + print("recv %s", sourcefile) + + -- remove source files + os.rm(sourcefile) +end + -- set stream function server_session:stream_set(stream) self._STREAM = stream diff --git a/xmake/modules/private/service/message.lua b/xmake/modules/private/service/message.lua index ee190cc2f..82ec2e6ff 100644 --- a/xmake/modules/private/service/message.lua +++ b/xmake/modules/private/service/message.lua @@ -32,7 +32,7 @@ message.CODE_DATA = 4 -- send data message.CODE_RUNCMD = 5 -- run the given command in server message.CODE_DIFF = 6 -- diff files between server and client message.CODE_SYNC = 7 -- sync files between server and client -message.CODE_PULL = 8 -- pull the given file from server +message.CODE_COMPILE = 8 -- compile the given file from client in server -- init message function message:init(body) @@ -69,9 +69,9 @@ function message:is_sync() return self:code() == message.CODE_SYNC end --- is pull message? -function message:is_pull() - return self:code() == message.CODE_PULL +-- is compile message? +function message:is_compile() + return self:code() == message.CODE_COMPILE end -- is clean message? @@ -213,6 +213,19 @@ function new_data(session_id, size, opt) }) end +-- new compile command message +function new_compile(session_id, compiler, flags, sourcename, opt) + opt = opt or {} + return _new({ + code = message.CODE_COMPILE, + session_id = session_id, + token = opt.token, + compiler = compiler, + flags = flags, + sourcename = sourcename + }) +end + function main(body) return _new(body) end |
