-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathTupfile.lua
491 lines (433 loc) · 16.5 KB
/
Tupfile.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
config = {
target = 'native',
debug = true,
optimize = false,
supercharge = false,
sanitize = false,
strict = true,
glfw = true,
luajit = false,
glslang = true,
utf8 = true,
modules = {
audio = true,
data = true,
event = true,
filesystem = true,
graphics = true,
headset = true,
math = true,
physics = true,
system = true,
thread = true,
timer = true
},
headsets = {
simulator = true,
openxr = false,
webxr = false
},
renderers = {
vulkan = true,
webgpu = false
},
spatializers = {
simple = true,
oculus = false,
phonon = false
},
android = {
sdk = '/path/to/sdk',
ndk = '/path/to/ndk',
version = 29,
buildtools = '30.0.3',
keystore = '/path/to/keystore',
keystorepass = 'pass:password',
manifest = nil,
package = 'org.lovr.app',
project = nil
}
}
-- config notes:
-- target can be native or win32/macos/linux/android/wasm
-- supercharge adds dangerous/aggressive optimizations that may reduce stability
-- sanitize adds checks for memory leaks and undefined behavior (reduces performance)
-- strict will make warnings fail the build
-- luajit and headsets.openxr should be a path to a folder with the lib (tup can't build them yet)
-- android.package should be something like 'org.lovr.app'
-- android.project is a path to a lovr project folder that will be included in the apk
-- tup.config can also be used to override properties without modifying this file:
-- create a tup.config file and add lines like CONFIG_<KEY>=<val> to override properties
-- boolean values use y and n, numbers and unquoted strings are also supported
-- subtables should be formatted as CONFIG_MODULE_<m>=y, CONFIG_ANDROID_VERSION=29, etc.
function merge(t, prefix)
for k, v in pairs(t) do
if type(v) == 'table' then
merge(v, k:gsub('s$', ''):upper() .. '_')
else
local str = tup.getconfig((prefix or '') .. k:upper())
if str == 'y' or str == 'n' then
t[k] = str == 'y'
elseif #str > 0 then
t[k] = str
end
end
end
end
merge(config)
---> setup
cc = 'clang'
cxx = 'clang++'
host = tup.getconfig('TUP_PLATFORM'):gsub('macosx', 'macos')
target = config.target == 'native' and host or config.target
flags = {
'-fPIE',
config.debug and '-g' or '',
config.optimize and '-Os' or '',
config.supercharge and '-flto -march=native -DLOVR_UNCHECKED' or '',
config.sanitize and '-fsanitize=address -fsanitize=undefined' or '',
}
cflags = {
'-std=c11 -pedantic',
'-Wall -Wextra -Wno-unused-parameter -Wno-strict-prototypes',
config.strict and '-Werror' or '',
config.optimize and '-fdata-sections -ffunction-sections' or '',
'-fdiagnostics-color=always',
'-fvisibility=hidden',
'-Ietc',
'-Isrc',
'-Isrc/modules',
'-Isrc/lib/std',
'-Ideps/vulkan-headers/include'
}
bin = target == 'android' and 'bin/apk/lib/arm64-v8a' or 'bin'
lflags = '-L' .. bin
lflags += not config.debug and '-Wl,-s' or ''
lflags += config.optimize and (target == 'macos' and '-Wl,-dead_strip' or '-Wl,--gc-sections') or ''
lflags += '-rdynamic'
lflags += config.sanitize and '-lubsan' or ''
if target == 'win32' then
cflags += '-D_CRT_SECURE_NO_WARNINGS'
cflags += '-DWINVER=0x0600' -- Vista
cflags += '-D_WIN32_WINNT=0x0600'
lflags += '-lshell32 -lole32 -luuid'
lflags += config.debug and '-Wl,--subsystem,console' or '-Wl,--subsystem,windows'
if not cc:match('mingw') then
extras += { 'bin/lovr.lib', 'bin/lovr.exp' }
if config.debug then
extras += { 'bin/lovr.pdb', 'bin/lovr.ilk' }
end
end
end
if target == 'macos' then
cflags_os_macos += '-xobjective-c'
lflags += '-Wl,-rpath,@executable_path'
lflags += '-lobjc'
lflags += '-framework AVFoundation'
end
if target == 'linux' then
cflags += '-D_POSIX_C_SOURCE=200809L'
cflags += '-D_DEFAULT_SOURCE'
lflags += '-lm -lpthread -ldl'
lflags += '-Wl,-rpath,\\$ORIGIN'
if config.glfw then
lflags += '-lX11 -lxcb -lX11-xcb'
else
lflags += '-lxcb -lxcb-xinput -lxcb-xkb -lxkbcommon -lxkbcommon-x11'
end
end
if target == 'wasm' then
cc = 'emcc'
cxx = 'em++'
cflags += '-std=gnu11'
cflags += '-D_POSIX_C_SOURCE=200809L'
lflags += '-s FORCE_FILESYSTEM'
exported_functions = { '_main', '_lovrDestroy' }
if config.headsets.webxr then
exported_functions += { '_webxr_attach', '_webxr_detach' }
lflags += '--js-library etc/webxr.js'
end
lflags += '--shell-file etc/lovr.html'
lflags += '-s EXPORTED_FUNCTIONS=' .. table.concat(exported_functions, ',')
extras += { 'bin/lovr.js', 'bin/lovr.wasm' }
if config.renderers.webgpu then
lflags += '-s USE_WEBGPU=1'
end
if config.modules.thread then
cflags += '-s USE_PTHREADS=1'
lflags += '-s USE_PTHREADS=1'
extras += 'bin/lovr.worker.js'
end
end
if target == 'android' then
assert(config.headsets.openxr, 'You probably want to enable OpenXR')
hosts = { win32 = 'windows-x86_64', macos = 'darwin-x86_64', linux = 'linux-x86_64' }
cc = ('%s/toolchains/llvm/prebuilt/%s/bin/clang'):format(config.android.ndk, hosts[host])
cxx = cc .. '++'
flags += '--target=aarch64-linux-android' .. config.android.version
flags += config.debug and '-funwind-tables' or ''
cflags += '-D_POSIX_C_SOURCE=200809L'
cflags += ('-I%s/sources/android/native_app_glue'):format(config.android.ndk)
cflags += '-DLOVR_JAVA_PACKAGE=' .. config.android.package:gsub('%.', '_')
lflags += '-shared -landroid'
end
troublemakers = {
os_android = '-Wno-format-pedantic',
miniaudio = '-Wno-unused-function -Wno-pedantic',
}
for file, flags in pairs(troublemakers) do
_G['cflags_' .. file] = flags
end
---> deps
local function lib(name)
return (target == 'win32' and '$(bin)/%s.dll' or '$(bin)/lib%s.so'):format(name)
end
local function copy(from, to)
tup.rule(from, '^ CP %b^ cp %f %o', to)
end
if config.luajit then
if type(config.luajit) ~= 'string' then
error('Sorry, building LuaJIT is not supported yet. However, you can set config.luajit to a path to a folder containing the LuaJIT library.')
end
cflags += '-Ideps/luajit/src'
lflags += '-L' .. config.luajit
lflags += '-lluajit-5.1'
else
cflags += '-Ideps/lua'
lflags += '-llua'
lua_cflags += '-fPIC'
lua_cflags += '-Wno-empty-body'
lua_lflags += '-shared'
lua_lflags += '-z global'
lua_lflags += '-lm'
lua_src = {
'lapi.c', 'lauxlib.c', 'lbaselib.c', 'lcode.c', 'ldblib.c', 'ldebug.c', 'ldo.c', 'ldump.c',
'lfunc.c', 'lgc.c', 'linit.c', 'liolib.c', 'llex.c', 'lmathlib.c', 'lmem.c', 'loadlib.c',
'lobject.c', 'lopcodes.c', 'loslib.c', 'lparser.c', 'lstate.c', 'lstring.c', 'lstrlib.c',
'ltable.c', 'ltablib.c', 'ltm.c', 'lundump.c', 'lvm.c', 'lzio.c'
}
lua_cflags += ({ linux = '-DLUA_USE_LINUX', android = '-DLUA_USE_LINUX', macos = '-DLUA_USE_MACOSX' })[target] or ''
for i = 1, #lua_src do lua_src[i] = 'deps/lua/' .. lua_src[i] end
tup.foreach_rule(lua_src, '^ CC lua/%b^ $(cc) $(flags) $(lua_cflags) -c %f -o %o', '.obj/lua/%B.o')
tup.rule('.obj/lua/*.o', '^ LD %o^ $(cc) $(flags) -o %o %f $(lua_lflags)', lib('lua'))
end
if config.glfw and (target == 'win32' or target == 'macos' or target == 'linux') then
cflags += '-Ideps/glfw/include'
cflags += '-DLOVR_USE_GLFW'
lflags += '-lglfw'
glfw_cflags += '-fPIC'
glfw_cflags += ({ win32 = '-D_GLFW_WIN32', macos = '-D_GLFW_COCOA', linux = '-D_GLFW_X11' })[target]
glfw_src += { 'init.c', 'context.c', 'platform.c', 'null*.c', 'input.c', 'monitor.c', 'vulkan.c', 'window.c' }
glfw_src += ({
win32 = { 'win32*.c', 'wgl*.c', 'egl*.c', 'osmesa*.c' },
macos = { 'cocoa*.c', 'cocoa*.m', 'posix_module.c', 'posix_thread.c', 'egl*.c', 'nsgl*.m', 'osmesa*.c' },
linux = { 'x11*.c', 'xkb*.c', 'posix*.c', 'glx*.c', 'egl*.c', 'linux*.c', 'osmesa*.c' }
})[target]
for i = 1, #glfw_src do glfw_src[i] = 'deps/glfw/src/' .. glfw_src[i] end
glfw_lflags += '-shared'
glfw_lflags += target == 'win32' and '-lgdi32' or ''
glfw_lflags += target == 'macos' and '-lobjc -framework Cocoa -framework IOKit -framework CoreFoundation' or ''
tup.foreach_rule(glfw_src, '^ CC glfw/%b^ $(cc) $(flags) $(glfw_cflags) -c %f -o %o', '.obj/glfw/%B.o')
tup.rule('.obj/glfw/*.o', '^ LD %o^ $(cc) $(flags) -o %o %f $(glfw_lflags)', lib('glfw'))
end
if config.modules.graphics and config.glslang then
cflags_graphics += '-Ideps/glslang/glslang/Include'
cflags_graphics += '-Ideps/glslang/glslang/Public'
cflags += '-DLOVR_USE_GLSLANG'
lflags += '-lglslang'
glslang_cflags += '-fPIC'
glslang_cflags += '-std=c++17'
glslang_cflags += '-fno-exceptions'
glslang_cflags += '-fno-rtti'
glslang_cflags += '-Ideps/glslang'
glslang_cflags += '-I.obj'
glslang_lflags += '-shared'
glslang_src += 'deps/glslang/glslang/CInterface/*.cpp'
glslang_src += 'deps/glslang/glslang/MachineIndependent/*.cpp'
glslang_src += 'deps/glslang/glslang/MachineIndependent/preprocessor/*.cpp'
glslang_src += ('deps/glslang/glslang/OSDependent/%s/ossource.cpp'):format(target == 'win32' and 'Windows' or 'Unix')
glslang_src += 'deps/glslang/glslang/GenericCodeGen/*.cpp'
glslang_src += 'deps/glslang/SPIRV/GlslangToSpv.cpp'
glslang_src += 'deps/glslang/SPIRV/Logger.cpp'
glslang_src += 'deps/glslang/SPIRV/SpvTools.cpp'
glslang_src += 'deps/glslang/SPIRV/SpvBuilder.cpp'
glslang_src += 'deps/glslang/SPIRV/SpvPostProcess.cpp'
glslang_src += 'deps/glslang/SPIRV/InReadableOrder.cpp'
glslang_src += 'deps/glslang/SPIRV/CInterface/spirv_c_interface.cpp'
glslang_src += 'deps/glslang/glslang/ResourceLimits/resource_limits_c.cpp'
glslang_src += 'deps/glslang/glslang/ResourceLimits/ResourceLimits.cpp'
glslang_src.extra_inputs = '.obj/glslang/build_info.h'
tup.rule('deps/glslang/build_info.h.tmpl', 'python3 deps/glslang/build_info.py deps/glslang -i %f -o %o', '.obj/glslang/build_info.h')
tup.foreach_rule(glslang_src, '^ CC glslang/%b^ $(cc) $(flags) $(glslang_cflags) -c %f -o %o', '.obj/glslang/%B.o')
tup.rule('.obj/glslang/*.o', '^ LD %o^ $(cxx) $(flags) -o %o %f $(glslang_lflags)', lib('glslang'))
end
if config.modules.data then
cflags_rasterizer += '-Ideps/msdfgen'
cflags_rasterizer += '-DMSDFGEN_PUBLIC='
lflags += '-lmsdfgen'
msdfgen_cflags += '-fPIC'
msdfgen_cflags += target == 'win32' and '-DMSDFGEN_PUBLIC=__declspec(dllexport)' or '-DMSDFGEN_PUBLIC='
msdfgen_src += 'deps/msdfgen/core/*.cpp'
tup.foreach_rule(msdfgen_src, '^ CC msdfgen/%b^ $(cxx) $(flags) $(msdfgen_cflags) -c %f -o %o', '.obj/msdfgen/%B.o')
tup.rule('.obj/msdfgen/*.o', '^ LD %o^ $(cxx) $(flags) -shared -o %o %f', lib('msdfgen'))
end
if config.modules.physics then
error('Compiling Jolt is not supported yet')
end
if config.headsets.openxr then
if target == 'android' then
cflags_headset_openxr += '-Ideps/openxr/include'
lflags += '-lopenxr_loader'
copy('deps/oculus-openxr/Libs/Android/arm64-v8a/Release/libopenxr_loader.so', '$(bin)/%b')
else
if type(config.headsets.openxr) ~= 'string' then
error('Sorry, building OpenXR is not supported yet. However, you can set config.headsets.openxr to a path to a folder containing the OpenXR library.')
end
cflags_headset_openxr += '-Ideps/openxr/include'
lflags += '-L' .. config.headsets.openxr
lflags += '-lopenxr_loader'
end
end
if config.spatializers.oculus then
cflags_headset_oculus += '-Ideps/AudioSDK/Include'
ovraudio_libs = {
win32 = 'deps/AudioSDK/Lib/x64/ovraudio64.dll',
linux = 'deps/AudioSDK/Lib/Linux64/libovraudio64.so',
android = 'deps/AudioSDK/Lib/Android/arm64-v8a/libovraudio64.so'
}
assert(ovraudio_libs[target], 'Oculus Audio is not supported on this target')
copy(ovraudio_libs[target], '$(bin)/%b')
end
if config.spatializers.phonon then
cflags_spatializer_phonon += '-Ideps/phonon/include'
phonon_libs = {
win32 = 'deps/phonon/bin/Windows/x64/phonon.dll',
macos = 'deps/phonon/lib/OSX/libphonon.dylib',
linux = 'deps/phonon/lib/Linux/x64/libphonon.so',
android = 'deps/phonon/lib/Android/arm64/libphonon.so'
}
assert(phonon_libs[target], 'Phonon is not supported on this target')
copy(phonon_libs[target], '$(bin)/%b')
end
---> lovr
src = {
'src/main.c',
'src/util.c',
'src/core/fs.c',
('src/core/os_%s.c'):format(target),
'src/core/spv.c',
'src/api/api.c',
'src/api/l_lovr.c'
}
for module, enabled in pairs(config.modules) do
if enabled then
override = {
audio = 'src/modules/audio/audio.c',
headset = 'src/modules/headset/headset.c'
}
src += override[module] or ('src/modules/%s/*.c'):format(module)
src += ('src/api/l_%s*.c'):format(module)
else
cflags += '-DLOVR_DISABLE_' .. module:upper()
end
end
for headset, enabled in pairs(config.headsets) do
if enabled then
cflags += '-DLOVR_USE_' .. headset:upper()
src += ('src/modules/headset/headset_%s.c'):format(headset)
end
end
for renderer, enabled in pairs(config.renderers) do
if enabled then
cflags += '-DLOVR_' .. ({ vulkan = 'VK', webgpu = 'WEBGPU' })[renderer]
src += 'src/core/gpu_' .. ({ vulkan = 'vk', webgpu = 'web' })[renderer] .. '.c'
end
end
for spatializer, enabled in pairs(config.spatializers) do
if enabled then
cflags += ('-DLOVR_ENABLE_%s_SPATIALIZER'):format(spatializer:upper())
src += ('src/modules/audio/spatializer_%s.c'):format(spatializer)
end
end
if config.utf8 then
src += 'src/lib/luax/lutf8lib.c'
else
cflags += '-DLOVR_DISABLE_UTF8'
end
src += 'src/lib/stb/*.c'
src += 'src/lib/miniz/*.c'
src += (config.modules.audio or config.modules.data) and 'src/lib/miniaudio/*.c' or nil
src += config.modules.data and 'src/lib/jsmn/*.c' or nil
src += config.modules.data and 'src/lib/minimp3/*.c' or nil
src += config.modules.filesystem and 'src/lib/dmon/*.c' or nil
src += config.modules.math and 'src/lib/noise/*.c' or nil
src += config.modules.thread and 'src/core/job.c' or nil
-- embed resource files with xxd
res = { 'etc/boot.lua', 'etc/*.ttf', 'etc/shaders/*.glsl' }
tup.foreach_rule(res, '^ XD %b^ xxd -i %f > %o', '%f.h')
for i, pattern in ipairs(res) do
src.extra_inputs += pattern .. '.h'
end
-- shaders
vert = 'etc/shaders/*.vert'
frag = 'etc/shaders/*.frag'
comp = 'etc/shaders/*.comp'
glslang_flags += '--quiet'
glslang_flags += config.debug and '-gVS' or ''
glslang_flags += '--target-env vulkan1.1'
function compileShaders(stage)
pattern = 'etc/shaders/*.' .. stage
tup.foreach_rule(pattern, 'glslangValidator $(glslang_flags) --vn lovr_shader_%B_' .. stage .. ' -o %o %f', '%f.h')
end
compileShaders('vert')
compileShaders('frag')
compileShaders('comp')
src.extra_inputs += 'etc/shaders/*.h'
-- compile
tup.foreach_rule(src, '^ CC %b^ $(cc) $(flags) $(cflags) $(cflags_%B) -o %o -c %f', '.obj/%B.o')
-- link final output
outputs = {
win32 = '$(bin)/lovr.exe',
macos = '$(bin)/lovr',
linux = '$(bin)/lovr',
android = '$(bin)/liblovr.so',
wasm = '$(bin)/lovr.html'
}
obj = { '.obj/*.o', extra_inputs = lib('*') }
output = { outputs[target], extra_outputs = extras }
tup.rule(obj, '^ LD %o^ $(cc) $(flags) -o %o %f $(lflags)', output)
---> apk
if target == 'android' then
for _, key in pairs({ 'manifest', 'package', 'project' }) do
local value = tup.getconfig('ANDROID_' .. key:upper())
config.android[key] = #value > 0 and value or config.android[key]
end
java = 'bin/Activity.java'
class = config.android.package:gsub('%.', '/') .. '/Activity.class'
binclass = 'bin/' .. class
jar = 'bin/lovr.jar'
dex = 'bin/apk/classes.dex'
unaligned = 'bin/.lovr.apk.unaligned'
unsigned = 'bin/.lovr.apk.unsigned'
apk = 'bin/lovr.apk'
manifest = config.android.manifest or 'etc/AndroidManifest.xml'
package = '--rename-manifest-package ' .. config.android.package
project = config.android.project and #config.android.project > 0 and ('-A ' .. config.android.project) or ''
version = config.android.version
ks = config.android.keystore
kspass = config.android.keystorepass
androidjar = ('%s/platforms/android-%d/android.jar'):format(config.android.sdk, version)
tools = config.android.sdk .. '/build-tools/' .. config.android.buildtools
copy(manifest, 'bin/AndroidManifest.xml')
tup.rule('etc/Activity.java', 'tup varsed %f %o', java)
tup.rule(java, '^ JAVAC %b^ javac -classpath $(androidjar) -d bin %f', binclass)
tup.rule(binclass, '^ JAR %b^ jar -cf %o -C bin $(class)', jar)
tup.rule(jar, '^ D8 %b^ $(tools)/d8 --min-api $(version) --output bin/apk %f', dex)
tup.rule(
{ 'bin/AndroidManifest.xml', extra_inputs = { lib('*'), dex } },
'^ AAPT %b^ $(tools)/aapt package $(package) -F %o -M %f -0 so -I $(androidjar) $(project) bin/apk',
unaligned
)
tup.rule(unaligned, '^ ZIPALIGN %f^ $(tools)/zipalign -f -p 4 %f %o', unsigned)
tup.rule(unsigned, '^ APKSIGNER %o^ $(tools)/apksigner sign --ks $(ks) --ks-pass $(kspass) --out %o %f', { apk, extra_outputs = 'bin/lovr.apk.idsig' })
end