update build for windows

This commit is contained in:
Nikola Petrov 2024-07-19 04:09:51 +02:00
parent ccc8d7ae78
commit 390c81b30b
2 changed files with 213 additions and 132 deletions

141
build.cpp
View File

@ -1,40 +1,22 @@
#ifdef _WIN32
#define EXT_LINK
#endif
#define EXT_INC
#include "build.hpp" #include "build.hpp"
#include <unordered_set> #include <unordered_set>
std::filesystem::path OBJ_DIR = "obj"; #ifdef _WIN32
std::filesystem::path INC_DIR = "inc"; std::vector<std::string> LINK = {"-lopengl32", "-lkernel32", "-luser32", "-lshell32","-lwinmm","-lgdi32"};
std::filesystem::path SRC_DIR = "src"; #endif
std::vector<std::string> RAYINCLUDE = {"-Iraylib/src", "-Iraylib/src/external/glfw/include", "-Iraylib/src/external/glfw/deps/mingw"};
std::filesystem::path RAYLIB_DIR = "raylib"; std::filesystem::path RAYLIB_DIR = "raylib";
std::filesystem::path BUILD_FILE = "treender";
bool build = false; bool build = false;
bool clear = false; bool clear = false;
bool run = false; bool run = false;
std::string cpp_compiler = "g++";
std::string c_compiler = "gcc";
std::string opt_flags = "-ggdb";
std::vector<std::string> command;
std::vector<std::string> 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() void compile_raylib_dir()
{ {
std::filesystem::path out_dir = OBJ_DIR / 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"); out = out_dir / ray_srcs[i].replace_extension(".o");
if (check_if_rebuild(src, out)) if (check_if_rebuild(src, out))
{ {
command[src_loc] = src; command[src_loc] = src.string();
command[obj_loc] = out; command[obj_loc] = out.string();
nob_cmd_run_sync(command); 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<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))
{
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<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;
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[]) int main(int argc, char const *argv[])
{ {
if (rebuild_my_self(__FILE__, argc, 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")) if (!std::strcmp(argv[i], "clear"))
{ {
clear_build_dir(true, true); clear_all_build();
clear = true; clear = true;
} }
} }
@ -204,18 +95,18 @@ int main(int argc, char const *argv[])
compile_raylib_dir(); compile_raylib_dir();
int res = compile_src_dir(); int res = compile_src_dir(RAYINCLUDE);
switch (res) switch (res)
{ {
case 1: case 1:
compile_obj_dir(); compile_obj_dir(LINK);
break; break;
case -1: case -1:
return 0; return 0;
} }
if (!std::filesystem::exists(BUILD_FILE)) if (!std::filesystem::exists(BUILD_FILE))
compile_obj_dir(); compile_obj_dir(LINK);
if (run) if (run)
{ {

204
build.hpp
View File

@ -1,11 +1,33 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <thread>
#include <vector> #include <vector>
#include <string> #include <string>
#include <list> #include <list>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <unordered_set>
// #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<std::string> command;
enum class Color 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) 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<std::string> input; std::vector<std::string> input;
for (int i = 0; i < argc; ++i) for (int i = 0; i < argc; ++i)
input.push_back(exec_path[i]); input.push_back(exec_path[i]);
std::filesystem::path exec = input[0];
if (!check_if_rebuild(src_path, exec)) std::vector<std::string> comand = {cpp_compiler, src_path.string(), "-o", exec_path[0], "-g"};
return false;
std::vector<std::string> comand = {"g++", src_path, "-o", exec_path[0], "-g"};
if (!nob_cmd_run_sync(comand)) if (!nob_cmd_run_sync(comand))
return false; return false;
@ -112,6 +136,11 @@ bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec
return false; return false;
printf("rebuild\n"); printf("rebuild\n");
#else
pick_color(Color::red);
printf("rebuild me\n");
pick_color(Color::white);
#endif
return true; return true;
} }
@ -164,14 +193,175 @@ std::vector<std::string> get_includes(const std::filesystem::path &path)
while (std::getline(ifs, line)) while (std::getline(ifs, line))
{ {
if (!line[0] == '#' && !line.empty()) if (line.empty())
continue;
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));
} }
} }
return ret; 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<std::string> 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<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))
{
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<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.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<std::string> 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<std::string> ext_obj, std::vector<std::string> ext_inc)
#elif defined(EXT_OBJ)
void build_as_one(std::vector<std::string> ext_obj)
#elif defined(EXT_INC)
void build_as_one(std::vector<std::string> 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);
}