summaryrefslogtreecommitdiff
path: root/xmake/rules
diff options
context:
space:
mode:
authorruki <[email protected]>2019-09-12 12:32:53 +0800
committerGitHub <[email protected]>2019-09-12 12:32:53 +0800
commit961b833b032ee06ffd4f5de96a11013edb8498a7 (patch)
treea7f2378ae72fa63607267286ffd6dacd11086b89 /xmake/rules
parentc58d64d433d2a3e2c97ead29738722aa653ae88a (diff)
parentce51c2de7c11b6368ce8956329f9ebbe4ffd1b67 (diff)
Merge pull request #569 from xmake-io/cppmodules
Add c++ modules ts build rules
Diffstat (limited to 'xmake/rules')
-rw-r--r--xmake/rules/asm/xmake.lua4
-rw-r--r--xmake/rules/c++/modules/build_modulefiles.lua138
-rw-r--r--xmake/rules/c++/modules/xmake.lua25
-rw-r--r--xmake/rules/c++/xmake.lua22
-rw-r--r--xmake/rules/dlang/xmake.lua4
-rw-r--r--xmake/rules/go/xmake.lua4
-rw-r--r--xmake/rules/objc++/xmake.lua10
-rw-r--r--xmake/rules/rust/xmake.lua4
-rw-r--r--xmake/rules/swift/xmake.lua4
-rw-r--r--xmake/rules/winsdk/xmake.lua4
10 files changed, 181 insertions, 38 deletions
diff --git a/xmake/rules/asm/xmake.lua b/xmake/rules/asm/xmake.lua
index 3a7e85f59..9adc87ab7 100644
--- a/xmake/rules/asm/xmake.lua
+++ b/xmake/rules/asm/xmake.lua
@@ -21,9 +21,7 @@
-- define rule: asm.build
rule("asm.build")
set_extensions(".s", ".asm")
- on_build_files(function (target, sourcebatch, opt)
- import("private.action.build.object")(target, sourcebatch, opt)
- end)
+ on_build_files("private.action.build.object")
-- define rule: asm
rule("asm")
diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua
new file mode 100644
index 000000000..08924e814
--- /dev/null
+++ b/xmake/rules/c++/modules/build_modulefiles.lua
@@ -0,0 +1,138 @@
+--!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 - 2019, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file build_modulefiles.lua
+--
+
+-- imports
+import("core.tool.compiler")
+
+-- build module files using clang
+function _build_modulefiles_clang(target, sourcebatch, opt)
+
+ -- attempt to compile the module files as cxx
+ sourcebatch.sourcekind = "cxx"
+ sourcebatch.objectfiles = sourcebatch.objectfiles or {}
+ sourcebatch.dependfiles = sourcebatch.dependfiles or {}
+ for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ local objectfile = target:objectfile(sourcefile) .. ".pcm"
+ table.insert(sourcebatch.objectfiles, objectfile)
+ table.insert(sourcebatch.dependfiles, target:dependfile(objectfile))
+ end
+
+ -- compile module files to *.pcm
+ opt = table.join(opt, {configs = {cxxflags = {"-fmodules-ts", "--precompile", "-x c++-module"}}})
+ import("private.action.build.object")(target, sourcebatch, opt)
+
+ -- compile *.pcm to object files
+ local modulefiles = {}
+ for idx, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ local modulefile = sourcebatch.objectfiles[idx]
+ local objectfile = target:objectfile(sourcefile)
+ sourcebatch.sourcefiles[idx] = modulefile
+ sourcebatch.objectfiles[idx] = objectfile
+ sourcebatch.dependfiles[idx] = target:dependfile(objectfile)
+ table.insert(modulefiles, modulefile)
+ end
+ opt.configs = {cxxflags = {"-fmodules-ts"}}
+ opt.quiet = true
+ import("private.action.build.object")(target, sourcebatch, opt)
+
+ -- add module files
+ target:add("cxxflags", "-fmodules-ts")
+ for _, modulefile in ipairs(modulefiles) do
+ target:add("cxxflags", "-fmodule-file=" .. modulefile)
+ end
+end
+
+-- TODO
+-- build module files using gcc
+function _build_modulefiles_gcc(target, sourcebatch, opt)
+
+ --[[
+ -- attempt to compile the module files as cxx
+ local modulefiles = {}
+ opt = table.join(opt, {configs = {}})
+ sourcebatch.sourcekind = "cxx"
+ sourcebatch.objectfiles = sourcebatch.objectfiles or {}
+ sourcebatch.dependfiles = sourcebatch.dependfiles or {}
+ for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ local objectfile = target:objectfile(sourcefile)
+ local dependfile = target:dependfile(objectfile)
+ local modulefile = objectfile .. ".pcm"
+
+ -- compile module file to *.pcm
+ local singlebatch = {sourcekind = "cxx", sourcefiles = {sourcefile}, objectfiles = {objectfile}, dependfiles = {dependfile}}
+ opt.configs.cxxflags = {"-fmodules-ts", "-fmodule-output=" .. modulefile, "-x c++"}
+ import("private.action.build.object")(target, singlebatch, opt)
+ table.insert(modulefiles, modulefile)
+ table.insert(sourcebatch.objectfiles, objectfile)
+ table.insert(sourcebatch.dependfiles, dependfile)
+ end
+
+ -- add module files
+ for _, modulefile in ipairs(modulefiles) do
+ target:add("cxxflags", "-fmodules-ts", "-fmodule-file=" .. modulefile)
+ end]]
+ raise("compiler(gcc): not implemented for c++ module!")
+end
+
+-- build module files using msvc
+function _build_modulefiles_msvc(target, sourcebatch, opt)
+
+ -- attempt to compile the module files as cxx
+ local modulefiles = {}
+ opt = table.join(opt, {configs = {}})
+ sourcebatch.sourcekind = "cxx"
+ sourcebatch.objectfiles = sourcebatch.objectfiles or {}
+ sourcebatch.dependfiles = sourcebatch.dependfiles or {}
+ for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ local objectfile = target:objectfile(sourcefile)
+ local dependfile = target:dependfile(objectfile)
+ local modulefile = objectfile .. ".pcm"
+
+ -- compile module file to *.pcm
+ local singlebatch = {sourcekind = "cxx", sourcefiles = {sourcefile}, objectfiles = {objectfile}, dependfiles = {dependfile}}
+ opt.configs.cxxflags = {"/experimental:module /module:interface /module:output " .. os.args(modulefile), "/TP"}
+ import("private.action.build.object")(target, singlebatch, opt)
+ table.insert(modulefiles, modulefile)
+ table.insert(sourcebatch.objectfiles, objectfile)
+ table.insert(sourcebatch.dependfiles, dependfile)
+ end
+
+ -- add module files
+ for _, modulefile in ipairs(modulefiles) do
+ target:add("cxxflags", "/experimental:module /module:reference " .. os.args(modulefile))
+ end
+end
+
+-- build module files
+function main(target, sourcebatch, opt)
+
+ -- do compile
+ local compinst = compiler.load("cxx")
+ if compinst:name() == "clang" and compinst:has_flags("-fmodules-ts") then
+ _build_modulefiles_clang(target, sourcebatch, opt)
+ elseif compinst:name() == "gcc" and compinst:has_flags("-fmodules-ts") then
+ _build_modulefiles_gcc(target, sourcebatch, opt)
+ elseif compinst:name() == "cl" and compinst:has_flags("/experimental:module") then
+ _build_modulefiles_msvc(target, sourcebatch, opt)
+ else
+ raise("compiler(%s): does not support c++ module!", compinst:name())
+ end
+end
+
diff --git a/xmake/rules/c++/modules/xmake.lua b/xmake/rules/c++/modules/xmake.lua
new file mode 100644
index 000000000..29f0b28f7
--- /dev/null
+++ b/xmake/rules/c++/modules/xmake.lua
@@ -0,0 +1,25 @@
+--!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 - 2019, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+-- define rule: c++.build.modules
+rule("c++.build.modules")
+ set_extensions(".mpp", ".mxx", ".cppm", ".ixx")
+ before_build_files("build_modulefiles")
+
diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua
index 1330fbfca..cb5f5b43c 100644
--- a/xmake/rules/c++/xmake.lua
+++ b/xmake/rules/c++/xmake.lua
@@ -28,24 +28,20 @@ rule("c.build.pcheader")
rule("c.build")
set_extensions(".c")
add_deps("c.build.pcheader")
- on_build_files(function (target, sourcebatch, opt)
- import("private.action.build.object")(target, sourcebatch, opt)
- end)
+ on_build_files("private.action.build.object")
--- define rule: cpp.build.pcheader
-rule("cpp.build.pcheader")
+-- define rule: c++.build.pcheader
+rule("c++.build.pcheader")
before_build(function (target, opt)
import("private.action.build.pcheader")(target, "cxx", opt)
end)
--- define rule: cpp.build
-rule("cpp.build")
+-- define rule: c++.build
+rule("c++.build")
set_extensions(".cpp", ".cc", ".cxx")
- add_deps("cpp.build.pcheader")
- on_build_files(function (target, sourcebatch, opt)
- import("private.action.build.object")(target, sourcebatch, opt)
- end)
+ add_deps("c++.build.pcheader", "c++.build.modules")
+ on_build_files("private.action.build.object")
-- define rule: cpp
-rule("cpp")
- add_deps("cpp.build", "c.build", "utils.merge.object", "utils.merge.archive")
+rule("c++")
+ add_deps("c++.build", "c.build", "utils.merge.object", "utils.merge.archive")
diff --git a/xmake/rules/dlang/xmake.lua b/xmake/rules/dlang/xmake.lua
index 27f06c432..95615a3ec 100644
--- a/xmake/rules/dlang/xmake.lua
+++ b/xmake/rules/dlang/xmake.lua
@@ -21,9 +21,7 @@
-- define rule: dlang.build
rule("dlang.build")
set_extensions(".d")
- on_build_files(function (target, sourcebatch, opt)
- import("private.action.build.object")(target, sourcebatch, opt)
- end)
+ on_build_files("private.action.build.object")
-- define rule: dlang
rule("dlang")
diff --git a/xmake/rules/go/xmake.lua b/xmake/rules/go/xmake.lua
index 97d929e08..2830ce6fb 100644
--- a/xmake/rules/go/xmake.lua
+++ b/xmake/rules/go/xmake.lua
@@ -21,9 +21,7 @@
-- define rule: go.build
rule("go.build")
set_extensions(".go")
- on_build_files(function (target, sourcebatch, opt)
- import("build.object")(target, sourcebatch, opt)
- end)
+ on_build_files("build.object")
-- define rule: cpp
rule("go")
diff --git a/xmake/rules/objc++/xmake.lua b/xmake/rules/objc++/xmake.lua
index 986e3d1a3..7356cf36d 100644
--- a/xmake/rules/objc++/xmake.lua
+++ b/xmake/rules/objc++/xmake.lua
@@ -22,17 +22,13 @@
rule("objc.build")
set_extensions(".m")
add_deps("c.build.pcheader")
- on_build_files(function (target, sourcebatch, opt)
- import("private.action.build.object")(target, sourcebatch, opt)
- end)
+ on_build_files("private.action.build.object")
-- define rule: objc++.build
rule("objc++.build")
set_extensions(".mm")
- add_deps("cpp.build.pcheader")
- on_build_files(function (target, sourcebatch, opt)
- import("private.action.build.object")(target, sourcebatch, opt)
- end)
+ add_deps("c++.build.pcheader")
+ on_build_files("private.action.build.object")
-- define rule: objc
rule("objc++")
diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua
index 3d88f2c96..50ad79a89 100644
--- a/xmake/rules/rust/xmake.lua
+++ b/xmake/rules/rust/xmake.lua
@@ -21,9 +21,7 @@
-- define rule: rust.build
rule("rust.build")
set_extensions(".rs")
- on_build(function (target, opt)
- import("build.target")(target, opt)
- end)
+ on_build("build.target")
-- define rule: cpp
rule("rust")
diff --git a/xmake/rules/swift/xmake.lua b/xmake/rules/swift/xmake.lua
index 9be96096d..04fbfbcab 100644
--- a/xmake/rules/swift/xmake.lua
+++ b/xmake/rules/swift/xmake.lua
@@ -21,9 +21,7 @@
-- define rule: swift.build
rule("swift.build")
set_extensions(".swift")
- on_build_files(function (target, sourcebatch, opt)
- import("private.action.build.object")(target, sourcebatch, opt)
- end)
+ on_build_files("private.action.build.object")
-- define rule: swift
rule("swift")
diff --git a/xmake/rules/winsdk/xmake.lua b/xmake/rules/winsdk/xmake.lua
index e5190fc31..81fc72650 100644
--- a/xmake/rules/winsdk/xmake.lua
+++ b/xmake/rules/winsdk/xmake.lua
@@ -21,9 +21,7 @@
-- define rule: win.sdk.resource
rule("win.sdk.resource")
set_extensions(".rc")
- on_build_files(function (target, sourcebatch, opt)
- import("private.action.build.object")(target, sourcebatch, opt)
- end)
+ on_build_files("private.action.build.object")
-- define rule: application
rule("win.sdk.application")