diff options
| author | ruki <[email protected]> | 2024-11-29 12:25:15 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-11-29 12:25:15 +0800 |
| commit | 50d3f61df2a7b42684580412872dff779e662d25 (patch) | |
| tree | 6c16fae7752d3a9f10202b7115b7b81347cb4e7e | |
| parent | ea71bb8cbde3706e801414a2ef993557fe388b79 (diff) | |
| parent | 27486d1e9015096eef5b59df0e78e2d6af4cd92f (diff) | |
Merge pull request #5902 from xmake-io/gcc
fix gcc pch #5858
| -rw-r--r-- | xmake/modules/core/tools/gcc.lua | 34 | ||||
| -rw-r--r-- | xmake/modules/private/action/build/pcheader.lua | 25 |
2 files changed, 49 insertions, 10 deletions
diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 821bd9910..10ac810f3 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -630,6 +630,19 @@ function _has_color_diagnostics(self) return colors_diagnostics end +-- has gnu-line-marker flag? +function _has_gnu_line_marker_flag(self) + local gnu_line_marker = _g._HAS_GNU_LINE_MARKER + if gnu_line_marker == nil then + if self:has_flags({"-Wno-gnu-line-marker", "-Werror"}, "cxflags") then + gnu_line_marker = true + end + gnu_line_marker = gnu_line_marker or false + _g._HAS_GNU_LINE_MARKER = gnu_line_marker + end + return gnu_line_marker +end + -- get preprocess file path function _get_cppfile(sourcefile, objectfile) return path.join(path.directory(objectfile), "__cpp_" .. path.basename(objectfile) .. path.extension(sourcefile)) @@ -742,6 +755,15 @@ function _preprocess(program, argv, opt) if linemarkers == false then table.insert(cppflags, "-P") end + -- if we want to support pch for gcc, we need to enable this flag + -- and clang need not this flag, it will use '-include-pch' to include and preprocess header files + -- but it will be slower than non-ccache mode. + -- + -- @see https://github.com/xmake-io/xmake/issues/5858 + -- https://musescore.org/en/node/182331 + if is_gcc then + table.insert(cppflags, "-fpch-preprocess") + end table.insert(cppflags, "-o") table.insert(cppflags, cppfile) table.insert(cppflags, sourcefile) @@ -758,7 +780,7 @@ function _preprocess(program, argv, opt) -- suppress -Wgnu-line-marker warnings -- @see https://github.com/xmake-io/xmake/issues/5737 - if is_gcc or is_clang then + if (is_gcc or is_clang) and _has_gnu_line_marker_flag(tool) then table.insert(flags, "-Wno-gnu-line-marker") end @@ -788,8 +810,14 @@ function _compile_preprocessed_file(program, cppinfo, opt) end local outdata, errdata = os.iorunv(program, argv, opt) -- we need to get warning information from output - cppinfo.outdata = outdata - cppinfo.errdata = errdata + -- and we need to reserve warnings output from preprocessing + -- @see https://github.com/xmake-io/xmake/issues/5858 + if outdata then + cppinfo.outdata = (cppinfo.outdata or "") .. outdata + end + if errdata then + cppinfo.errdata = (cppinfo.errdata or "") .. errdata + end end -- do compile diff --git a/xmake/modules/private/action/build/pcheader.lua b/xmake/modules/private/action/build/pcheader.lua index 37fe085d0..20e857d7f 100644 --- a/xmake/modules/private/action/build/pcheader.lua +++ b/xmake/modules/private/action/build/pcheader.lua @@ -25,12 +25,19 @@ import("object") function config(target, langkind, opt) local pcheaderfile = target:pcheaderfile(langkind) if pcheaderfile then - local headerfile = target:autogenfile(pcheaderfile) - if target:is_plat("windows") and - target:has_tool(langkind == "cxx" and "cxx" or "cc", "cl", "clang_cl") then - -- fix `#pragma once` for msvc - -- https://github.com/xmake-io/xmake/issues/2667 - if not os.isfile(headerfile) then + local sourcekind = language.langkinds()[langkind] or "cxx" + if target:has_tool(sourcekind, "cl", "clang_cl", "gcc", "gxx") then + local headerfile = target:autogenfile(pcheaderfile) + local gcc = false + if target:has_tool(sourcekind, "gcc", "gxx") then + local pcoutputfile = target:pcoutputfile(langkind) + headerfile = path.join(path.directory(pcoutputfile), path.filename(headerfile)) + gcc = true + end + -- fix `#pragma once` for msvc + -- https://github.com/xmake-io/xmake/issues/2667 + -- https://github.com/xmake-io/xmake/issues/5858 + if not os.isfile(headerfile) then io.writefile(headerfile, ([[ #pragma system_header #ifdef __cplusplus @@ -38,7 +45,11 @@ function config(target, langkind, opt) #endif // __cplusplus ]]):format(path.absolute(pcheaderfile):gsub("\\", "/"))) end - target:pcheaderfile_set(langkind, headerfile) + -- we need only to add a header wrapper in .gch directory + -- @see https://github.com/xmake-io/xmake/issues/5858#issuecomment-2506918167 + if not gcc then + target:pcheaderfile_set(langkind, headerfile) + end end end end |
