This commit is contained in:
Nikola Petrov 2024-07-19 18:18:05 +02:00
parent 33b3c10e4d
commit 748ab00289
2 changed files with 87 additions and 40 deletions

View File

@ -15,7 +15,6 @@ int main(int argc, char const *argv[])
{ {
if (!std::strcmp(argv[i], "opt")) if (!std::strcmp(argv[i], "opt"))
{ {
opt_flags = "-O3";
opt = true; opt = true;
} }
if (!std::strcmp(argv[i], "build")) if (!std::strcmp(argv[i], "build"))
@ -24,13 +23,18 @@ int main(int argc, char const *argv[])
if (!std::strcmp(argv[i], "run")) if (!std::strcmp(argv[i], "run"))
run = true; run = true;
if (!std::strcmp(argv[i], "cla")) if (!std::strcmp(argv[i], "clear"))
{ {
clear_all_build();
clear = true; clear = true;
} }
} }
if (clear)
{
clear_all_build();
}
// clean recompile of code
if (clear && !build) if (clear && !build)
{ {
return 0; return 0;
@ -38,32 +42,35 @@ int main(int argc, char const *argv[])
std::filesystem::create_directory(OBJ_DIR); std::filesystem::create_directory(OBJ_DIR);
if (!opt) if (opt)
{ {
opt_flags = "-O3";
int res = compile_src_dir(); build_as_one();
switch (res)
{
case 1:
compile_obj_dir();
break;
case -1:
return 0;
}
if (!std::filesystem::exists(BUILD_FILE))
compile_obj_dir();
} }
else else
{ {
build_as_one(); int res = compile_src_dir();
switch (res)
{
// correcty recompile
case 1:
compile_obj_dir();
break;
// failed recompile
case -1:
return 0;
// no need for recompile
case 0:
break;
}
// files dont need to recompile but exe is mising
if (!std::filesystem::exists(BUILD_FILE))
compile_obj_dir();
} }
if (run) if (run)
{ {
command = {"./" + BUILD_FILE.string(), "Pot", "20", "1080"}; run_main();
nob_cmd_run_sync(command);
} }
return 0; return 0;
} }

View File

@ -7,6 +7,8 @@
#include <fstream> #include <fstream>
#include <unordered_set> #include <unordered_set>
// #define EXT_ARGS
// #define EXT_LINK
// #define EXT_INC // #define EXT_INC
// #define EXT_OBJ // #define EXT_OBJ
// #define NO_Werror // #define NO_Werror
@ -14,10 +16,17 @@
std::filesystem::path OBJ_DIR = "obj"; std::filesystem::path OBJ_DIR = "obj";
std::filesystem::path INC_DIR = "include"; std::filesystem::path INC_DIR = "include";
std::filesystem::path SRC_DIR = "source"; std::filesystem::path SRC_DIR = "source";
std::filesystem::path BUILD_FILE = "main";
#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 cpp_compiler = "g++";
std::string c_compiler = "gcc"; std::string c_compiler = "gcc";
#endif
std::string opt_flags = "-ggdb"; std::string opt_flags = "-ggdb";
std::vector<std::string> command; std::vector<std::string> command;
@ -81,7 +90,7 @@ void print_command(std::vector<std::string> &arguments)
printf("\n"); printf("\n");
} }
bool nob_cmd_run_sync(std::vector<std::string> &arguments) bool run_command(std::vector<std::string> &arguments)
{ {
print_command(arguments); print_command(arguments);
std::string command = ""; std::string command = "";
@ -109,8 +118,7 @@ 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) bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec_path)
{ {
std::string p = exec_path[0]; std::filesystem::path exec = exec_path[0];
std::filesystem::path exec = p;
if (!check_if_rebuild(src_path, exec) && !check_if_rebuild(__FILE__, exec)) if (!check_if_rebuild(src_path, exec) && !check_if_rebuild(__FILE__, exec))
return false; return false;
@ -122,10 +130,10 @@ bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec
input.push_back(exec_path[i]); input.push_back(exec_path[i]);
std::vector<std::string> comand = {cpp_compiler, src_path.string(), "-o", exec_path[0], "-g"}; std::vector<std::string> comand = {cpp_compiler, src_path.string(), "-o", exec_path[0], "-g"};
if (!nob_cmd_run_sync(comand)) if (!run_command(comand))
return false; return false;
if (!nob_cmd_run_sync(input)) if (!run_command(input))
return false; return false;
printf("rebuild\n"); printf("rebuild\n");
@ -137,17 +145,17 @@ bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec
return true; return true;
} }
struct nob_directory struct all_in_directory
{ {
std::vector<std::filesystem::path> files; std::vector<std::filesystem::path> files;
std::vector<std::filesystem::path> dirs; std::vector<std::filesystem::path> dirs;
}; };
nob_directory get_all_files_in_dir(std::filesystem::path directory_path) all_in_directory get_all_files_in_dir(std::filesystem::path directory_path)
{ {
namespace fs = std::filesystem; namespace fs = std::filesystem;
nob_directory dir; all_in_directory dir;
if (!fs::is_directory(directory_path)) if (!fs::is_directory(directory_path))
return dir; return dir;
@ -192,8 +200,11 @@ std::vector<std::string> get_includes(const std::filesystem::path &path)
if (line[0] != '#') if (line[0] != '#')
break; break;
// if include ends with " its my .h
if (line[line.size() - 1] == '\"') 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)); ret.push_back(line.substr(10, line.length() - 10 - 1));
} }
} }
@ -203,8 +214,8 @@ std::vector<std::string> get_includes(const std::filesystem::path &path)
void clear_all_build() void clear_all_build()
{ {
std::filesystem::remove(OBJ_DIR); std::filesystem::remove_all(OBJ_DIR);
std::filesystem::remove(BUILD_FILE); std::filesystem::remove_all(BUILD_FILE);
} }
#ifdef EXT_INC #ifdef EXT_INC
@ -213,8 +224,8 @@ int compile_src_dir(std::vector<std::string> ext_inc)
int compile_src_dir() int compile_src_dir()
#endif #endif
{ {
nob_directory src_dir = get_all_files_in_dir(SRC_DIR); all_in_directory src_dir = get_all_files_in_dir(SRC_DIR);
nob_directory obj_dir; all_in_directory obj_dir;
for (auto &&i : src_dir.dirs) for (auto &&i : src_dir.dirs)
{ {
@ -222,7 +233,7 @@ int compile_src_dir()
} }
std::unordered_set<std::string> modified_heders; std::unordered_set<std::string> modified_heders;
nob_directory inc_dir = get_all_files_in_dir(INC_DIR); all_in_directory inc_dir = get_all_files_in_dir(INC_DIR);
for (size_t i = 0; i < inc_dir.files.size(); i++) for (size_t i = 0; i < inc_dir.files.size(); i++)
{ {
@ -260,9 +271,11 @@ int compile_src_dir()
std::filesystem::path tmp = i; std::filesystem::path tmp = i;
std::filesystem::path out = OBJ_DIR / tmp.replace_extension(".o"); std::filesystem::path out = OBJ_DIR / tmp.replace_extension(".o");
bool reb = false; bool reb = false;
// check if .cpp changed
if (check_if_rebuild(i, out)) if (check_if_rebuild(i, out))
reb = true; reb = true;
// if .cpp didnt change check include files changed
if (!reb) if (!reb)
{ {
std::vector<std::string> incudes = get_includes(i); std::vector<std::string> incudes = get_includes(i);
@ -280,7 +293,7 @@ int compile_src_dir()
{ {
command[src_loc] = i.string(); command[src_loc] = i.string();
command[obj_loc] = out.string(); command[obj_loc] = out.string();
if (!nob_cmd_run_sync(command)) if (!run_command(command))
return -1; return -1;
build = 1; build = 1;
@ -289,9 +302,13 @@ int compile_src_dir()
return build; return build;
} }
#if defined(EXT_LINK)
void compile_obj_dir(std::vector<std::string> ext_link)
#else
void compile_obj_dir() void compile_obj_dir()
#endif
{ {
nob_directory obj_dir = get_all_files_in_dir(OBJ_DIR); all_in_directory obj_dir = get_all_files_in_dir(OBJ_DIR);
command.clear(); command.clear();
command.push_back(cpp_compiler); command.push_back(cpp_compiler);
@ -304,7 +321,11 @@ void compile_obj_dir()
command.push_back("-o"); command.push_back("-o");
command.push_back(BUILD_FILE.string()); command.push_back(BUILD_FILE.string());
nob_cmd_run_sync(command); #if defined(EXT_LINK)
command.insert(command.end(), ext_link.begin(), ext_link.end());
#endif
run_command(command);
} }
#if defined(EXT_OBJ) && defined(EXT_INC) #if defined(EXT_OBJ) && defined(EXT_INC)
@ -317,7 +338,7 @@ void build_as_one(std::vector<std::string> ext_inc)
void build_as_one() void build_as_one()
#endif #endif
{ {
nob_directory src_dir = get_all_files_in_dir(SRC_DIR); all_in_directory src_dir = get_all_files_in_dir(SRC_DIR);
command.clear(); command.clear();
command.push_back(cpp_compiler); command.push_back(cpp_compiler);
@ -343,5 +364,24 @@ void build_as_one()
#ifdef EXT_OBJ #ifdef EXT_OBJ
command.insert(command.end(), ext_obj.begin(), ext_obj.end()); command.insert(command.end(), ext_obj.begin(), ext_obj.end());
#endif #endif
nob_cmd_run_sync(command); run_command(command);
}
#ifdef EXT_ARGS
bool run_main(std::vector<std::string> ext_args)
#else
bool run_main()
#endif
{
command.clear();
#ifndef _WIN32
command.push_back("./");
#endif
command.push_back(BUILD_FILE.string());
#ifdef EXT_ARGS
command.insert(command.end(), ext_args.begin(), ext_args.end());
#endif
return run_command(command);
} }