summaryrefslogtreecommitdiff
path: root/xmake/actions/config/main.lua
blob: fb3ddd1bd3df9e086700e5044158bca654603fa1 (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
--!A cross-platform build 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        main.lua
--

-- imports
import("core.base.option")
import("core.project.config")
import("core.base.global")
import("core.project.project")
import("core.platform.platform")
import("core.project.cache", {nocache = true})
import("lib.detect.cache", {alias = "detectcache"})
import("scangen")
import("menuconf", {alias = "menuconf_show"})
import("configheader", {alias = "generate_configheader"})
import("actions.require.install", {alias = "install_requires", rootdir = os.programdir()})

-- filter option 
function _option_filter(name)
    local options = 
    {
        target      = true
    ,   file        = true
    ,   root        = true
    ,   yes         = true
    ,   quiet       = true
    ,   profile     = true
    ,   project     = true
    ,   verbose     = true
    ,   backtrace   = true
    ,   require     = true
    }
    return not options[name]
end

-- host changed?
function _host_changed(targetname)
    return os.host() ~= config.read("host", targetname)
end

-- need check
function _need_check(changed)

    -- clean?
    if not changed then
        changed = option.get("clean")
    end

    -- get the current mtimes 
    local mtimes = project.mtimes()

    -- get the previous mtimes 
    if not changed then
        local mtimes_prev = cache.get("mtimes")
        if mtimes_prev then 

            -- check for all project files
            for file, mtime in pairs(mtimes) do

                -- modified? reconfig and rebuild it
                local mtime_prev = mtimes_prev[file]
                if not mtime_prev or mtime > mtime_prev then
                    changed = true
                    break
                end
            end
        end
    end

    -- update mtimes
    cache.set("mtimes", mtimes)

    -- changed?
    return changed
end

-- check dependent target
function _check_target_deps(target)

    -- check 
    for _, depname in ipairs(target:get("deps")) do

        -- check dependent target name
        assert(depname ~= target:name(), "the target(%s) cannot depend self!", depname)

        -- get dependent target
        local deptarget = project.target(depname)

        -- check dependent target name
        assert(deptarget, "unknown target(%s) for %s.deps!", depname, target:name())

        -- check the dependent targets
        _check_target_deps(deptarget)
    end
end

-- check target
function _check_target(targetname)

    -- check
    assert(targetname)

    -- all?
    if targetname == "all" then

        -- check the dependent targets
        for _, target in pairs(project.targets()) do
            _check_target_deps(target)
        end
    else

        -- get target
        local target = project.target(targetname)

        -- check target name
        assert(target, "unknown target: %s", targetname)

        -- check the dependent targets
        _check_target_deps(target)
    end
end

-- main
function main()

    -- avoid to run this task repeatly
    if _g.configured then return end
    _g.configured = true

    -- enter menu config
    if option.get("menu") then
        menuconf_show()
    end

    -- scan project and generate it if xmake.lua not exists
    if not os.isfile(project.file()) then

        -- need some tips?
        local autogen = true
        if not option.get("quiet") and not option.get("yes") then

            -- show tips
            cprint("${bright yellow}note: ${default yellow}xmake.lua not found, try generating it (pass -y to skip confirm)?")
            cprint("please input: n (y/n)")

            -- get answer
            io.flush()
            if io.read() ~= 'y' then
                autogen = false
            end
        end

        -- do not generate it
        if not autogen then
            os.exit() 
        end

        -- scan and generate it automatically
        scangen()
    end

    -- the target name
    local targetname = option.get("target") or "all"

    -- enter cache scope
    cache.enter("local.config")

    -- load global configure
    global.load()

    -- load the project configure
    --
    -- priority: option > option_cache > global > option_default > config_check > project_check > config_cache
    --

    -- get the options
    local options = nil
    for name, value in pairs(option.options()) do
        if _option_filter(name) then
            options = options or {}
            options[name] = value
        end
    end

    -- override configure from the options or cache 
    local options_changed = false
    local options_history = {}
    if not option.get("clean") then
        options_history = cache.get("options_" .. targetname) or {}
        options = options or options_history
    end
    for name, value in pairs(options) do
            
        -- options is changed by argument options?
        options_changed = options_changed or options_history[name] ~= value

        -- @note override it and mark as readonly (highest priority)
        config.set(name, value, {readonly = true})
    end

    -- merge the cached configure
    --
    -- @note we cannot load cache config when switching platform, arch .. 
    -- so we need known whether options have been changed
    --
    local configcache = false
    if not options_changed and not option.get("clean") and not _host_changed(targetname) then
        configcache = config.load(targetname) 
    end

    -- merge the global configure 
    for name, value in pairs(global.options()) do 
        if config.get(name) == nil then
            config.set(name, value)
        end
    end

    -- merge the default options 
    for name, value in pairs(option.defaults()) do
        if _option_filter(name) and config.get(name) == nil then
            config.set(name, value)
        end
    end

    -- merge the checked configure 
    local recheck = _need_check(options_changed or not configcache)
    if recheck then

        -- check configure
        config.check()

        -- check project options
        project.check()

        -- clear detect cache
        detectcache.clear()
    end

    -- load platform
    platform.load(config.plat())

    -- translate the build directory
    local buildir = config.get("buildir")
    if buildir and path.is_absolute(buildir) then
        config.set("buildir", path.relative(buildir, project.directory()), {readonly = true, force = true})
    end

    -- check target
    _check_target(targetname)

    -- install and update requires and config header
    local require_enable = option.boolean(option.get("require"))
    if (recheck or require_enable) and require_enable ~= false then
        install_requires()
    end

    -- update the config header
    if recheck then
        generate_configheader()
    end

    -- dump config
    if option.get("verbose") then
        config.dump()
    end

    -- save options and configure for the given target
    config.save(targetname)
    cache.set("options_" .. targetname, options)

    -- save options and configure for each targets if be all
    if targetname == "all" then
        for _, target in pairs(project.targets()) do
            config.save(target:name())
            cache.set("options_" .. target:name(), options)
        end
    end

    -- flush cache
    cache.flush()
end