summaryrefslogtreecommitdiff
path: root/xmake/scripts/base/project.lua
blob: a36b71af10a36e166cc7b7340272897509678abb (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
--!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        project.lua
--

-- load modules
local utils = require("base/utils")

-- enter project 
local _PROJECT = _PROJECT or {}
local _MAINENV = getfenv()
setmetatable(_PROJECT, {__index = _G})  
setfenv(1, _PROJECT)

-- init the current scope
local current = nil

-- configure scope end
function _end()

    -- check
    assert(current)

    -- leave the current scope
    current = current._PARENT
end

-- preprocess value
function _preprocess(value)

    -- the value is string?
    if type(value) == "string" then

        -- replace $(variable)
        value = value:gsub("%$%((.*)%)",    function (v) 
                                                if v == "buildir" then
                                                    -- TODO
                                                    return v
                                                    --return xmake._CONFIGS.all.output
                                                elseif v == "projectdir" then
                                                    return xmake._OPTIONS.project
                                                end
                                                return v 
                                            end)
    end

    -- ok
    return value
end

-- register configures
function _register(names)

    -- check
    assert(_PROJECT)
    assert(names and type(names) == "table")

    -- register all configures
    for _, name in ipairs(names) do

        -- register the configure 
        _PROJECT[name] = _PROJECT[name] or function(...)

            -- check
            assert(current)

            -- init ldflags
            current[name] = current[name] or {}

            -- get arguments
            local arg = arg or {...}
            if table.getn(arg) == 0 then
                -- no argument
                current[name] = nil
            elseif table.getn(arg) == 1 then
                -- save only one argument
                current[name] = _preprocess(arg[1])
            else
                -- save all arguments
                for i, v in ipairs(arg) do
                    current[name][i] = _preprocess(v)
                end
            end
        end
    end
end

-- init project
function _init()

    -- register all configures
    _register   {   "kind"
                ,   "deps"
                ,   "files"
                ,   "links" 
                ,   "mflags" 
                ,   "headers" 
                ,   "headerdir" 
                ,   "targetdir" 
                ,   "objectdir" 
                ,   "linkdirs" 
                ,   "includedirs" 
                ,   "cflags" 
                ,   "cxxflags" 
                ,   "ldflags" 
                ,   "mxflags" 
                ,   "defines"} 
end

-- configure platforms
function plarforms(...)

    -- check
    assert(current)

    -- init platforms
    current._PLATFORMS = current._PLATFORMS or {}

    -- init scope
    local scope = {}

    -- configure all platforms
    local arg = arg or {...}
    for _, name in ipairs(arg) do

        -- check
        if current._PLATFORMS[name] then
            -- error
            utils.error("the platform: %s has been defined repeatly!", name)
            assert(false) 
        end

        -- init the platform scope
        current._PLATFORMS[name] = scope

    end

    -- enter scope
    local parent = current
    current = scope
    current._PARENT = parent
end

-- configure target
function target(name)

    -- check
    assert(name and current)

    -- init targets
    current._TARGETS = current._TARGETS or {}

    -- init target scope
    current._TARGETS[name] = {}

    -- enter target scope
    local parent = current
    current = current._TARGETS[name]
    current._PARENT = parent
end

-- configure project
function project(name)

    -- check
    assert(name)

    -- init the root scope, must be only one project
    if not _CONFIGS then
        _CONFIGS = {}
    else
        -- error
        utils.error("the project: %s is redundant!", name)
        return
    end

    -- init the project name
    _CONFIGS.name = name

    -- init the current scope
    current = _CONFIGS
    current._PARENT = nil

end

-- load xproj
function loadxproj(file)

    -- check
    assert(file)

    -- load and execute the xmake.xproj
    local script = preprocessor.loadx(file)
    if script then

        -- init the project envirnoment
        _PROJECT._init()
        setfenv(script, _PROJECT)

        -- execute it
        local ok, err = pcall(script)
        if not ok then
            -- error
            return err
        end
    else
        -- error
        return string.format("load %s failed!", file)
    end

    -- ok
    return nil
end

-- dump all configures
function dump()
     
    -- check
    assert(_CONFIGS)

    -- dump
    if xmake._OPTIONS.verbose then
        utils.dump(_CONFIGS, "_PARENT")
    end
   
end

-- leave project 
setfenv(1, _MAINENV)
return _PROJECT