summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-08-11 00:53:18 +0800
committerruki <[email protected]>2022-08-11 00:53:18 +0800
commitae1521c83b8d19b832b1fb1636ae2c22c299cac8 (patch)
treeec3db9772f4bd9428f2fb91b166a1968e2120cd2
parenteb348d65c260d13a47bea41ee461fff95b9dfb60 (diff)
fix pcheader for msvc
-rw-r--r--xmake/core/project/target.lua6
-rw-r--r--xmake/modules/private/action/build/pcheader.lua23
-rw-r--r--xmake/rules/c++/precompiled_header/xmake.lua36
-rw-r--r--xmake/rules/c++/xmake.lua10
4 files changed, 64 insertions, 11 deletions
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index b00cbe15a..4397fa701 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -1918,6 +1918,12 @@ function _instance:pcheaderfile(langkind)
return pcheaderfile
end
+-- set the precompiled header file
+function _instance:pcheaderfile_set(langkind, headerfile)
+ self:set("p" .. langkind .. "header", headerfile)
+ self._PCOUTPUTFILES = nil
+end
+
-- get the output of precompiled header file (xxx.h.pch)
--
-- @param langkind c/cxx
diff --git a/xmake/modules/private/action/build/pcheader.lua b/xmake/modules/private/action/build/pcheader.lua
index 91dcfb172..bc16bd29f 100644
--- a/xmake/modules/private/action/build/pcheader.lua
+++ b/xmake/modules/private/action/build/pcheader.lua
@@ -22,8 +22,29 @@
import("core.language.language")
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") then
+ -- fix `#pragma once` for msvc
+ -- https://github.com/xmake-io/xmake/issues/2667
+ 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)
+ end
+ end
+end
+
-- add batch jobs to build the precompiled header file
-function main(target, langkind, opt)
+function build(target, langkind, opt)
local pcheaderfile = target:pcheaderfile(langkind)
if pcheaderfile then
local sourcefile = pcheaderfile
diff --git a/xmake/rules/c++/precompiled_header/xmake.lua b/xmake/rules/c++/precompiled_header/xmake.lua
new file mode 100644
index 000000000..7216a1f13
--- /dev/null
+++ b/xmake/rules/c++/precompiled_header/xmake.lua
@@ -0,0 +1,36 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+rule("c.build.pcheader")
+ on_config(function (target, opt)
+ import("private.action.build.pcheader").config(target, "c", opt)
+ end)
+ before_build(function (target, opt)
+ import("private.action.build.pcheader").build(target, "c", opt)
+ end)
+
+rule("c++.build.pcheader")
+ on_config(function (target, opt)
+ import("private.action.build.pcheader").config(target, "cxx", opt)
+ end)
+ before_build(function (target, opt)
+ import("private.action.build.pcheader").build(target, "cxx", opt)
+ end)
+
diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua
index bce2606d7..2f7977768 100644
--- a/xmake/rules/c++/xmake.lua
+++ b/xmake/rules/c++/xmake.lua
@@ -18,21 +18,11 @@
-- @file xmake.lua
--
-rule("c.build.pcheader")
- before_build(function (target, opt)
- import("private.action.build.pcheader")(target, "c", opt)
- end)
-
rule("c.build")
set_sourcekinds("cc")
add_deps("c.build.pcheader", "c.build.optimization")
on_build_files("private.action.build.object", {batch = true, distcc = true})
-rule("c++.build.pcheader")
- before_build(function (target, opt)
- import("private.action.build.pcheader")(target, "cxx", opt)
- end)
-
rule("c++.build")
set_sourcekinds("cxx")
add_deps("c++.build.pcheader", "c++.build.modules", "c++.build.optimization")