diff options
| author | ruki <[email protected]> | 2018-12-12 00:48:18 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-12-11 22:49:50 +0800 |
| commit | bdfab7b2180fd0652c135b735019b43b5dabc6db (patch) | |
| tree | 1e539aa11f65718853247082e958ace983cdf2da | |
| parent | e429d90d867a8919b0480d1838f0d7d2bab94f27 (diff) | |
add target:installdir() and set_installdir
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | xmake/actions/install/install.lua | 20 | ||||
| -rw-r--r-- | xmake/actions/install/install_admin.lua | 14 | ||||
| -rw-r--r-- | xmake/actions/install/main.lua | 32 | ||||
| -rw-r--r-- | xmake/actions/uninstall/main.lua | 32 | ||||
| -rw-r--r-- | xmake/actions/uninstall/uninstall.lua | 20 | ||||
| -rw-r--r-- | xmake/actions/uninstall/uninstall_admin.lua | 9 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 30 |
8 files changed, 83 insertions, 76 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dd47462b..79edcd2f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Add `string.serialize` and `string.deserialize` to serialize and deserialize object, function and others. * Add `xmake g --menu` +* [#283](https://github.com/tboox/xmake/issues/283): Add `target:installdir()` and `set_installdir()` api for target ### Changes @@ -535,6 +536,7 @@ * 添加`string.serialize`和`string.deserialize`去序列化,反序列化对象,函数以及其他类型 * 添加`xmake g --menu`去图形化配置全局选项 +* [#283](https://github.com/tboox/xmake/issues/283): 添加`target:installdir()`和`set_installdir()`接口 ### 改进 diff --git a/xmake/actions/install/install.lua b/xmake/actions/install/install.lua index 399d33e25..e03c3a0bb 100644 --- a/xmake/actions/install/install.lua +++ b/xmake/actions/install/install.lua @@ -36,7 +36,7 @@ function _install_binary(target) end -- the binary directory - local binarydir = path.join(_g.installdir, "bin") + local binarydir = path.join(target:installdir(), "bin") -- make the binary directory os.mkdir(binarydir) @@ -54,10 +54,10 @@ function _install_library(target) end -- the library directory - local librarydir = path.join(_g.installdir, "lib") + local librarydir = path.join(target:installdir(), "lib") -- the include directory - local includedir = path.join(_g.installdir, "include") + local includedir = path.join(target:installdir(), "include") -- make the library directory os.mkdir(librarydir) @@ -108,6 +108,15 @@ function _on_install_target(target) return end + -- get install directory + local installdir = target:installdir() + if not installdir then + return + end + + -- trace + print("installing %s to %s ...", target:name(), installdir) + -- build target with rules local done = false for _, r in ipairs(target:orderules()) do @@ -200,14 +209,11 @@ function _install_target_and_deps(target) end -- install targets -function main(targetname, installdir) +function main(targetname) -- init finished states _g.finished = {} - -- init install directory - _g.installdir = installdir - -- install the given target? if targetname and not targetname:startswith("__") then _install_target_and_deps(project.target(targetname)) diff --git a/xmake/actions/install/install_admin.lua b/xmake/actions/install/install_admin.lua index 5dfbaa42f..dc8e658af 100644 --- a/xmake/actions/install/install_admin.lua +++ b/xmake/actions/install/install_admin.lua @@ -30,7 +30,7 @@ import("core.platform.platform") import("install") -- install -function main(targetname, installdir) +function main(targetname, installdir, prefix) -- enter project directory os.cd(project.directory()) @@ -44,8 +44,18 @@ function main(targetname, installdir) -- save the current option and push a new option context option.save() + -- pass installdir to option + if installdir then + option.set("installdir", installdir) + end + + -- pass prefix to option + if prefix then + option.set("prefix", prefix) + end + -- install target - install(targetname, installdir) + install(targetname) -- restore the previous option context option.restore() diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua index c119e4ce4..60ff38ff5 100644 --- a/xmake/actions/install/main.lua +++ b/xmake/actions/install/main.lua @@ -30,26 +30,6 @@ import("core.base.privilege") import("privilege.sudo") import("install") --- get install directory -function _installdir() - - -- the install directory - -- - -- DESTDIR: be compatible with https://www.gnu.org/prep/standards/html_node/DESTDIR.html - -- - local installdir = option.get("installdir") or os.getenv("INSTALLDIR") or os.getenv("DESTDIR") or platform.get("installdir") - assert(installdir, "unknown install directory!") - - -- append prefix - local prefix = option.get("prefix") or os.getenv("PREFIX") - if prefix then - installdir = path.join(installdir, prefix) - end - - -- ok - return installdir -end - -- main function main() @@ -59,19 +39,13 @@ function main() -- build it first task.run("build", {target = targetname, all = option.get("all")}) - -- get install directory - local installdir = _installdir() - - -- trace - print("installing to %s ...", installdir) - -- attempt to install directly try { function () -- install target - install(targetname or ifelse(option.get("all"), "__all", "__def"), installdir) + install(targetname or ifelse(option.get("all"), "__all", "__def")) -- trace cprint("${bright}install ok!${clear}${ok_hand}") @@ -89,7 +63,7 @@ function main() function () -- install target - install(targetname or ifelse(option.get("all"), "__all", "__def"), installdir) + install(targetname or ifelse(option.get("all"), "__all", "__def")) -- trace cprint("${bright}install ok!${clear}${ok_hand}") @@ -132,7 +106,7 @@ function main() if confirm then -- install target with administrator permission - sudo.runl(path.join(os.scriptdir(), "install_admin.lua"), {targetname or ifelse(option.get("all"), "__all", "__def"), installdir}) + sudo.runl(path.join(os.scriptdir(), "install_admin.lua"), {targetname or ifelse(option.get("all"), "__all", "__def"), option.get("installdir"), option.get("prefix")}) -- trace cprint("${bright}install ok!${clear}${ok_hand}") diff --git a/xmake/actions/uninstall/main.lua b/xmake/actions/uninstall/main.lua index 25ecf70d9..1a2a80577 100644 --- a/xmake/actions/uninstall/main.lua +++ b/xmake/actions/uninstall/main.lua @@ -30,26 +30,6 @@ import("core.base.privilege") import("privilege.sudo") import("uninstall") --- get install directory -function _installdir() - - -- the install directory - -- - -- DESTDIR: be compatible with https://www.gnu.org/prep/standards/html_node/DESTDIR.html - -- - local installdir = option.get("installdir") or os.getenv("INSTALLDIR") or os.getenv("DESTDIR") or platform.get("installdir") - assert(installdir, "unknown install directory!") - - -- append prefix - local prefix = option.get("prefix") or os.getenv("PREFIX") - if prefix then - installdir = path.join(installdir, prefix) - end - - -- ok - return installdir -end - -- main function main() @@ -59,19 +39,13 @@ function main() -- config it first task.run("config", {target = targetname, require = "n"}) - -- get install directory - local installdir = _installdir() - - -- trace - print("uninstalling from %s ...", installdir) - -- attempt to uninstall directly try { function () -- uninstall target - uninstall(targetname, installdir) + uninstall(targetname) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") @@ -89,7 +63,7 @@ function main() function () -- uninstall target - uninstall(targetname, installdir) + uninstall(targetname) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") @@ -132,7 +106,7 @@ function main() if confirm then -- uninstall target with administrator permission - sudo.runl(path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname or "__all", installdir}) + sudo.runl(path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname or "__all", option.get("installdir"), option.get("prefix")}) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") diff --git a/xmake/actions/uninstall/uninstall.lua b/xmake/actions/uninstall/uninstall.lua index abd6acad3..a3fb79ebe 100644 --- a/xmake/actions/uninstall/uninstall.lua +++ b/xmake/actions/uninstall/uninstall.lua @@ -36,7 +36,7 @@ function _uninstall_binary(target) end -- the binary directory - local binarydir = path.join(_g.installdir, "bin") + local binarydir = path.join(target:installdir(), "bin") -- remove the target file os.rm(path.join(binarydir, path.filename(target:targetfile()))) @@ -51,10 +51,10 @@ function _uninstall_library(target) end -- the library directory - local librarydir = path.join(_g.installdir, "lib") + local librarydir = path.join(target:installdir(), "lib") -- the include directory - local includedir = path.join(_g.installdir, "include") + local includedir = path.join(target:installdir(), "include") -- remove the target file os.rm(path.join(librarydir, path.filename(target:targetfile()))) @@ -92,6 +92,15 @@ function _on_uninstall_target(target) return end + -- get install directory + local installdir = target:installdir() + if not installdir then + return + end + + -- trace + print("uninstalling %s from %s ...", target:name(), installdir) + -- build target with rules local done = false for _, r in ipairs(target:orderules()) do @@ -184,14 +193,11 @@ function _uninstall_target_and_deps(target) end -- uninstall -function main(targetname, installdir) +function main(targetname) -- init finished states _g.finished = {} - -- init install directory - _g.installdir = installdir - -- uninstall given target? if targetname then _uninstall_target_and_deps(project.target(targetname)) diff --git a/xmake/actions/uninstall/uninstall_admin.lua b/xmake/actions/uninstall/uninstall_admin.lua index 10cf012d1..73adb908d 100644 --- a/xmake/actions/uninstall/uninstall_admin.lua +++ b/xmake/actions/uninstall/uninstall_admin.lua @@ -30,7 +30,7 @@ import("core.platform.platform") import("uninstall") -- uninstall -function main(targetname, installdir) +function main(targetname, installdir, prefix) -- enter project directory os.cd(project.directory()) @@ -49,8 +49,13 @@ function main(targetname, installdir) option.set("installdir", installdir) end + -- pass prefix to option + if prefix then + option.set("prefix", prefix) + end + -- uninstall target - uninstall(ifelse(targetname ~= "__all", targetname, nil)) + uninstall(targetname ~= "__all" and targetname or nil) -- restore the previous option context option.restore() diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 908f55ad5..8484c0712 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -30,6 +30,7 @@ local os = require("base/os") local path = require("base/path") local utils = require("base/utils") local table = require("base/table") +local baseoption = require("base/option") local deprecated = require("base/deprecated") local rule = require("project/rule") local option = require("project/option") @@ -85,6 +86,7 @@ function target.apis() "target.set_targetdir" , "target.set_objectdir" , "target.set_dependir" + , "target.set_installdir" -- target.add_xxx , "target.add_files" -- target.del_xxx @@ -686,6 +688,34 @@ function target:headerdir() return self:get("headerdir") or config.buildir() end +-- get install directory +function target:installdir() + + -- get it from the cache + local installdir = self._INSTALLDIR + if not installdir then + + -- get it from target + installdir = self:get("installdir") + if not installdir then + + -- DESTDIR: be compatible with https://www.gnu.org/prep/standards/html_node/DESTDIR.html + installdir = baseoption.get("installdir") or os.getenv("INSTALLDIR") or os.getenv("DESTDIR") or platform.get("installdir") + assert(installdir, "unknown install directory!") + + -- append prefix + local prefix = baseoption.get("prefix") or os.getenv("PREFIX") + if prefix then + installdir = path.join(installdir, prefix) + end + end + self._INSTALLDIR = installdir + end + + -- ok + return installdir +end + -- get rules of the source file function target:filerules(sourcefile) |
