diff options
| author | ruki <[email protected]> | 2018-04-14 00:54:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-04-13 23:27:52 +0800 |
| commit | 7467328ac81ac59a95125420b4dbca1bc3abc5f3 (patch) | |
| tree | 3f5bdaa14fdb149f27489be4493e7a0d35fdf8a1 | |
| parent | 9e80cf8b5075b5eb4af1a2a0b652b486ddfe2da9 (diff) | |
add is_mode is_plat and is_arch builtin apis
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 30 | ||||
| -rw-r--r-- | xmake/core/project/config.lua | 45 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 48 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/is_arch.lua | 27 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/is_host.lua | 27 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/is_mode.lua | 27 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/is_plat.lua | 27 | ||||
| -rw-r--r-- | xmake/rules/mode/xmake.lua | 5 |
9 files changed, 191 insertions, 47 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index eb1f698f3..6d5152e11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#158](https://github.com/tboox/xmake/issues/158): Support CUDA Toolkit and Compiler * Add `set_tools` and `add_tools` apis to change the toolchains for special target * Add builtin rules: `mode:debug` and `mode:release` +* Add `is_mode`, `is_arch` and `is_plat` builtin apis in the custom scripts ### Changes @@ -450,6 +451,7 @@ * [#158](https://github.com/tboox/xmake/issues/158): 增加对Cuda编译环境的支持 * 添加`set_tools`和`add_tools`接口为指定target目标设置编译工具链 * 添加内建规则:`mode:debug`和`mode:release` +* 添加`is_mode`, `is_arch` 和`is_plat`内置接口到自定义脚本域 ### 改进 diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 70af9c6bb..a3b66b674 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -692,6 +692,36 @@ function os.arch() return xmake._ARCH end +-- the current host is belong to the given hosts? +function os.is_host(...) + + -- get the current host + local host = os.host() + if not host then return false end + + -- exists this host? and escape '-' + for _, h in ipairs(table.join(...)) do + if h and type(h) == "string" and host:find(h:gsub("%-", "%%-")) then + return true + end + end +end + +-- the current platform is belong to the given architectures? +function os.is_arch(...) + + -- get the host architecture + local arch = os.arch() + if not arch then return false end + + -- exists this architecture? and escape '-' + for _, a in ipairs(table.join(...)) do + if a and type(a) == "string" and arch:find("^" .. a:gsub("%-", "%%-") .. "$") then + return true + end + end +end + -- get the system null device function os.nuldev(input) diff --git a/xmake/core/project/config.lua b/xmake/core/project/config.lua index 598212b03..e04fad967 100644 --- a/xmake/core/project/config.lua +++ b/xmake/core/project/config.lua @@ -237,6 +237,51 @@ function config.clear() config._CONFIGS = nil end +-- the current mode is belong to the given modes? +function config.is_mode(...) + + -- get the current mode + local mode = config.get("mode") + if not mode then return false end + + -- exists this mode? + for _, m in ipairs(table.join(...)) do + if m and type(m) == "string" and m == mode then + return true + end + end +end + +-- the current platform is belong to the given platforms? +function config.is_plat(...) + + -- get the current platform + local plat = config.get("plat") + if not plat then return false end + + -- exists this platform? and escape '-' + for _, p in ipairs(table.join(...)) do + if p and type(p) == "string" and plat:find("^" .. p:gsub("%-", "%%-") .. "$") then + return true + end + end +end + +-- the current platform is belong to the given architectures? +function config.is_arch(...) + + -- get the current architecture + local arch = config.get("arch") + if not arch then return false end + + -- exists this architecture? and escape '-' + for _, a in ipairs(table.join(...)) do + if a and type(a) == "string" and arch:find("^" .. a:gsub("%-", "%%-") .. "$") then + return true + end + end +end + -- dump the configure function config.dump() diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index 9c8020c38..f54198132 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -65,47 +65,17 @@ end -- the current mode is belong to the given modes? function project._api_is_mode(interp, ...) - - -- get the current mode - local mode = config.get("mode") - if not mode then return false end - - -- exists this mode? - for _, m in ipairs(table.join(...)) do - if m and type(m) == "string" and m == mode then - return true - end - end + return config.is_mode(...) end -- the current platform is belong to the given platforms? function project._api_is_plat(interp, ...) - - -- get the current platform - local plat = config.get("plat") - if not plat then return false end - - -- exists this platform? and escape '-' - for _, p in ipairs(table.join(...)) do - if p and type(p) == "string" and plat:find("^" .. p:gsub("%-", "%%-") .. "$") then - return true - end - end + return config.is_plat(...) end -- the current platform is belong to the given architectures? function project._api_is_arch(interp, ...) - - -- get the current architecture - local arch = config.get("arch") - if not arch then return false end - - -- exists this architecture? and escape '-' - for _, a in ipairs(table.join(...)) do - if a and type(a) == "string" and arch:find("^" .. a:gsub("%-", "%%-") .. "$") then - return true - end - end + return config.is_arch(...) end -- the current kind is belong to the given kinds? @@ -125,17 +95,7 @@ end -- the current host is belong to the given hosts? function project._api_is_host(interp, ...) - - -- get the current host - local host = os.host() - if not host then return false end - - -- exists this host? and escape '-' - for _, h in ipairs(table.join(...)) do - if h and type(h) == "string" and host:find(h:gsub("%-", "%%-")) then - return true - end - end + return os.is_host(...) end -- enable options? diff --git a/xmake/core/sandbox/modules/is_arch.lua b/xmake/core/sandbox/modules/is_arch.lua new file mode 100644 index 000000000..9cac20f0a --- /dev/null +++ b/xmake/core/sandbox/modules/is_arch.lua @@ -0,0 +1,27 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file is_arch.lua +-- + +-- return module +return require("project/config").is_arch + diff --git a/xmake/core/sandbox/modules/is_host.lua b/xmake/core/sandbox/modules/is_host.lua new file mode 100644 index 000000000..bbb7ed06d --- /dev/null +++ b/xmake/core/sandbox/modules/is_host.lua @@ -0,0 +1,27 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file is_host.lua +-- + +-- return module +return require("base/os").is_host + diff --git a/xmake/core/sandbox/modules/is_mode.lua b/xmake/core/sandbox/modules/is_mode.lua new file mode 100644 index 000000000..66ecc99ff --- /dev/null +++ b/xmake/core/sandbox/modules/is_mode.lua @@ -0,0 +1,27 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file is_mode.lua +-- + +-- return module +return require("project/config").is_mode + diff --git a/xmake/core/sandbox/modules/is_plat.lua b/xmake/core/sandbox/modules/is_plat.lua new file mode 100644 index 000000000..4ec754638 --- /dev/null +++ b/xmake/core/sandbox/modules/is_plat.lua @@ -0,0 +1,27 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file is_plat.lua +-- + +-- return module +return require("project/config").is_plat + diff --git a/xmake/rules/mode/xmake.lua b/xmake/rules/mode/xmake.lua index 1d0aa11d3..24765a6f8 100644 --- a/xmake/rules/mode/xmake.lua +++ b/xmake/rules/mode/xmake.lua @@ -26,9 +26,8 @@ rule("mode:debug") on_load(function (target) - -- TODO if is_mode("debug") -- is debug mode now? xmake f -m debug - if val("mode") == "debug" then + if is_mode("debug") then -- enable the debug symbols target:set("symbols", "debug") @@ -43,7 +42,7 @@ rule("mode:release") on_load(function (target) -- is release mode now? xmake f -m release - if val("mode") == "release" then + if is_mode("release") then -- set the symbols visibility: hidden target:set("symbols", "hidden") |
