summaryrefslogtreecommitdiff
path: root/xmake/actions/clean/main.lua
blob: 6606ddc2f63c31d162ca3aba79db52b4780337a5 (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
--!A cross-platform build utility based on Lua
--
-- Licensed 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-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        main.lua
--

-- imports
import("core.base.option")
import("core.base.task")
import("core.project.rule")
import("core.project.config")
import("core.base.global")
import("core.project.project")
import("core.platform.platform")
import("private.action.clean.remove_files")
import("target.action.clean", {alias = "_do_clean_target"})
import("private.service.remote_build.action", {alias = "remote_build_action"})
import("private.action.utils", {alias = "action_utils"})

-- on clean target
function _on_clean_target(target)

    -- build target with rules
    local done = false
    for _, r in ipairs(target:orderules()) do
        local on_clean = r:script("clean")
        if on_clean then
            on_clean(target)
            done = true
        end
    end
    if done then return end

    -- do clean
    _do_clean_target(target)
end

-- clean the given target files
function _clean_target(target)

    -- has been disabled?
    if not target:is_enabled() then
        return
    end

    -- enter the environments of the target packages
    local oldenvs = os.addenvs(target:pkgenvs())

    -- the target scripts
    local scripts =
    {
        target:script("clean_before")
    ,   function (target)
            for _, r in ipairs(target:orderules()) do
                local before_clean = r:script("clean_before")
                if before_clean then
                    before_clean(target)
                end
            end
        end
    ,   target:script("clean", _on_clean_target)
    ,   function (target)
            for _, r in ipairs(target:orderules()) do
                local after_clean = r:script("clean_after")
                if after_clean then
                    after_clean(target)
                end
            end
        end
    ,   target:script("clean_after")
    }

    -- run the target scripts
    for i = 1, 5 do
        local script = scripts[i]
        if script ~= nil then
            script(target)
        end
    end

    -- leave the environments of the target packages
    os.setenvs(oldenvs)
end

-- clean the given targets
function _clean_targets(targets)
    for _, target in ipairs(targets) do
        _clean_target(target)
    end
end

-- clean targets
function _clean(targetnames, group_pattern)
    if (targetnames and #targetnames > 0) or group_pattern then
        _clean_targets(action_utils.get_targets(targetnames, {group_pattern = group_pattern}))
    else
        -- clean all targets by default
        _clean_targets(project.ordertargets())
    end
end

-- clean configuration cache
function _clean_configs()
    if option.get("all") then
        -- we need to close it first after removing file lock
        project.filelock():close()
        remove_files(config.directory())
    end
end

-- do clean for the third-party buildsystem
function _try_clean()

    -- load config
    config.load()

    -- get the buildsystem tool
    local configfile = nil
    local tool = nil
    local trybuild = config.get("trybuild")
    if trybuild then
        tool = import("private.action.trybuild." .. trybuild, {try = true, anonymous = true})
        if tool then
            configfile = tool.detect()
        end
    end

    -- try cleaning it
    if configfile and tool and trybuild then
        tool.clean()
    end
end

function main()

    -- try cleaning it using third-party buildsystem if xmake.lua not exists
    if not os.isfile(project.rootfile()) then
        return _try_clean()
    end

    -- do action for remote?
    if remote_build_action.enabled() then
        return remote_build_action()
    end

    -- load config first
    task.run("config", {require = false}, {disable_dump = true})

    -- lock the whole project
    project.lock()

    -- get the target names and group pattern
    local targetnames, group_pattern = action_utils.get_targets_and_group()

    -- enter project directory
    local oldir = os.cd(project.directory())

    -- clean targets
    _clean(targetnames, group_pattern)

    -- unlock the whole project
    project.unlock()

    -- we must call it after unlocking project because it will remove project lockfile
    _clean_configs()

    -- leave project directory
    os.cd(oldir)
end