summaryrefslogtreecommitdiff
path: root/xmake/modules/package/manager/conan/install_package.lua
blob: c2496f46ddead3d37ea550ac20156bf5fb8dfe62 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
--!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 - 2019, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        install_package.lua
--

-- imports
import("core.base.option")
import("core.project.config")
import("lib.detect.find_tool")
import("devel.git")
import("net.fasturl")

-- get build directory
function _conan_get_build_directory(name)
    return path.absolute(path.join(config.buildir() or os.tmpdir(), ".conan", name))
end

-- generate conanfile.txt
function _conan_generate_conanfile(name, opt)

    -- trace
    dprint("generate %s ..", path.join(_conan_get_build_directory(name), "conanfile.txt"))

    -- get conan options, imports and build_requires
    local options        = table.wrap(opt.options)
    local imports        = table.wrap(opt.imports)
    local build_requires = table.wrap(opt.build_requires)

    -- generate it
    io.writefile("conanfile.txt", ([[
[generators]
xmake
[requires]
%s
[options]
%s
[imports]
%s
[build_requires]
%s
    ]]):format(name, table.concat(options, "\n"), table.concat(imports, "\n"), table.concat(build_requires, "\n")))
end

-- install xmake generator
function _conan_install_xmake_generator(conan)
    local xmake_generator_localdir = path.join(config.directory(), "conan", "xmake_generator")
    if not os.isdir(xmake_generator_localdir) then

        -- sort main urls
        local mainurls = {"https://github.com/xmake-io/conan-xmake_generator.git", 
                          "https://gitlab.com/tboox/conan-xmake_generator.git", 
                          "https://gitee.com/tboox/conan-xmake_generator.git"}
        fasturl.add(mainurls)
        mainurls = fasturl.sort(mainurls)

        -- clone xmake generator repository
        local ok = false
        for _, url in ipairs(mainurls) do
            ok = try { function () git.clone(url, {depth = 1, branch = "0.1.0/testing", outputdir = xmake_generator_localdir}); return true end }
            if ok then
                break
            end
        end
        if ok then
            os.vrunv(conan.program, {"export", xmake_generator_localdir, "bincrafters/testing"})
        end
    end
end

-- get configurations 
function configurations()
    return 
    {
        build          = {description = "use it to choose if you want to build from sources.", default = "missing", values = {"all", "never", "missing", "outdated"}},
        remote         = {description = "Set the conan remote server."},
        options        = {description = "Set the options values, e.g. OpenSSL:shared=True"},
        imports        = {description = "Set the imports for conan."},
        settings       = {description = "Set the build settings for conan."},
        build_requires = {description = "Set the build requires for conan.", default = "xmake_generator/0.1.0@bincrafters/testing"}
    }
end

-- install package
--
-- @param name  the package name, e.g. conan::OpenSSL/1.0.2n@conan/stable 
-- @param opt   the options, .e.g { verbose = true, mode = "release", plat = , arch = ,
--                                  remote = "", build = "all", options = {}, imports = {}, build_requires = {},
--                                  settings = {"compiler=Visual Studio", "compiler.version=10", "compiler.runtime=MD"}}
--
-- @return      true or false
--
function main(name, opt)

    -- find conan
    local conan = find_tool("conan")
    if not conan then
        raise("conan not found!")
    end

    -- get build directory
    local buildir = _conan_get_build_directory(name)

    -- clean the build directory
    os.tryrm(buildir)
    if not os.isdir(buildir) then
        os.mkdir(buildir)
    end

    -- enter build directory
    local oldir = os.cd(buildir)

    -- install xmake generator
    _conan_install_xmake_generator(conan)

    -- generate conanfile.txt
    _conan_generate_conanfile(name, opt)

    -- install package
    local argv = {"install", "."}
    if opt.build then
        if opt.build == "all" then
            table.insert(argv, "--build")
        else
            table.insert(argv, "--build=" .. opt.build)
        end
    end

    -- set platform
    table.insert(argv, "-s")
    if opt.plat == "macosx" then
        table.insert(argv, "os=Macos")
    elseif opt.plat == "linux" then
        table.insert(argv, "os=Linux")
    elseif opt.plat == "windows" then
        table.insert(argv, "os=Windows")
    else
        raise("cannot install package(%s) on platform(%s)!", name, opt.plat)
    end

    -- set architecture
    table.insert(argv, "-s")
    if opt.arch == "x86_64" or opt.arch == "x64" then
        table.insert(argv, "arch=x86_64")
    elseif opt.arch == "i386" or opt.arch == "x86" then
        table.insert(argv, "arch=x86")
    else
        raise("cannot install package(%s) for arch(%s)!", name, opt.arch)
    end

    -- set build mode
    table.insert(argv, "-s")
    if opt.mode == "debug" then
        table.insert(argv, "build_type=Debug")
    else
        table.insert(argv, "build_type=Release")
    end

    -- set compiler settings
    if opt.plat == "windows" then
        local vsvers = {["2019"] = "16", ["2017"] = "15", ["2015"] = "14", ["2013"] = "12", ["2012"] = "11", ["2010"] = "10", ["2008"] = "9", ["2005"] = "8"}
        local vs = assert(config.get("vs"), "vs not found!")
        table.insert(argv, "-s")
        table.insert(argv, "compiler=Visual Studio")
        table.insert(argv, "-s")
        table.insert(argv, "compiler.version=" .. assert(vsvers[vs], "unknown msvc version!"))
        if opt.vs_runtime then
            table.insert(argv, "-s")
            table.insert(argv, "compiler.runtime=" .. opt.vs_runtime)
        end
    end

    -- set custom settings
    for _, setting in ipairs(opt.settings) do
        table.insert(argv, "-s")
        table.insert(argv, setting)
    end

    -- set remote
    if opt.remote then
        table.insert(argv, "-r")
        table.insert(argv, opt.remote)
    end

    -- do install
    os.vrunv(conan.program, argv)

    -- leave build directory
    os.cd(oldir)
end