diff --git a/build.cpp b/build.cpp index a70384d..833b101 100644 --- a/build.cpp +++ b/build.cpp @@ -1,40 +1,22 @@ + +#ifdef _WIN32 +#define EXT_LINK +#endif + +#define EXT_INC #include "build.hpp" #include -std::filesystem::path OBJ_DIR = "obj"; -std::filesystem::path INC_DIR = "inc"; -std::filesystem::path SRC_DIR = "src"; +#ifdef _WIN32 +std::vector LINK = {"-lopengl32", "-lkernel32", "-luser32", "-lshell32","-lwinmm","-lgdi32"}; +#endif +std::vector RAYINCLUDE = {"-Iraylib/src", "-Iraylib/src/external/glfw/include", "-Iraylib/src/external/glfw/deps/mingw"}; std::filesystem::path RAYLIB_DIR = "raylib"; -std::filesystem::path BUILD_FILE = "treender"; bool build = false; bool clear = false; bool run = false; -std::string cpp_compiler = "g++"; -std::string c_compiler = "gcc"; -std::string opt_flags = "-ggdb"; -std::vector command; -std::vector RAYINCLUDE = {"-Iraylib/src", "-Iraylib/src/external/glfw/include", "-Iraylib/src/external/glfw/deps/mingw"}; - -void clear_build_dir(bool src, bool raylib) -{ - if (src) - { - std::filesystem::remove_all(OBJ_DIR / SRC_DIR); - } - if (raylib) - { - std::filesystem::remove_all(OBJ_DIR / RAYLIB_DIR); - } - if (src && raylib) - { - if (std::filesystem::exists(OBJ_DIR)) - std::filesystem::remove_all(OBJ_DIR); - } - std::filesystem::remove(BUILD_FILE); -} - void compile_raylib_dir() { std::filesystem::path out_dir = OBJ_DIR / RAYLIB_DIR; @@ -64,104 +46,13 @@ void compile_raylib_dir() out = out_dir / ray_srcs[i].replace_extension(".o"); if (check_if_rebuild(src, out)) { - command[src_loc] = src; - command[obj_loc] = out; + command[src_loc] = src.string(); + command[obj_loc] = out.string(); nob_cmd_run_sync(command); } } } -int compile_src_dir() -{ - nob_directory src_dir = get_all_files_in_dir(SRC_DIR); - nob_directory obj_dir; - - for (auto &&i : src_dir.dirs) - { - std::filesystem::create_directory(OBJ_DIR / i); - } - - std::unordered_set modified_heders; - - nob_directory inc_dir = get_all_files_in_dir(INC_DIR); - for (size_t i = 0; i < inc_dir.files.size(); i++) - { - if (check_if_rebuild(inc_dir.files[i], BUILD_FILE)) - { - std::string heder = inc_dir.files[i].string().substr(4); - modified_heders.insert(heder); - } - } - - command.clear(); - command.push_back(cpp_compiler); - command.push_back("-c"); - int src_loc = command.size(); - command.push_back(""); - command.push_back("-o"); - int obj_loc = command.size(); - command.push_back(""); - command.push_back(opt_flags); - command.insert(command.end(), RAYINCLUDE.begin(), RAYINCLUDE.end()); - command.push_back("-Iinc"); - - command.push_back("-std=c++23"); - command.push_back("-Wall"); - command.push_back("-Werror"); - - int build = 0; - for (auto &&i : src_dir.files) - { - std::filesystem::path tmp = i; - std::filesystem::path out = OBJ_DIR / tmp.replace_extension(".o"); - bool reb = false; - if (check_if_rebuild(i, out)) - reb = true; - - if (!reb) - { - std::vector incudes = get_includes(i); - for (auto &&j : incudes) - { - if (modified_heders.find(j) != modified_heders.end()) - { - reb = true; - break; - } - } - } - - if (reb) - { - command[src_loc] = i; - command[obj_loc] = out; - if (!nob_cmd_run_sync(command)) - return -1; - - build = 1; - } - } - return build; -} - -void compile_obj_dir() -{ - nob_directory obj_dir = get_all_files_in_dir(OBJ_DIR); - - command.clear(); - command.push_back(cpp_compiler); - - for (auto &&i : obj_dir.files) - { - command.push_back(i); - } - - command.push_back("-o"); - command.push_back(BUILD_FILE); - - nob_cmd_run_sync(command); -} - int main(int argc, char const *argv[]) { if (rebuild_my_self(__FILE__, argc, argv)) @@ -180,7 +71,7 @@ int main(int argc, char const *argv[]) if (!std::strcmp(argv[i], "clear")) { - clear_build_dir(true, true); + clear_all_build(); clear = true; } } @@ -204,18 +95,18 @@ int main(int argc, char const *argv[]) compile_raylib_dir(); - int res = compile_src_dir(); + int res = compile_src_dir(RAYINCLUDE); switch (res) { case 1: - compile_obj_dir(); + compile_obj_dir(LINK); break; case -1: return 0; } if (!std::filesystem::exists(BUILD_FILE)) - compile_obj_dir(); + compile_obj_dir(LINK); if (run) { diff --git a/build.hpp b/build.hpp index a8605b4..39859ac 100644 --- a/build.hpp +++ b/build.hpp @@ -1,11 +1,33 @@ #include #include -#include #include #include #include #include #include +#include + +// #define EXT_LINK +// #define EXT_INC +// #define EXT_OBJ +// #define NO_Werror + +std::filesystem::path OBJ_DIR = "obj"; +std::filesystem::path INC_DIR = "inc"; +std::filesystem::path SRC_DIR = "src"; + +#ifdef _WIN32 +std::filesystem::path BUILD_FILE = "main.exe"; +std::string cpp_compiler = "zig c++"; +std::string c_compiler = "zig cc"; +#else +std::filesystem::path BUILD_FILE = "main"; +std::string cpp_compiler = "g++"; +std::string c_compiler = "gcc"; +#endif + +std::string opt_flags = "-ggdb"; +std::vector command; enum class Color { @@ -95,16 +117,18 @@ bool check_if_rebuild(const std::filesystem::path &org_path, const std::filesyst bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec_path) { + std::filesystem::path exec = exec_path[0]; + + if (!check_if_rebuild(src_path, exec) && !check_if_rebuild(__FILE__, exec)) + return false; + +#ifndef _WIN32 std::vector input; for (int i = 0; i < argc; ++i) input.push_back(exec_path[i]); - std::filesystem::path exec = input[0]; - if (!check_if_rebuild(src_path, exec)) - return false; - - std::vector comand = {"g++", src_path, "-o", exec_path[0], "-g"}; + std::vector comand = {cpp_compiler, src_path.string(), "-o", exec_path[0], "-g"}; if (!nob_cmd_run_sync(comand)) return false; @@ -112,6 +136,11 @@ bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec return false; printf("rebuild\n"); +#else + pick_color(Color::red); + printf("rebuild me\n"); + pick_color(Color::white); +#endif return true; } @@ -164,14 +193,175 @@ std::vector get_includes(const std::filesystem::path &path) while (std::getline(ifs, line)) { - if (!line[0] == '#' && !line.empty()) + if (line.empty()) + continue; + + if (line[0] != '#') break; + // if include ends with " its my .h if (line[line.size() - 1] == '\"') { + // magic num 10 is lenOf(#include ") so start at 10 until end of + // line witch is len of line - 10 - 1 of cahar " ret.push_back(line.substr(10, line.length() - 10 - 1)); } } return ret; } + +void clear_all_build() +{ + std::filesystem::remove_all(OBJ_DIR); + std::filesystem::remove_all(BUILD_FILE); +} + +#ifdef EXT_INC +int compile_src_dir(std::vector ext_inc) +#else +int compile_src_dir() +#endif +{ + nob_directory src_dir = get_all_files_in_dir(SRC_DIR); + nob_directory obj_dir; + + for (auto &&i : src_dir.dirs) + { + std::filesystem::create_directory(OBJ_DIR / i); + } + + std::unordered_set modified_heders; + nob_directory inc_dir = get_all_files_in_dir(INC_DIR); + + for (size_t i = 0; i < inc_dir.files.size(); i++) + { + if (check_if_rebuild(inc_dir.files[i], BUILD_FILE)) + { + std::string heder = inc_dir.files[i].string().substr(4); + modified_heders.insert(heder); + } + } + + command.clear(); + command.push_back(cpp_compiler); + command.push_back("-c"); + int src_loc = command.size(); + command.push_back(""); + command.push_back("-o"); + int obj_loc = command.size(); + command.push_back(""); + command.push_back(opt_flags); + +#ifdef EXT_INC + command.insert(command.end(), ext_inc.begin(), ext_inc.end()); +#endif + + command.push_back("-I" + INC_DIR.string()); + command.push_back("-std=c++23"); + command.push_back("-Wall"); +#ifndef NO_Werror + command.push_back("-Werror"); +#endif + + int build = 0; + for (auto &&i : src_dir.files) + { + std::filesystem::path tmp = i; + std::filesystem::path out = OBJ_DIR / tmp.replace_extension(".o"); + bool reb = false; + // check if .cpp changed + if (check_if_rebuild(i, out)) + reb = true; + + // if .cpp didnt change check include files changed + if (!reb) + { + std::vector incudes = get_includes(i); + for (auto &&j : incudes) + { + if (modified_heders.find(j) != modified_heders.end()) + { + reb = true; + break; + } + } + } + + if (reb) + { + command[src_loc] = i.string(); + command[obj_loc] = out.string(); + if (!nob_cmd_run_sync(command)) + return -1; + + build = 1; + } + } + return build; +} + +#if defined(EXT_LINK) +void compile_obj_dir(std::vector ext_link) +#else +void compile_obj_dir() +#endif +{ + nob_directory obj_dir = get_all_files_in_dir(OBJ_DIR); + + command.clear(); + command.push_back(cpp_compiler); + + for (auto &&i : obj_dir.files) + { + command.push_back(i.string()); + } + + command.push_back("-o"); + command.push_back(BUILD_FILE.string()); + +#if defined(EXT_LINK) + command.insert(command.end(), ext_link.begin(), ext_link.end()); +#endif + + nob_cmd_run_sync(command); +} + +#if defined(EXT_OBJ) && defined(EXT_INC) +void build_as_one(std::vector ext_obj, std::vector ext_inc) +#elif defined(EXT_OBJ) +void build_as_one(std::vector ext_obj) +#elif defined(EXT_INC) +void build_as_one(std::vector ext_inc) +#else +void build_as_one() +#endif +{ + nob_directory src_dir = get_all_files_in_dir(SRC_DIR); + + command.clear(); + command.push_back(cpp_compiler); + command.push_back("-o"); + command.push_back(BUILD_FILE.string()); + command.push_back(opt_flags); + +#ifdef EXT_INC + command.insert(command.end(), ext_inc.begin(), ext_inc.end()); +#endif + + command.push_back("-I" + INC_DIR.string()); + command.push_back("-std=c++23"); + command.push_back("-Wall"); +#ifndef NO_Werror + command.push_back("-Werror"); +#endif + for (auto &&i : src_dir.files) + { + command.push_back(i.string()); + } + +#ifdef EXT_OBJ + command.insert(command.end(), ext_obj.begin(), ext_obj.end()); +#endif + nob_cmd_run_sync(command); +} \ No newline at end of file