-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmake.lua
334 lines (277 loc) · 9.54 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
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
-- Copyright (C) 2024 kanel
-- This file is part of "kanel-CLI"
-- For conditions of distribution and use, see copyright notice in LICENSE
-- Credits to SirLynix (https://github.com/SirLynix) for this xmake.lua
-- Inspired by https://github.com/NazaraEngine/NazaraEngine
set_version("0.0.1beta")
add_repositories("local-repo Xmake")
add_repositories("nazara-engine-repo https://github.com/NazaraEngine/xmake-repo")
set_languages("clatest", "cxx20")
add_rules("mode.debug", "mode.release")
set_allowedplats("windows", "mingw", "linux", "macosx", "wasm")
set_allowedmodes("debug", "release")
set_defaultmode("release")
set_project("kanelcli")
if is_mode("debug") then
add_defines("KANEL_CLI_DEBUG")
elseif is_mode("release") then
add_defines("KANEL_CLI_RELEASE")
set_fpmodels("precise")
add_vectorexts("sse", "sse2", "sse3", "ssse3")
add_vectorexts("mmx")
add_vectorexts("neon")
add_vectorexts("avx", "avx2", "avx512")
end
add_includedirs("Kanel/Runtime/Includes")
add_includedirs("Kanel/Build")
set_objectdir("build/Objs/$(os)_$(arch)")
set_targetdir("build/Bin/$(os)_$(arch)")
set_rundir("build/Bin/$(os)_$(arch)")
set_dependir("build/.deps")
set_optimize("fastest")
set_configdir("Kanel/Build/")
add_configfiles("Kanel/Build/Config.h.in")
local gpu_backends = {
Vulkan = {
option = "vulkan",
deps = {"kanel_gpu"},
packages = {"nzsl", "vulkan-headers", "vulkan-memory-allocator"},
dir = "GPU/",
custom = function()
add_defines("VK_NO_PROTOTYPES")
end
}
}
local modules = {
GPU = {
option = "gpu",
deps = {},
overrideDir = "GPU/RHI/",
custom = function()
if has_config("embed_gpu_backends", "static") then
add_defines("KANEL_CLI_EMBEDDED_RHI_BACKENDS")
for name, module in table.orderpairs(gpu_backends) do
if not module.option or has_config(module.option) then
ModuleTargetConfig(name, module)
end
end
end
end
},
}
local sanitizers = {
asan = "address",
lsan = "leak",
tsan = "thread",
}
for opt, policy in table.orderpairs(sanitizers) do
option(opt, { description = "Enable " .. opt, default = false })
if has_config(opt) then
set_policy("build.sanitizer." .. policy, true)
end
end
if is_plat("wasm") then
gpu_backends.Vulkan = nil
end
if not has_config("embed_gpu_backends", "static") then
-- Register renderer backends as separate modules
for name, module in pairs(gpu_backends) do
if (modules[name] ~= nil) then
os.raise("overriding module " .. name)
end
modules[name] = module
end
end
for name, module in table.orderpairs(modules) do
if module.option then
option(module.option, { description = "Enables the " .. name .. " module", default = true, category = "Modules" })
end
end
includes("Xmake/actions/**.lua")
add_rules("build.gpu_plugins")
option("compile_shaders", { description = "Compile nzsl shaders into an includable binary version", default = true })
option("static", { description = "Build the engine statically (implies embed_gpu_backends)", default = is_plat("wasm") or false })
option("embed_gpu_backends", { description = "Embed GPU backend code into libkanelcli_gpu instead of loading them dynamically", default = is_plat("wasm") or false })
option("unitybuild", { description = "Build the engine using unity build", default = false })
add_requires("vrg")
if has_config("vulkan") and not is_plat("wasm") then
add_requires("vulkan-headers", "vulkan-memory-allocator")
add_requires("nzsl >=2023.12.31", { debug = is_mode("debug"), configs = { symbols = not is_mode("release"), shared = false } })
-- When cross-compiling, compile shaders using host shader compiler
if has_config("compile_shaders") and is_cross() then
add_requires("nzsl~host", { kind = "binary", host = true })
end
end
function ModuleTargetConfig(name, module)
add_defines("KANEL_CLI_" .. name:upper() .. "_BUILD")
if is_mode("debug") then
add_defines("KANEL_CLI_" .. name:upper() .. "_DEBUG")
end
if is_plat("wasm") or has_config("static") then
add_defines("KANEL_CLI_".. name:upper() .. "_STATIC", { public = true })
end
-- Add header and source files
for _, ext in ipairs({".h", ".inl"}) do
if module.overrideDir then
add_headerfiles("Kanel/Runtime/Includes/Modules/" .. module.overrideDir .. "/**" .. ext)
add_headerfiles("Kanel/Runtime/Sources/Modules/" .. module.overrideDir .. "/**" .. ext, { prefixdir = "private", install = false })
elseif module.dir then
add_headerfiles("Kanel/Runtime/Includes/Modules/" .. module.dir .. name .. "/**" .. ext)
add_headerfiles("Kanel/Runtime/Sources/Modules/" .. module.dir .. name .. "/**" .. ext, { prefixdir = "private", install = false })
else
add_headerfiles("Kanel/Runtime/Includes/Modules/" .. name .. "/**" .. ext)
add_headerfiles("Kanel/Runtime/Sources/Modules/" .. name .. "/**" .. ext, { prefixdir = "private", install = false })
end
end
if module.overrideDir then
remove_headerfiles("Kanel/Runtime/Sources/Modules/" .. module.overrideDir .. "/Resources/**.h")
elseif module.dir then
remove_headerfiles("Kanel/Runtime/Sources/Modules/" .. module.dir .. name .. "/Resources/**.h")
else
remove_headerfiles("Kanel/Runtime/Sources/Modules/" .. name .. "/Resources/**.h")
end
-- Add extra files for projects
for _, ext in ipairs({".nzsl"}) do
if module.overrideDir then
add_extrafiles("Kanel/Runtime/Includes/Modules/" .. module.overrideDir .. "/**" .. ext)
add_extrafiles("Kanel/Runtime/Sources/Modules/" .. module.overrideDir .. "/**" .. ext)
elseif module.dir then
add_extrafiles("Kanel/Runtime/Includes/Modules/" .. module.dir .. name .. "/**" .. ext)
add_extrafiles("Kanel/Runtime/Sources/Modules" .. module.dir .. name .. "/**" .. ext)
else
add_extrafiles("Kanel/Runtime/Includes/Modules/" .. name .. "/**" .. ext)
add_extrafiles("Kanel/Runtime/Sources/Modules" .. name .. "/**" .. ext)
end
end
if module.packages then
add_packages(table.unpack(module.packages))
end
if module.publicPackages then
for _, pkg in ipairs(module.publicPackages) do
add_packages(pkg, { public = true })
end
end
if module.deps then
local moduleName = "kanelcli_" .. name
table.remove_if(module.deps, function(dep)
return module.deps[dep] == moduleName
end)
if #module.deps > 0 then
add_deps(table.unpack(module.deps))
end
end
if module.overrideDir then
add_files("Kanel/Runtime/Sources/Modules/" .. module.overrideDir .. "/**.c")
add_files("Kanel/Runtime/Sources/Modules/" .. module.overrideDir .. "/**.cpp")
elseif module.dir then
add_files("Kanel/Runtime/Sources/Modules/" .. module.dir .. name .. "/**.c")
add_files("Kanel/Runtime/Sources/Modules/" .. module.dir .. name .. "/**.cpp")
else
add_files("Kanel/Runtime/Sources/Modules/" .. name .. "/**.c")
add_files("Kanel/Runtime/Sources/Modules/" .. name .. "/**.cpp")
end
if has_config("compile_shaders") then
add_rules("@nzsl/compile.shaders", { inplace = true })
local dir = ""
if module.overrideDir then
dir = "Kanel/Runtime/Sources/Modules/" .. module.overrideDir
elseif module.dir then
dir = "Kanel/Runtime/Sources/Modules/" .. module.dir .. name
else
dir = "Kanel/Runtime/Sources/Modules/" .. name
end
for _, filepath in pairs(table.join(os.files(dir .. "/Resources/**.nzsl"), os.files(dir .. "/Resources/**.nzslb"))) do
add_files(filepath)
end
end
if module.custom then
module.custom()
end
end
for name, module in pairs(modules) do
if module.option and not has_config(module.option) then
goto continue
end
target("kanel_" .. name:lower(), function()
set_group("Modules")
-- handle shared/static kind
if is_plat("wasm") or has_config("static") then
set_kind("static")
else
set_kind("shared")
add_cflags("-fPIC")
end
add_ldflags("-Wl,--export-dynamic")
add_includedirs("Kanel/Runtime/Sources")
add_rpathdirs("$ORIGIN")
if has_config("unitybuild") then
add_rules("c.unity_build", { batchsize = 12 })
end
on_clean(function(target)
if target:objectfiles() then
for _, file in ipairs(target:objectfiles()) do
if os.exists(file) then
print("Removing " .. file)
os.rm(file)
end
end
end
if target:targetfile() and os.exists(target:targetfile()) then
print("Removing " .. target:targetfile())
os.rm(target:targetfile())
end
end)
ModuleTargetConfig(name, module)
end)
::continue::
end
target("kanel_cli")
set_kind("binary")
add_includedirs("Kanel/Runtime/Includes")
add_includedirs("Kanel/Runtime/Sources")
add_rpathdirs("$ORIGIN")
add_ldflags("-Wl,--export-dynamic")
if is_plat("linux") then
add_syslinks("dl")
end
add_packages("vrg")
if has_config("unitybuild") then
add_rules("c.unity_build", { batchsize = 0 })
end
for _, dir in ipairs(os.dirs("Kanel/Runtime/Sources/*")) do
if dir ~= "Kanel/Runtime/Sources/Modules" then
add_files(dir .. "/**.c", { unity_group = dir })
add_files(dir .. "/**.cpp")
end
end
on_clean(function(target)
if target:objectfiles() then
for _, file in ipairs(target:objectfiles()) do
if os.exists(file) then
print("Removing " .. file)
os.rm(file)
end
end
end
if target:targetfile() and os.exists(target:targetfile()) then
print("Removing " .. target:targetfile())
os.rm(target:targetfile())
end
end)
target_end()
rule("build.gpu_plugins")
on_load(function(target)
if has_config("static") then
return
end
local deps = table.wrap(target:get("deps"))
if target:kind() == "binary" and table.contains(deps, "kanelcli_gpu") then
for name, _ in pairs(gpu_backends) do
local depName = "kanelcli_" .. name:lower()
if not table.contains(deps, depName) then -- don't overwrite dependency
target:add("deps", depName, {inherit = false})
end
end
end
end)
rule_end()