diff options
| author | ruki <[email protected]> | 2020-10-12 22:59:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-10-12 22:59:59 +0800 |
| commit | c79cdeab5474507647f1c29b2cb759550f98e8b6 (patch) | |
| tree | e414374cef6a4f5c460e1f5dba23eb8f3d8aa268 | |
| parent | 55209d22fedd5a0ea5323e81356cfe0c88603f30 (diff) | |
add libomp for openmp examples
| -rw-r--r-- | tests/projects/openmp/hello/src/main.c | 2 | ||||
| -rw-r--r-- | tests/projects/openmp/hello/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/tool/compiler.lua | 21 | ||||
| -rw-r--r-- | xmake/rules/c++/openmp/load.lua | 43 | ||||
| -rw-r--r-- | xmake/rules/c++/openmp/xmake.lua | 31 |
5 files changed, 95 insertions, 5 deletions
diff --git a/tests/projects/openmp/hello/src/main.c b/tests/projects/openmp/hello/src/main.c index cea215566..54ba9db96 100644 --- a/tests/projects/openmp/hello/src/main.c +++ b/tests/projects/openmp/hello/src/main.c @@ -5,7 +5,7 @@ int main(int argc, char** argv) { #pragma omp parallel { - printf("hello %d\n", omp_get_thread_num()); + 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 index 685a6a34b..c1f837287 100644 --- a/tests/projects/openmp/hello/xmake.lua +++ b/tests/projects/openmp/hello/xmake.lua @@ -1,3 +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/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) |
