diff --git a/.gitignore b/.gitignore index 60fe2a5..9c9b6c3 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ /CppProperties.json /lbs.exe /build/lbs.ilk +/obj/thirdparty/uniproc/src diff --git a/lbs.lua b/lbs.lua index 38e7783..3226b5e 100644 --- a/lbs.lua +++ b/lbs.lua @@ -47,22 +47,24 @@ local function get_required_objects(dirs, src_extension, obj_prefix, obj_postfix obj_postfix = obj_postfix or ".o" local objs = {} local new_src = {} - for fsobj in fs.forall(dirs) do - -- Get all files in the directory - local src_files - if fs.is_dir(fsobj) then src_files = fs.list_alldirs(fsobj) - else src_files = {fsobj} end - -- For each of the files, - for _, src_file in pairs(src_files) do repeat - -- Skip files that don't end in the provided extension - if (fs.extension(src_file) ~= src_extension) then break end - local obj_file = obj_prefix .. src_file .. obj_postfix - -- Skip old files that do not need to be recompiled - if not fs.is_newer(src_file) then break end - -- Add the src and obj file to a table that will be returned - objs[#objs+1] = obj_file - new_src[#new_src+1] = src_file - until true end + for _,dir in pairs(dirs) do + for fsobj in fs.forall(dir) do + -- Get all files in the directory + local src_files + if fs.is_dir(fsobj) then src_files = fs.list_alldirs(fsobj) + else src_files = {fsobj} end + -- For each of the files, + for _, src_file in pairs(src_files) do repeat + -- Skip files that don't end in the provided extension + if (fs.extension(src_file) ~= src_extension) then break end + local obj_file = obj_prefix .. src_file .. obj_postfix + -- Skip old files that do not need to be recompiled + 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 + objs[#objs+1] = obj_file + new_src[#new_src+1] = src_file + until true end + end end return objs, new_src end @@ -84,37 +86,59 @@ local function compile(CXX, src_dirs, include_dirs, defines, additional_argument argv[#argv+1] = args end + print("Compiling...") local ec, stdout, stderr = platform.exec_parallel(CXX, config.max_parallel, argv) for i,ec in pairs(ec) do if (ec ~= 0) then - print("Command \"" .. 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("Command ".. tostring(i) .." \"" .. CXX .. " " .. table.concat(argv[i], " ") .. "\" failed with exit code " .. tostring(ec) .. " and output;") + print(stderr[i]) + print(stdout[i]) print() 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 = {} 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(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) if (ec ~= 0) then - 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("Command \"" .. LNK .. " " .. table.concat(argv, " ") .. "\" failed with exit code " .. tostring(ec) .. " and output;") + print(stderr) + print(stdout) print() end end function build(...) 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) print("Building " .. config.buildmode .. " with up to " .. tostring(config.max_parallel) .. " concurrent jobs") 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 +function main(...) build(...) end + return 0 \ No newline at end of file diff --git a/makefile b/makefile index 0eaad6e..6736d45 100644 --- a/makefile +++ b/makefile @@ -9,6 +9,7 @@ DEBUG_DEFINES := DEFINES := CXX_FLAGS := -std=c11 -std=c++20 -g -O0 +CC_FLAGS := -std=c11 -g -O0 CXX_DEBUG_FLAGS := LNK_FLAGS := -g -O0 LNK_DEBUG_FLAGS := @@ -42,21 +43,24 @@ endif clean: $(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)) -obj/thirdparty/uniproc/%.c.o: thirdparty/uniproc/src/%.c - $(CC) -c $(CXX_FLAGS) $(foreach inc,$(INCLUDES),-I$(inc)) $(foreach def,$(DEFINES),-D$(def)) $< -o $@ +obj/thirdparty/uniproc/src/%.c.o: thirdparty/uniproc/src/%.c + $(CC) -c $(CC_FLAGS) $(foreach inc,$(INCLUDES),-I$(inc)) $(foreach def,$(DEFINES),-D$(def)) $< -o $@ obj/src/%.cpp.o: src/%.cpp $(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) $(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: - git submodule update --init --recursive + git submodule update --init --recursive --remote cd thirdparty/luajit/src $(PLATFORM_SEPERATOR) $(LUAJUT_BUILD_CMD) - $(call touch,".make/thirdparty_luajit") \ No newline at end of file + $(call touch,".make/thirdparty_luajit") + +.make/thirdparty_uniproc: + git submodule update --init --recursive --remote + \ No newline at end of file diff --git a/src/lua_filesystem.cpp b/src/lua_filesystem.cpp index 62dc5a5..207c935 100644 --- a/src/lua_filesystem.cpp +++ b/src/lua_filesystem.cpp @@ -94,11 +94,12 @@ int lua_fs_list_alldirs(lua_State* L) const char* fpath = lua_tostring(L, 1); lua_newtable(L); 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(); lua_pushlstring(L, x.c_str(), x.size()); lua_rawseti(L, -2, i); + i++; } return 1; } diff --git a/src/lua_platform.cpp b/src/lua_platform.cpp index e1dcab1..812872c 100644 --- a/src/lua_platform.cpp +++ b/src/lua_platform.cpp @@ -4,7 +4,7 @@ #include "mutex" #include #include "simple_string.h" -#include "uniproc/include/uniproc.h" +#include "uniproc.h" 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"); 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) { - lua_rawgeti(L, -1, i + 1); - if (!lua_isstring(L, -1)) - 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))); + lua_rawgeti(L, 2, i+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+1, lua_typename(L, lua_type(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 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**)); // 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); if (lua_istable(L, -1)) { - const size_t num_args = lua_objlen(L, -1); - args[i] = (const char**)malloc(num_args*sizeof(const char*)); - for (size_t j = 0; j < num_args; ++j) + const size_t tbl_num_args = lua_objlen(L, -1); + num_args[i] = tbl_num_args; + 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); 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) { + num_args[i] = 1; args[i] = (const char**)malloc(1*sizeof(const char*)); args[i][0] = lua_tostring(L, -1); } @@ -123,12 +126,12 @@ int lua_platform_exec_parallel(lua_State* L) { char buf[64]; // Read the std. streams into the output vectors - while (fgets(buf, sizeof(buf), p.out) != NULL) - std_outs[p.userdata].append(buf); - while (fgets(buf, sizeof(buf), p.err) != NULL) - std_errs[p.userdata].append(buf); + while (fgets(buf, sizeof(buf), p->out) != NULL) + std_outs[p->userdata].append(buf); + while (fgets(buf, sizeof(buf), p->err) != NULL) + std_errs[p->userdata].append(buf); // 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 uniproc_close_process(p); } @@ -139,7 +142,7 @@ int lua_platform_exec_parallel(lua_State* L) { // Create process *p = uniproc_create_process(cmd, num_args[cmd_idx], args[cmd_idx]); - uniproc_set_userdata(p, num_cmds); + p->userdata = cmd_idx; cmd_idx++; // If we failed to create the process, set the output for it to an error value diff --git a/thirdparty/uniproc b/thirdparty/uniproc index 500fe17..8948e75 160000 --- a/thirdparty/uniproc +++ b/thirdparty/uniproc @@ -1 +1 @@ -Subproject commit 500fe170769d9608c2c36163da055b5fc2157c11 +Subproject commit 8948e75d06e41b69a59fc562b3c02d739feddac9