diff options
| author | ruki <[email protected]> | 2016-06-28 16:41:13 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-06-28 16:41:13 +0800 |
| commit | 53b5a11577bc390a142e490595fab2dcae17930c (patch) | |
| tree | eebd50649bbae01ebb24a1c97b90b36f82075f1b | |
| parent | a74f1547843d0cbd5d6f58026496be6942a1a023 (diff) | |
rewrite install and uninstall
| -rwxr-xr-x | xmake/actions/install/main.lua | 17 | ||||
| -rwxr-xr-x | xmake/actions/uninstall/main.lua | 14 | ||||
| -rw-r--r-- | xmake/core/platform/environment.lua | 15 | ||||
| -rw-r--r-- | xmake/core/platform/installer.lua | 86 | ||||
| -rw-r--r-- | xmake/core/platform/platform.lua | 155 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/platform/installer.lua | 52 | ||||
| -rw-r--r-- | xmake/platforms/macosx/installer.lua | 160 | ||||
| -rwxr-xr-x | xmake/platforms/macosx/xmake.lua | 3 |
8 files changed, 386 insertions, 116 deletions
diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua index 34e4f15eb..034b6a889 100755 --- a/xmake/actions/install/main.lua +++ b/xmake/actions/install/main.lua @@ -27,16 +27,7 @@ import("core.project.config") import("core.project.global") import("core.project.project") import("core.platform.platform") - --- install package -function _install_package(target) - - -- make the package directory first if not exists - os.mkdir("$(packagedir)") - - -- copy the package to the output directory - os.cp(format("$(buildir)/%s.pkg", target:name()), "$(packagedir)") -end +import("core.platform.installer") -- install target function _install_target(target) @@ -47,9 +38,9 @@ function _install_target(target) -- get script local scripts = { - binary = _install_package - , static = _install_package - , shared = _install_package + binary = installer.install + , static = installer.install + , shared = installer.install } -- check diff --git a/xmake/actions/uninstall/main.lua b/xmake/actions/uninstall/main.lua index e1c3192d5..6b5aa6a23 100755 --- a/xmake/actions/uninstall/main.lua +++ b/xmake/actions/uninstall/main.lua @@ -27,13 +27,7 @@ import("core.project.config") import("core.project.global") import("core.project.project") import("core.platform.platform") - --- uninstall package -function _uninstall_package(target) - - -- remove the package - os.rm(format("$(packagedir)/%s.pkg", target:name())) -end +import("core.platform.installer") -- uninstall target function _uninstall_target(target) @@ -44,9 +38,9 @@ function _uninstall_target(target) -- get script local scripts = { - binary = _uninstall_package - , static = _uninstall_package - , shared = _uninstall_package + binary = installer.uninstall + , static = installer.uninstall + , shared = installer.uninstall } -- check diff --git a/xmake/core/platform/environment.lua b/xmake/core/platform/environment.lua index 8c8569674..3897d00e8 100644 --- a/xmake/core/platform/environment.lua +++ b/xmake/core/platform/environment.lua @@ -25,6 +25,7 @@ local environment = environment or {} -- load modules local platform = require("platform/platform") +local sandbox = require("sandbox/sandbox") -- load the given environment from the given platform function environment.load(plat) @@ -43,14 +44,17 @@ end function environment.enter(name) -- load the environment module - local module, errors = environment.load(plat) + local module, errors = environment.load() if not module and errors then return false, errors end -- enter it if module then - module.enter(name) + local ok, errors = sandbox.load(module.enter, name) + if not ok then + return false, errors + end end -- ok @@ -61,14 +65,17 @@ end function environment.leave(name) -- load the environment module - local module, errors = environment.load(plat) + local module, errors = environment.load() if not module and errors then return false, errors end -- leave it if module then - module.leave(name) + local ok, errors = sandbox.load(module.leave, name) + if not ok then + return false, errors + end end -- ok diff --git a/xmake/core/platform/installer.lua b/xmake/core/platform/installer.lua new file mode 100644 index 000000000..2f68d4fbb --- /dev/null +++ b/xmake/core/platform/installer.lua @@ -0,0 +1,86 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> +-- +-- Copyright (C) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file installer.lua +-- + +-- define module +local installer = installer or {} + +-- load modules +local platform = require("platform/platform") +local sandbox = require("sandbox/sandbox") + +-- load the given installer from the given platform +function installer.load(plat) + + -- load platform + local instance, errors = platform.load(plat) + if not instance then + return nil, errors + end + + -- get installer + return instance:installer() +end + +-- install the target for the current platform +function installer.install(target, plat) + + -- load the installer module + local module, errors = installer.load() + if not module and errors then + return false, errors + end + + -- install it + if module then + local ok, errors = sandbox.load(module.install, target) + if not ok then + return false, errors + end + end + + -- ok + return true +end + +-- uninstall target for the current platform +function installer.uninstall(target) + + -- load the installer module + local module, errors = installer.load() + if not module and errors then + return false, errors + end + + -- uninstall it + if module then + local ok, errors = sandbox.load(module.uninstall, target) + if not ok then + return false, errors + end + end + + -- ok + return true +end + +-- return module +return installer diff --git a/xmake/core/platform/platform.lua b/xmake/core/platform/platform.lua index 57c0299ae..3c6d35e6e 100644 --- a/xmake/core/platform/platform.lua +++ b/xmake/core/platform/platform.lua @@ -34,6 +34,60 @@ local sandbox = require("sandbox/sandbox") local config = require("project/config") local global = require("project/global") +-- load the platform module +function _instance:_load(modulename) + + -- return it directly if cached + local cachename = "_" .. modulename:upper() + if self[cachename] then + return self[cachename] + end + + -- no this module? + if not self._INFO[modulename] then + return nil + end + + -- get the script path + local scriptpath = path.join(self._ROOTDIR, self._INFO[modulename] .. ".lua") + + -- not exists? + if not scriptpath or not os.isfile(scriptpath) then + return nil, string.format("the %s of %s not found!", modulename, self._NAME) + end + + -- load script + local script, errors = loadfile(scriptpath) + if script then + + -- make sandbox instance with the given script + local instance, errors = sandbox.new(script, nil, self._ROOTDIR) + if not instance then + return nil, errors + end + + -- import the module + local module, errors = instance:import() + if not module then + return nil, errors + end + + -- init the module + if module.init then + module.init() + end + + -- save it to the cache + self[cachename] = module + + -- ok? + return module + end + + -- failed + return nil, errors +end + -- new an instance function _instance.new(name, info, rootdir) @@ -87,102 +141,22 @@ end -- get the checker function _instance:checker() - -- return it directly if cached - if self._CHECKER then - return self._CHECKER - end - - -- no checker - if not self._INFO.checker then - return nil - end - - -- get the script path - local scriptpath = path.join(self._ROOTDIR, self._INFO.checker .. ".lua") - - -- not exists? - if not scriptpath or not os.isfile(scriptpath) then - return nil, string.format("the checker of %s not found!", self._NAME) - end - - -- load script - local script, errors = loadfile(scriptpath) - if script then - - -- make sandbox instance with the given script - local instance, errors = sandbox.new(script, nil, self._ROOTDIR) - if not instance then - return nil, errors - end - - -- import the module - local module, errors = instance:import() - if not module then - return nil, errors - end - - -- init the module - if module.init then - module.init() - end - - -- save it to the cache - self._CHECKER = module + -- load checker + return self:_load("checker") +end - -- ok? - return module - end +-- get the installer +function _instance:installer() - -- failed - return nil, errors + -- load installer + return self:_load("installer") end -- get the environment function _instance:environment() - -- return it directly if cached - if self._ENVIRONMENT then - return self._ENVIRONMENT - end - - -- no environment - if not self._INFO.environment then - return nil - end - - -- get the script path - local scriptpath = path.join(self._ROOTDIR, self._INFO.environment .. ".lua") - - -- not exists? - if not scriptpath or not os.isfile(scriptpath) then - return nil, string.format("the environment of %s not found!", self._NAME) - end - - -- load script - local script, errors = loadfile(scriptpath) - if script then - - -- make sandbox instance with the given script - local instance, errors = sandbox.new(script, nil, self._ROOTDIR) - if not instance then - return nil, errors - end - - -- import the module - local module, errors = instance:import() - if not module then - return nil, errors - end - - -- save it to the cache - self._ENVIRONMENT = module - - -- ok? - return module - end - - -- failed - return nil, errors + -- load environment + return self:_load("environment") end -- get the platform configure @@ -256,6 +230,9 @@ function platform._interpreter() -- register api: set_environment() interp:api_register_set_values("platform", "environment") + -- register api: set_installer() + interp:api_register_set_values("platform", "installer") + -- register api: on_load() interp:api_register_on_script("platform", "load") diff --git a/xmake/core/sandbox/modules/import/core/platform/installer.lua b/xmake/core/sandbox/modules/import/core/platform/installer.lua new file mode 100644 index 000000000..3fd91f4fc --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/platform/installer.lua @@ -0,0 +1,52 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> +-- +-- Copyright (C) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file installer.lua +-- + +-- define module +local sandbox_core_platform_installer = sandbox_core_platform_installer or {} + +-- load modules +local platform = require("platform/platform") +local installer = require("platform/installer") +local raise = require("sandbox/modules/raise") + +-- install target +function sandbox_core_platform_installer.install(target) + + -- enter it + local ok, errors = installer.install(target) + if not ok then + raise(errors) + end +end + +-- uninstall target +function sandbox_core_platform_installer.uninstall(target) + + -- enter it + local ok, errors = installer.uninstall(target) + if not ok then + raise(errors) + end +end + +-- return module +return sandbox_core_platform_installer diff --git a/xmake/platforms/macosx/installer.lua b/xmake/platforms/macosx/installer.lua new file mode 100644 index 000000000..426adc832 --- /dev/null +++ b/xmake/platforms/macosx/installer.lua @@ -0,0 +1,160 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> +-- +-- Copyright (C) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file install.lua +-- + +-- imports +import("core.base.option") +--import("platforms.checker", {rootdir = os.programdir()}) + +-- install binary +function _install_binary(target) + + -- the output directory + local outputdir = option.get("outputdir") or "/usr/local" + + -- the binary directory + local binarydir = path.join(outputdir, "bin") + + -- make the binary directory + os.mkdir(binarydir) + + -- copy the target file + os.cp(target:targetfile(), binarydir) +end + +-- install library +function _install_library(target) + + -- the output directory + local outputdir = option.get("outputdir") or "/usr/local" + + -- the library directory + local librarydir = path.join(outputdir, "lib") + + -- the include directory + local includedir = path.join(outputdir, "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 config_h = target:get("config_h") + if config_h then + os.cp(config_h, includedir) + 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(target) + + -- the output directory + local outputdir = option.get("outputdir") or "/usr/local" + + -- the binary directory + local binarydir = path.join(outputdir, "bin") + + -- remove the target file + os.rm(path.join(binarydir, path.filename(target:targetfile()))) +end + +-- uninstall library +function _uninstall_library(target) + + -- the output directory + local outputdir = option.get("outputdir") or "/usr/local" + + -- the library directory + local librarydir = path.join(outputdir, "lib") + + -- the include directory + local includedir = path.join(outputdir, "include") + + -- remove the target file + os.rm(path.join(librarydir, path.filename(target:targetfile()))) + + -- reove the config.h from the include directory + local config_h = target:get("config_h") + if config_h then + os.rm(path.join(includedir, path.filename(config_h))) + end + + -- remove headers from the include directory + local _, dstheaders = target:headerfiles(includedir) + for _, dstheader in ipairs(dstheaders) do + os.rm(dstheader) + end +end + +-- install target +function 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 + +-- uninstall target +function 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 + diff --git a/xmake/platforms/macosx/xmake.lua b/xmake/platforms/macosx/xmake.lua index dbaf98174..22e79387e 100755 --- a/xmake/platforms/macosx/xmake.lua +++ b/xmake/platforms/macosx/xmake.lua @@ -35,6 +35,9 @@ platform("macosx") -- set checker set_checker("checker") + -- set installer + set_installer("installer") + -- set tooldirs set_tooldirs("/usr/bin", "/usr/local/bin", "/opt/bin", "/opt/local/bin") |
