summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-10-12 12:58:09 +0800
committerGitHub <[email protected]>2020-10-12 12:58:09 +0800
commite775e49fd87240391f5b26cd50faf21029b6c013 (patch)
tree1aa30bc612af92a7b42ed1f455103a94a3733a26
parentf674268196d2b5d25178d0209cc5f6f57a21e338 (diff)
parentd7c0299b40a37fcf32c3f3a0a71ec60c60e4bd07 (diff)
Merge pull request #990 from xmake-io/openmp
Add openmp rules for c/c++
-rw-r--r--tests/projects/openmp/hello/src/main.c11
-rw-r--r--tests/projects/openmp/hello/xmake.lua6
-rw-r--r--tests/projects/openmp/loop/src/main.cpp12
-rw-r--r--tests/projects/openmp/loop/xmake.lua6
-rw-r--r--xmake/core/sandbox/modules/import/core/tool/compiler.lua21
-rw-r--r--xmake/rules/c++/openmp/load.lua43
-rw-r--r--xmake/rules/c++/openmp/xmake.lua31
7 files changed, 126 insertions, 4 deletions
diff --git a/tests/projects/openmp/hello/src/main.c b/tests/projects/openmp/hello/src/main.c
new file mode 100644
index 000000000..54ba9db96
--- /dev/null
+++ b/tests/projects/openmp/hello/src/main.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <omp.h>
+
+int main(int argc, char** argv)
+{
+ #pragma omp parallel
+ {
+ printf("hello %d\n", omp_get_thread_num());
+ }
+ return 0;
+}
diff --git a/tests/projects/openmp/hello/xmake.lua b/tests/projects/openmp/hello/xmake.lua
new file mode 100644
index 000000000..c1f837287
--- /dev/null
+++ b/tests/projects/openmp/hello/xmake.lua
@@ -0,0 +1,6 @@
+add_requires("libomp", {optional = true})
+target("hello")
+ set_kind("binary")
+ add_files("src/*.c")
+ add_rules("c.openmp")
+ add_packages("libomp")
diff --git a/tests/projects/openmp/loop/src/main.cpp b/tests/projects/openmp/loop/src/main.cpp
new file mode 100644
index 000000000..273a8538e
--- /dev/null
+++ b/tests/projects/openmp/loop/src/main.cpp
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <omp.h>
+
+int main(int argc, char** argv)
+{
+ #pragma omp parallel for
+ for (int i = 0; i < 10; ++i)
+ {
+ printf("hello(%d): i = %d\n", omp_get_thread_num(), i);
+ }
+ return 0;
+}
diff --git a/tests/projects/openmp/loop/xmake.lua b/tests/projects/openmp/loop/xmake.lua
new file mode 100644
index 000000000..eb14be1a3
--- /dev/null
+++ b/tests/projects/openmp/loop/xmake.lua
@@ -0,0 +1,6 @@
+add_requires("libomp", {optional = true})
+target("loop")
+ set_kind("binary")
+ add_files("src/*.cpp")
+ add_rules("c++.openmp")
+ add_packages("libomp")
diff --git a/xmake/core/sandbox/modules/import/core/tool/compiler.lua b/xmake/core/sandbox/modules/import/core/tool/compiler.lua
index a60a8401a..ef7bb4371 100644
--- a/xmake/core/sandbox/modules/import/core/tool/compiler.lua
+++ b/xmake/core/sandbox/modules/import/core/tool/compiler.lua
@@ -32,14 +32,10 @@ local sandbox = require("sandbox/sandbox")
-- load the compiler from the given source kind
function sandbox_core_tool_compiler.load(sourcekind, opt)
-
- -- get the compiler instance
local instance, errors = compiler.load(sourcekind, opt and opt.target or nil)
if not instance then
raise(errors)
end
-
- -- ok
return instance
end
@@ -166,6 +162,23 @@ function sandbox_core_tool_compiler.build(sourcefiles, targetfile, opt)
end
end
+-- get the current compiler name of given language kind
+--
+-- @param langkind the language kind, e.g. c, cxx, mm, mxx, swift, go, rust, d, as
+--
+-- @return the compiler name
+--
+function sandbox_core_tool_compiler.name(langkind, opt)
+
+ -- get sourcekind from the language kind
+ local sourcekind = language.langkinds()[langkind]
+ assert(sourcekind, "unknown language kind: " .. langkind)
+
+ -- get the compiler instance
+ local instance = sandbox_core_tool_compiler.load(sourcekind, opt)
+ return instance:name()
+end
+
-- get all compiler features
--
-- @param langkind the language kind, e.g. c, cxx, mm, mxx, swift, go, rust, d, as
diff --git a/xmake/rules/c++/openmp/load.lua b/xmake/rules/c++/openmp/load.lua
new file mode 100644
index 000000000..6678e46e3
--- /dev/null
+++ b/xmake/rules/c++/openmp/load.lua
@@ -0,0 +1,43 @@
+--!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-2020, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file load.lua
+--
+
+-- imports
+import("core.tool.compiler")
+
+-- main entry
+function main(target, langkind)
+ local compiler_name = compiler.name(langkind)
+ local flag_name = langkind .. "flags"
+ if compiler_name == "cl" then
+ target:add(flag_name, "/openmp")
+ elseif compiler_name == "clang" or compiler_name == "clangxx" then
+ if target:is_plat("macosx") then
+ target:add(flag_name, "-Xpreprocessor -fopenmp")
+ else
+ target:add(flag_name, "-fopenmp")
+ end
+ elseif compiler_name == "gcc" or compiler_name == "gxx" then
+ target:add(flag_name, "-fopenmp")
+ elseif compiler_name == "icc" or compiler_name == "icpc" then
+ target:add(flag_name, "-qopenmp")
+ elseif compiler_name == "icl" then
+ target:add(flag_name, "-Qopenmp")
+ end
+end
diff --git a/xmake/rules/c++/openmp/xmake.lua b/xmake/rules/c++/openmp/xmake.lua
new file mode 100644
index 000000000..6a43810ad
--- /dev/null
+++ b/xmake/rules/c++/openmp/xmake.lua
@@ -0,0 +1,31 @@
+--!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-2020, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+-- define rule: c.openmp
+rule("c.openmp")
+ on_load(function (target)
+ import("load")(target, "c")
+ end)
+
+-- define rule: c++.openmp
+rule("c++.openmp")
+ on_load(function (target)
+ import("load")(target, "cxx")
+ end)