-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmake.lua
161 lines (148 loc) · 6.33 KB
/
xmake.lua
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
set_project("swallow")
set_version("0.0.1")
set_xmakever("2.8.1")
add_rules("mode.debug", "mode.release")
includes("**/xmake.lua")
after_build(function()
-- imports
import("core.project.config")
import("core.project.depend")
import("core.project.project")
import("core.base.task")
-- we should not update it if we are installing xmake package
if os.getenv("XMAKE_IN_XREPO") then
return
end
function update_compile_commands()
-- run only once for all xmake process
local tmpfile = path.join(config.buildir(), ".gens", "rules", "plugin.compile_commands.autoupdate")
local dependfile = tmpfile .. ".d"
local lockfile = io.openlock(tmpfile .. ".lock")
if lockfile:trylock() then
local outputdir = "."
local lsp
local sourcefiles = {}
for _, target in pairs(project.targets()) do
table.join2(sourcefiles, target:sourcefiles(), (target:headerfiles()))
local extraconf = target:extraconf("rules", "plugin.compile_commands.autoupdate")
if extraconf then
outputdir = extraconf.outputdir
lsp = extraconf.lsp
end
end
table.sort(sourcefiles)
depend.on_changed(function()
-- we use task instead of os.exec("xmake") to avoid the project lock
local filename = "compile_commands.json"
local filepath = outputdir and path.join(outputdir, filename) or filename
task.run("project", { kind = "compile_commands", outputdir = outputdir, lsp = lsp })
print("[ RUN]: update compile_commands.json")
end, {
dependfile = dependfile,
files = table.join(project.allfiles(), config.filepath()),
values = sourcefiles
})
lockfile:close()
end
end
function update_makefile()
-- run only once for all xmake process
local tmpfile = path.join(config.buildir(), ".gens", "rules", "plugin.makefile.autoupdate")
local dependfile = tmpfile .. ".d"
local lockfile = io.openlock(tmpfile .. ".lock")
if lockfile:trylock() then
local outputdir = "."
local lsp
local sourcefiles = {}
for _, target in pairs(project.targets()) do
table.join2(sourcefiles, target:sourcefiles(), (target:headerfiles()))
local extraconf = target:extraconf("rules", "plugin.makefile.autoupdate")
if extraconf then
outputdir = extraconf.outputdir
lsp = extraconf.lsp
end
end
table.sort(sourcefiles)
depend.on_changed(function()
-- we use task instead of os.exec("xmake") to avoid the project lock
local filename = "makefile"
local filepath = outputdir and path.join(outputdir, filename) or filename
task.run("project", { kind = "makefile", outputdir = outputdir, lsp = lsp })
print("[ RUN]: update makefile")
end, {
dependfile = dependfile,
files = table.join(project.allfiles(), config.filepath()),
values = sourcefiles
})
lockfile:close()
end
end
function update_cmake()
-- run only once for all xmake process
local tmpfile = path.join(config.buildir(), ".gens", "rules", "plugin.cmake.autoupdate")
local dependfile = tmpfile .. ".d"
local lockfile = io.openlock(tmpfile .. ".lock")
if lockfile:trylock() then
local outputdir = "."
local lsp
local sourcefiles = {}
for _, target in pairs(project.targets()) do
table.join2(sourcefiles, target:sourcefiles(), (target:headerfiles()))
local extraconf = target:extraconf("rules", "plugin.cmake.autoupdate")
if extraconf then
outputdir = extraconf.outputdir
lsp = extraconf.lsp
end
end
table.sort(sourcefiles)
depend.on_changed(function()
-- we use task instead of os.exec("xmake") to avoid the project lock
local filename = "CMakeLists.txt"
local filepath = outputdir and path.join(outputdir, filename) or filename
task.run("project", { kind = "cmake", outputdir = outputdir, lsp = lsp })
print("[ RUN]: update CMakeLists.txt")
end, {
dependfile = dependfile,
files = table.join(project.allfiles(), config.filepath()),
values = sourcefiles
})
lockfile:close()
end
end
function update_ninja()
-- run only once for all xmake process
local tmpfile = path.join(config.buildir(), ".gens", "rules", "plugin.ninja.autoupdate")
local dependfile = tmpfile .. ".d"
local lockfile = io.openlock(tmpfile .. ".lock")
if lockfile:trylock() then
local outputdir = "."
local lsp
local sourcefiles = {}
for _, target in pairs(project.targets()) do
table.join2(sourcefiles, target:sourcefiles(), (target:headerfiles()))
local extraconf = target:extraconf("rules", "plugin.ninja.autoupdate")
if extraconf then
outputdir = extraconf.outputdir
lsp = extraconf.lsp
end
end
table.sort(sourcefiles)
depend.on_changed(function()
-- we use task instead of os.exec("xmake") to avoid the project lock
local filename = "build.ninja"
local filepath = outputdir and path.join(outputdir, filename) or filename
task.run("project", { kind = "ninja", outputdir = outputdir, lsp = lsp })
print("[ RUN]: update build.ninja")
end, {
dependfile = dependfile,
files = table.join(project.allfiles(), config.filepath()),
values = sourcefiles
})
lockfile:close()
end
end
update_cmake()
update_makefile()
update_compile_commands()
update_ninja()
end)