diff options
| author | ruki <[email protected]> | 2022-09-23 12:26:58 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-09-23 12:26:58 +0800 |
| commit | e7dccdf5eb397d31031d242bc5d39990bdc370de (patch) | |
| tree | cb68fceb2011c864529c910422dba7b03b02bead | |
| parent | 766f61852a6665a6c180baf37518f7ea988e48e3 (diff) | |
| parent | 31e4124be2ae66eb10ac43f0b673ee1728104a67 (diff) | |
Merge pull request #2858 from xmake-io/trybuild
Improve trybuild to build 3rd source library using xmake-repo scripts
| -rw-r--r-- | xmake/actions/build/main.lua | 29 | ||||
| -rw-r--r-- | xmake/actions/config/xmake.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/package/manager/xmake/search_package.lua | 16 | ||||
| -rw-r--r-- | xmake/modules/package/tools/autoconf.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/package/tools/cmake.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/package/tools/ninja.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/search_packages.lua | 10 | ||||
| -rw-r--r-- | xmake/modules/private/action/trybuild/xrepo.lua | 197 |
8 files changed, 238 insertions, 26 deletions
diff --git a/xmake/actions/build/main.lua b/xmake/actions/build/main.lua index 9e05f6948..8c5ede701 100644 --- a/xmake/actions/build/main.lua +++ b/xmake/actions/build/main.lua @@ -35,6 +35,18 @@ import("statistics") import("private.cache.build_cache") import("private.service.remote_build.action", {alias = "remote_build_action"}) +-- try building it +function _do_try_build(configfile, tool, trybuild, trybuild_detected, targetname) + if configfile and tool and (trybuild or utils.confirm({default = true, + description = "${bright}" .. path.filename(configfile) .. "${clear} found, try building it or you can run `${bright}xmake f --trybuild=${clear}` to set buildsystem"})) then + if not trybuild then + task.run("config", {target = targetname, trybuild = trybuild_detected}) + end + tool.build() + return true + end +end + -- do build for the third-party buildsystem function _try_build() @@ -59,26 +71,19 @@ function _try_build() else raise("unknown build tool: %s", trybuild) end + return _do_try_build(configfile, tool, trybuild, trybuild_detected, targetname) else - for _, name in ipairs({"autoconf", "cmake", "meson", "scons", "bazel", "msbuild", "xcodebuild", "make", "ninja", "ndkbuild"}) do + for _, name in ipairs({"xrepo", "autoconf", "cmake", "meson", "scons", "bazel", "msbuild", "xcodebuild", "make", "ninja", "ndkbuild"}) do tool = import("private.action.trybuild." .. name, {anonymous = true}) configfile = tool.detect() if configfile then trybuild_detected = name - break + if _do_try_build(configfile, tool, trybuild, trybuild_detected, targetname) then + return true + end end end end - - -- try building it - if configfile and tool and (trybuild or utils.confirm({default = true, - description = "${bright}" .. path.filename(configfile) .. "${clear} found, try building it or you can run `${bright}xmake f --trybuild=${clear}` to set buildsystem"})) then - if not trybuild then - task.run("config", {target = targetname, trybuild = trybuild_detected}) - end - tool.build() - return true - end end -- do global project rules diff --git a/xmake/actions/config/xmake.lua b/xmake/actions/config/xmake.lua index 3835ca20f..b54066835 100644 --- a/xmake/actions/config/xmake.lua +++ b/xmake/actions/config/xmake.lua @@ -220,7 +220,7 @@ task("config") " - xmake f --trybuild=autoconf -p android --ndk=xxx; xmake", "", "the third-party buildsystems:" - , values = {"auto", "make", "autoconf", "cmake", "scons", "meson", "bazel", "ninja", "msbuild", "xcodebuild", "ndkbuild"}}, + , values = {"auto", "make", "autoconf", "cmake", "scons", "meson", "bazel", "ninja", "msbuild", "xcodebuild", "ndkbuild", "xrepo"}}, {nil, "tryconfigs", "kv", nil , "Set the extra configurations of the third-party buildsystem for the try-build mode.", "e.g.", " - xmake f --trybuild=autoconf --tryconfigs='--enable-shared=no'"}, diff --git a/xmake/modules/package/manager/xmake/search_package.lua b/xmake/modules/package/manager/xmake/search_package.lua index f4a235246..cfd384ac6 100644 --- a/xmake/modules/package/manager/xmake/search_package.lua +++ b/xmake/modules/package/manager/xmake/search_package.lua @@ -26,19 +26,31 @@ import("private.action.require.impl.repository") -- search package using the xmake package manager -- -- @param name the package name with pattern +-- @param opt the options, e.g. {require_version = "1.x"} -- -function main(name) +function main(name, opt) + opt = opt or {} local results = {} for _, packageinfo in ipairs(repository.searchdirs(name)) do local package = core_package.load_from_repository(packageinfo.name, packageinfo.repo, packageinfo.packagedir) if package then local repo = package:repo() + local version local versions = package:versions() if versions then versions = table.copy(versions) table.sort(versions, function (a, b) return semver.compare(a, b) > 0 end) + if opt.require_version then + for _, ver in ipairs(versions) do + if semver.satisfies(ver, opt.require_version) then + version = ver + end + end + else + version = versions[1] + end end - table.insert(results, {name = package:name(), version = versions and versions[1], description = package:get("description"), reponame = repo and repo:name()}) + table.insert(results, {name = package:name(), version = version, description = package:get("description"), reponame = repo and repo:name()}) end end return results diff --git a/xmake/modules/package/tools/autoconf.lua b/xmake/modules/package/tools/autoconf.lua index b8429e9f5..c9fa019bf 100644 --- a/xmake/modules/package/tools/autoconf.lua +++ b/xmake/modules/package/tools/autoconf.lua @@ -444,7 +444,7 @@ function build(package, configs, opt) opt = opt or {} local njob = opt.jobs or option.get("jobs") or tostring(os.default_njob()) local argv = {"-j" .. njob} - if option.get("verbose") then + if option.get("diagnosis") then table.insert(argv, "V=1") end if opt.makeconfigs then @@ -471,7 +471,7 @@ function install(package, configs, opt) -- do install local argv = {"install"} - if option.get("verbose") then + if option.get("diagnosis") then table.insert(argv, "V=1") end if opt.makeconfigs then diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index a9223ce85..30f809671 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -649,7 +649,7 @@ function _build_for_make(package, configs, opt) end local jobs = _get_parallel_njobs(opt) table.insert(argv, "-j" .. jobs) - if option.get("verbose") then + if option.get("diagnosis") then table.insert(argv, "VERBOSE=1") end if is_host("bsd") then @@ -727,7 +727,7 @@ end function _install_for_make(package, configs, opt) local jobs = _get_parallel_njobs(opt) local argv = {"-j" .. jobs} - if option.get("verbose") then + if option.get("diagnosis") then table.insert(argv, "VERBOSE=1") end if is_host("bsd") then diff --git a/xmake/modules/package/tools/ninja.lua b/xmake/modules/package/tools/ninja.lua index d61e144b2..42dc73ed6 100644 --- a/xmake/modules/package/tools/ninja.lua +++ b/xmake/modules/package/tools/ninja.lua @@ -34,7 +34,7 @@ function build(package, configs, opt) end table.insert(argv, "-C") table.insert(argv, buildir) - if option.get("verbose") then + if option.get("diagnosis") then table.insert(argv, "-v") end table.insert(argv, "-j") diff --git a/xmake/modules/private/action/require/impl/search_packages.lua b/xmake/modules/private/action/require/impl/search_packages.lua index 276747db4..525c8d8bf 100644 --- a/xmake/modules/private/action/require/impl/search_packages.lua +++ b/xmake/modules/private/action/require/impl/search_packages.lua @@ -19,7 +19,7 @@ -- -- search packages from repositories -function _search_packages(name) +function _search_packages(name, opt) -- get package manager name local manager_name, package_name = table.unpack(name:split("::", {plain = true, strict = true})) @@ -32,7 +32,7 @@ function _search_packages(name) -- search packages local packages = {} - local result = import("package.manager." .. manager_name .. ".search_package", {anonymous = true})(package_name) + local result = import("package.manager." .. manager_name .. ".search_package", {anonymous = true})(package_name, opt) if result then table.join2(packages, result) end @@ -40,12 +40,10 @@ function _search_packages(name) end -- search packages -function main(names) - - -- search all names +function main(names, opt) local results = {} for _, name in ipairs(names) do - local packages = _search_packages(name) + local packages = _search_packages(name, opt) if packages then results[name] = packages end diff --git a/xmake/modules/private/action/trybuild/xrepo.lua b/xmake/modules/private/action/trybuild/xrepo.lua new file mode 100644 index 000000000..073ecd57f --- /dev/null +++ b/xmake/modules/private/action/trybuild/xrepo.lua @@ -0,0 +1,197 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed 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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xrepo.lua +-- + +-- imports +import("core.base.option") +import("core.base.semver") +import("core.project.config") +import("lib.detect.find_file") +import("lib.detect.find_tool") +import("private.action.require.impl.search_packages") + +-- get build directory +function _get_buildir() + return config.buildir() or "build" +end + +-- get artifacts directory +function _get_artifacts_dir() + return path.absolute(path.join(_get_buildir(), "artifacts")) +end + +-- detect build-system and configuration file +function detect() + + -- we need xrepo + local xrepo = find_tool("xrepo") + if not xrepo then + return + end + + -- get package name and version + local dirname = path.filename(os.curdir()) + local packagename = dirname + local version = semver.match(dirname) + if version then + local pos = dirname:find(version:rawstr(), 1, true) + if pos then + packagename = dirname:sub(1, pos - 1) + if packagename:endswith("-") or packagename:endswith("_") then + packagename = packagename:sub(1, #packagename - 1) + end + end + end + packagename = packagename:trim() + if #packagename == 0 then + return + end + + -- search packages + local result + for name, packages in pairs(search_packages(packagename, {require_version = version and version:rawstr() or nil})) do + if #packages > 0 then + local package = packages[1] + _g.package = package + if package.version then + return ("%s %s in %s"):format(package.name, package.version, package.reponame) + else + return ("%s in %s"):format(package.name, package.reponame) + end + end + end +end + +-- get common configs +function _get_common_configs(argv) + table.insert(argv, "--shallow") + table.insert(argv, "-v") + if option.get("diagnosis") then + table.insert(argv, "-D") + end + if config.get("plat") then + table.insert(argv, "-p") + table.insert(argv, config.get("plat")) + end + if config.get("arch") then + table.insert(argv, "-a") + table.insert(argv, config.get("arch")) + end + if config.get("mode") then + table.insert(argv, "-m") + table.insert(argv, config.get("mode")) + end + if config.get("kind") then + table.insert(argv, "-k") + table.insert(argv, config.get("kind")) + end + if config.get("toolchain") then + table.insert(argv, "--toolchain=" .. config.get("toolchain")) + end + if config.get("vs_runtime") then + table.insert(argv, "-f") + table.insert(argv, "vs_runtime='" .. config.get("vs_runtime") .. "'") + end +end + +-- get install configs +function _get_install_configs(argv) + + -- cross compilation + if config.get("sdk") then + table.insert(argv, "--sdk=" .. config.get("sdk")) + end + + -- android + if config.get("ndk") then + table.insert(argv, "--ndk=" .. config.get("ndk")) + end + + -- mingw + if config.get("mingw") then + table.insert(argv, "--mingw=" .. config.get("mingw")) + end + + -- msvc + if config.get("vs") then + table.insert(argv, "--vs=" .. config.get("vs")) + end + if config.get("vs_toolset") then + table.insert(argv, "--vs_toolset=" .. config.get("vs_toolset")) + end + if config.get("vs_sdkver") then + table.insert(argv, "--vs_sdkver=" .. config.get("vs_sdkver")) + end + + -- xcode + if config.get("xcode") then + table.insert(argv, "--xcode=" .. config.get("xcode")) + end + if config.get("xcode_sdkver") then + table.insert(argv, "--xcode_sdkver=" .. config.get("xcode_sdkver")) + end + if config.get("target_minver") then + table.insert(argv, "--target_minver=" .. config.get("target_minver")) + end + if config.get("appledev") then + table.insert(argv, "--appledev=" .. config.get("appledev")) + end +end + +-- do clean +function clean() +end + +-- do build +function build() + + -- get xrepo + local xrepo = assert(find_tool("xrepo"), "xrepo not found!") + + -- get package info + local package = assert(_g.package, "package not found!") + + -- do install for building + local argv = {"install"} + _get_common_configs(argv) + _get_install_configs(argv) + table.insert(argv, "-d") + table.insert(argv, ".") + if package.version then + table.insert(argv, package.name .. " " .. package.version) + else + table.insert(argv, package.name) + end + os.vexecv(xrepo.program, argv) + + -- do export + local artifacts_dir = _get_artifacts_dir() + local argv = {"export"} + _get_common_configs(argv) + table.insert(argv, "-o") + table.insert(argv, artifacts_dir) + if package.version then + table.insert(argv, package.name .. " " .. package.version) + else + table.insert(argv, package.name) + end + os.vexecv(xrepo.program, argv) + cprint("output to ${bright}%s", artifacts_dir) + cprint("${color.success}build ok!") +end |
