From 62f98d381a0df66af80b266ff2325c10941ac036 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Sat, 30 Mar 2024 00:39:00 +0100 Subject: [PATCH] Update build system --- .gitignore | 1 + .vscode/settings.json | 34 +++++++++++++- .vscode/tasks.json | 6 +-- Makefile | 73 ------------------------------ Readme.md | 15 ++++++ build.cpp | 103 ++++++++++++++++++++++++++++++++++++++++-- build.hpp | 15 ++++-- 7 files changed, 161 insertions(+), 86 deletions(-) delete mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 8a7df3f..e208add 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ main obj/ rl/ raylib/ +emsdk/ build \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 237f0d6..e56356f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -46,6 +46,38 @@ "cinttypes": "cpp", "typeinfo": "cpp", "ctime": "cpp", - "list": "cpp" + "list": "cpp", + "chrono": "cpp", + "codecvt": "cpp", + "cstring": "cpp", + "ratio": "cpp", + "iomanip": "cpp", + "sstream": "cpp", + "any": "cpp", + "strstream": "cpp", + "barrier": "cpp", + "bitset": "cpp", + "charconv": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "map": "cpp", + "set": "cpp", + "unordered_set": "cpp", + "optional": "cpp", + "regex": "cpp", + "fstream": "cpp", + "future": "cpp", + "mutex": "cpp", + "semaphore": "cpp", + "shared_mutex": "cpp", + "stop_token": "cpp", + "thread": "cpp", + "cfenv": "cpp", + "valarray": "cpp", + "__bit_reference": "cpp", + "__config": "cpp", + "__split_buffer": "cpp", + "__verbose_abort": "cpp", + "locale": "cpp" } } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 94db072..066e96a 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "label": "build", "type": "shell", - "command": "make", + "command": "./build", "args": [ "" ] @@ -12,9 +12,9 @@ { "label": "build-deb", "type": "shell", - "command": "make", + "command": "./build", "args": [ - "main" + "build" ] } ] diff --git a/Makefile b/Makefile deleted file mode 100644 index 4545015..0000000 --- a/Makefile +++ /dev/null @@ -1,73 +0,0 @@ -RAYFLAGS= -D_GNU_SOURCE -DPLATFORM_DESKTOP -DGRAPHICS_API_OPENGL_33 -RAYINCLUDE= -Iraylib/src -Iraylib/src/external/glfw/include -Iraylib/src/external/glfw/deps/mingw -RAYOBJECTS= obj/rcore.o obj/rshapes.o obj/rtextures.o obj/rtext.o obj/utils.o obj/rglfw.o obj/rmodels.o obj/raudio.o - -#RAYOPT= -O3 -RAYOPT= -ggdb - -all: clean setup main run - -setup: - if [ ! -d "raylib" ]; then \ - git clone --depth 1 --branch "5.0" git@github.com:raysan5/raylib.git; \ - rm -rf raylib/.git; \ - fi - if [ ! -d "obj" ]; then \ - mkdir -p obj; \ - fi - -obj/rcore.o: raylib/src/rcore.c - gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE) - -obj/rshapes.o: raylib/src/rshapes.c - gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE) - -obj/rtextures.o: raylib/src/rtextures.c - gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE) - -obj/rtext.o: raylib/src/rtext.c - gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE) - -obj/utils.o: raylib/src/utils.c - gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE) - -obj/rglfw.o: raylib/src/rglfw.c - gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE) - -obj/rmodels.o: raylib/src/rmodels.c - gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE) - -obj/raudio.o: raylib/src/raudio.c - gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE) - -SRCDIR = src -OBJDIR = obj -INCLUDE = -Iinc - -CFLAGS= -std=c++23 -Wall -Werror - -SOURCES = $(wildcard $(SRCDIR)/*.cpp) - -OBJECTS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SOURCES)) - -$(OBJDIR)/%.o: $(SRCDIR)/%.cpp - g++ -c $< -o $@ $(RAYOPT) $(RAYINCLUDE) $(INCLUDE) $(CFLAGS) - -main: setup $(RAYOBJECTS) $(OBJECTS) - g++ $(RAYOBJECTS) $(OBJECTS) -o main $(RAYOPT) $(RAYINCLUDE) $(INCLUDE) $(CFLAGS) - -example: example.cpp setup $(RAYOBJECTS) $(OBJECTS) - g++ example.cpp $(RAYOBJECTS) $(OBJECTS) -o example $(RAYOPT) $(RAYINCLUDE) $(INCLUDE) $(CFLAGS) - -run: main - ./main - -clean: - rm -rf $(OBJECTS) - rm -f main example - -cleanAll: clean - rm -rf raylib - -countLine: - cloc --exclude-dir=raylib --exclude-lang=JSON,make,Markdown . \ No newline at end of file diff --git a/Readme.md b/Readme.md index cfaa8b7..9e6a61a 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,18 @@ +# how to build +``` +g++ build.cpp -o build +``` + +after first emsdk clone run this + +``` +cd emsdk +./emsdk install latest +./emsdk activate latest +source ./emsdk_env.sh +cd .. +``` + # Code style ## Naming Conventions: diff --git a/build.cpp b/build.cpp index c1dcfb6..7b36509 100644 --- a/build.cpp +++ b/build.cpp @@ -4,15 +4,38 @@ 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; std::string cpp_compiler = "g++"; std::string c_compiler = "gcc"; std::string opt_flags = "-ggdb"; std::vector command; -std::vector RAYINCLUDE = {"-Iraylib/src", "-Iraylib/src/external/glfw/include", "-Iraylib/src/external/glfw/deps/mingw"}; +std::vector 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() { @@ -26,6 +49,8 @@ void compile_raylib_dir() { 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(); @@ -118,30 +143,100 @@ void compile_obj_dir() 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__, argv[0])) + 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], "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; + } + std::filesystem::create_directory(OBJ_DIR); 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) + { + clear_build_dir(true, true); + cpp_compiler = "em++"; + c_compiler = "emcc"; + opt_flags = "-Os"; + RAYINCLUDE.push_back("-DPLATFORM_WEB"); + BUILD_FILE = BUILD_FILE.replace_extension(".html"); + } + compile_raylib_dir(); if (compile_src_dir()) compile_obj_dir(); - command = {"./main"}; - nob_cmd_run_sync(command); + if (!std::filesystem::exists(BUILD_FILE)) + compile_obj_dir(); + if (!web_build && !build) + { + command = {"./" + BUILD_FILE.string()}; + nob_cmd_run_sync(command); + } return 0; } diff --git a/build.hpp b/build.hpp index 23865ae..5e2e465 100644 --- a/build.hpp +++ b/build.hpp @@ -107,17 +107,22 @@ bool check_if_rebuild(std::filesystem::path &org_path, std::filesystem::path &ne return file_stat_one.st_ctime > file_stat_two.st_ctime; } -bool rebuild_my_self(std::filesystem::path src_path, std::filesystem::path exec_path) +bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec_path) { - if (!check_if_rebuild(src_path, exec_path)) + std::vector input; + + for (int i = 0; i < argc; ++i) + input.push_back(exec_path[i]); + std::filesystem::path exec = input[0]; + + if (!check_if_rebuild(src_path, exec)) return false; - std::vector comand = {"g++", src_path, "-o", exec_path, "-g"}; + std::vector comand = {"g++", src_path, "-o", exec_path[0], "-g"}; if (!nob_cmd_run_sync(comand)) return false; - comand = {exec_path}; - if (!nob_cmd_run_sync(comand)) + if (!nob_cmd_run_sync(input)) return false; printf("rebuild\n");