From 485fa8c1c748eced249d7661b2fb045e9007b127 Mon Sep 17 00:00:00 2001
From: Wei Shi S <wei.s.shi@ericsson.com>
Date: Fri, 23 Mar 2018 16:52:58 +0800
Subject: [PATCH] use BINDIR in Makefile

Change-Id: I0a6a051d26f8bc82e5172c9905ef46b34fa12ed5
---
 Makefile | 69 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 35 insertions(+), 34 deletions(-)

diff --git a/Makefile b/Makefile
index 6cf63bd3..aaf3d0ee 100644
--- a/Makefile
+++ b/Makefile
@@ -21,6 +21,7 @@
 PLATFORM ?= $(shell sh -c 'uname -s | tr "[A-Z]" "[a-z]"')
 HELPER ?=
 BINEXT ?=
+BINDIR ?= $(CURDIR)
 SOLIBNAME = libhttp_parser
 SOMAJOR = 2
 SOMINOR = 8
@@ -72,73 +73,73 @@ else
 LDFLAGS_LIB += -Wl,-soname=$(SONAME)
 endif
 
-test: test_g test_fast
-	$(HELPER) ./test_g$(BINEXT)
-	$(HELPER) ./test_fast$(BINEXT)
+test: $(BINDIR)/test_g $(BINDIR)/test_fast
+	$(HELPER) $(BINDIR)/test_g$(BINEXT)
+	$(HELPER) $(BINDIR)/test_fast$(BINEXT)
 
-test_g: http_parser_g.o test_g.o
-	$(CC) $(CFLAGS_DEBUG) $(LDFLAGS) http_parser_g.o test_g.o -o $@
+$(BINDIR)/test_g: $(BINDIR)/http_parser_g.o $(BINDIR)/test_g.o
+	$(CC) $(CFLAGS_DEBUG) $(LDFLAGS) -o $@ $^
 
-test_g.o: test.c http_parser.h Makefile
+$(BINDIR)/test_g.o: test.c http_parser.h Makefile
 	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) -c test.c -o $@
 
-http_parser_g.o: http_parser.c http_parser.h Makefile
+$(BINDIR)/http_parser_g.o: http_parser.c http_parser.h Makefile
 	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) -c http_parser.c -o $@
 
-test_fast: http_parser.o test.o http_parser.h
-	$(CC) $(CFLAGS_FAST) $(LDFLAGS) http_parser.o test.o -o $@
+$(BINDIR)/test_fast: $(BINDIR)/http_parser.o $(BINDIR)/test.o http_parser.h
+	$(CC) $(CFLAGS_FAST) $(LDFLAGS) $(BINDIR)/http_parser.o $(BINDIR)/test.o -o $@
 
-test.o: test.c http_parser.h Makefile
+$(BINDIR)/test.o: test.c http_parser.h Makefile
 	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c test.c -o $@
 
-bench: http_parser.o bench.o
-	$(CC) $(CFLAGS_BENCH) $(LDFLAGS) http_parser.o bench.o -o $@
+$(BINDIR)/bench: $(BINDIR)/http_parser.o $(BINDIR)/bench.o
+	$(CC) $(CFLAGS_BENCH) $(LDFLAGS) -o $@ $<
 
-bench.o: bench.c http_parser.h Makefile
+$(BINDIR)/bench.o: bench.c http_parser.h Makefile
 	$(CC) $(CPPFLAGS_BENCH) $(CFLAGS_BENCH) -c bench.c -o $@
 
-http_parser.o: http_parser.c http_parser.h Makefile
-	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c http_parser.c
+$(BINDIR)/http_parser.o: http_parser.c http_parser.h Makefile
+	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c http_parser.c -o $@
 
-test-run-timed: test_fast
-	while(true) do time $(HELPER) ./test_fast$(BINEXT) > /dev/null; done
+test-run-timed: $(BINDIR)/test_fast
+	while(true) do time $(HELPER) $(BINDIR)/test_fast$(BINEXT) > /dev/null; done
 
-test-valgrind: test_g
-	valgrind ./test_g
+test-valgrind: $(BINDIR)/test_g
+	valgrind $(BINDIR)/test_g
 
-libhttp_parser.o: http_parser.c http_parser.h Makefile
-	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_LIB) -c http_parser.c -o libhttp_parser.o
+$(BINDIR)/libhttp_parser.o: http_parser.c http_parser.h Makefile
+	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_LIB) -c http_parser.c -o $@
 
-library: libhttp_parser.o
-	$(CC) $(LDFLAGS_LIB) -o $(LIBNAME) $<
+library: $(BINDIR)/libhttp_parser.o
+	$(CC) $(LDFLAGS_LIB) -o $(BINDIR)/$(LIBNAME) $<
 
-package: http_parser.o
-	$(AR) rcs libhttp_parser.a http_parser.o
+package: $(BINDIR)/http_parser.o
+	$(AR) rcs $(BINDIR)/libhttp_parser.a $(BINDIR)/http_parser.o
 
-url_parser: http_parser.o contrib/url_parser.c
+$(BINDIR)/url_parser: $(BINDIR)/http_parser.o contrib/url_parser.c
 	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o $@
 
-url_parser_g: http_parser_g.o contrib/url_parser.c
+$(BINDIR)/url_parser_g: $(BINDIR)/http_parser_g.o contrib/url_parser.c
 	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o $@
 
-parsertrace: http_parser.o contrib/parsertrace.c
-	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o parsertrace$(BINEXT)
+parsertrace: $(BINDIR)/http_parser.o contrib/parsertrace.c
+	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o $(BINDIR)/parsertrace$(BINEXT)
 
-parsertrace_g: http_parser_g.o contrib/parsertrace.c
-	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o parsertrace_g$(BINEXT)
+parsertrace_g: $(BINDIR)/http_parser_g.o contrib/parsertrace.c
+	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o $(BINDIR)/parsertrace_g$(BINEXT)
 
 tags: http_parser.c http_parser.h test.c
 	ctags $^
 
 install: library
 	$(INSTALL) -D  http_parser.h $(DESTDIR)$(INCLUDEDIR)/http_parser.h
-	$(INSTALL) -D $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
+	$(INSTALL) -D $(BINDIR)/$(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
 	ln -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SONAME)
 	ln -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SOLIBNAME).$(SOEXT)
 
 install-strip: library
 	$(INSTALL) -D  http_parser.h $(DESTDIR)$(INCLUDEDIR)/http_parser.h
-	$(INSTALL) -D -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
+	$(INSTALL) -D -s $(BINDIR)/$(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
 	ln -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SONAME)
 	ln -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SOLIBNAME).$(SOEXT)
 
@@ -149,7 +150,7 @@ uninstall:
 	rm $(DESTDIR)$(LIBDIR)/$(LIBNAME)
 
 clean:
-	rm -f *.o *.a tags test test_fast test_g \
+	rm -f tags && cd $(BINDIR) && rm -f *.o *.a tags test test_fast test_g \
 		http_parser.tar libhttp_parser.so.* \
 		url_parser url_parser_g parsertrace parsertrace_g \
 		*.exe *.exe.so