diff options
| author | ruki <[email protected]> | 2022-05-17 00:52:52 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-17 00:52:52 +0800 |
| commit | 0894984d573ee66e8b8f860464f2fc0c6a8208c8 (patch) | |
| tree | d35e70f9c4f14fe898ac51c68a3c64a2f6870ce1 | |
| parent | 61719b2242a0ea7f0bc263b4012f02b8f5daf216 (diff) | |
improve preprocess
| -rw-r--r-- | xmake/modules/core/tools/cl.lua | 49 | ||||
| -rw-r--r-- | xmake/modules/core/tools/gcc.lua | 49 | ||||
| -rw-r--r-- | xmake/modules/private/service/distcc_build/client.lua | 9 | ||||
| -rw-r--r-- | xmake/modules/private/service/distcc_build/client_session.lua | 128 |
4 files changed, 105 insertions, 130 deletions
diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index b6769860d..006f1b402 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -395,6 +395,53 @@ function _is_in_vstudio() return is_in_vstudio end +-- do preprocess +function _preprocess(program, argv, opt) + + -- get flags and source file + local flags = {} + local cppflags = {} + local skipped = 0 + local objectfile + for _, flag in ipairs(argv) do + if flag:startswith("-Fo") or flag:startswith("/Fo") then + objectfile = flag:sub(4) + break + end + + -- get preprocessor flags + table.insert(cppflags, flag) + + -- get compiler flags + if flag == "-showIncludes" or flag == "/showIncludes" or + flag:startswith("-I") or flag:startswith("/I") or + flag:startswith("-external:") or flag:startswith("/external:") then + skipped = 1 + elseif flag == "-I" or flag == "-sourceDependencies" or flag == "/sourceDependencies" then + skipped = 2 + end + if skipped > 0 then + skipped = skipped - 1 + else + table.insert(flags, flag) + end + end + local sourcefile = argv[#argv] + assert(objectfile and sourcefile, "%s: iorunv(%s): invalid arguments!", self, program) + + -- do preprocess + local cppfile = objectfile .. ".p" + local cppfiledir = path.directory(cppfile) + if not os.isdir(cppfiledir) then + os.mkdir(cppfiledir) + end + table.insert(cppflags, "-P") + table.insert(cppflags, "-Fi" .. cppfile) + table.insert(cppflags, sourcefile) + local outdata, errdata = vstool.iorunv(program, cppflags, opt) + return outdata, errdata, sourcefile, objectfile, cppfile, flags +end + -- make the compile arguments list function compargv(self, sourcefile, objectfile, flags, opt) @@ -449,7 +496,7 @@ function compile(self, sourcefile, objectfile, dependinfo, flags, opt) -- use vstool to compile and enable vs_unicode_output @see https://github.com/xmake-io/xmake/issues/528 local program, argv = compargv(self, sourcefile, objectfile, compflags, opt) if distcc_build_client.is_distccjob() and distcc_build_client.singleton():has_freejobs() then - return distcc_build_client.singleton():iorunv(program, argv, {envs = self:runenvs(), tool = self}) + return distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), preprocess = _preprocess, tool = self}) else 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 553ed9fb6..14c1a4b2b 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -389,6 +389,53 @@ function _has_color_diagnostics(self) return colors_diagnostics end +-- do preprocess +function _preprocess(program, argv, opt) + + -- get flags and source file + local flags = {} + local cppflags = {} + local skipped = program:endswith("cache") and 1 or 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") or flag:startswith("--sysroot=") then + skipped = 1 + elseif flag == "-MF" or flag == "-I" or flag == "-isystem" or flag == "-isysroot" or flag == "-gcc-toolchain" 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 .. ".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) + local outdata, errdata = os.iorunv(program, cppflags, opt) + return outdata, errdata, sourcefile, objectfile, cppfile, flags +end + -- make the compile arguments list for the precompiled header function _compargv_pch(self, pcheaderfile, pcoutputfile, flags) @@ -458,7 +505,7 @@ 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():iorunv(program, argv, {envs = self:runenvs(), tool = self}) + return distcc_build_client.singleton():compile(program, argv, {envs = self:runenvs(), preprocess = _preprocess, tool = self}) else return os.iorunv(program, argv, {envs = self:runenvs()}) end diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index deca085c0..a814cdf6d 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -217,7 +217,7 @@ function distcc_build_client:clean() end -- run compilation job -function distcc_build_client:iorunv(program, argv, opt) +function distcc_build_client:compile(program, argv, opt) -- get free host local host = assert(self:_get_freehost(), "free host not found!") @@ -228,8 +228,13 @@ function distcc_build_client:iorunv(program, argv, opt) -- open the host session local session = assert(self:_host_status_session_open(host), "open session failed!") + -- do preprocess + opt = opt or {} + local preprocess = assert(opt.preprocess, "preprocessor not found!") + local outdata, errdata, sourcefile, objectfile, cppfile, cppflags = preprocess(program, argv, opt) + -- do distcc compilation - local outdata, errdata = session:iorunv(program, argv, opt) + session:compile(sourcefile, objectfile, cppfile, cppflags, opt) -- close session self:_host_status_session_close(host, session) diff --git a/xmake/modules/private/service/distcc_build/client_session.lua b/xmake/modules/private/service/distcc_build/client_session.lua index c42b969a5..71d048214 100644 --- a/xmake/modules/private/service/distcc_build/client_session.lua +++ b/xmake/modules/private/service/distcc_build/client_session.lua @@ -78,132 +78,8 @@ function client_session:is_opened() end -- run compilation job -function client_session:iorunv(program, argv, opt) +function client_session:compile(sourcefile, objectfile, cppfile, cppflags, opt) assert(self:is_opened(), "%s: has been not opened!", self) - opt = opt or {} - local tool = opt.tool - local toolname = tool:name() - local iorunv = assert(self["_" .. toolname .. "_iorunv"], "%s: iorunv(%s) is not supported!", self, program) - return iorunv(self, program, argv, opt) -end - --- run compilation job for gcc -function client_session:_gcc_iorunv(program, argv, opt) - - -- get flags and source file - local flags = {} - local cppflags = {} - local skipped = program:endswith("cache") and 1 or 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") or flag:startswith("--sysroot=") then - skipped = 1 - elseif flag == "-MF" or flag == "-I" or flag == "-isystem" or flag == "-isysroot" or flag == "-gcc-toolchain" 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 .. ".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) - local outdata, errdata = os.iorunv(program, cppflags, opt) - - -- do compile - self:_compile(sourcefile, cppfile, objectfile, flags, opt) - return outdata, errdata -end - --- run compilation job for g++ -function client_session:_gxx_iorunv(program, argv, opt) - return self:_gcc_iorunv(program, argv, opt) -end - --- run compilation job for clang -function client_session:_clang_iorunv(program, argv, opt) - return self:_gcc_iorunv(program, argv, opt) -end - --- run compilation job for clang++ -function client_session:_clangxx_iorunv(program, argv, opt) - return self:_gcc_iorunv(program, argv, opt) -end - --- run compilation job for cl -function client_session:_cl_iorunv(program, argv, opt) - - -- get flags and source file - local flags = {} - local cppflags = {} - local skipped = 0 - local objectfile - for _, flag in ipairs(argv) do - if flag:startswith("-Fo") or flag:startswith("/Fo") then - objectfile = flag:sub(4) - break - end - - -- get preprocessor flags - table.insert(cppflags, flag) - - -- get compiler flags - if flag == "-showIncludes" or flag == "/showIncludes" or - flag:startswith("-I") or flag:startswith("/I") or - flag:startswith("-external:") or flag:startswith("/external:") then - skipped = 1 - elseif flag == "-I" or flag == "-sourceDependencies" or flag == "/sourceDependencies" then - skipped = 2 - end - if skipped > 0 then - skipped = skipped - 1 - else - table.insert(flags, flag) - end - end - local sourcefile = argv[#argv] - assert(objectfile and sourcefile, "%s: iorunv(%s): invalid arguments!", self, program) - - -- do preprocess - local cppfile = objectfile .. ".p" - local cppfiledir = path.directory(cppfile) - if not os.isdir(cppfiledir) then - os.mkdir(cppfiledir) - end - table.insert(cppflags, "-P") - table.insert(cppflags, "-Fi" .. cppfile) - table.insert(cppflags, sourcefile) - local outdata, errdata = os.iorunv(program, cppflags, opt) - - -- do compile - self:_compile(sourcefile, cppfile, objectfile, flags, opt) - return outdata, errdata -end - --- do compile -function client_session:_compile(sourcefile, cppfile, objectfile, flags, opt) local ok = false local errors local tool = opt.tool @@ -214,7 +90,7 @@ function client_session:_compile(sourcefile, cppfile, objectfile, flags, opt) local toolchain = tool:toolchain():name() local stream = self:stream() if stream:send_msg(message.new_compile(self:id(), toolname, toolkind, plat, arch, toolchain, - flags, path.filename(sourcefile), {token = self:token()})) and + cppflags, path.filename(sourcefile), {token = self:token()})) and stream:send_file(cppfile, {compress = os.filesize(cppfile) > 4096}) and stream:flush() then local recv = stream:recv_file(objectfile) if recv ~= nil then |
