diff options
| author | ruki <[email protected]> | 2019-01-30 22:33:27 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-01-30 09:20:43 +0800 |
| commit | e1fa828266e4532507af568bd505212dcd15b81d (patch) | |
| tree | 62e26e185e8671cbbe6e1b93b89c3deff0a04903 | |
| parent | e5aa64826addb4fb5c036615d324a2b1149dbd7d (diff) | |
improve install_package and support vcpkg
7 files changed, 124 insertions, 44 deletions
diff --git a/xmake/modules/package/manager/apt/install_package.lua b/xmake/modules/package/manager/apt/install_package.lua index 419d87288..fdc739337 100644 --- a/xmake/modules/package/manager/apt/install_package.lua +++ b/xmake/modules/package/manager/apt/install_package.lua @@ -42,7 +42,7 @@ function main(name, opt) -- find apt local apt = find_tool("apt") if not apt then - return false + raise("apt not found!") end -- init argv @@ -75,9 +75,6 @@ function main(name, opt) sudo.vrunv(apt.program, argv) end else - return false + raise("cannot get administrator permission!") end - - -- ok - return true end diff --git a/xmake/modules/package/manager/brew/install_package.lua b/xmake/modules/package/manager/brew/install_package.lua index 2c41721d5..7c39f5c36 100644 --- a/xmake/modules/package/manager/brew/install_package.lua +++ b/xmake/modules/package/manager/brew/install_package.lua @@ -38,7 +38,12 @@ function main(name, opt) -- find brew local brew = find_tool("brew") if not brew then - return false + raise("brew not found!") + end + + -- check architecture + if opt.arch ~= os.arch() then + raise("cannot install package(%s) for arch(%s)!", name, opt.arch) end -- init argv @@ -49,7 +54,4 @@ function main(name, opt) -- install package os.vrunv(brew.program, argv) - - -- ok - return true end diff --git a/xmake/modules/package/manager/conan/install_package.lua b/xmake/modules/package/manager/conan/install_package.lua index 90a88abb0..607f676a8 100644 --- a/xmake/modules/package/manager/conan/install_package.lua +++ b/xmake/modules/package/manager/conan/install_package.lua @@ -70,7 +70,7 @@ function main(name, opt) -- find conan local conan = find_tool("conan") if not conan then - return false + raise("conan not found!") end -- get build directory @@ -137,7 +137,4 @@ function main(name, opt) -- leave build directory os.cd(oldir) - - -- ok - return true end diff --git a/xmake/modules/package/manager/install_package.lua b/xmake/modules/package/manager/install_package.lua index 845c10feb..b32ea3fac 100644 --- a/xmake/modules/package/manager/install_package.lua +++ b/xmake/modules/package/manager/install_package.lua @@ -29,30 +29,57 @@ import("core.project.config") function _install_package(manager_name, package_name, opt) -- get managers - local managers = {} if manager_name then - table.insert(managers, manager_name) - else - if is_host("windows") then - table.insert(managers, "pacman") -- msys/mingw - elseif is_host("linux") then - table.insert(managers, "apt") - table.insert(managers, "yum") - table.insert(managers, "pacman") - table.insert(managers, "brew") - elseif is_host("macosx") then - table.insert(managers, "brew") - end + dprint("installing %s from %s ..", package_name, manager_name) + return import("package.manager." .. manager_name .. ".install_package", {anonymous = true})(package_name, opt) + end + + -- get suitable package managers + local managers = {} + if is_host("windows") then + table.insert(managers, "pacman") -- msys/mingw + elseif is_host("linux") then + table.insert(managers, "apt") + table.insert(managers, "yum") + table.insert(managers, "pacman") + table.insert(managers, "brew") + elseif is_host("macosx") then + table.insert(managers, "vcpkg") + table.insert(managers, "brew") end assert(#managers > 0, "no suitable package manager!") - -- find package from the given package manager - for _, manager_name in ipairs(managers) do - dprint("installing %s from %s ..", package_name, manager_name) - if import("package.manager." .. manager_name .. ".install_package", {anonymous = true})(package_name, opt) then - return true + -- install package from the given package managers + local errors = nil + for _, manager in ipairs(managers) do + + -- trace + dprint("installing %s from %s ..", package_name, manager) + + -- try to install it + local ok = try + { + function () + import("package.manager." .. manager .. ".install_package", {anonymous = true})(package_name, opt) + return true + end, + catch + { + function (errs) + errors = errs + end + } + } + + -- install ok? + if ok then + dprint("install %s ok from %s", package_name, manager) + return end end + + -- install failed + raise("install %s failed! %s", package_name, errors or "") end -- install package using the package manager @@ -83,7 +110,5 @@ function main(name, opt) opt.version = require_version or opt.version -- do install package - if not _install_package(manager_name, package_name, opt) then - raise("install %s failed!", name) - end + _install_package(manager_name, package_name, opt) end diff --git a/xmake/modules/package/manager/pacman/install_package.lua b/xmake/modules/package/manager/pacman/install_package.lua index cfdabd30c..30dab71e3 100644 --- a/xmake/modules/package/manager/pacman/install_package.lua +++ b/xmake/modules/package/manager/pacman/install_package.lua @@ -42,7 +42,7 @@ function main(name, opt) -- find pacman local pacman = find_tool("pacman") if not pacman then - return false + raise("pacman not found!") end -- init argv @@ -78,9 +78,6 @@ function main(name, opt) sudo.vrunv(pacman.program, argv) end else - return false + raise("cannot get administrator permission!") end - - -- ok - return true end diff --git a/xmake/modules/package/manager/vcpkg/install_package.lua b/xmake/modules/package/manager/vcpkg/install_package.lua new file mode 100644 index 000000000..8716b004f --- /dev/null +++ b/xmake/modules/package/manager/vcpkg/install_package.lua @@ -0,0 +1,65 @@ +--!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 - 2019, TBOOX Open Source Group. +-- +-- @author ruki +-- @file install_package.lua +-- + +-- imports +import("core.base.option") +import("detect.sdks.find_vcpkgdir") + +-- install package +-- +-- @param name the package name, e.g. pcre2, pcre2/libpcre2-8 +-- @param opt the options, .e.g {verbose = true} +-- +-- @return true or false +-- +function main(name, opt) + + -- attempt to find the vcpkg root directory + local vcpkgdir = find_vcpkgdir(opt.vcpkgdir) + if not vcpkgdir then + raise("vcpkg not found!") + end + + -- get arch, plat and mode + local arch = opt.arch + local plat = opt.plat + local mode = opt.mode + if plat == "macosx" then + plat = "osx" + if arch == "x86_64" then + arch = "x64" + end + end + + -- check architecture + if opt.arch ~= os.arch() then + raise("cannot install package(%s) for arch(%s)!", name, opt.arch) + end + + -- init argv + local argv = {"install", string.format("%s:%s-%s", name, arch, plat)} + + -- install package + os.vrunv(path.join(vcpkgdir, "vcpkg"), argv) +end diff --git a/xmake/modules/package/manager/yum/install_package.lua b/xmake/modules/package/manager/yum/install_package.lua index 4986df28a..4ba8000e5 100644 --- a/xmake/modules/package/manager/yum/install_package.lua +++ b/xmake/modules/package/manager/yum/install_package.lua @@ -42,7 +42,7 @@ function main(name, opt) -- find yum local yum = find_tool("yum") if not yum then - return false + raise("yum not found!") end -- init argv @@ -78,9 +78,6 @@ function main(name, opt) sudo.vrunv(yum.program, argv) end else - return false + raise("cannot get administrator permission!") end - - -- ok - return true end |
