summaryrefslogtreecommitdiff
path: root/xmake/modules/detect/sdks/find_emsdk.lua
blob: 642e7aeb9757547a9db6fbf37f23f9cc9af5d158 (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
--!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      SirLynix
-- @file        find_emsdk.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_file")

-- find sdk directory
function _find_emsdkdir(sdkdir)
    local paths = {}
    if sdkdir then
        table.insert(paths, sdkdir)
    end
    table.insert(paths, "$(env EMSDK)")
    if is_host("linux") then
        table.join2(paths, {"/usr/share/emscripten/", "/usr/lib/emscripten/"})
    end
    local emsdk = find_file("emsdk.py", paths, {suffixes = subdirs})
    if emsdk then
        return path.directory(emsdk)
    end
    local emcc_py = find_file("emcc.py", paths, {suffixes = subdirs})
    if emcc_py then
        return path.directory(emcc_py)
    end
end

-- find emsdk
function _find_emsdk(sdkdir)

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

    -- find emscripten toolchain directory
    local emscripten
    local subdirs = {}
    table.insert(subdirs, path.join("*", "emscripten"))
    local emcc = find_file("emcc", sdkdir, {suffixes = subdirs})
    emcc = emcc or find_file("emcc.py", sdkdir)
    if emcc then
        emscripten = path.directory(emcc)
    end
    return {sdkdir = sdkdir, emscripten = emscripten}

end

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

    -- init arguments
    opt = opt or {}

    -- attempt to load cache first
    local key = "detect.sdks.find_emsdk"
    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_emsdk(sdkdir or config.get("emsdk") or global.get("emsdk"))
    if sdk and sdk.sdkdir then
        config.set("emsdk", sdk.sdkdir, {force = true, readonly = true})
        if opt.verbose or option.get("verbose") then
            cprint("checking for emsdk directory ... ${color.success}%s", sdk.sdkdir)
        end

    else
        if opt.verbose or option.get("verbose") then
            cprint("checking for emsdk directory ... ${color.nothing}${text.nothing}")
        end
    end

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