summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-05-26 09:46:18 +0800
committerruki <[email protected]>2017-05-26 09:46:18 +0800
commit27887740f2be1e2381511632a44b16f2dcddbc49 (patch)
treebdec3bd3b0f54aa715046b0e2e0497577915fccf
parente5207e47e6b5811f4386d42120b804f740d442c2 (diff)
improve xxx_script to support pattern match
-rw-r--r--CHANGELOG.md2
-rw-r--r--tests/apis/add_xxx/test.lua3
-rw-r--r--tests/apis/add_xxx/xmake.lua11
-rw-r--r--tests/apis/xxx_script/test.lua9
-rw-r--r--tests/apis/xxx_script/xmake.lua26
-rw-r--r--xmake/actions/build/builder.lua7
-rw-r--r--xmake/actions/clean/main.lua6
-rw-r--r--xmake/actions/config/main.lua29
-rw-r--r--xmake/actions/install/install.lua6
-rw-r--r--xmake/actions/package/main.lua8
-rw-r--r--xmake/actions/uninstall/uninstall.lua6
-rw-r--r--xmake/core/base/interpreter.lua32
-rw-r--r--xmake/core/project/target.lua25
13 files changed, 132 insertions, 38 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ca42f2e72..7c22c172a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@
* Improve `print` interface to dump table
* [#111](https://github.com/tboox/xmake/issues/111): Add `--root` common option to allow run xmake command as root
* [#113](https://github.com/tboox/xmake/pull/113): Privilege manage when running as root, store the root privilege and degrade.
+* Improve `xxx_script` in `xmake.lua` to support pattern match, .e.g `on_build("macosx|.*", function (target) end)`
### Bugs fixed
@@ -311,6 +312,7 @@
* 改进`print`接口去更好些显示table数据
* [#111](https://github.com/tboox/xmake/issues/111): 添加`--root`通用选项去临时支持作为root运行
* [#113](https://github.com/tboox/xmake/pull/113): 改进权限管理,现在作为root运行也是非常安全的
+* 改进`xxx_script`工程描述api,支持多平台模式选择, 例如:`on_build("macosx|.*", function (target) end)`
### Bugs修复
diff --git a/tests/apis/add_xxx/test.lua b/tests/apis/add_xxx/test.lua
new file mode 100644
index 000000000..a4a38b0ce
--- /dev/null
+++ b/tests/apis/add_xxx/test.lua
@@ -0,0 +1,3 @@
+function main()
+ os.exec("xmake")
+end
diff --git a/tests/apis/add_xxx/xmake.lua b/tests/apis/add_xxx/xmake.lua
new file mode 100644
index 000000000..80f2ed518
--- /dev/null
+++ b/tests/apis/add_xxx/xmake.lua
@@ -0,0 +1,11 @@
+add_defines("TEST1")
+
+target("test")
+
+ add_defines("TEST2")
+ on_build(function (target)
+ local defines = table.concat(target:get("defines"), " ")
+ assert(defines:find("TEST1", 1, true))
+ assert(defines:find("TEST2", 1, true))
+ end)
+
diff --git a/tests/apis/xxx_script/test.lua b/tests/apis/xxx_script/test.lua
new file mode 100644
index 000000000..bb2762d7d
--- /dev/null
+++ b/tests/apis/xxx_script/test.lua
@@ -0,0 +1,9 @@
+function main()
+ os.exec("xmake")
+ if os.host() == "macosx" then
+ os.exec("xmake f -p iphoneos")
+ os.exec("xmake")
+ os.exec("xmake f -p iphoneos -a arm64")
+ os.exec("xmake")
+ end
+end
diff --git a/tests/apis/xxx_script/xmake.lua b/tests/apis/xxx_script/xmake.lua
new file mode 100644
index 000000000..d522b11fb
--- /dev/null
+++ b/tests/apis/xxx_script/xmake.lua
@@ -0,0 +1,26 @@
+target("test")
+
+ before_build("iphoneos|arm64", function (target)
+ assert(vformat("$(plat)") == "iphoneos")
+ assert(vformat("$(arch)") == "arm64")
+ end)
+
+ before_build("macosx|.*", function (target)
+ assert(vformat("$(plat)") == "macosx")
+ end)
+
+ before_build(function (target)
+ print("before_build")
+ end)
+
+ on_build(function (target)
+ print("build")
+ end)
+
+ after_build(function (target)
+ print("after_build")
+ end)
+
+ after_build("linux|.*", function (target)
+ assert(vformat("$(plat)") == "linux")
+ end)
diff --git a/xmake/actions/build/builder.lua b/xmake/actions/build/builder.lua
index 0a187241a..df4ed8322 100644
--- a/xmake/actions/build/builder.lua
+++ b/xmake/actions/build/builder.lua
@@ -24,6 +24,7 @@
-- imports
import("core.base.option")
+import("core.project.config")
import("core.project.project")
import("core.platform.environment")
@@ -42,9 +43,9 @@ function _build_target(target)
-- the target scripts
local scripts =
{
- target:get("build_before")
- , target:get("build") or _on_build_target
- , target:get("build_after")
+ target:script("build_before")
+ , target:script("build", _on_build_target)
+ , target:script("build_after")
}
-- run the target scripts
diff --git a/xmake/actions/clean/main.lua b/xmake/actions/clean/main.lua
index 8c5253525..3d439e008 100644
--- a/xmake/actions/clean/main.lua
+++ b/xmake/actions/clean/main.lua
@@ -89,9 +89,9 @@ function _clean_target(target)
-- the target scripts
local scripts =
{
- target:get("clean_before")
- , target:get("clean") or _on_clean_target
- , target:get("clean_after")
+ target:script("clean_before")
+ , target:script("clean", _on_clean_target)
+ , target:script("clean_after")
}
-- run the target scripts
diff --git a/xmake/actions/config/main.lua b/xmake/actions/config/main.lua
index 0900aecb9..2f5edceba 100644
--- a/xmake/actions/config/main.lua
+++ b/xmake/actions/config/main.lua
@@ -58,31 +58,30 @@ end
function _need_check()
-- clean?
- if option.get("clean") then
- return true
- end
+ local changed = option.get("clean")
-- the configure has been changed? reconfig it
- if config.changed() then
- return true
+ if not changed and config.changed() then
+ changed = true
end
-- get the current mtimes
local mtimes = project.mtimes()
-- get the previous mtimes
- local changed = false
- local mtimes_prev = cache.get("mtimes")
- if mtimes_prev then
+ if not changed then
+ local mtimes_prev = cache.get("mtimes")
+ if mtimes_prev then
- -- check for all project files
- for file, mtime in pairs(mtimes) do
+ -- check for all project files
+ for file, mtime in pairs(mtimes) do
- -- modified? reconfig and rebuild it
- local mtime_prev = mtimes_prev[file]
- if not mtime_prev or mtime > mtime_prev then
- changed = true
- break
+ -- modified? reconfig and rebuild it
+ local mtime_prev = mtimes_prev[file]
+ if not mtime_prev or mtime > mtime_prev then
+ changed = true
+ break
+ end
end
end
end
diff --git a/xmake/actions/install/install.lua b/xmake/actions/install/install.lua
index b9bd6ca79..31f23a667 100644
--- a/xmake/actions/install/install.lua
+++ b/xmake/actions/install/install.lua
@@ -36,9 +36,9 @@ function _install_target(target)
-- the target scripts
local scripts =
{
- target:get("install_before")
- , target:get("install") or platform.get("install")
- , target:get("install_after")
+ target:script("install_before")
+ , target:script("install", platform.get("install"))
+ , target:script("install_after")
}
-- install the target scripts
diff --git a/xmake/actions/package/main.lua b/xmake/actions/package/main.lua
index 729026e0c..59387936a 100644
--- a/xmake/actions/package/main.lua
+++ b/xmake/actions/package/main.lua
@@ -113,7 +113,7 @@ option("[targetname]")
end
-- package target
-function _package_target(target)
+function _on_package_target(target)
-- is phony target?
if target:isphony() then
@@ -147,9 +147,9 @@ function _package(target)
-- the target scripts
local scripts =
{
- target:get("package_before")
- , target:get("package") or _package_target
- , target:get("package_after")
+ target:script("package_before")
+ , target:script("package", _on_package_target)
+ , target:script("package_after")
}
-- package the target scripts
diff --git a/xmake/actions/uninstall/uninstall.lua b/xmake/actions/uninstall/uninstall.lua
index a3a8b869f..3d0c6e0c7 100644
--- a/xmake/actions/uninstall/uninstall.lua
+++ b/xmake/actions/uninstall/uninstall.lua
@@ -36,9 +36,9 @@ function _uninstall_target(target)
-- the target scripts
local scripts =
{
- target:get("uninstall_before")
- , target:get("uninstall") or platform.get("uninstall")
- , target:get("uninstall_after")
+ target:script("uninstall_before")
+ , target:script("uninstall", platform.get("uninstall"))
+ , target:script("uninstall_after")
}
-- uninstall the target scripts
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index 108eaaa1f..746347f32 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -262,12 +262,17 @@ function interpreter:_api_register_xxx_script(scope_kind, action, ...)
-- define implementation
local implementation = function (self, scope, name, arg1, arg2)
- -- on_xxx(mode, script)?
+ -- patch action to name
+ if action ~= "on" then
+ name = name .. "_" .. action
+ end
+
+ -- on_xxx(pattern, script)?
if arg1 and arg2 then
- -- get mode
- local mode = arg1
- assert(type(mode) == "string")
+ -- get pattern
+ local pattern = arg1
+ assert(type(pattern) == "string")
-- get script
local script, errors = self:_script(arg2)
@@ -275,8 +280,15 @@ function interpreter:_api_register_xxx_script(scope_kind, action, ...)
os.raise("%s_%s(%s, %s): %s", action, name, tostring(arg1), tostring(arg2), errors)
end
- -- save mode and script
- scope[name] = {mode = mode, script = script}
+ -- save script
+ local scripts = scope[name] or {}
+ if type(scripts) == "table" then
+ scripts[pattern] = script
+ elseif type(scripts) == "function" then
+ scripts = {__generic__ = scripts}
+ scripts[pattern] = script
+ end
+ scope[name] = scripts
-- on_xxx(script)?
elseif arg1 then
@@ -288,7 +300,13 @@ function interpreter:_api_register_xxx_script(scope_kind, action, ...)
end
-- save script
- scope[name] = script
+ local scripts = scope[name]
+ if type(scripts) == "table" then
+ scripts["__generic__"] = script
+ else
+ scripts = script
+ end
+ scope[name] = scripts
end
end
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 7d336ea03..4edf0f6d1 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -631,5 +631,30 @@ function target:sourcebatches()
return sourcebatches, modified
end
+-- get xxx_script
+function target:script(name, generic)
+
+ -- get script
+ local script = self:get(name)
+ if type(script) == "function" then
+ return script
+ elseif type(script) == "table" then
+
+ -- match script for special plat and arch
+ local pattern = (config.get("plat") or "") .. '|' .. (config.get("arch") or "")
+ for _pattern, _script in pairs(script) do
+ if not _pattern:startswith("__") and pattern:find('^' .. _pattern .. '$') then
+ return _script
+ end
+ end
+
+ -- get generic script
+ return script["__generic__"] or generic
+ end
+
+ -- only generic script
+ return generic
+end
+
-- return module
return target