diplomska_naloga/build.cpp
Nikola Petrov e62b0eb6bf Change fro, RL_QUADS to RL_TRIANGLES
for web support
2024-03-31 00:35:33 +01:00

246 lines
5.7 KiB
C++

#include "build.hpp"
std::filesystem::path OBJ_DIR = "obj";
std::filesystem::path INC_DIR = "inc";
std::filesystem::path SRC_DIR = "src";
std::filesystem::path RAYLIB_DIR = "raylib";
std::filesystem::path EMSDK_DIR = "emsdk";
std::filesystem::path BUILD_FILE = "main";
bool web_build = false;
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<std::string> command;
std::vector<std::string> RAYINCLUDE = {"-Iraylib/src"};
void clear_build_dir(bool raylib, bool src)
{
if (raylib)
{
std::filesystem::remove_all(OBJ_DIR / SRC_DIR);
}
if (src)
{
std::filesystem::remove_all(OBJ_DIR / RAYLIB_DIR);
}
if (src && raylib)
std::filesystem::remove(OBJ_DIR);
std::filesystem::remove(BUILD_FILE);
std::filesystem::remove(BUILD_FILE.replace_extension(".html"));
std::filesystem::remove(BUILD_FILE.replace_extension(".js"));
std::filesystem::remove(BUILD_FILE.replace_extension(".wasm"));
BUILD_FILE.replace_extension("");
}
void compile_raylib_dir()
{
std::filesystem::path out_dir = OBJ_DIR / RAYLIB_DIR;
std::filesystem::path ray_src_dir = RAYLIB_DIR / "src";
std::filesystem::create_directory(out_dir);
std::vector<std::filesystem::path> ray_srcs = {"rcore.c", "rshapes.c", "rtextures.c", "rtext.c", "utils.c", "rmodels.c", "raudio.c"};
std::vector<std::string> ray_flags = {"-DPLATFORM_WEB", "-DGRAPHICS_API_OPENGL_ES2"};
if (!web_build)
{
ray_srcs.push_back("rglfw.c");
ray_flags = {"-D_GNU_SOURCE", "-DPLATFORM_DESKTOP", "-DGRAPHICS_API_OPENGL_33"};
RAYINCLUDE.push_back("-Iraylib/src/external/glfw/include");
RAYINCLUDE.push_back("-Iraylib/src/external/glfw/deps/mingw");
}
command.clear();
command.push_back(c_compiler);
command.push_back("-c");
command.push_back("");
command.push_back("-o");
command.push_back("");
command.insert(command.end(), ray_flags.begin(), ray_flags.end());
command.push_back(opt_flags);
command.insert(command.end(), RAYINCLUDE.begin(), RAYINCLUDE.end());
std::filesystem::path out, src;
for (size_t i = 0; i < ray_srcs.size(); i++)
{
src = ray_src_dir / ray_srcs[i];
out = out_dir / ray_srcs[i].replace_extension(".o");
if (check_if_rebuild(src, out))
{
command[2] = src.c_str();
command[4] = out.c_str();
nob_cmd_run_sync(command);
}
}
}
bool 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);
}
for (auto &&i : obj_dir.dirs)
{
std::filesystem::create_directory(i);
}
command.clear();
command.push_back(cpp_compiler);
command.push_back("-c");
command.push_back("");
command.push_back("-o");
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");
bool build = false;
for (size_t i = 0; i < src_dir.files.size(); i++)
{
if (check_if_rebuild(src_dir.files[i], obj_dir.files[i]))
{
command[2] = src_dir.files[i];
command[4] = obj_dir.files[i];
if (!nob_cmd_run_sync(command))
return false;
build = true;
}
}
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);
if (web_build)
{
command.push_back("-s");
command.push_back("USE_GLFW=3");
command.push_back("--shell-file");
command.push_back("raylib/src/minshell.html");
}
nob_cmd_run_sync(command);
}
int main(int argc, char const *argv[])
{
if (rebuild_my_self(__FILE__, argc, argv))
return 0;
for (int i = 1; i < argc; ++i)
{
if (!std::strcmp(argv[i], "web"))
web_build = true;
if (!std::strcmp(argv[i], "opt"))
opt_flags = "-O3";
if (!std::strcmp(argv[i], "build"))
build = true;
if (!std::strcmp(argv[i], "run"))
run = true;
if (!std::strcmp(argv[i], "cla"))
{
clear_build_dir(true, true);
clear = true;
}
if (!std::strcmp(argv[i], "clr"))
{
clear_build_dir(false, true);
clear = true;
}
if (!std::strcmp(argv[i], "cls"))
{
clear_build_dir(true, false);
clear = true;
}
}
if (clear && !build)
{
return 0;
}
if (!std::filesystem::is_directory(RAYLIB_DIR))
{
command = {"git", "clone", "--depth", "1", "--branch", "5.0", "git@github.com:raysan5/raylib.git"};
nob_cmd_run_sync(command);
std::filesystem::remove_all("raylib/.git");
}
if (!std::filesystem::is_directory(RAYLIB_DIR))
return 0;
if (!std::filesystem::is_directory(EMSDK_DIR) && web_build)
{
command = {"git", "clone", "https://github.com/emscripten-core/emsdk.git"};
nob_cmd_run_sync(command);
std::filesystem::remove_all("emsdk/.git");
return 0;
}
if (web_build)
{
cpp_compiler = "em++";
c_compiler = "emcc";
opt_flags = "-Os";
RAYINCLUDE.push_back("-DPLATFORM_WEB");
BUILD_FILE = BUILD_FILE.replace_extension(".html");
}
std::filesystem::create_directory(OBJ_DIR);
compile_raylib_dir();
if (compile_src_dir())
compile_obj_dir();
if (!std::filesystem::exists(BUILD_FILE))
compile_obj_dir();
if (!web_build && run)
{
command = {"./" + BUILD_FILE.string()};
nob_cmd_run_sync(command);
}
return 0;
}