summaryrefslogtreecommitdiff
path: root/xmake/modules/detect/sdks/find_vcpkgdir.lua
blob: 8990c4c7061650bb41c4dd581672b65143d9baf2 (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
--!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_vcpkgdir.lua
--

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

-- find vcpkg directory
function _find_vcpkgdir(sdkdir)

    -- init the search directories
    local pathes = {}
    if sdkdir then
        table.insert(pathes, sdkdir)
    end
    if is_host("windows") then
        -- attempt to read path info after running `vcpkg integrate install`
        local pathfile = "~/../Local/vcpkg/vcpkg.path.txt"
        if os.isfile(pathfile) then
            local dir = io.readfile(pathfile):trim()
            if os.isdir(dir) then
                table.insert(pathes, dir)
            end
        end
    else
        -- TODO
    end

    -- attempt to find vcpkg
    local vcpkg = find_file(is_host("windows") and "vcpkg.exe" or "vcpkg", pathes)
    if vcpkg then
        return path.directory(vcpkg)
    end
end

-- find vcpkg directory
--
-- @param sdkdir    the vcpkg directory
-- @param opt       the argument options, .e.g {verbose = true, force = false} 
--
-- @return          the vcpkg directory
--
-- @code 
--
-- local vcpkgdir = find_vcpkgdir()
-- 
-- @endcode
--
function main(sdkdir, opt)

    -- init arguments
    opt = opt or {}

    -- attempt to load cache first
    local key = "detect.sdks.find_vcpkgdir." .. (sdkdir or "")
    local cacheinfo = cache.load(key)
    if not opt.force and cacheinfo.vcpkg ~= nil then
        return cacheinfo.vcpkg
    end
       
    -- find vcpkg
    local vcpkg = _find_vcpkgdir(sdkdir or config.get("vcpkg") or global.get("vcpkg"))
    if vcpkg then

        -- save to config
        config.set("vcpkg", vcpkg, {force = true, readonly = true})

        -- trace
        if opt.verbose or option.get("verbose") then
            cprint("checking for the vcpkg directory ... ${green}%s", vcpkg)
        end
    else

        -- trace
        if opt.verbose or option.get("verbose") then
            cprint("checking for the vcpkg directory ... ${red}no")
        end
    end

    -- save to cache
    cacheinfo.vcpkg = vcpkg or false
    cache.save(key, cacheinfo)

    -- ok?
    return vcpkg
end