-OBJ = \
- util.o \
- code.o \
- ast.o \
- ir.o
-OBJ_A = test/ast-test.o
-OBJ_I = test/ir-test.o
-OBJ_C = main.o lexer.o parser.o
-
-#default is compiler only
-default: gmqcc
-%.o: %.c
- $(CC) -c $< -o $@ $(CFLAGS)
-
-# test targets
-test_ast: $(OBJ_A) $(OBJ)
- $(CC) -o $@ $^ $(CFLAGS)
-test_ir: $(OBJ_I) $(OBJ)
- $(CC) -o $@ $^ $(CFLAGS)
-test: test_ast test_ir
-
-# compiler target
-gmqcc: $(OBJ_C) $(OBJ)
- $(CC) -o $@ $^ $(CFLAGS)
-
-#all target is test and all
-all: test gmqcc
+
+ifeq ($(shell valgrind --version 2>/dev/null),)
+ CFLAGS += -DNVALGRIND
+endif
+
+# do this last otherwise there is whitespace in the command output and
+# it makes my OCD act up
+CFLAGS += $(OPTIONAL)
+
+#we have duplicate object files when dealing with creating a simple list
+#for dependinces. To combat this we use some clever recrusive-make to
+#filter the list and remove duplicates which we use for make depend
+RMDUP = $(if $1,$(firstword $1) $(call RMDUP,$(filter-out $(firstword $1),$1)))
+DEPS := $(call RMDUP, $(OBJ_P) $(OBJ_T) $(OBJ_C) $(OBJ_X))
+
+ifneq ("$(CYGWIN)", "")
+ #nullify the common variables that
+ #most *nix systems have (for windows)
+ PREFIX :=
+ BINDIR :=
+ DATADIR :=
+ MANDIR :=
+ QCVM = qcvm.exe
+ GMQCC = gmqcc.exe
+ TESTSUITE = testsuite.exe
+ PAK = gmqpak.exe
+ CFLAGS += -DNVALGRIND
+else
+ifneq ("$(MINGW)", "")
+ #nullify the common variables that
+ #most *nix systems have (for windows)
+ PREFIX :=
+ BINDIR :=
+ DATADIR :=
+ MANDIR :=
+ QCVM = qcvm.exe
+ GMQCC = gmqcc.exe
+ TESTSUITE = testsuite.exe
+ PAK = gmqpak.exe
+ CFLAGS += -DNVALGRIND
+else
+ QCVM = qcvm
+ GMQCC = gmqcc
+ TESTSUITE = testsuite
+ PAK = gmqpak
+endif
+endif
+
+#standard rules
+c.o:
+ $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
+
+exec-standalone.o: exec.c
+ $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) -DQCVM_EXECUTOR=1
+
+$(QCVM): $(OBJ_X)
+ $(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
+
+$(GMQCC): $(OBJ_C) $(OBJ_D)
+ $(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
+
+$(TESTSUITE): $(OBJ_T)
+ $(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
+
+$(PAK): $(OBJ_P)
+ $(CC) -o $@ $^ $(LDFLAGS)
+
+all: $(GMQCC) $(QCVM) $(TESTSUITE) $(PAK)
+
+check: all
+ @ ./$(TESTSUITE)
+test: all
+ @ ./$(TESTSUITE)