summaryrefslogtreecommitdiff
path: root/xmake/core/platform/platform.lua
blob: bdcf803ad4a153bd924c7ca0dbd9c1003de25e46 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
--!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) 2015 - 2016, ruki All rights reserved.
--
-- @author      ruki
-- @file        platform.lua
--

-- define module
local platform      = platform or {}
local _instance     = _instance or {}

-- load modules
local os            = require("base/os")
local path          = require("base/path")
local utils         = require("base/utils")
local table         = require("base/table")
local interpreter   = require("base/interpreter")
local sandbox       = require("sandbox/sandbox")
local config        = require("project/config")
local global        = require("project/global")

-- load the platform module
function _instance:_load(modulename)

    -- return it directly if cached
    local cachename = "_" .. modulename:upper()
    if self[cachename] then
        return self[cachename]
    end

    -- no this module?
    if not self._INFO[modulename] then
        return nil
    end

    -- get the script path
    local scriptpath = path.join(self._ROOTDIR, self._INFO[modulename] .. ".lua")
    
    -- not exists?
    if not scriptpath or not os.isfile(scriptpath) then
        return nil, string.format("the %s of %s not found!", modulename, self._NAME)
    end

    -- load script
    local script, errors = loadfile(scriptpath)
    if script then

        -- make sandbox instance with the given script
        local instance, errors = sandbox.new(script, nil, self._ROOTDIR)
        if not instance then
            return nil, errors
        end

        -- import the module
        local module, errors = instance:import()
        if not module then
            return nil, errors
        end

        -- init the module
        if module.init then
            module.init()
        end
    
        -- save it to the cache
        self[cachename] = module

        -- ok?
        return module
    end

    -- failed
    return nil, errors
end

-- new an instance
function _instance.new(name, info, rootdir)

    -- new an instance
    local instance = table.inherit(_instance)

    -- init instance
    instance._NAME      = name
    instance._INFO      = info
    instance._ROOTDIR   = rootdir

    -- ok
    return instance
end

-- get the platform os
function _instance:os()

    -- get it
    return self._INFO.os
end

-- get the platform menu
function _instance:menu()

    -- get it
    return self._INFO.menu
end

-- get the platform hosts
function _instance:hosts()

    -- get it
    return self._INFO.hosts
end

-- get the platform archs
function _instance:archs()

    -- get it
    return self._INFO.archs
end

-- get the platform tooldirs
function _instance:tooldirs()

    -- get it
    return self._INFO.tooldirs
end

-- get the checker
function _instance:checker()

    -- load checker
    return self:_load("checker")
end

-- get the installer
function _instance:installer()

    -- load installer
    return self:_load("installer")
end

-- get the environment
function _instance:environment()

    -- load environment
    return self:_load("environment")
end

-- get the platform configure
function _instance:get(name)

    -- the info
    local info = self._INFO

    -- load it first
    if self._g == nil and info.load ~= nil then

        -- load it
        local ok, errors = sandbox.load(info.load)
        if not ok then
            os.raise(errors)
        end

        -- save _g
        self._g = getfenv(info.load)._g
    end

    -- get it
    if self._g ~= nil then
        return self._g[name]
    end
end

-- the directories of platform
function platform._directories()

    -- the directories
    return  {   path.join(config.directory(), "platforms")
            ,   path.join(global.directory(), "platforms")
            ,   path.join(xmake._PROGRAM_DIR, "platforms")
            }
end

-- the interpreter
function platform._interpreter()

    -- the interpreter has been initialized? return it directly
    if platform._INTERPRETER then
        return platform._INTERPRETER
    end

    -- init interpreter
    local interp = interpreter.new()
    assert(interp)
 
    -- register api: platform()
    interp:api_register_scope("platform")

    -- register api: set_os()
    interp:api_register_set_values("platform", "os")

    -- register api: set_hosts() 
    interp:api_register_set_values("platform", "hosts")

    -- register api: set_archs() 
    interp:api_register_set_values("platform", "archs")

    -- register api: set_menu() 
    interp:api_register_set_values("platform", "menu")

    -- register api: set_checker()
    interp:api_register_set_values("platform", "checker")

    -- register api: set_tooldirs()
    interp:api_register_set_values("platform", "tooldirs")

    -- register api: set_environment()
    interp:api_register_set_values("platform", "environment")

    -- register api: set_installer()
    interp:api_register_set_values("platform", "installer")

    -- register api: on_load()
    interp:api_register_on_script("platform", "load")

    -- save interpreter
    platform._INTERPRETER = interp

    -- ok?
    return interp
end

-- load the given platform 
function platform.load(plat)

    -- get platform name
    plat = plat or config.get("plat") or xmake._HOST
    if not plat then
        return nil, string.format("unknown platform!")
    end

    -- get it directly from cache dirst
    platform._PLATFORMS = platform._PLATFORMS or {}
    if platform._PLATFORMS[plat] then
        return platform._PLATFORMS[plat]
    end

    -- find the platform script path
    local scriptpath = nil
    for _, dir in ipairs(platform._directories()) do

        -- find this directory
        scriptpath = path.join(path.join(dir, plat), "xmake.lua")
        if os.isfile(scriptpath) then
            break
        end
    end

    -- not exists?
    if not scriptpath or not os.isfile(scriptpath) then
        return nil, string.format("the platform %s not found!", plat)
    end

    -- load platform
    local results, errors = platform._interpreter():load(scriptpath, "platform", true, false)
    if not results and os.isfile(scriptpath) then
        return nil, errors
    end

    -- check the platform name
    if not results[plat] then
        return nil, string.format("the platform %s not found!", plat)
    end

    -- new an instance
    local instance, errors = _instance.new(plat, results[plat], platform._interpreter():rootdir())
    if not instance then
        return nil, errors
    end

    -- save instance to the cache
    platform._PLATFORMS[plat] = instance

    -- ok
    return instance

end

-- get the platform os
function platform.os(plat)

    -- load the platform 
    local instance = platform.load(plat)
    if instance then
        return instance:os()
    end
end

-- get the given platform configure
function platform.get(name)

    -- get the current platform configure
    local instance = platform.load()
    if instance then
        return instance:get(name)
    end
end

-- get the platform tool from the kind
--
-- .e.g cc, cxx, mm, mxx, as, ar, ld, sh, ..
--
function platform.tool(kind)

    -- get it
    return config.get(kind)
end

-- get the platform archs
function platform.archs(plat)

    -- load the platform 
    local instance = platform.load(plat)
    if instance then
        return instance:archs()
    end
end

-- get the all platforms
function platform.plats()
    
    -- return it directly if exists
    if platform._PLATS then
        return platform._PLATS 
    end

    -- get all platforms
    local plats = {}
    local dirs  = platform._directories()
    for _, dir in ipairs(dirs) do

        -- get the platform list 
        local platpathes = os.match(path.join(dir, "*"), true)
        if platpathes then
            for _, platpath in ipairs(platpathes) do
                if os.isfile(path.join(platpath, "xmake.lua")) then
                    table.insert(plats, path.basename(platpath))
                end
            end
        end
    end

    -- save them
    platform._PLATS = plats

    -- ok
    return plats
end

-- get the tool directories
function platform.tooldirs(plat)

    -- get the tool directories for the current platform
    local instance = platform.load(plat)
    if instance then
        return instance:tooldirs()
    end
end

-- get the given format
function platform.format(kind)

    -- get formats
    local formats = platform.get("formats")
    if formats then
        return formats[kind]
    end
end


-- return module
return platform