2025-06-20 15:10:02 +10:00
parent 912d5cb768
commit 62b1e79ec5
15 changed files with 522 additions and 0 deletions

98
buildscripts/tests.lua Normal file
View File

@@ -0,0 +1,98 @@
local vulkan_sdk_dir = os.getenv("VULKAN_SDK")
local args = {...}
args[1] = args[1] or "basic"
if type(args[1]) ~= "string" or not fs.is_dir("tests/"..args[1]) then args[1] = "basic" end
local output_name = args[1] .. '.exe'
local include_dirs = "-I\"" .. table.concat({
"include",
"thirdparty/glfw/include",
vulkan_sdk_dir .. "/Include",
vulkan_sdk_dir .. "/Include/vulkan",
"tests/" .. args[1] .. '/include'
}, "\" -I\"") .. '"'
local src_dirs = {
"tests/" .. args[1] .. "/src"
}
local obj_dirs = {
"tests/" .. args[1] .. "/obj"
}
local compiler_flags = table.concat({
"-c",
"-g",
"-O0"
}, ' ')
local library_dirs = '-L\"' .. table.concat({
"bin/",
"thirdparty/glfw/bin/src",
vulkan_sdk_dir .. "/Lib"
}, '\" -L\"') .. '"'
local library_linker_inputs = '-l' .. table.concat({
"basalt",
"glfw3",
"vulkan-1",
-- Windows
"msvcrtd",
"user32",
"gdi32",
"shell32"
-- Linux
}, ' -l')
local linker_flags = table.concat({
}, ' ')
local shader_src_dir = "bin/shaders/src"
local shader_out_dir = "bin/shaders/out"
if include_dirs == "-I\"\"" then include_dirs = "" end
if library_dirs == "-L\"\"" then library_dirs = "" end
if library_linker_inputs == "-l" then library_linker_inputs = "" end
local cpp_extensions = {
['.cpp']=true,
['.hpp']=true
}
local compiler = require("buildscripts.helpers.compiler");
-- NOTE: This is not scalable
-- It does not care what source folder the object comes from
local function srcobj_mapping(src_file, obj_file)
local base = "./tests/".. args[1]
-- Return the object file that the source file should be associated with
if (src_file ~= nil and obj_file == nil) then
-- Make the source file relative to the test root directory'
local file = src_file .. '.o'
return base .. '/obj/' .. fs.relative_to(file, base)
-- Return the source files associated with an object file
elseif (src_file == nil and obj_file ~= nil) then
return {base .. '/' .. obj_file:sub(#base+6, -3)}
else error("Invalid usage of srcobj mapping function: type(src_file) = " .. type(src_file) .. ", type(obj_file) = " .. type(obj_file)) end
end
-- Get translation units
local source_files = compiler.get_newer_src_files(src_dirs, cpp_extensions, srcobj_mapping)
local translation_units = compiler.generate_translation_units(source_files, srcobj_mapping)
-- Compile the binary
compiler.print_errors(compiler.compile_translation_units("clang++.exe", 4, translation_units, compiler_flags, include_dirs))
-- Link binary
compiler.print_errors(compiler.link_translation_units("clang++.exe", obj_dirs, {['.o']=true}, "-o bin/" .. output_name, library_dirs, library_linker_inputs, linker_flags))
-- Build shaders
local function shader_srcobj_mapping(src_file, obj_file)
if (src_file ~= nil and obj_file == nil) then
return fs.dir(src_file):sub(1,-5) .. 'out/' .. fs.file_name(src_file) .. '.spv'
elseif (src_file == nil and obj_file ~= nil) then
return { fs.dir(obj_file):sub(1,-5) .. 'src/' .. fs.file_name(obj_file):sub(1,-#".spv"-1) }
else error("Invalid usage of srcobj mapping function for shader compilation") end
end
-- Get source files
local shader_sources = compiler.get_newer_src_files(shader_src_dir, {[".frag"]=true,[".vert"]=true,[".glsl"]=true}, shader_srcobj_mapping)
local shader_translation_units = compiler.generate_translation_units(shader_sources, shader_srcobj_mapping)
compiler.print_errors(compiler.compile_translation_units("glslc", 4, shader_translation_units))