Commit b923f2f97dc26e981c18da93b98bd5e7b44c3b79

Shawn O. Pearce 2008-11-03T17:00:54

Fix Makefile to correctly handle 'make -j4 test' If we have more than one test build running we cannot use the same file for each test case; instead we need to use a per-test path so there aren't any collisions. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

diff --git a/Makefile b/Makefile
index bbd29af..2cb057e 100644
--- a/Makefile
+++ b/Makefile
@@ -28,7 +28,7 @@ all:: $(GIT_LIB)
 clean:
 	rm -f $(GIT_LIB)
 	rm -f src/*.o
-	rm -f tests/*.o tests/*.exe
+	rm -f tests/*.o tests/*.exe tests/*.toc
 	rm -f include/git/config.h
 	rm -rf apidocs
 
@@ -50,22 +50,36 @@ $(GIT_LIB): $(OBJS)
 	rm -f $(LIB)
 	$(AR) cr $(GIT_LIB) $(OBJS)
 
-T_HDR    = tests/test_lib.h
-T_LIB    = tests/test_lib.o
-T_MAIN_C  = tests/test_main.c
-T_MAIN_O = tests/test_main.o
+T_HDR         = tests/test_lib.h
+T_LIB         = tests/test_lib.o
+T_MAIN_C      = tests/test_main.c
 
-$(T_LIB): tests/test_lib.h $(HDRS)
-$(TEST_EXE): $(T_LIB) $(T_HDR) $(T_MAIN_C) $(HDRS) $(GIT_LIB)
+$(T_LIB):    $(T_HDR) $(HDRS)
+$(TEST_OBJ): $(T_HDR) $(HDRS)
 
-tests/%.exe: tests/%.o
-	grep BEGIN_TEST $(patsubst %.o,%.c,$<) >tests/test_contents
-	$(CC) $(CFLAGS) -Iinclude -c $(T_MAIN_C) -o $(T_MAIN_O)
-	$(CC) -o $@ $(T_MAIN_O) $< $(T_LIB) -L. -lgit2
-	rm -f $(T_MAIN_O) tests/test_contents
+$(patsubst %.exe,%.toc,$(TEST_EXE)): tests/%.toc: tests/%.c
+	grep BEGIN_TEST $< >$@+
+	mv $@+ $@
 
-$(TEST_RUN): $(TEST_EXE)
-	$<
+$(TEST_OBJ): tests/%.o: tests/%.c
+	$(CC) -Iinclude $(CFLAGS) -c $< -o $@
+
+$(patsubst %.exe,%_main.o,$(TEST_EXE)): tests/%_main.o: $(HDRS)
+$(patsubst %.exe,%_main.o,$(TEST_EXE)): tests/%_main.o: $(T_MAIN_C)
+$(patsubst %.exe,%_main.o,$(TEST_EXE)): tests/%_main.o: tests/%.toc
+	$(CC) -Iinclude -I. '-DTEST_TOC="$<"' \
+		-c $(T_MAIN_C) \
+		-o $@
+
+$(TEST_EXE): tests/%.exe: $(T_LIB) $(GIT_LIB)
+$(TEST_EXE): tests/%.exe: tests/%.o tests/%_main.o
+	$(CC) -o $@ \
+		$(patsubst %.exe,%_main.o,$@) \
+		$(patsubst %.exe,%.o,$@) \
+		$(T_LIB) -L. -lgit2
+
+$(TEST_RUN): tests/%.run: tests/%.exe
+	@$<
 
 .PHONY: all
 .PHONY: clean
diff --git a/tests/.gitignore b/tests/.gitignore
new file mode 100644
index 0000000..690624b
--- /dev/null
+++ b/tests/.gitignore
@@ -0,0 +1 @@
+*.toc
diff --git a/tests/test_main.c b/tests/test_main.c
index a870abd..6267cbc 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -27,13 +27,13 @@
 
 #undef BEGIN_TEST
 #define BEGIN_TEST(name) extern void testfunc__##name(void);
-#include "test_contents"
+#include TEST_TOC
 
 int main(int argc, char **argv)
 {
 #undef BEGIN_TEST
 #define BEGIN_TEST(name) testfunc__##name();
-#include "test_contents"
+#include TEST_TOC
 
 	return 0;
 }