From 0e5942af3439ee6e7ea89586dda37143b5ce9580 Mon Sep 17 00:00:00 2001
From: Abdelrahman Said <said.abdelrahman@flawlessai.com>
Date: Sun, 13 Apr 2025 20:11:05 +0100
Subject: [PATCH] Switch to using Makefile for *nix systems

---
 Makefile |  57 ++++++++++++++++++++++++++++++
 build    |  19 +++++++++-
 compile  | 103 -------------------------------------------------------
 3 files changed, 75 insertions(+), 104 deletions(-)
 create mode 100644 Makefile
 delete mode 100755 compile

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..6e69657
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,57 @@
+CC           = clang
+BUILD_TYPE   = debug
+CFLAGS       = -Wall -Wextra -Werror -pedantic
+LIBFLAGS     = -fPIC -shared
+KERNEL       = $(shell uname -s)
+MACHINE      = $(shell uname -m)
+PLATFORM     = $(KERNEL)_$(MACHINE)
+TEST_INCLUDE = -Isrc $(shell find tests -type d | xargs -I{} echo -n "-I{} ")
+TEST_SRC     = src/wapp.c $(shell find tests -type f -name "*.c" | xargs -I{} echo -n "{} ")
+BUILD_DIR    = libwapp-build/$(PLATFORM)-$(BUILD_TYPE)
+LIB_OUT      = $(BUILD_DIR)/libwapp.so
+TEST_OUT     = $(BUILD_DIR)/wapptest
+
+ifeq ($(BUILD_TYPE),debug)
+	CFLAGS += -g -fsanitize=address,undefined
+else ifeq ($(BUILD_TYPE),release)
+	CFLAGS += -O3
+else
+	$(error Invalid BUILD type '$(BUILD_TYPE)'. Use 'debug' or 'release')
+endif
+
+ifeq ($(CC),gcc)
+  # Used to disable the "ASan runtime does not come first in initial library list" error when compiling with gcc
+  export ASAN_OPTIONS=verify_asan_link_order=0
+endif
+
+.PHONY: all clean builddir codegen build-test run-test build-lib full testing core
+
+all: clean builddir codegen run-test full
+
+clean:
+	@rm -rf $(BUILD_DIR)
+
+builddir:
+	@mkdir -p $(BUILD_DIR)
+
+codegen:
+	python3 -m codegen
+
+build-test:
+	$(CC) $(CFLAGS) $(TEST_INCLUDE) $(TEST_SRC) -o $(TEST_OUT)
+
+run-test: build-test
+	@$(TEST_OUT)
+	@rm $(TEST_OUT)
+
+build-lib:
+	$(CC) $(CFLAGS) $(LIBFLAGS) $(LIB_SRC) -o $(LIB_OUT)
+
+full: LIB_SRC = src/wapp.c
+full: build-lib
+
+testing: LIB_SRC = src/testing/wapp_testing.c
+testing: build-lib
+
+core: LIB_SRC = src/core/wapp_core.c
+core: build-lib
diff --git a/build b/build
index cd3a103..7087aab 100755
--- a/build
+++ b/build
@@ -1,3 +1,20 @@
 #!/bin/bash
 
-bear -- ./compile $@
+BUILD_TYPE="debug"
+ARGS=""
+
+while [[ $# > 0 ]];do
+  case $1 in
+    --release)
+      BUILD_TYPE="release"
+      shift
+      ;;
+    *|-*|--*)
+      rest=("$@")
+      ARGS+=" ${rest[0]}"
+      shift
+      ;;
+  esac
+done
+
+bear -- make BUILD_TYPE=$BUILD_TYPE $ARGS
diff --git a/compile b/compile
deleted file mode 100755
index 3d782cd..0000000
--- a/compile
+++ /dev/null
@@ -1,103 +0,0 @@
-#!/bin/bash
-
-BUILD_TYPE="debug"
-COMPONENTS="all"
-
-function join_array {
-  DELIM="$1"
-  shift
-  local IFS="$DELIM"
-  echo "$*"
-}
-
-SUPPORTED_COMPONENTS=("all" "testing" "core")
-COMPONENTS_STRING="$(join_array "|" ${SUPPORTED_COMPONENTS[@]})"
-
-while [[ $# > 0 ]];do
-  case $1 in
-    --release)
-      BUILD_TYPE="release"
-      shift
-      ;;
-    --components)
-      COMPONENTS="$2"
-      shift
-      shift
-      ;;
-    *|-*|--*)
-      echo "Unknown option $1"
-      echo "Usage: $0 [--release] [--components $COMPONENTS_STRING (Default: all)]"
-      exit 1
-      ;;
-  esac
-done
-
-CC=clang
-CFLAGS="-Wall -Wextra -Werror -pedantic"
-LIBFLAGS="-fPIC -shared"
-KERNEL="$(uname -s)"
-MACHINE="$(uname -m)"
-PLATFORM="${KERNEL}_${MACHINE}"
-
-if [[ $CC == "gcc" ]]; then
-  # Used to disable the "ASan runtime does not come first in initial library list" error when compiling with gcc
-  export ASAN_OPTIONS=verify_asan_link_order=0
-fi
-
-case $COMPONENTS in
-  all)
-    SRC="src/wapp.c"
-    ;;
-  testing)
-    SRC="src/testing/wapp_testing.c"
-    ;;
-  core)
-    SRC="src/core/wapp_core.c"
-    ;;
-  *)
-    echo "Unrecognised option for components: $COMPONENTS"
-    echo "Accepted options: $COMPONENTS_STRING"
-    exit 2
-    ;;
-esac
-
-# Run code generation
-(set -x ; python3 -m codegen)
-
-TEST_INCLUDE="-Isrc $(find tests -type d | xargs -I{} echo -n "-I{} ")"
-TEST_SRC="$(find tests -type f -name "*.c" | xargs -I{} echo -n "{} ")"
-
-BUILD_DIR="libwapp-build/$PLATFORM-$BUILD_TYPE"
-if [[ -d $BUILD_DIR ]]; then
-  rm -rf $BUILD_DIR
-fi
-mkdir -p $BUILD_DIR
-
-if [[ $BUILD_TYPE == "release" ]]; then
-  CFLAGS+=" -O3"
-else
-  CFLAGS+=" -g -fsanitize=address,undefined"
-fi
-
-OUT="$BUILD_DIR/libwapp.so"
-TEST_OUT="$BUILD_DIR/wapptest"
-
-# Compile tests
-if [[ $(echo $TEST_SRC | xargs) != "" ]]; then
-  (set -x ; $CC $CFLAGS $TEST_INCLUDE $SRC $TEST_SRC -o $TEST_OUT)
-fi
-
-# Run tests and exit on failure
-if [[ -f $TEST_OUT ]]; then
-  $TEST_OUT
-  STATUS="$?"
-
-  rm $TEST_OUT
-
-  if [[ $STATUS != "0" ]]; then
-    exit 1
-  fi
-fi
-
-# Compile library
-(set -x ; $CC $CFLAGS $LIBFLAGS $SRC -o $OUT)