diff --git a/build.cpp b/build.cpp index 42ce563..a70384d 100644 --- a/build.cpp +++ b/build.cpp @@ -1,4 +1,5 @@ #include "build.hpp" +#include std::filesystem::path OBJ_DIR = "obj"; std::filesystem::path INC_DIR = "inc"; @@ -75,32 +76,23 @@ int compile_src_dir() nob_directory src_dir = get_all_files_in_dir(SRC_DIR); nob_directory obj_dir; - for (auto &&i : src_dir.files) - { - std::filesystem::path tmp = i; - obj_dir.files.push_back(OBJ_DIR / tmp.replace_extension(".o")); - } - for (auto &&i : src_dir.dirs) { - obj_dir.dirs.push_back(OBJ_DIR / i); + 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)) { - clear_build_dir(true, false); - break; + std::string heder = inc_dir.files[i].string().substr(4); + modified_heders.insert(heder); } } - for (auto &&i : obj_dir.dirs) - { - std::filesystem::create_directory(i); - } - command.clear(); command.push_back(cpp_compiler); command.push_back("-c"); @@ -118,12 +110,31 @@ int compile_src_dir() command.push_back("-Werror"); int build = 0; - for (size_t i = 0; i < src_dir.files.size(); i++) + for (auto &&i : src_dir.files) { - if (check_if_rebuild(src_dir.files[i], obj_dir.files[i])) + 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) { - command[src_loc] = src_dir.files[i]; - command[obj_loc] = obj_dir.files[i]; + 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; diff --git a/build.hpp b/build.hpp index 4633124..a8605b4 100644 --- a/build.hpp +++ b/build.hpp @@ -1,12 +1,11 @@ #include -#include #include -#include #include #include #include #include #include +#include enum class Color { @@ -81,7 +80,7 @@ bool nob_cmd_run_sync(std::vector &arguments) return std::system(command.c_str()) == 0; } -bool check_if_rebuild(std::filesystem::path &org_path, std::filesystem::path &new_path) +bool check_if_rebuild(const std::filesystem::path &org_path, const std::filesystem::path &new_path) { if (!std::filesystem::exists(org_path)) return false; @@ -155,3 +154,24 @@ nob_directory get_all_files_in_dir(std::filesystem::path directory_path) return dir; } + +std::vector get_includes(const std::filesystem::path &path) +{ + std::vector ret; + + std::ifstream ifs(path); + std::string line; + + while (std::getline(ifs, line)) + { + if (!line[0] == '#' && !line.empty()) + break; + + if (line[line.size() - 1] == '\"') + { + ret.push_back(line.substr(10, line.length() - 10 - 1)); + } + } + + return ret; +}