diff options
| author | ruki <[email protected]> | 2018-09-29 00:48:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-09-28 22:50:04 +0800 |
| commit | b17ed1ae2522107175a7abc03b11c0126f7503f3 (patch) | |
| tree | 74873875b3a861ed658b28d4c24d7539213dde09 | |
| parent | 619eda909993a3ed64816b53b860a9c559a77fed (diff) | |
improve to find ndk
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_directory.lua | 17 | ||||
| -rw-r--r-- | xmake/modules/detect/sdks/find_ndk.lua | 137 | ||||
| -rw-r--r-- | xmake/modules/detect/sdks/find_ndk_sdkvers.lua | 62 | ||||
| -rw-r--r-- | xmake/platforms/android/check.lua | 78 | ||||
| -rw-r--r-- | xmake/platforms/android/load.lua | 6 | ||||
| -rw-r--r-- | xmake/platforms/android/xmake.lua | 4 |
6 files changed, 135 insertions, 169 deletions
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_directory.lua b/xmake/core/sandbox/modules/import/lib/detect/find_directory.lua index 06b250787..7da1658f0 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_directory.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_directory.lua @@ -71,7 +71,6 @@ function sandbox_lib_detect_find_directory.main(name, pathes, opt) end -- find file - local result = nil for _, _path in ipairs(pathes) do -- format path for builtin variables @@ -86,20 +85,12 @@ function sandbox_lib_detect_find_directory.main(name, pathes, opt) _path = vformat(_path) end - -- directory exists? - for _, dir in ipairs(os.dirs(path.join(_path, name))) do - result = dir - break - end - - -- found? - if result then - break + -- find the first directory + local results = os.dirs(path.join(_path, name), function (file, isdir) return false end) + if results and #results > 0 then + return results[1] end end - - -- ok? - return result end -- return module diff --git a/xmake/modules/detect/sdks/find_ndk.lua b/xmake/modules/detect/sdks/find_ndk.lua index 73f643f4f..a3df87ba3 100644 --- a/xmake/modules/detect/sdks/find_ndk.lua +++ b/xmake/modules/detect/sdks/find_ndk.lua @@ -23,13 +23,100 @@ -- -- imports +import("lib.detect.cache") +import("core.base.option") +import("core.base.global") import("core.project.config") +import("lib.detect.find_directory") + +-- find ndk directory +function _find_ndkdir(sdkdir) + + -- get ndk directory + if not sdkdir then + sdkdir = os.getenv("ANDROID_NDK_ROOT") + if not sdkdir and is_host("macosx") then + sdkdir = "~/Library/Android/sdk/ndk-bundle" + end + end + + -- get ndk directory + if sdkdir and os.isdir(sdkdir) then + return sdkdir + end +end + +-- find the sdk version of ndk +function _find_ndk_sdkver(sdkdir) + + -- find the max version + local sdkver_max = 0 + for _, sdkdir in ipairs(os.dirs(path.join(sdkdir, "platforms", "android-*"))) do + + -- get version + local filename = path.filename(sdkdir) + local version, count = filename:gsub("android%-", "") + if count > 0 then + + -- get the max version + local sdkver = tonumber(version) + if sdkver > sdkver_max then + sdkver_max = sdkver + end + end + end + + -- get the max sdk version + return sdkver_max > 0 and sdkver_max or nil +end + +-- find the toolchains version of ndk +function _find_ndk_toolchains_ver(bindir) + return bindir:match("%-(%d*%.%d*)[/\\]") +end + +-- find the ndk toolchain +function _find_ndk(sdkdir, arch, ndk_sdkver, ndk_toolchains_ver) + + -- find ndk root directory + sdkdir = _find_ndkdir(sdkdir) + if not sdkdir then + return {} + end + + -- is arm64? + local arm64 = arch and arch:startswith("arm64") + + -- the cross + local cross = arm64 and "aarch64-linux-android-" or "arm-linux-androideabi-" + + -- find the binary directory + local bindir = find_directory("bin", path.join(sdkdir, "toolchains", cross .. "*", "prebuilt", "*")) + if not bindir then + return {} + end + + -- find the sdk version + local sdkver = ndk_sdkver or _find_ndk_sdkver(sdkdir) + if not sdkver then + return {} + end + + -- find the toolchains version + local toolchains_ver = ndk_toolchains_ver or _find_ndk_toolchains_ver(bindir) + if not toolchains_ver then + return {} + end + + -- ok? + return {sdkdir = sdkdir, bindir = bindir, cross = cross, sdkver = sdkver, toolchains_ver = toolchains_ver} +end -- find ndk toolchains -- --- @param ndkdir the ndk directory +-- @param sdkdir the ndk directory -- @param opt the argument options --- .e.g {arch = "[armv5te|armv6|armv7-a|armv8-a|arm64-v8a]"} +-- .e.g {arch = "[armv5te|armv6|armv7-a|armv8-a|arm64-v8a]", verbose = true, force = false, sdkver = 19, toolchains_ver = "4.9"} -- -- @return the ndk toolchains array. .e.g {{bin = .., cross = ..}, .. } -- @@ -40,34 +127,48 @@ import("core.project.config") -- -- @endcode -- -function main(ndkdir, opt) +function main(sdkdir, opt) -- init arguments opt = opt or {} - -- get ndk directory - if not ndkdir or not os.isdir(ndkdir) then - return {} + -- attempt to load cache first + local key = "detect.sdks.find_ndk." .. (sdkdir or "") + local cacheinfo = cache.load(key) + if not opt.force and cacheinfo.ndk then + return cacheinfo.ndk end -- get arch local arch = opt.arch or config.get("arch") or "armv7-a" + + -- find ndk + local ndk = _find_ndk(sdkdir or config.get("ndk") or global.get("ndk"), arch, opt.sdkver or config.get("ndk_sdkver"), opt.toolchains_ver or config.get("ndk_toolchains_ver")) + if ndk then - -- is arm64? - local arm64 = arch and arch:startswith("arm64") + -- save to config + config.set("ndk", ndk.sdkdir, {force = true, readonly = true}) + config.set("ndk_sdkver", ndk.sdkver, {force = true, readonly = true}) + config.set("ndk_toolchains_ver", ndk.toolchains_ver, {force = true, readonly = true}) - -- the cross - local cross = ifelse(arm64, "aarch64-linux-android-", "arm-linux-androideabi-") + -- trace + if opt.verbose or option.get("verbose") then + cprint("checking for the NDK directory ... ${green}%s", ndk.sdkdir) + cprint("checking for the SDK version of NDK ... ${green}%s", ndk.sdkver) + cprint("checking for the toolchains version of NDK ... ${green}%s", ndk.toolchains_ver) + end + else - -- save the toolchains directory - local toolchains = {} - for _, bindir in ipairs(os.dirs(path.join(ndkdir, "toolchains", cross .. "*", "prebuilt/*/bin"))) do - local binfiles = os.files(path.join(bindir, cross .. "*")) - if binfiles and #binfiles > 0 then - table.insert(toolchains, {bin = bindir, cross = cross}) + -- trace + if opt.verbose or option.get("verbose") then + cprint("checking for the NDK directory ... ${red}no") end end - -- ok? - return toolchains + -- save to cache + cacheinfo.ndk = ndk or false + cache.save(key, cacheinfo) + + -- ok? + return ndk end diff --git a/xmake/modules/detect/sdks/find_ndk_sdkvers.lua b/xmake/modules/detect/sdks/find_ndk_sdkvers.lua deleted file mode 100644 index eb959c933..000000000 --- a/xmake/modules/detect/sdks/find_ndk_sdkvers.lua +++ /dev/null @@ -1,62 +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 - 2018, TBOOX Open Source Group. --- --- @author ruki --- @file find_ndk_sdkvers.lua --- - --- find ndk sdk versions --- --- @param ndk_dir the ndk directory --- --- @return the ndk sdk version array --- --- @code --- --- local ndk_sdkvers = find_ndk_sdkvers("/xxx/android-ndk-r10e") --- --- @endcode --- -function main(ndk_dir) - - -- get ndk directory - if not ndk_dir or not os.isdir(ndk_dir) then - return {} - end - - -- find all sdk directories - local ndk_sdkvers = {} - for _, sdkdir in ipairs(os.dirs(path.join(ndk_dir, "platforms/android-*"))) do - - -- get version - local filename = path.filename(sdkdir) - local version, count = filename:gsub("android%-", "") - if count > 0 then - - -- get the max version - if tonumber(version) > 0 then - table.insert(ndk_sdkvers, version) - end - end - end - - -- ok? - return ndk_sdkvers -end diff --git a/xmake/platforms/android/check.lua b/xmake/platforms/android/check.lua index 64f487693..4af8a6143 100644 --- a/xmake/platforms/android/check.lua +++ b/xmake/platforms/android/check.lua @@ -24,77 +24,14 @@ -- imports import(".checker") -import("detect.sdks.find_ndk_sdkvers") import("detect.sdks.find_ndk") --- check the sdk version for ndk -function _check_ndk_sdkver(config) - - -- get ndk sdk version - local ndk_sdkver = config.get("ndk_sdkver") - if not ndk_sdkver then - - -- find the max version - local sdkver_max = 0 - for _, sdkver in ipairs(find_ndk_sdkvers(config.get("ndk"))) do - - -- get the max version - sdkver = tonumber(sdkver) - if sdkver > sdkver_max then - sdkver_max = sdkver - end - end - - -- save the version - if sdkver_max > 0 then ndk_sdkver = sdkver_max end - - -- probe ok? update it - if ndk_sdkver ~= nil and ndk_sdkver > 0 then - - -- save it - config.set("ndk_sdkver", ndk_sdkver) - - -- trace - cprint("checking for the SDK version of NDK ... ${green}android-%d", ndk_sdkver) - else - - -- trace - cprint("checking for the SDK version of NDK ... ${red}no") - end - end -end - --- check toolchains -function _check_toolchains(config) - - -- get toolchain bin directory - local bindir = config.get("bin") - if not bindir then - - -- find first toolchains - for _, toolchains in ipairs(find_ndk(config.get("ndk"), config.get("arch"))) do - config.set("bin", toolchains.bin) - config.set("cross", toolchains.cross) - break - end - end - bindir = config.get("bin") - - -- get toolchains version - local toolchains_ver = config.get("toolchains_ver") - if not toolchains_ver and bindir then - local toolchains_ver = bindir:match("%-(%d*%.%d*)[/\\]") - if toolchains_ver then - - -- save the toolchains version - config.set("toolchains_ver", toolchains_ver) - - -- trace - cprint("checking for the version of toolchains ... ${green}%s", toolchains_ver) - else - -- trace - cprint("checking for the version of toolchains ... ${red}no") - end +-- check the ndk toolchain +function _check_ndk(config) + local ndk = find_ndk(config.get("ndk"), {verbose = true}) + if ndk then + config.set("bin", ndk.bindir) + config.set("cross", ndk.cross) end end @@ -151,8 +88,7 @@ function main(kind, toolkind) _g.config = { { checker.check_arch, "armv7-a" } - , _check_ndk_sdkver - , _check_toolchains + , _check_ndk , { checker.toolchain_check, "sh", _toolchains } , { checker.toolchain_check, "ld", _toolchains } } diff --git a/xmake/platforms/android/load.lua b/xmake/platforms/android/load.lua index ffb9bef27..230233ea5 100644 --- a/xmake/platforms/android/load.lua +++ b/xmake/platforms/android/load.lua @@ -111,11 +111,11 @@ function main() insert(_g.ldflags, "-pie") -- only for c++ stl - local toolchains_ver = config.get("toolchains_ver") - if toolchains_ver then + local ndk_toolchains_ver = config.get("ndk_toolchains_ver") + if ndk_toolchains_ver then -- get c++ stl sdk directory - local cxxstl_sdkdir = path.translate(format("%s/sources/cxx-stl/gnu-libstdc++/%s", ndk, toolchains_ver)) + local cxxstl_sdkdir = path.translate(format("%s/sources/cxx-stl/gnu-libstdc++/%s", ndk, ndk_toolchains_ver)) -- the toolchains archs local toolchains_archs = diff --git a/xmake/platforms/android/xmake.lua b/xmake/platforms/android/xmake.lua index 0e8edb8c7..46b67f26b 100644 --- a/xmake/platforms/android/xmake.lua +++ b/xmake/platforms/android/xmake.lua @@ -55,8 +55,8 @@ platform("android") , global = { {} - , {nil, "ndk", "kv", nil, "The NDK Directory" } - , {nil, "ndk_sdkver", "kv", "auto", "The SDK Version for NDK" } + , {nil, "ndk", "kv", nil, "The NDK Directory" } + , {nil, "ndk_sdkver", "kv", "auto", "The SDK Version for NDK" } } } |
