diff options
| author | ruki <[email protected]> | 2017-09-01 15:58:39 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-09-01 15:58:39 +0800 |
| commit | 4c846e018afd9a71694bf3aa3a3ad335149d9fb0 (patch) | |
| tree | 93c342e082276f08701aa99b8758e644d6a35e5b | |
| parent | 0748e1a1f4f96a7606d710147647aca804c1c654 (diff) | |
improve install and uninstall actions, support DESTDIR and PREFIX env
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | xmake/actions/install/install.lua | 87 | ||||
| -rw-r--r-- | xmake/actions/install/install_admin.lua | 7 | ||||
| -rw-r--r-- | xmake/actions/install/main.lua | 31 | ||||
| -rw-r--r-- | xmake/actions/install/xmake.lua | 16 | ||||
| -rw-r--r-- | xmake/actions/uninstall/main.lua | 31 | ||||
| -rw-r--r-- | xmake/actions/uninstall/uninstall.lua | 71 | ||||
| -rw-r--r-- | xmake/core/platform/platform.lua | 4 | ||||
| -rw-r--r-- | xmake/platforms/installer.lua | 149 | ||||
| -rw-r--r-- | xmake/platforms/linux/install.lua | 46 | ||||
| -rw-r--r-- | xmake/platforms/linux/uninstall.lua | 45 | ||||
| -rw-r--r-- | xmake/platforms/linux/xmake.lua | 6 | ||||
| -rw-r--r-- | xmake/platforms/macosx/install.lua | 46 | ||||
| -rw-r--r-- | xmake/platforms/macosx/uninstall.lua | 45 | ||||
| -rw-r--r-- | xmake/platforms/macosx/xmake.lua | 6 |
15 files changed, 222 insertions, 370 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index eae4621be..1da9a3792 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Support `@loader_path` and `$ORIGIN` for `add_rpathdirs()` * Improve `set_version("x.x.x", {build = "%Y%m%d%H%M"})` and add build version * Move docs directory to xmake-docs repo +* Improve install and uninstall actions and support DESTDIR and PREFIX envirnoment variables ### Bugs fixed @@ -356,6 +357,7 @@ * 对`add_rpathdirs()`增加对`@loader_path`和`$ORIGIN`的内置变量支持,提供可迁移动态库加载 * 改进`set_version("x.x.x", {build = "%Y%m%d%H%M"})` 支持buildversion设置 * 移除docs目录,将其放置到独立xmake-docs仓库中,减少xmake.zip的大小,优化下载安装的效率 +* 改进安装和卸载脚本,支持DESTDIR和PREFIX环境变量设置 ### Bugs修复 diff --git a/xmake/actions/install/install.lua b/xmake/actions/install/install.lua index 357d2ec78..b640be7d2 100644 --- a/xmake/actions/install/install.lua +++ b/xmake/actions/install/install.lua @@ -25,7 +25,85 @@ -- imports import("core.base.task") import("core.project.project") -import("core.platform.platform") + +-- install binary +function _install_binary(target) + + -- is phony target? + if target:isphony() then + return + end + + -- the binary directory + local binarydir = path.join(_g.installdir, "bin") + + -- make the binary directory + os.mkdir(binarydir) + + -- copy the target file + os.cp(target:targetfile(), binarydir) +end + +-- install library +function _install_library(target) + + -- is phony target? + if target:isphony() then + return + end + + -- the library directory + local librarydir = path.join(_g.installdir, "lib") + + -- the include directory + local includedir = path.join(_g.installdir, "include") + + -- make the library directory + os.mkdir(librarydir) + + -- make the include directory + os.mkdir(includedir) + + -- copy the target file + os.cp(target:targetfile(), librarydir) + + -- copy the config.h to the include directory + local configheader, configoutput = target:configheader(includedir) + if configheader and configoutput then + os.cp(configheader, configoutput) + end + + -- copy headers to the include directory + local srcheaders, dstheaders = target:headerfiles(includedir) + if srcheaders and dstheaders then + local i = 1 + for _, srcheader in ipairs(srcheaders) do + local dstheader = dstheaders[i] + if dstheader then + os.cp(srcheader, dstheader) + end + i = i + 1 + end + end +end + +-- on install +function _on_install(target) + + -- the scripts + local scripts = + { + binary = _install_binary + , static = _install_library + , shared = _install_library + } + + -- call script + local script = scripts[target:get("kind")] + if script then + script(target) + end +end -- install the given target function _install_target(target) @@ -37,7 +115,7 @@ function _install_target(target) local scripts = { target:script("install_before") - , target:script("install", platform.get("install")) + , target:script("install", _on_install) , target:script("install_after") } @@ -74,11 +152,14 @@ function _install_target_and_deps(target) end -- install targets -function main(targetname) +function main(targetname, installdir) -- 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 ff73d7d5a..730d225f9 100644 --- a/xmake/actions/install/install_admin.lua +++ b/xmake/actions/install/install_admin.lua @@ -44,13 +44,8 @@ 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 - -- install target - install(targetname) + install(targetname, installdir) -- restore the previous option context option.restore() diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua index 2130b8b2c..15f743c97 100644 --- a/xmake/actions/install/main.lua +++ b/xmake/actions/install/main.lua @@ -30,6 +30,26 @@ 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() @@ -39,8 +59,11 @@ 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 ...", option.get("installdir") or platform.get("installdir")) + print("installing to %s ...", installdir) -- attempt to install directly try @@ -48,7 +71,7 @@ function main() function () -- install target - install(targetname or ifelse(option.get("all"), "__all", "__def")) + install(targetname or ifelse(option.get("all"), "__all", "__def"), installdir) -- trace cprint("${bright}install ok!${clear}${ok_hand}") @@ -66,7 +89,7 @@ function main() function () -- install target - install(targetname or ifelse(option.get("all"), "__all", "__def")) + install(targetname or ifelse(option.get("all"), "__all", "__def"), installdir) -- trace cprint("${bright}install ok!${clear}${ok_hand}") @@ -99,7 +122,7 @@ function main() if answer == 'y' or answer == '' then -- install target with administrator permission - sudo.runl(path.join(os.scriptdir(), "install_admin.lua"), {targetname or ifelse(option.get("all"), "__all", "__def"), option.get("installdir")}) + sudo.runl(path.join(os.scriptdir(), "install_admin.lua"), {targetname or ifelse(option.get("all"), "__all", "__def"), installdir}) -- trace cprint("${bright}install ok!${clear}${ok_hand}") diff --git a/xmake/actions/install/xmake.lua b/xmake/actions/install/xmake.lua index 2d137498d..e3bd69312 100644 --- a/xmake/actions/install/xmake.lua +++ b/xmake/actions/install/xmake.lua @@ -45,11 +45,19 @@ task("install") -- options , options = { - {'o', "installdir", "kv", nil, "Set the install directory." } - , {'a', "all", "k", nil, "Install all targets." } + {'o', "installdir", "kv", nil, "Set the install directory.", + ".e.g", + " $ xmake install -o /usr/local", + "or $ export DESTDIR=/usr/local; xmake install", + "or $ export INSTALLDIR=/usr/local; xmake install" } + , {'p', "prefix", "kv", nil, "Set the prefix directory.", + ".e.g", + " $ xmake install --prefix=local", + "or $ export PREFIX=local; xmake install" } + , {'a', "all", "k", nil, "Install all targets." } - , {} - , {nil, "target", "v", nil, "Install the given target." } + , { } + , {nil, "target", "v", nil, "Install the given target." } } } diff --git a/xmake/actions/uninstall/main.lua b/xmake/actions/uninstall/main.lua index 0e0707132..d93c5ac06 100644 --- a/xmake/actions/uninstall/main.lua +++ b/xmake/actions/uninstall/main.lua @@ -30,6 +30,26 @@ 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() @@ -39,8 +59,11 @@ function main() -- config it first task.run("config", {target = targetname}) + -- get install directory + local installdir = _installdir() + -- trace - print("uninstalling from %s ...", option.get("installdir") or platform.get("installdir")) + print("uninstalling from %s ...", installdir) -- attempt to uninstall directly try @@ -48,7 +71,7 @@ function main() function () -- uninstall target - uninstall(targetname) + uninstall(targetname, installdir) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") @@ -66,7 +89,7 @@ function main() function () -- uninstall target - uninstall(targetname) + uninstall(targetname, installdir) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") @@ -99,7 +122,7 @@ function main() if answer == 'y' or answer == '' then -- uninstall target with administrator permission - sudo.runl(path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname or "__all", option.get("installdir")}) + sudo.runl(path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname or "__all", installdir}) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") diff --git a/xmake/actions/uninstall/uninstall.lua b/xmake/actions/uninstall/uninstall.lua index a4ef331e8..16cb84db9 100644 --- a/xmake/actions/uninstall/uninstall.lua +++ b/xmake/actions/uninstall/uninstall.lua @@ -25,7 +25,69 @@ -- imports import("core.base.task") import("core.project.project") -import("core.platform.platform") + +-- uninstall binary +function _uninstall_binary(target) + + -- is phony target? + if target:isphony() then + return + end + + -- the binary directory + local binarydir = path.join(_g.installdir, "bin") + + -- remove the target file + os.rm(path.join(binarydir, path.filename(target:targetfile()))) +end + +-- uninstall library +function _uninstall_library(target) + + -- is phony target? + if target:isphony() then + return + end + + -- the library directory + local librarydir = path.join(_g.installdir, "lib") + + -- the include directory + local includedir = path.join(_g.installdir, "include") + + -- remove the target file + os.rm(path.join(librarydir, path.filename(target:targetfile()))) + + -- reove the config.h from the include directory + local _, configheader = target:configheader(includedir) + if configheader then + os.rm(configheader) + end + + -- remove headers from the include directory + local _, dstheaders = target:headerfiles(includedir) + for _, dstheader in ipairs(dstheaders) do + os.rm(dstheader) + end +end + +-- uninstall target +function _on_uninstall(target) + + -- the scripts + local scripts = + { + binary = _uninstall_binary + , static = _uninstall_library + , shared = _uninstall_library + } + + -- call script + local script = scripts[target:get("kind")] + if script then + script(target) + end +end -- uninstall the given target function _uninstall_target(target) @@ -37,7 +99,7 @@ function _uninstall_target(target) local scripts = { target:script("uninstall_before") - , target:script("uninstall", platform.get("uninstall")) + , target:script("uninstall", _on_uninstall) , target:script("uninstall_after") } @@ -74,11 +136,14 @@ function _uninstall_target_and_deps(target) end -- uninstall -function main(targetname) +function main(targetname, installdir) -- 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/core/platform/platform.lua b/xmake/core/platform/platform.lua index 82ac2eab9..9b546fad7 100644 --- a/xmake/core/platform/platform.lua +++ b/xmake/core/platform/platform.lua @@ -138,8 +138,6 @@ function platform._interpreter() -- platform.on_xxx "platform.on_load" , "platform.on_check" - , "platform.on_install" - , "platform.on_uninstall" } , module = { @@ -165,7 +163,7 @@ end function platform.load(plat) -- get platform name - plat = plat or config.get("plat") or xmake._HOST + plat = plat or config.get("plat") or os.host() if not plat then return nil, string.format("unknown platform!") end diff --git a/xmake/platforms/installer.lua b/xmake/platforms/installer.lua deleted file mode 100644 index def185423..000000000 --- a/xmake/platforms/installer.lua +++ /dev/null @@ -1,149 +0,0 @@ ---!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 - 2017, TBOOX Open Source Group. --- --- @author ruki --- @file installer.lua --- - --- imports -import("core.base.option") -import("core.platform.platform") - --- install binary -function install_binary_on_unix(target) - - -- is phony target? - if target:isphony() then - return - end - - -- the install directory - local installdir = option.get("installdir") or platform.get("installdir") - assert(installdir, "unknown install directory!") - - -- the binary directory - local binarydir = path.join(installdir, "bin") - - -- make the binary directory - os.mkdir(binarydir) - - -- copy the target file - os.cp(target:targetfile(), binarydir) -end - --- install library -function install_library_on_unix(target) - - -- is phony target? - if target:isphony() then - return - end - - -- the install directory - local installdir = option.get("installdir") or platform.get("installdir") - assert(installdir, "unknown install directory!") - - -- the library directory - local librarydir = path.join(installdir, "lib") - - -- the include directory - local includedir = path.join(installdir, "include") - - -- make the library directory - os.mkdir(librarydir) - - -- make the include directory - os.mkdir(includedir) - - -- copy the target file - os.cp(target:targetfile(), librarydir) - - -- copy the config.h to the include directory - local configheader, configoutput = target:configheader(includedir) - if configheader and configoutput then - os.cp(configheader, configoutput) - end - - -- copy headers to the include directory - local srcheaders, dstheaders = target:headerfiles(includedir) - if srcheaders and dstheaders then - local i = 1 - for _, srcheader in ipairs(srcheaders) do - local dstheader = dstheaders[i] - if dstheader then - os.cp(srcheader, dstheader) - end - i = i + 1 - end - end -end - --- uninstall binary -function uninstall_binary_on_unix(target) - - -- is phony target? - if target:isphony() then - return - end - - -- the install directory - local installdir = option.get("installdir") or platform.get("installdir") - assert(installdir, "unknown install directory!") - - -- the binary directory - local binarydir = path.join(installdir, "bin") - - -- remove the target file - os.rm(path.join(binarydir, path.filename(target:targetfile()))) -end - --- uninstall library -function uninstall_library_on_unix(target) - - -- is phony target? - if target:isphony() then - return - end - - -- the install directory - local installdir = option.get("installdir") or platform.get("installdir") - assert(installdir, "unknown install directory!") - - -- the library directory - local librarydir = path.join(installdir, "lib") - - -- the include directory - local includedir = path.join(installdir, "include") - - -- remove the target file - os.rm(path.join(librarydir, path.filename(target:targetfile()))) - - -- reove the config.h from the include directory - local _, configheader = target:configheader(includedir) - if configheader then - os.rm(configheader) - end - - -- remove headers from the include directory - local _, dstheaders = target:headerfiles(includedir) - for _, dstheader in ipairs(dstheaders) do - os.rm(dstheader) - end -end diff --git a/xmake/platforms/linux/install.lua b/xmake/platforms/linux/install.lua deleted file mode 100644 index db63878e4..000000000 --- a/xmake/platforms/linux/install.lua +++ /dev/null @@ -1,46 +0,0 @@ ---!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 - 2017, TBOOX Open Source Group. --- --- @author ruki --- @file install.lua --- - --- imports -import("platforms.installer", {rootdir = os.programdir()}) - --- install target -function main(target) - - -- the scripts - local scripts = - { - binary = installer.install_binary_on_unix - , static = installer.install_library_on_unix - , shared = installer.install_library_on_unix - } - - -- call script - local script = scripts[target:get("kind")] - if script then - script(target) - end -end - - diff --git a/xmake/platforms/linux/uninstall.lua b/xmake/platforms/linux/uninstall.lua deleted file mode 100644 index d3760d041..000000000 --- a/xmake/platforms/linux/uninstall.lua +++ /dev/null @@ -1,45 +0,0 @@ ---!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 - 2017, TBOOX Open Source Group. --- --- @author ruki --- @file uninstall.lua --- - --- imports -import("platforms.installer", {rootdir = os.programdir()}) - --- uninstall target -function main(target) - - -- the scripts - local scripts = - { - binary = installer.uninstall_binary_on_unix - , static = installer.uninstall_library_on_unix - , shared = installer.uninstall_library_on_unix - } - - -- call script - local script = scripts[target:get("kind")] - if script then - script(target) - end -end - diff --git a/xmake/platforms/linux/xmake.lua b/xmake/platforms/linux/xmake.lua index 97f82130f..84a3790a4 100644 --- a/xmake/platforms/linux/xmake.lua +++ b/xmake/platforms/linux/xmake.lua @@ -40,12 +40,6 @@ platform("linux") -- set installdir set_installdir("/usr/local") - -- on install - on_install("install") - - -- on uninstall - on_uninstall("uninstall") - -- on check on_check("check") diff --git a/xmake/platforms/macosx/install.lua b/xmake/platforms/macosx/install.lua deleted file mode 100644 index db63878e4..000000000 --- a/xmake/platforms/macosx/install.lua +++ /dev/null @@ -1,46 +0,0 @@ ---!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 - 2017, TBOOX Open Source Group. --- --- @author ruki --- @file install.lua --- - --- imports -import("platforms.installer", {rootdir = os.programdir()}) - --- install target -function main(target) - - -- the scripts - local scripts = - { - binary = installer.install_binary_on_unix - , static = installer.install_library_on_unix - , shared = installer.install_library_on_unix - } - - -- call script - local script = scripts[target:get("kind")] - if script then - script(target) - end -end - - diff --git a/xmake/platforms/macosx/uninstall.lua b/xmake/platforms/macosx/uninstall.lua deleted file mode 100644 index d3760d041..000000000 --- a/xmake/platforms/macosx/uninstall.lua +++ /dev/null @@ -1,45 +0,0 @@ ---!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 - 2017, TBOOX Open Source Group. --- --- @author ruki --- @file uninstall.lua --- - --- imports -import("platforms.installer", {rootdir = os.programdir()}) - --- uninstall target -function main(target) - - -- the scripts - local scripts = - { - binary = installer.uninstall_binary_on_unix - , static = installer.uninstall_library_on_unix - , shared = installer.uninstall_library_on_unix - } - - -- call script - local script = scripts[target:get("kind")] - if script then - script(target) - end -end - diff --git a/xmake/platforms/macosx/xmake.lua b/xmake/platforms/macosx/xmake.lua index 49e7eebd2..277385c26 100644 --- a/xmake/platforms/macosx/xmake.lua +++ b/xmake/platforms/macosx/xmake.lua @@ -43,12 +43,6 @@ platform("macosx") -- on check on_check("check") - -- on install - on_install("install") - - -- on uninstall - on_uninstall("uninstall") - -- on load on_load("load") |
