Skip to content

Commit

Permalink
Merge pull request xmake-io#5902 from xmake-io/gcc
Browse files Browse the repository at this point in the history
fix gcc pch xmake-io#5858
  • Loading branch information
waruqi authored Nov 29, 2024
2 parents ea71bb8 + 27486d1 commit 50d3f61
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
34 changes: 31 additions & 3 deletions xmake/modules/core/tools/gcc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions xmake/modules/private/action/build/pcheader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,31 @@ 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
#include "%s"
#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
Expand Down

0 comments on commit 50d3f61

Please sign in to comment.