From 41d59e7004aecf871a30828ff3a54beeabcee041 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 29 Nov 2024 22:36:58 +0800 Subject: fix gcc pch #5858 --- xmake/modules/private/action/build/pcheader.lua | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) 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 -- cgit v1.3.1 From aedfd1c46c942392fbfefb1750f2cd3bde6d1e6c Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 29 Nov 2024 22:40:23 +0800 Subject: suppress unknown flags --- xmake/modules/core/tools/gcc.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 821bd9910..08f898c8e 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)) @@ -758,7 +771,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 -- cgit v1.3.1 From 780e73424b778b7fa5b7ac7f170459dcc49c6f7c Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 29 Nov 2024 22:48:46 +0800 Subject: get more warnings output --- xmake/modules/core/tools/gcc.lua | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 08f898c8e..9e1a658a7 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -755,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) @@ -801,8 +810,12 @@ 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 + if outdata then + cppinfo.outdata = (cppinfo.outdata or "") .. outdata + end + if errdata then + cppinfo.errdata = (cppinfo.errdata or "") .. errdata + end end -- do compile -- cgit v1.3.1 From 27486d1e9015096eef5b59df0e78e2d6af4cd92f Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 29 Nov 2024 22:49:03 +0800 Subject: improve comments --- xmake/modules/core/tools/gcc.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 9e1a658a7..10ac810f3 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -810,6 +810,8 @@ function _compile_preprocessed_file(program, cppinfo, opt) end local outdata, errdata = os.iorunv(program, argv, opt) -- we need to get warning information from output + -- 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 -- cgit v1.3.1