From f44c6f69145ef678d9ae13831769c886f0d1fc22 Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 12 Jan 2025 15:11:05 +0100 Subject: [PATCH] switch to a makefile build system --- .gitignore | 3 ++- build.sh | 71 ------------------------------------------------------ makefile | 36 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 72 deletions(-) delete mode 100755 build.sh create mode 100644 makefile diff --git a/.gitignore b/.gitignore index 2575ab0..8a116e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode/* !.vscode/tasks.json !.vscode/launch.json -build +bin +obj diff --git a/build.sh b/build.sh deleted file mode 100755 index 756d52f..0000000 --- a/build.sh +++ /dev/null @@ -1,71 +0,0 @@ -#!/bin/bash - -# define variables -PROJECT_NAME=${PWD##*/} -BUILD_DIR="./build" -INCLUDE_IN_BUILD="./assets" - -# compilation targets -args_linux86_64() -{ - ARCHITECTURE="linux-86_64" - COMPILER="/bin/gcc" - ARGS="-lSDL2" - FILE_EXSTENSION="" -} - -args_win86-64() -{ - ARCHITECTURE="win-86_64" - COMPILER="/usr/bin/x86_64-w64-mingw32-gcc" - ARGS="-lmingw32 -lSDL2main -lSDL2 -mwindows" - INCLUDE_IN_BUILD="$INCLUDE_IN_BUILD /usr/x86_64-w64-mingw32/bin/SDL2.dll" - FILE_EXSTENSION=".exe" -} - -args_emscripten() -{ - ARCHITECTURE="web" - COMPILER="emcc" # just make sure it's in $PATH (it's a pain to install) - ARGS="-s USE_SDL=2" - FILE_EXSTENSION=".html" -} - -# handle arguments -if [ "$1" = "linux86_64" ]; then args_linux86_64 -elif [ "$1" = "win86_64" ]; then args_win86-64 -elif [ "$1" = "web" ]; then args_emscripten -else echo -e "\033[91mdidn't include any arguments! D:\033[0m" && exit 1 -fi - -# check whether $BUILD_DIR or $ARCHITECTURE isn't set -if [[ -z $BUILD_DIR ]] || [[ -z "$ARCHITECTURE" ]]; then - echo -e "\033[91mBUILD_DIR or ARCHITECTURE not set D:\033[0m" - exit 1 -fi - - -# make (and clear) the build directory -mkdir -p "$BUILD_DIR/$ARCHITECTURE" -rm -rf "${BUILD_DIR:?}/$ARCHITECTURE/*" - -# copy included files or directories to the build directory -if [[ -n $INCLUDE_IN_BUILD ]]; then - cp -r "$INCLUDE_IN_BUILD" "$BUILD_DIR/$ARCHITECTURE" -fi - -# get the executable path -EXE_PATH=$BUILD_DIR/$ARCHITECTURE/$PROJECT_NAME$FILE_EXSTENSION -echo "building at: $EXE_PATH" - -# check whether the compiler can actually be executed -if [ ! -x "$COMPILER" ] && ! command -v "$COMPILER" > /dev/null; then - echo -e "\033[91mCouldn't find an executable at path: \033[0m $COMPILER" - exit 1 -fi - -# compile the code -COMMAND="$COMPILER $(find ./src -name "*.c") -o $EXE_PATH -Wall -g -lm $ARGS" -echo "using command: $COMMAND" -$COMMAND -exit $? diff --git a/makefile b/makefile new file mode 100644 index 0000000..db1601a --- /dev/null +++ b/makefile @@ -0,0 +1,36 @@ +NAME = sdl_template +CFLAGS = -Wall -g -march=x86-64 -lm + +DIR_BIN = bin +DIR_OBJ = obj + +SRC = $(wildcard src/*.c) $(wildcard src/**/*.c) $(wildcard src/**/**/*.c) $(wildcard src/**/**/**/*.c) $(wildcard src/**/**/**/**/*.c) +OBJ = $(patsubst src/%,$(DIR_OBJ)/$(ARCH)/%,$(SRC:.c=.o)) + +TARGET = $(DIR_BIN)/$(ARCH)/$(NAME)$(EXT) + +clean: + rm -rf $(DIR_BIN) $(DIR_OBJ) + +# sets the variables for the different targets +linux-x86_64: + $(MAKE) build ARCH=linux-x86_64 CC=gcc PCFLAGS="-lSDL2" +win86-64: + $(MAKE) build ARCH=win-x86-64 CC=x86_64-w64-mingw32-gcc PCFLAGS="-lmingw32 -lSDL2main -lSDL2 -mwindows" EXT=".exe" +web: + $(MAKE) build ARCH=web CC=emcc PCFLAGS="-s USE_SDL=2" EXT=".html" + +build: dirs binary + +# creates the binary +binary: $(OBJ) + $(CC) -o $(TARGET) $^ $(CFLAGS) $(PCFLAGS) + +# creates the object files +$(DIR_OBJ)/$(ARCH)/%.o: src/%.c + mkdir -p $(dir $@) + $(CC) -o $@ -c $< $(CFLAGS) $(PCFLAGS) + +dirs: + mkdir -p $(DIR_BIN)/$(ARCH) + mkdir -p $(DIR_OBJ)/$(ARCH)