Update build system

This commit is contained in:
Nikola Petrov 2024-03-30 00:39:00 +01:00
parent e3cc91359e
commit 62f98d381a
7 changed files with 161 additions and 86 deletions

1
.gitignore vendored
View File

@ -2,4 +2,5 @@ main
obj/
rl/
raylib/
emsdk/
build

34
.vscode/settings.json vendored
View File

@ -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"
}
}

6
.vscode/tasks.json vendored
View File

@ -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"
]
}
]

View File

@ -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 .

View File

@ -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:

103
build.cpp
View File

@ -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<std::string> command;
std::vector<std::string> RAYINCLUDE = {"-Iraylib/src", "-Iraylib/src/external/glfw/include", "-Iraylib/src/external/glfw/deps/mingw"};
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()
{
@ -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;
}

View File

@ -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<std::string> 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<std::string> comand = {"g++", src_path, "-o", exec_path, "-g"};
std::vector<std::string> 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");