Build Check include "" if heder updated to rebuild

This commit is contained in:
Nikola Petrov 2024-06-07 23:42:53 +02:00
parent dbf97df86a
commit 0d7db04aa8
2 changed files with 52 additions and 21 deletions

View File

@ -1,4 +1,5 @@
#include "build.hpp"
#include <unordered_set>
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<std::string> 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<std::string> 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;

View File

@ -1,12 +1,11 @@
#include <cstdio>
#include <cassert>
#include <cstring>
#include <ctime>
#include <thread>
#include <vector>
#include <string>
#include <list>
#include <filesystem>
#include <fstream>
enum class Color
{
@ -81,7 +80,7 @@ bool nob_cmd_run_sync(std::vector<std::string> &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<std::string> get_includes(const std::filesystem::path &path)
{
std::vector<std::string> 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;
}