summaryrefslogtreecommitdiff
path: root/xmake/modules/detect/sdks/find_android_sdk.lua
blob: c493ae6c7034c18e020b5d9dc7009886caee6e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
--!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, Xmake Open Source Community.
--
-- @author      ruki
-- @file        find_android_sdk.lua
--

-- imports
import("core.base.semver")
import("core.base.option")
import("core.base.global")
import("core.project.config")
import("core.cache.detectcache")
import("lib.detect.find_directory")

-- find sdk directory
function _find_android_sdkdir(sdkdir)

    -- get sdk directory
    if not sdkdir then
        sdkdir = os.getenv("ANDROID_SDK_HOME") or os.getenv("ANDROID_SDK_ROOT")
        if not sdkdir and is_host("macosx") then
            sdkdir = "~/Library/Android/sdk"
        end
    end

    -- get sdk directory
    if sdkdir and os.isdir(sdkdir) then
        return sdkdir
    end
end

-- find the build-tools version of sdk
function _find_sdk_build_toolver(sdkdir)

    -- find the max version
    local toolver_max = "0"
    for _, dir in ipairs(os.dirs(path.join(sdkdir, "build-tools", "*"))) do
        local toolver = path.filename(dir)
        if semver.is_valid(toolver) and semver.compare(toolver, toolver_max) > 0 then
            toolver_max = toolver
        end
    end

    -- get the max sdk version
    return toolver_max ~= "0" and tostring(toolver_max) or nil
end

-- find the android sdk
function _find_android_sdk(sdkdir, build_toolver)

    -- find sdk root directory
    sdkdir = _find_android_sdkdir(sdkdir)
    if not sdkdir then
        return {}
    end

    -- find the build-tools version of sdk
    build_toolver = build_toolver or _find_sdk_build_toolver(sdkdir)

    -- ok?
    return {sdkdir = sdkdir, build_toolver = build_toolver}
end

-- find android sdk directory
--
-- @param sdkdir    the android sdk directory
-- @param opt       the argument options, e.g. {force = true, build_toolver = "28.0.3"}
--
-- @return          the sdk toolchains. e.g. {sdkdir = .., build_toolver = "28.0.3"}
--
-- @code
--
-- local sdk = find_android_sdk("~/Library/Android/sdk")
--
-- @endcode
--
function main(sdkdir, opt)

    -- init arguments
    opt = opt or {}

    -- attempt to load cache first
    local key = "detect.sdks.find_android_sdk"
    local cacheinfo = detectcache:get(key) or {}
    if not opt.force and cacheinfo.sdk and cacheinfo.sdk.sdkdir and os.isdir(cacheinfo.sdk.sdkdir) then
        return cacheinfo.sdk
    end

    -- find sdk
    local sdk = _find_android_sdk(sdkdir or config.get("android_sdk") or global.get("android_sdk"), opt.build_toolver or config.get("build_toolver"))
    if sdk and sdk.sdkdir then

        -- save to config
        config.set("android_sdk", sdk.sdkdir, {force = true, readonly = true})
        config.set("build_toolver", sdk.build_toolver, {force = true, readonly = true})

        -- trace
        if opt.verbose or option.get("verbose") then
            local extra = ""
            if sdk.build_toolver then
                extra = " (buildtools: " .. sdk.build_toolver .. ")"
            end
            cprint("checking for Android SDK ... ${color.success}%s%s", sdk.sdkdir, extra)
        end
    else

        -- trace
        if opt.verbose or option.get("verbose") then
            cprint("checking for Android SDK ... ${color.nothing}${text.nothing}")
        end
    end

    -- save to cache
    cacheinfo.sdk = sdk or false
    detectcache:set(key, cacheinfo)
    return sdk
end