Updated lbs to use new uniproc library

Fixed bug in fs.list_alldirs where it used directory_iterator rather that recursive_directory_iterator
Fixed memory/type bugs in lua_platform.cpp
Updated lbs.lua to reflect this
This commit is contained in:
2025-03-06 19:27:19 +11:00
parent 40ea3caf2a
commit 68e2c49c8f
6 changed files with 80 additions and 47 deletions

1
.gitignore vendored
View File

@@ -15,3 +15,4 @@
/CppProperties.json /CppProperties.json
/lbs.exe /lbs.exe
/build/lbs.ilk /build/lbs.ilk
/obj/thirdparty/uniproc/src

40
lbs.lua
View File

@@ -47,7 +47,8 @@ local function get_required_objects(dirs, src_extension, obj_prefix, obj_postfix
obj_postfix = obj_postfix or ".o" obj_postfix = obj_postfix or ".o"
local objs = {} local objs = {}
local new_src = {} local new_src = {}
for fsobj in fs.forall(dirs) do for _,dir in pairs(dirs) do
for fsobj in fs.forall(dir) do
-- Get all files in the directory -- Get all files in the directory
local src_files local src_files
if fs.is_dir(fsobj) then src_files = fs.list_alldirs(fsobj) if fs.is_dir(fsobj) then src_files = fs.list_alldirs(fsobj)
@@ -58,12 +59,13 @@ local function get_required_objects(dirs, src_extension, obj_prefix, obj_postfix
if (fs.extension(src_file) ~= src_extension) then break end if (fs.extension(src_file) ~= src_extension) then break end
local obj_file = obj_prefix .. src_file .. obj_postfix local obj_file = obj_prefix .. src_file .. obj_postfix
-- Skip old files that do not need to be recompiled -- Skip old files that do not need to be recompiled
if not fs.is_newer(src_file) then break end if not fs.is_newer(src_file, obj_file) then break end
-- Add the src and obj file to a table that will be returned -- Add the src and obj file to a table that will be returned
objs[#objs+1] = obj_file objs[#objs+1] = obj_file
new_src[#new_src+1] = src_file new_src[#new_src+1] = src_file
until true end until true end
end end
end
return objs, new_src return objs, new_src
end end
@@ -84,37 +86,59 @@ local function compile(CXX, src_dirs, include_dirs, defines, additional_argument
argv[#argv+1] = args argv[#argv+1] = args
end end
print("Compiling...")
local ec, stdout, stderr = platform.exec_parallel(CXX, config.max_parallel, argv) local ec, stdout, stderr = platform.exec_parallel(CXX, config.max_parallel, argv)
for i,ec in pairs(ec) do for i,ec in pairs(ec) do
if (ec ~= 0) then if (ec ~= 0) then
print("Command \"" .. CXX .. " " .. table.concat(argv[i], " ") .. "\" failed with exit code " .. tostring(ec) .. " and output;") print("Command ".. tostring(i) .." \"" .. CXX .. " " .. table.concat(argv[i], " ") .. "\" failed with exit code " .. tostring(ec) .. " and output;")
if (#stderr ~= 0) then print(stderr[i]) else print(stdout[i]) end print(stderr[i])
print(stdout[i])
print() print()
end end
end end
end end
local function link(LNK, obj_dirs, library_dirs, linker_inputs, additional_arguments) local function link(LNK, obj_dirs, library_dirs, linker_inputs, additional_arguments, file_output)
local argv = {} local argv = {}
for _,v in pairs(library_dirs) do argv[#argv+1] = "-L\"" .. v .. "\"" end for _,v in pairs(library_dirs) do argv[#argv+1] = "-L\"" .. v .. "\"" end
for _,v in pairs(linker_inputs) do argv[#argv+1] = "-l\"" .. v .. "\"" end for _,v in pairs(linker_inputs) do argv[#argv+1] = "-l\"" .. v .. "\"" end
for _,v in pairs(additional_arguments) do argv[#argv+1] = v end for _,v in pairs(additional_arguments) do argv[#argv+1] = v end
for _,obj_dir in pairs(obj_dirs) do
local obj_files = fs.list_alldirs("obj")
for _,obj_file in pairs(obj_files) do repeat
if fs.extension(obj_file) ~= ".o" then break end
argv[#argv+1] = obj_file
until true end
end
argv[#argv+1] = "-o " .. file_output
print("Linking...")
local ec, stdout, stderr = platform.exec(LNK, argv) local ec, stdout, stderr = platform.exec(LNK, argv)
if (ec ~= 0) then if (ec ~= 0) then
print("Command \"" .. LNK .. " " .. table.concat(argv) .. "\" failed with exit code " .. tostring(ec) .. " and output;") print("Command \"" .. LNK .. " " .. table.concat(argv, " ") .. "\" failed with exit code " .. tostring(ec) .. " and output;")
if (#stderr ~= 0) then print(stderr[i]) else print(stdout[i]) end print(stderr)
print(stdout)
print() print()
end end
end end
function build(...) function build(...)
local args = {...} local args = {...}
local program_name = program_output
for section in config.target:gmatch("[^-]+") do
if section == "windows" then program_name = program_name .. ".exe" end
end
config = load_config(config, args) config = load_config(config, args)
print("Building " .. config.buildmode .. " with up to " .. tostring(config.max_parallel) .. " concurrent jobs") print("Building " .. config.buildmode .. " with up to " .. tostring(config.max_parallel) .. " concurrent jobs")
compile(CXX, source_dirs, include_dirs, defines, additional_arguments) compile(CXX, source_dirs, include_dirs, defines, additional_arguments)
link(CXX, "obj", library_dirs, linker_inputs, {})
link(CXX, {"obj"}, library_dirs, linker_inputs, {}, program_name)
end end
function main(...) build(...) end
return 0 return 0

View File

@@ -9,6 +9,7 @@ DEBUG_DEFINES :=
DEFINES := DEFINES :=
CXX_FLAGS := -std=c11 -std=c++20 -g -O0 CXX_FLAGS := -std=c11 -std=c++20 -g -O0
CC_FLAGS := -std=c11 -g -O0
CXX_DEBUG_FLAGS := CXX_DEBUG_FLAGS :=
LNK_FLAGS := -g -O0 LNK_FLAGS := -g -O0
LNK_DEBUG_FLAGS := LNK_DEBUG_FLAGS :=
@@ -42,21 +43,24 @@ endif
clean: clean:
$(call RM,$(call rwildcard, obj,*.o) build/$(OUTPUT_NAME)) $(call RM,$(call rwildcard, obj,*.o) build/$(OUTPUT_NAME))
SOURCE_FILES := $(call rwildcard,src,*.cpp,*.hpp) $(call rwildcard,thirdparty/uniproc/src,*.cpp,*.hpp) SOURCE_FILES := $(call rwildcard,src,*.cpp,*.hpp) $(call rwildcard,thirdparty/uniproc/src,*.c)
OBJECT_FILES := $(foreach d,$(addsuffix .o,$(SOURCE_FILES)), obj/$(d)) OBJECT_FILES := $(foreach d,$(addsuffix .o,$(SOURCE_FILES)), obj/$(d))
obj/thirdparty/uniproc/%.c.o: thirdparty/uniproc/src/%.c obj/thirdparty/uniproc/src/%.c.o: thirdparty/uniproc/src/%.c
$(CC) -c $(CXX_FLAGS) $(foreach inc,$(INCLUDES),-I$(inc)) $(foreach def,$(DEFINES),-D$(def)) $< -o $@ $(CC) -c $(CC_FLAGS) $(foreach inc,$(INCLUDES),-I$(inc)) $(foreach def,$(DEFINES),-D$(def)) $< -o $@
obj/src/%.cpp.o: src/%.cpp obj/src/%.cpp.o: src/%.cpp
$(CXX) -c $(CXX_FLAGS) $(foreach inc,$(INCLUDES),-I$(inc)) $(foreach def,$(DEFINES),-D$(def)) $< -o $@ $(CXX) -c $(CXX_FLAGS) $(foreach inc,$(INCLUDES),-I$(inc)) $(foreach def,$(DEFINES),-D$(def)) $< -o $@
obj/src/%.hpp.o: src/%.hpp
link_lbs: $(OBJECT_FILES) link_lbs: $(OBJECT_FILES)
$(CXX) $(LNK_FLAGS) $(LNK_FLAGS) $(foreach lib,$(LIBRARY_DIRECTORIES),-L$(lib)) $(foreach lnk,$(LINKER_INPUTS),-l$(lnk)) $^ -o build/$(OUTPUT_NAME) $(CXX) $(LNK_FLAGS) $(LNK_FLAGS) $(foreach lib,$(LIBRARY_DIRECTORIES),-L$(lib)) $(foreach lnk,$(LINKER_INPUTS),-l$(lnk)) $^ -o build/$(OUTPUT_NAME)
build_deps: .make/thirdparty_luajit build_deps: .make/thirdparty_luajit .make/thirdparty_uniproc
.make/thirdparty_luajit: .make/thirdparty_luajit:
git submodule update --init --recursive git submodule update --init --recursive --remote
cd thirdparty/luajit/src $(PLATFORM_SEPERATOR) $(LUAJUT_BUILD_CMD) cd thirdparty/luajit/src $(PLATFORM_SEPERATOR) $(LUAJUT_BUILD_CMD)
$(call touch,".make/thirdparty_luajit") $(call touch,".make/thirdparty_luajit")
.make/thirdparty_uniproc:
git submodule update --init --recursive --remote

View File

@@ -94,11 +94,12 @@ int lua_fs_list_alldirs(lua_State* L)
const char* fpath = lua_tostring(L, 1); const char* fpath = lua_tostring(L, 1);
lua_newtable(L); lua_newtable(L);
uint32_t i = 1; uint32_t i = 1;
for (const std::filesystem::path& p : std::filesystem::directory_iterator(fpath)) for (const std::filesystem::path& p : std::filesystem::recursive_directory_iterator(fpath))
{ {
const std::string x = p.string(); const std::string x = p.string();
lua_pushlstring(L, x.c_str(), x.size()); lua_pushlstring(L, x.c_str(), x.size());
lua_rawseti(L, -2, i); lua_rawseti(L, -2, i);
i++;
} }
return 1; return 1;
} }

View File

@@ -4,7 +4,7 @@
#include "mutex" #include "mutex"
#include <chrono> #include <chrono>
#include "simple_string.h" #include "simple_string.h"
#include "uniproc/include/uniproc.h" #include "uniproc.h"
int luaopen_platform(lua_State* L) int luaopen_platform(lua_State* L)
{ {
@@ -26,12 +26,12 @@ int lua_platform_exec(lua_State* L)
luaL_argcheck(L, lua_istable(L, 2), 2, "Expected second argument to be a table of strings"); luaL_argcheck(L, lua_istable(L, 2), 2, "Expected second argument to be a table of strings");
size_t nargs = lua_objlen(L, 2); size_t nargs = lua_objlen(L, 2);
char** argv = malloc(nargs * sizeof(char*)); const char** argv = (const char**)malloc(nargs * sizeof(const char*));
for (size_t i = 0; i < nargs; ++i) for (size_t i = 0; i < nargs; ++i)
{ {
lua_rawgeti(L, -1, i + 1); lua_rawgeti(L, 2, i+1);
if (!lua_isstring(L, -1)) if (lua_type(L, -1) != LUA_TSTRING)
return luaL_error(L, "Expected all values in argument table to be a string. Index %d was a %s", i, lua_typename(L, lua_type(L, -1))); return luaL_error(L, "Expected all values in argument table to be a string. Index %d was a %s", i+1, lua_typename(L, lua_type(L, -1)));
argv[i] = lua_tostring(L, -1); argv[i] = lua_tostring(L, -1);
} }
@@ -72,7 +72,8 @@ int lua_platform_exec_parallel(lua_State* L)
const size_t max_parallel = lua_tointeger(L, 2); const size_t max_parallel = lua_tointeger(L, 2);
const size_t num_cmds = lua_objlen(L, 3); const size_t num_cmds = lua_objlen(L, 3);
const size_t* num_args = (size_t*)malloc(num_cmds * sizeof(size_t)); size_t* num_args = (size_t*)malloc(num_cmds * sizeof(size_t));
memset(num_args, 0, sizeof(size_t) * num_cmds);
const char*** args = (const char***)malloc(num_cmds*sizeof(const char**)); const char*** args = (const char***)malloc(num_cmds*sizeof(const char**));
// Generate the command stack from lua arguments // Generate the command stack from lua arguments
@@ -81,9 +82,10 @@ int lua_platform_exec_parallel(lua_State* L)
lua_rawgeti(L, 3, i + 1); lua_rawgeti(L, 3, i + 1);
if (lua_istable(L, -1)) if (lua_istable(L, -1))
{ {
const size_t num_args = lua_objlen(L, -1); const size_t tbl_num_args = lua_objlen(L, -1);
args[i] = (const char**)malloc(num_args*sizeof(const char*)); num_args[i] = tbl_num_args;
for (size_t j = 0; j < num_args; ++j) args[i] = (const char**)malloc(tbl_num_args*sizeof(const char*));
for (size_t j = 0; j < tbl_num_args; ++j)
{ {
lua_rawgeti(L, -1, j + 1); lua_rawgeti(L, -1, j + 1);
if (!lua_isstring(L, -1)) if (!lua_isstring(L, -1))
@@ -94,6 +96,7 @@ int lua_platform_exec_parallel(lua_State* L)
} }
else if (lua_type(L, -1) == LUA_TSTRING) else if (lua_type(L, -1) == LUA_TSTRING)
{ {
num_args[i] = 1;
args[i] = (const char**)malloc(1*sizeof(const char*)); args[i] = (const char**)malloc(1*sizeof(const char*));
args[i][0] = lua_tostring(L, -1); args[i][0] = lua_tostring(L, -1);
} }
@@ -123,12 +126,12 @@ int lua_platform_exec_parallel(lua_State* L)
{ {
char buf[64]; char buf[64];
// Read the std. streams into the output vectors // Read the std. streams into the output vectors
while (fgets(buf, sizeof(buf), p.out) != NULL) while (fgets(buf, sizeof(buf), p->out) != NULL)
std_outs[p.userdata].append(buf); std_outs[p->userdata].append(buf);
while (fgets(buf, sizeof(buf), p.err) != NULL) while (fgets(buf, sizeof(buf), p->err) != NULL)
std_errs[p.userdata].append(buf); std_errs[p->userdata].append(buf);
// Add the process return code to retcode array // Add the process return code to retcode array
uniproc_await_processes(p, retcodes + p.userdata, 1); uniproc_await_processes(p, retcodes + p->userdata, 1);
// Close the process and zero it // Close the process and zero it
uniproc_close_process(p); uniproc_close_process(p);
} }
@@ -139,7 +142,7 @@ int lua_platform_exec_parallel(lua_State* L)
{ {
// Create process // Create process
*p = uniproc_create_process(cmd, num_args[cmd_idx], args[cmd_idx]); *p = uniproc_create_process(cmd, num_args[cmd_idx], args[cmd_idx]);
uniproc_set_userdata(p, num_cmds); p->userdata = cmd_idx;
cmd_idx++; cmd_idx++;
// If we failed to create the process, set the output for it to an error value // If we failed to create the process, set the output for it to an error value