summaryrefslogtreecommitdiff
path: root/xmake/actions/require/action/install.lua
blob: a1602431f6234c21530de39a8af026b6d7b8393a (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
--!The Make-like install 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        install.lua
--

-- imports
import("core.base.option")
import("core.project.target")
import("build")
import("test")
import("filter")

-- install for xmake file
function _install_for_xmakefile(package)

    -- package to install directory
    os.vrunv("xmake", {"p", "-o", package:installdir()})
end

-- install for generic
function _install_for_generic(package)

    -- the package name
    local name = package:name()

    -- the install directory
    local installdir = path.join(package:installdir(), name .. ".pkg")

    -- the linkdir
    local linkdir = path.join(installdir, "lib/$(mode)/$(plat)/$(arch)")
    os.mkdir(linkdir)

    -- the includedir 
    local includedir = path.join(installdir, "inc")
    os.mkdir(includedir)

    -- the prefix directory exists?
    local prefixdir = ""
    if os.isdir(".prefix") and not os.emptydir(".prefix") then
        prefixdir = ".prefix" .. path.seperator()
    end

    -- install the library files and ignore hidden files (.xxx)
    if not os.trycp(prefixdir .. "**" .. target.filename("*", "static"), linkdir) and 
       not os.trycp(prefixdir .. "**" .. target.filename("*", "shared"), linkdir) then
        raise("the library files not found in package %s", name)
    end

    -- install the header files
    for _, headerfile in ipairs(table.join((os.files(prefixdir .. "**.h")), (os.files(prefixdir .. "**.hpp")))) do

        -- the destinate header
        local dstheaderfile = nil
        if #prefixdir > 0 then
            dstheaderfile = path.absolute(path.relative(headerfile, path.join(prefixdir, "include")), includedir)
        else
            dstheaderfile = path.join(includedir, path.filename(headerfile))
        end

        -- install header file
        os.cp(headerfile, dstheaderfile)
    end

    -- get links
    local links = {}
    for _, filename in ipairs(os.files(path.join(linkdir, target.filename("*", "static"))), path.filename) do
        local link, count = filename:gsub(target.filename("([%w%-_]+)", "static"):gsub("%.", "%%.") .. "$", "%1")
        if count > 0 then
            table.insert(links, link)
        end
    end
    if #links == 0 then
        for _, filename in ipairs(os.files(path.join(linkdir, target.filename("*", "shared"))), path.filename) do
            local link, count = filename:gsub(target.filename("([%w%-_]+)", "shared"):gsub("%.", "%%.") .. "$", "%1")
            if count > 0 then
                table.insert(links, link)
            end
        end
    end
    assert(#links > 0, "the library files not found in package %s", name)

    -- make xmake.lua 
    local file = io.open(path.join(installdir, "xmake.lua"), "w")
    if file then

        -- the xmake.lua content
        local content = [[ 
-- the %s package
option("%s")

    -- show menu
    set_showmenu(true)

    -- set category
    set_category("package")

    -- set description
    set_description("The %s package")

    -- add defines to config.h if checking ok
    add_defines_h("$(prefix)_PACKAGE_HAVE_%s")

    -- add links for checking
    add_links("%s")

    -- add link directories
    add_linkdirs("lib/$(mode)/$(plat)/$(arch)")

    -- add include directories
    add_includedirs("inc")
]]

        -- save file
        file:writef(content, name, name, name, name:upper(), table.concat(links, "\", \""))

        -- exit file
        file:close()
    end
end

-- on install the given package
function _on_install_package(package)

    -- init install scripts
    local installscripts =
    {
        {"xmake.lua",       _install_for_xmakefile    }
    ,   {"*",               _install_for_generic      }
    }

    -- attempt to install it
    for _, installscript in pairs(installscripts) do

        -- save the current directory 
        local oldir = os.curdir()

        -- try installing 
        local ok = try
        {
            function ()

                -- attempt to install it if file exists
                local files = os.files(installscript[1])
                if #files > 0 then
                    installscript[2](package)
                    return true
                end
            end,

            catch
            {
                function (errors)

                    -- trace verbose info
                    if errors then
                        vprint(errors)
                    end
                end
            }
        }

        -- restore directory
        os.cd(oldir)

        -- ok?
        if ok then return end
    end

    -- failed
    raise("attempt to install package %s failed!", package:fullname())
end

-- install the given package
function main(package)

    -- get working directory of this package
    local workdir = package:cachedir()

    -- enter the working directory
    local oldir = nil
    if #package:urls() > 0 then
        for _, srcdir in ipairs(os.dirs(path.join(workdir, "source", "*"))) do
            oldir = os.cd(srcdir)
            break
        end
    else
        os.mkdir(workdir)
        oldir = os.cd(workdir) 
    end

    -- init tipname 
    local tipname = package:fullname()
    if package:version_str() then
        tipname = tipname .. "-" .. package:version_str()
    end

    -- trace
    cprintf("${yellow}  => ${clear}installing %s .. ", tipname)
    if option.get("verbose") then
        print("")
    end

    -- install it
    try
    {
        function ()

            -- the package scripts
            local scripts =
            {
                package:script("install_before") 
            ,   package:script("install", _on_install_package)
            ,   package:script("install_after") 
            }

            -- create the install task
            local installtask = function () 

                -- build it
                build(package)

                -- install it
                for i = 1, 3 do
                    local script = scripts[i]
                    if script ~= nil then
                        filter.call(script, package)
                    end
                end

                -- test it
                test(package)
            end

            -- install package
            if option.get("verbose") then
                installtask()
            else
                process.asyncrun(installtask)
            end

            -- fetch package and force to flush the cache
            assert(package:fetch(true), "fetch %s failed!", tipname)

            -- trace
            cprint("${green}ok")
        end,

        catch
        {
            function (errors)

                -- verbose?
                if option.get("verbose") and errors then
                    cprint("${bright red}error: ${clear}%s", errors)
                end

                -- trace
                cprint("${red}failed")

                -- failed
                if not package:requireinfo().optional then
                    raise("install failed!")
                end
            end
        }
    }

    -- leave source codes directory
    os.cd(oldir)
end