From dbf91e0ebc196124c7342ddd976c8766986a2c39 Mon Sep 17 00:00:00 2001 From: Riley King-Saunders Date: Thu, 6 Mar 2025 17:52:50 +1100 Subject: [PATCH] Added uniproc library to project --- .gitignore | 1 + .gitmodules | 3 ++ lbs.lua | 107 ++++++++++++++++++++++++++------------------- makefile | 9 ++-- thirdparty/uniproc | 1 + 5 files changed, 72 insertions(+), 49 deletions(-) create mode 160000 thirdparty/uniproc diff --git a/.gitignore b/.gitignore index e671e44..60fe2a5 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ /.make/thirdparty_luajit /CppProperties.json /lbs.exe +/build/lbs.ilk diff --git a/.gitmodules b/.gitmodules index ef42472..0d4be57 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "thirdparty/luajit"] path = thirdparty/luajit url = https://luajit.org/git/luajit.git +[submodule "thirdparty/uniproc"] + path = thirdparty/uniproc + url = ssh://gitea@git.rileyk.au:19482/riley/uniproc.git diff --git a/lbs.lua b/lbs.lua index cbbb593..7b304c4 100644 --- a/lbs.lua +++ b/lbs.lua @@ -7,14 +7,14 @@ local config = { } local program_output = "build/lbs" -local source_dirs = {"src"} -local include_dirs = {"include", "src", "thirdparty/luajit/src"} +local source_dirs = {"src", "thirdparty/uniproc/src"} +local include_dirs = {"include", "src", "thirdparty/luajit/src", "thirdparty/uniproc/include"} local library_dirs = {"thirdparty/luajit/src"} local linker_inputs = {"lua51", "luajit"} local defines = {} local additional_arguments = {"-std=c++20"} -function load_config(cfg_table, args) +local function load_config(cfg_table, args) for i=1,#args,1 do k = args[i] if (k:sub(1,2) == "-j") then @@ -34,61 +34,75 @@ function load_config(cfg_table, args) return cfg_table end -local function compile() +function table.shallow_copy(tbl) + local new = {} + for k,v in pairs(tbl) do + new[k] = v + end + return new +end - local base_cmd = CXX .. " -c " +local function get_required_objects(dirs, src_extension, obj_prefix, obj_postfix) + obj_prefix = obj_prefix or "obj/" + 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 + end + return objs, new_src +end + +local function compile(CXX, src_dirs, include_dirs, defines, additional_arguments) + local base_argv = {} - for _,v in pairs(include_dirs) do base_cmd = base_cmd .. "-I\"" .. v .. "\" " end - for k,v in pairs(defines) do base_cmd = base_cmd .. "-D" .. k .. "=" .. v .. " " end - for _,v in pairs(additional_arguments) do base_cmd = base_cmd .. v .. " " end + additional_arguments[#additional_arguments+1] = "-c" + for _,v in pairs(additional_arguments) do base_argv[#base_argv+1] = v end + for _,v in pairs(include_dirs) do base_argv[#base_argv+1] = "-I\"" .. v .. "\"" end + for k,v in pairs(defines) do base_argv[#base_argv+1] = "-D" .. k .. "=" .. v end - local src_args = {} - for _,src_dir in pairs(source_dirs) do - if (fs.is_dir(src_dir)) then - for fsobj in fs.forall(src_dir) do - if (fs.is_file(fsobj) and fs.extension(fsobj) == ".cpp") then - src_args[#src_args+1] = fsobj .. " -o obj/" .. fsobj .. ".o" - end - end - else - print("[WARN ] Source directory " .. src_dir .. " does not exist!") - end + local obj_files, src_files = get_required_objects(src_dirs, ".cpp") + local argv = {} + for k,obj_file in pairs(obj_files) do + local args = table.shallow_copy(base_argv) + args[#args+1] = src_files[k] + args[#args+1] = "-o\"" .. obj_file .. "\"" + argv[#argv+1] = args end - ec, stdout = platform.exec_parallel(base_cmd, config.max_parallel, src_args) + local ec, stdout = platform.exec_parallel(CXX, config.max_parallel, argv) for i,ec in pairs(ec) do if (ec ~= 0) then - print("Command \"" .. base_cmd .. src_args[i] .. "\" failed with exit code " .. tostring(ec) .. "and output;") + print("Command \"" .. CXX .. " " .. table.concat(argv[i], " ") .. "\" failed with exit code " .. tostring(ec) .. " and output;") print(stdout[i]) print() end end end -local function link() - local cmd = CXX .. " " - for _,lib_dir in pairs(library_dirs) do cmd = cmd .. "-L\"" .. lib_dir .. "\" " end - for _,lnk in pairs(linker_inputs) do cmd = cmd .. "-l\"" .. lnk .. "\" " end - - for _,src_dir in pairs(source_dirs) do - if (fs.is_dir(src_dir)) then - for fsobj in fs.forall(src_dir) do - if (fs.is_file(fsobj) and fs.extension(fsobj) == ".cpp") then - cmd = cmd .. "obj/" .. fsobj .. ".o " - end - end - else - print("[WARN ] Source directory " .. src_dir .. " does not exist!") - end - end - cmd = cmd .. "-o " .. program_output - local platform_seps = {} - for match in config.target:gmatch("[^-]+") do platform_seps[#platform_seps+1] = match end - if (platform_seps[2] == "windows") then cmd = cmd .. ".exe " else cmd = cmd .. " " end - - ec, stdout = platform.exec(cmd, {}) +local function link(LNK, obj_dirs, library_dirs, linker_inputs, additional_arguments) + 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 + + local ec, stdout = platform.exec(LNK, argv) if (ec ~= 0) then - print("Command \"" .. cmd .. "\" failed with exit code " .. tostring(ec) .. " and output;") + print("Command \"" .. LNK .. " " .. table.concat(argv) .. "\" failed with exit code " .. tostring(ec) .. " and output;") print(stdout) print() end @@ -98,8 +112,9 @@ function build(...) local args = {...} config = load_config(config, args) print("Building " .. config.buildmode .. " with up to " .. tostring(config.max_parallel) .. " concurrent jobs") - compile() - link() + + compile(CXX, source_dirs, include_dirs, defines, additional_arguments) + link(CXX, "obj", library_dirs, linker_inputs, {}) end return 0 \ No newline at end of file diff --git a/makefile b/makefile index 746d563..0eaad6e 100644 --- a/makefile +++ b/makefile @@ -1,13 +1,14 @@ CXX := clang++ +CC := clang -INCLUDES := include src thirdparty/luajit/src +INCLUDES := include src thirdparty/luajit/src thirdparty/uniproc/include LIBRARY_DIRECTORIES := thirdparty/luajit/src LINKER_INPUTS := lua51 luajit DEBUG_DEFINES := DEFINES := -CXX_FLAGS := -std=c++20 -g -O0 +CXX_FLAGS := -std=c11 -std=c++20 -g -O0 CXX_DEBUG_FLAGS := LNK_FLAGS := -g -O0 LNK_DEBUG_FLAGS := @@ -41,9 +42,11 @@ endif clean: $(call RM,$(call rwildcard, obj,*.o) build/$(OUTPUT_NAME)) -SOURCE_FILES := $(call rwildcard,src,*.cpp,*.hpp) +SOURCE_FILES := $(call rwildcard,src,*.cpp,*.hpp) $(call rwildcard,thirdparty/uniproc/src,*.cpp,*.hpp) 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/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 diff --git a/thirdparty/uniproc b/thirdparty/uniproc new file mode 160000 index 0000000..f4927bf --- /dev/null +++ b/thirdparty/uniproc @@ -0,0 +1 @@ +Subproject commit f4927bf136dd9edda93527cc39299e760523318b