summaryrefslogtreecommitdiff
path: root/xmake/scripts/action/_package.lua
blob: e39604a33cd3b65ce99b459f2e77c0fbdcacb006 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
--!The Automatic Cross-platform Build Tool
-- 
-- XMake is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
-- 
-- XMake is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Lesser General Public License for more details.
-- 
-- You should have received a copy of the GNU Lesser General Public License
-- along with XMake; 
-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
-- 
-- Copyright (C) 2009 - 2015, ruki All rights reserved.
--
-- @author      ruki
-- @file        _package.lua
--

-- define module: _package
local _package = _package or {}

-- load modules
local rule      = require("base/rule")
local path      = require("base/path")
local utils     = require("base/utils")
local config    = require("base/config")
local global    = require("base/global")
local string    = require("base/string")
local project   = require("base/project")
local package   = require("base/package")
local platform  = require("platform/platform")
    
-- need access to the given file?
function _package.need(name)

    -- no accessors
    return false
end
 
-- configure target for the given architecture
function _package._config(arch, target_name)

    -- need not configure it
    if not arch then return true end

    -- done the command
    return os.execute(string.format("xmake f -P %s -f %s -a %s %s", xmake._PROJECT_DIR, xmake._PROJECT_FILE, arch, target_name)) == 0;
end

-- build target for the given architecture
function _package._build(arch, target_name)

    -- get the target name
    if not target_name or target_name == "all" then 
        target_name = ""
    end

    -- configure it first
    if not _package._config(arch, target_name) then return false end

    -- build it
    if os.execute(string.format("xmake -P %s %s", xmake._PROJECT_DIR, target_name)) ~= 0 then 
        -- errors
        utils.error("build failed!")
        return false 
    end

    -- ok
    return true
end

-- backup the target files
function _package._backup(rootdir, filepath)

    -- the relative file path
    if filepath and path.is_absolute(filepath) then
        filepath = path.relative(filepath, xmake._PROJECT_DIR)
    end

    -- not exists? return it directly
    if not filepath or not os.isfile(filepath) then return end

    -- the backup file
    local backupfile = string.format("%s/%s", rootdir, filepath)

    -- backup it
    local ok, errors = os.cp(filepath, backupfile)
    if not ok then
        -- errors 
        utils.error(errors)
        return 
    end

    -- ok
    return filepath
end

-- make configure for the given target 
function _package._makeconf(target_name, target)

    -- check
    assert(target_name and target)

    -- the options
    local options = xmake._OPTIONS
    assert(options)

    -- the configs
    local configs = _package._CONFIGS
    assert(configs)

    -- the architecture 
    local arch = config.get("arch")
    if not arch then return false end

    -- init configs for targets
    configs[target_name] = configs[target_name] or {}
    local configs_target = configs[target_name]

    -- init configs for architectures
    configs_target.archs = configs_target.archs or {}

    -- init configs for architecture
    configs_target.archs[arch] = configs_target.archs[arch] or {}
    local configs_arch = configs_target.archs[arch]

    -- save name
    configs_target.name = target_name

    -- save kind
    configs_target.kind = target.kind

    -- save the output directory
    configs_target.outputdir = options.outputdir or config.get("buildir")

    -- save the header files
    configs_target.headers = target.headers

    -- save the target directory
    configs_arch.targetdir = rule.backupdir(target_name, arch)

    -- save the config file
    configs_arch.config_h = _package._backup(configs_arch.targetdir, rule.config_h(target))

    -- save the target file
    configs_arch.targetfile = _package._backup(configs_arch.targetdir, rule.targetfile(target_name, target))

    -- save the package script
    local packagescript = target.packagescript
    if type(packagescript) == "string" and os.isfile(packagescript) then
        local script, errors = loadfile(packagescript)
        if script then
            packagescript = script()
            if type(packagescript) == "table" and packagescript.main then 
                packagescript = packagescript.main
            end
        else
            utils.error(errors)
            return false
        end
    end
    if target.packagescript and type(packagescript) ~= "function" then
        utils.error("invalid package script!")
        return false
    end
    configs_target.packagescript = packagescript

    -- ok
    return true
end

-- load configure for the given target 
function _package._loadconf(target_name)

    -- reload configure
    local errors = config.reload()
    if errors then
        -- error
        utils.error(errors)
        return false
    end

    -- make the platform configure
    if not platform.make() then
        utils.error("make platform configure: %s failed!", config.get("plat"))
        return false
    end

    -- reload project
    local errors = project.reload()
    if errors then
        -- error
        utils.error(errors)
        return false
    end

    -- the targets
    local targets = project.targets()
    assert(targets)

    -- make configure for the given target
    if target_name and target_name ~= "all" then
        if not _package._makeconf(target_name, targets[target_name]) then 
            utils.error("make target configure: %s failed!", target_name)
            return false
        end
    else
        for target_name, target in pairs(targets) do
            if not _package._makeconf(target_name, target) then 
                utils.error("make target configure: %s failed!", target_name)
                return false
            end
        end
    end

    -- ok
    return true
end

-- build target for all architectures
function _package._build_all(archs, target_name)

    -- exists the given architectures?
    if archs then
    
        -- split all architectures
        archs = archs:split(",")
        if not archs then return false end

        -- build for all architectures
        for _, arch in ipairs(archs) do

            -- trim it
            arch = arch:trim()

            -- build it
            if not _package._build(arch, target_name) then return false end

            -- load configure
            if not _package._loadconf(target_name) then return false end

        end

    -- build for single architecture
    else

        -- build it
        if not _package._build(nil, target_name) then return false end

        -- load configure
        if not _package._loadconf(target_name) then return false end

    end

    -- ok
    return true
end

-- done 
function _package.done()

    -- the options
    local options = xmake._OPTIONS
    assert(options)

    -- trace
    print("package: ...")

    -- init configs
    _package._CONFIGS = _package._CONFIGS or {}
    local configs = _package._CONFIGS

    -- load the global configure first
    global.load()

    -- enter the project directory
    if not os.cd(xmake._PROJECT_DIR) then
        -- errors
        utils.error("not found project: %s!", xmake._PROJECT_DIR)
        return false
    end

    -- build the given target first for all architectures
    if not _package._build_all(options.archs, options.target) then
        -- errors
        utils.error("build package failed!")
        return false
    end

    -- done package 
    if not package.done(configs) then
        -- errors
        utils.error("package: failed!")
        return false
    end

    -- trace
    print("package: ok!")

    -- ok
    return true
end

-- the menu
function _package.menu()

    return {
                -- xmake p
                shortname = 'p'

                -- usage
            ,   usage = "xmake package|p [options] [target]"

                -- description
            ,   description = "Package target."

                -- options
            ,   options = 
                {
                    {'a', "archs",      "kv", nil,          "Package multiple given architectures."                             
                                                          , "    .e.g --archs=\"armv7, arm64\" or -a i386"
                                                          , ""
                                                          , function () 
                                                              local descriptions = {}
                                                              local plats = platform.plats()
                                                              if plats then
                                                                  for i, plat in ipairs(plats) do
                                                                      descriptions[i] = "    - " .. plat .. ":"
                                                                      local archs = platform.archs(plat)
                                                                      if archs then
                                                                          for _, arch in ipairs(archs) do
                                                                              descriptions[i] = descriptions[i] .. " " .. arch
                                                                          end
                                                                      end
                                                                  end
                                                              end
                                                              return descriptions
                                                            end                                                             }

                ,   {}
                ,   {'f', "file",       "kv", "xmake.lua",  "Create a given xmake.lua file."                                }
                ,   {'P', "project",    "kv", nil,          "Create from the given project directory."
                                                          , "Search priority:"
                                                          , "    1. The Given Command Argument"
                                                          , "    2. The Envirnoment Variable: XMAKE_PROJECT_DIR"
                                                          , "    3. The Current Directory"                                  }
                ,   {'o', "outputdir",  "kv", nil,          "Set the output directory."                                     }

                ,   {}
                ,   {'v', "verbose",    "k",  nil,          "Print lots of verbose information."                            }
                ,   {nil, "version",    "k",  nil,          "Print the version number and exit."                            }
                ,   {'h', "help",       "k",  nil,          "Print this help message and exit."                             }
         
                ,   {}
                ,   {nil, "target",     "v",  "all",        "Package a given target"                                        }   
                }
            }
end

-- return module: _package
return _package