From d883445bd11364afc6c1683c8df1b04f4f488491 Mon Sep 17 00:00:00 2001
From: Emil <emilwilliams@tuta.io>
Date: Mon, 21 Aug 2023 07:05:27 -0600
Subject: [PATCH 1/4] SURPRISE Build System Update

---
 Makefile             | 41 +++++++++++++++++++++++++----------------
 chad.mk              | 22 ----------------------
 config.mk            | 36 ++++++++++++++++++++++++++++++++++++
 source/hl_xolatile.c |  2 ++
 source/main.c        |  4 +++-
 5 files changed, 66 insertions(+), 39 deletions(-)
 delete mode 100644 chad.mk
 create mode 100644 config.mk

diff --git a/Makefile b/Makefile
index b364514..3b90c07 100644
--- a/Makefile
+++ b/Makefile
@@ -1,27 +1,36 @@
-include chad.mk
-DEBUG:=1
-CFLAGS:=-std=c99 -O2 $(if ${DEBUG}, ${CHAD_DEBUG},'')
-CPPFLAGS:=-D_FORTIFY_SOURCE=2
+include config.mk
+
+SRC.dir:=source
+OBJ.dir:=obj
 
-SRC.dir:=source/
 SRC:=$(shell find ${SRC.dir} -iname '*.c')
 HDR:=$(shell find ${SRC.dir} -iname '*.h')
-OBJ:=$(subst .c,.o,${SRC})
+OBJ:=$(SRC:.c=.o)
 
-OUT:=hl
-OUTARGS:=${OUT} < source/main.c
+VPATH=${SRC.dir} ${OBJ.dir}
 
-main: ${OBJ} ${HDR}
-	${LINK.c} ${OBJ} -o hl
-
-%.o: %.c
+${OBJ.dir}/%.o: ${SRC.dir}/%.c
 	${COMPILE.c} $< -o $@
 
-install:
-	cp hl /usr/bin/hl
+${TARGET}: ${OBJ} | ${HDR}
+	${LINK.c} $+ -o $@
+
+${SRC} ${HDR}:
+
+${PREFIX} ${OBJ.dir}:
+	mkdir -p $@
+
+install: ${PREFIX}
+	install -v -g ${USER} -o ${USER} -m 744 ${TARGET} ${PREFIX}/bin/
+
+uninstall:
+	-rm ${PREFIX}/bin/${TARGET}
 
 clean:
-	-rm ${OBJ}
-	-rm ${OUT}
+	-rm ${OBJ} ${TARGET}
 
 test: chad_test
+
+.PHONY: test clean install
+
+.DEFAULT_GOAL:=${TARGET}
diff --git a/chad.mk b/chad.mk
deleted file mode 100644
index 0b30673..0000000
--- a/chad.mk
+++ /dev/null
@@ -1,22 +0,0 @@
-# Make script for Chad projects
-# This script depends on the following variables
-#  - OUT       : output program name
-#  - OUTARGS   : default flags to fork ${OUT} with
-
-#
-CHAD_DEBUG:=-Og -ggdb -pg -fno-inline
-
-# Programs to check warnings for as defined by the Chad standard
-GCC:=gcc
-GCC.warnings:=-Wall -Wextra -Wpedantic -Wvla -Wshadow -Wundef 
-CLANG:=clang
-CLANG.warnings:=-Weverything
-VALGRIND:=valgrind
-VALGRIND.flags:=--track-origins=yes --leak-check=full --show-leak-kinds=all
-
-chad_test:
-	${GCC} ${GCC.warnings} ${SRC} -o ${OUT}
-	${CLANG} ${GCC.warnings} ${SRC} -o ${OUT}
-	${VALGRIND} ${VALGRIND.flags} ${OUT} ${OUTARGS}
-
-.DEFAULT_GOAL:=main
diff --git a/config.mk b/config.mk
new file mode 100644
index 0000000..019ce15
--- /dev/null
+++ b/config.mk
@@ -0,0 +1,36 @@
+# Make script for Chad projects
+# This script depends on the following variables
+#  - TARGET    : output program name
+#  - ARGS      : default flags to fork ${OUT} with
+
+PREFIX:=/usr/bin
+
+CFLAGS:=-std=c99
+CPPFLAGS:=-D_GNU_SOURCE -D_FORTIFY_SOURCE=2
+
+DEBUG=1
+
+ifeq (${DEBUG},1)
+	CFLAGS += -Og -ggdb -pg -fno-inline
+else
+	CFLAGS += -O2 -flto=auto
+endif
+
+# Programs to check warnings for as defined by the chad standard
+
+GCC:=gcc
+GCC.warnings:=-Wall -Wextra -Wpedantic -Wvla -Wshadow -Wundef
+
+CLANG:=clang
+CLANG.warnings:=-Weverything
+
+VALGRIND:=valgrind
+VALGRIND.flags:=--track-origins=yes --leak-check=full --show-leak-kinds=all
+
+TARGET:=hl
+ARGS:=${TARGET} < source/main.c
+
+chad_test:
+	${GCC} ${GCC.warnings} ${SRC} -o ${TARGET}
+	${CLANG} ${GCC.warnings} ${SRC} -o ${TARGET}
+	${VALGRIND} ${VALGRIND.flags} ${TARGET} ${ARGS}
diff --git a/source/hl_xolatile.c b/source/hl_xolatile.c
index 3a5054c..a264c20 100755
--- a/source/hl_xolatile.c
+++ b/source/hl_xolatile.c
@@ -1,3 +1,4 @@
+#if 0
 // Listen, this is still prototype, it's not code-merged with other stuff...
 // Once I prototype out more stuff, this will use chad.h and hl.h...
 // So, this file can run on its' own when compiled, gonna rewrite later.
@@ -244,3 +245,4 @@ int main(int      argc,
 
 	return 0;
 }
+#endif
diff --git a/source/main.c b/source/main.c
index a43c8cf..889eee6 100644
--- a/source/main.c
+++ b/source/main.c
@@ -1,5 +1,6 @@
 //register
 //putchar()
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -52,7 +53,8 @@ int main(int      argc,
 			buffer = realloc(buffer, ++chunks * ALLOCATION_CHUNK);
 		}
 		buffer[buffer_size] = '\0';
-		read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer));
+		/* TODO handle me */
+		assert(read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer)) != -1);
 		++buffer_size;
 	} while (buffer[buffer_size - 1]);
 

From b21b46e825d44540f229a8d94ce686de752f806f Mon Sep 17 00:00:00 2001
From: Emil <emilwilliams@tuta.io>
Date: Mon, 21 Aug 2023 07:19:40 -0600
Subject: [PATCH 2/4] Isolate chad.mk

---
 Makefile      |  1 +
 tests/chad.mk | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 tests/chad.mk

diff --git a/Makefile b/Makefile
index 3b90c07..4bbf042 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
 include config.mk
+include tests/chad.mk
 
 SRC.dir:=source
 OBJ.dir:=obj
diff --git a/tests/chad.mk b/tests/chad.mk
new file mode 100644
index 0000000..fe9fa97
--- /dev/null
+++ b/tests/chad.mk
@@ -0,0 +1,18 @@
+# Programs to check warnings for as defined by the chad standard
+
+GCC:=gcc
+GCC.warnings:=-Wall -Wextra -Wpedantic -Wvla -Wshadow -Wundef
+
+CLANG:=clang
+CLANG.warnings:=-Weverything
+
+VALGRIND:=valgrind
+VALGRIND.flags:=--track-origins=yes --leak-check=full --show-leak-kinds=all
+
+TARGET:=hl
+ARGS:=${TARGET} < source/main.c
+
+chad_test:
+	${GCC} ${GCC.warnings} ${SRC} -o ${TARGET}
+	${CLANG} ${GCC.warnings} ${SRC} -o ${TARGET}
+	${VALGRIND} ${VALGRIND.flags} $(shell pwd)/${TARGET} ${ARGS}

From ce284f667b0a1e82b306a467b094575fe02ba642 Mon Sep 17 00:00:00 2001
From: Emil <emilwilliams@tuta.io>
Date: Mon, 21 Aug 2023 07:25:14 -0600
Subject: [PATCH 3/4] Fixed not isolating objects in obj

---
 Makefile  |  6 +++---
 config.mk | 19 -------------------
 2 files changed, 3 insertions(+), 22 deletions(-)

diff --git a/Makefile b/Makefile
index 4bbf042..7098c86 100644
--- a/Makefile
+++ b/Makefile
@@ -6,15 +6,15 @@ OBJ.dir:=obj
 
 SRC:=$(shell find ${SRC.dir} -iname '*.c')
 HDR:=$(shell find ${SRC.dir} -iname '*.h')
-OBJ:=$(SRC:.c=.o)
+OBJ:=$(subst $(SRC.dir),$(OBJ.dir),$(SRC:.c=.o))
 
 VPATH=${SRC.dir} ${OBJ.dir}
 
 ${OBJ.dir}/%.o: ${SRC.dir}/%.c
 	${COMPILE.c} $< -o $@
 
-${TARGET}: ${OBJ} | ${HDR}
-	${LINK.c} $+ -o $@
+${TARGET}: ${HDR} ${OBJ.dir} | ${OBJ}
+	${LINK.c} $| -o $@
 
 ${SRC} ${HDR}:
 
diff --git a/config.mk b/config.mk
index 019ce15..f883c75 100644
--- a/config.mk
+++ b/config.mk
@@ -15,22 +15,3 @@ ifeq (${DEBUG},1)
 else
 	CFLAGS += -O2 -flto=auto
 endif
-
-# Programs to check warnings for as defined by the chad standard
-
-GCC:=gcc
-GCC.warnings:=-Wall -Wextra -Wpedantic -Wvla -Wshadow -Wundef
-
-CLANG:=clang
-CLANG.warnings:=-Weverything
-
-VALGRIND:=valgrind
-VALGRIND.flags:=--track-origins=yes --leak-check=full --show-leak-kinds=all
-
-TARGET:=hl
-ARGS:=${TARGET} < source/main.c
-
-chad_test:
-	${GCC} ${GCC.warnings} ${SRC} -o ${TARGET}
-	${CLANG} ${GCC.warnings} ${SRC} -o ${TARGET}
-	${VALGRIND} ${VALGRIND.flags} ${TARGET} ${ARGS}

From f92a06b4107373f7e2b30fac61cb05e59d95a7e8 Mon Sep 17 00:00:00 2001
From: Emil <emilwilliams@tuta.io>
Date: Mon, 21 Aug 2023 07:30:48 -0600
Subject: [PATCH 4/4] Restructuring of build system

---
 Makefile                 | 4 ++--
 tests/chad.mk => chad.mk | 0
 2 files changed, 2 insertions(+), 2 deletions(-)
 rename tests/chad.mk => chad.mk (100%)

diff --git a/Makefile b/Makefile
index 7098c86..7d81f1a 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
 include config.mk
-include tests/chad.mk
+include chad.mk
 
 SRC.dir:=source
-OBJ.dir:=obj
+OBJ.dir:=object
 
 SRC:=$(shell find ${SRC.dir} -iname '*.c')
 HDR:=$(shell find ${SRC.dir} -iname '*.h')
diff --git a/tests/chad.mk b/chad.mk
similarity index 100%
rename from tests/chad.mk
rename to chad.mk