1# Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2#
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to
5# deal in the Software without restriction, including without limitation the
6# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7# sell copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9#
10# The above copyright notice and this permission notice shall be included in
11# all copies or substantial portions of the Software.
12#
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19# IN THE SOFTWARE.
20
21PLATFORM ?= $(shell sh -c 'uname -s | tr "[A-Z]" "[a-z]"')
22HELPER ?=
23BINEXT ?=
24SOLIBNAME = libhttp_parser
25SOMAJOR = 2
26SOMINOR = 9
27SOREV   = 4
28ifeq (darwin,$(PLATFORM))
29SOEXT ?= dylib
30SONAME ?= $(SOLIBNAME).$(SOMAJOR).$(SOMINOR).$(SOEXT)
31LIBNAME ?= $(SOLIBNAME).$(SOMAJOR).$(SOMINOR).$(SOREV).$(SOEXT)
32else ifeq (wine,$(PLATFORM))
33CC = winegcc
34BINEXT = .exe.so
35HELPER = wine
36else
37SOEXT ?= so
38SONAME ?= $(SOLIBNAME).$(SOEXT).$(SOMAJOR).$(SOMINOR)
39LIBNAME ?= $(SOLIBNAME).$(SOEXT).$(SOMAJOR).$(SOMINOR).$(SOREV)
40endif
41
42CC?=gcc
43AR?=ar
44
45CPPFLAGS ?=
46LDFLAGS ?=
47
48CPPFLAGS += -I.
49CPPFLAGS_DEBUG = $(CPPFLAGS) -DHTTP_PARSER_STRICT=1
50CPPFLAGS_DEBUG += $(CPPFLAGS_DEBUG_EXTRA)
51CPPFLAGS_FAST = $(CPPFLAGS) -DHTTP_PARSER_STRICT=0
52CPPFLAGS_FAST += $(CPPFLAGS_FAST_EXTRA)
53CPPFLAGS_BENCH = $(CPPFLAGS_FAST)
54
55CFLAGS += -Wall -Wextra -Werror
56CFLAGS_DEBUG = $(CFLAGS) -O0 -g $(CFLAGS_DEBUG_EXTRA)
57CFLAGS_FAST = $(CFLAGS) -O3 $(CFLAGS_FAST_EXTRA)
58CFLAGS_BENCH = $(CFLAGS_FAST) -Wno-unused-parameter
59CFLAGS_LIB = $(CFLAGS_FAST) -fPIC
60
61LDFLAGS_LIB = $(LDFLAGS) -shared
62
63INSTALL ?= install
64PREFIX ?= /usr/local
65LIBDIR = $(PREFIX)/lib
66INCLUDEDIR = $(PREFIX)/include
67
68ifeq (darwin,$(PLATFORM))
69LDFLAGS_LIB += -Wl,-install_name,$(LIBDIR)/$(SONAME)
70else
71# TODO(bnoordhuis) The native SunOS linker expects -h rather than -soname...
72LDFLAGS_LIB += -Wl,-soname=$(SONAME)
73endif
74
75test: test_g test_fast
76	$(HELPER) ./test_g$(BINEXT)
77	$(HELPER) ./test_fast$(BINEXT)
78
79test_g: http_parser_g.o test_g.o
80	$(CC) $(CFLAGS_DEBUG) $(LDFLAGS) http_parser_g.o test_g.o -o $@
81
82test_g.o: test.c http_parser.h Makefile
83	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) -c test.c -o $@
84
85http_parser_g.o: http_parser.c http_parser.h Makefile
86	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) -c http_parser.c -o $@
87
88test_fast: http_parser.o test.o http_parser.h
89	$(CC) $(CFLAGS_FAST) $(LDFLAGS) http_parser.o test.o -o $@
90
91test.o: test.c http_parser.h Makefile
92	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c test.c -o $@
93
94bench: http_parser.o bench.o
95	$(CC) $(CFLAGS_BENCH) $(LDFLAGS) http_parser.o bench.o -o $@
96
97bench.o: bench.c http_parser.h Makefile
98	$(CC) $(CPPFLAGS_BENCH) $(CFLAGS_BENCH) -c bench.c -o $@
99
100http_parser.o: http_parser.c http_parser.h Makefile
101	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c http_parser.c
102
103test-run-timed: test_fast
104	while(true) do time $(HELPER) ./test_fast$(BINEXT) > /dev/null; done
105
106test-valgrind: test_g
107	valgrind ./test_g
108
109libhttp_parser.o: http_parser.c http_parser.h Makefile
110	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_LIB) -c http_parser.c -o libhttp_parser.o
111
112library: libhttp_parser.o
113	$(CC) $(LDFLAGS_LIB) -o $(LIBNAME) $<
114
115package: http_parser.o
116	$(AR) rcs libhttp_parser.a http_parser.o
117
118url_parser: http_parser.o contrib/url_parser.c
119	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o $@
120
121url_parser_g: http_parser_g.o contrib/url_parser.c
122	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o $@
123
124parsertrace: http_parser.o contrib/parsertrace.c
125	$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o parsertrace$(BINEXT)
126
127parsertrace_g: http_parser_g.o contrib/parsertrace.c
128	$(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o parsertrace_g$(BINEXT)
129
130tags: http_parser.c http_parser.h test.c
131	ctags $^
132
133install: library
134	$(INSTALL) -D  http_parser.h $(DESTDIR)$(INCLUDEDIR)/http_parser.h
135	$(INSTALL) -D $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
136	ln -sf $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SONAME)
137	ln -sf $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SOLIBNAME).$(SOEXT)
138
139install-strip: library
140	$(INSTALL) -D  http_parser.h $(DESTDIR)$(INCLUDEDIR)/http_parser.h
141	$(INSTALL) -D -s $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
142	ln -sf $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SONAME)
143	ln -sf $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(SOLIBNAME).$(SOEXT)
144
145uninstall:
146	rm $(DESTDIR)$(INCLUDEDIR)/http_parser.h
147	rm $(DESTDIR)$(LIBDIR)/$(SOLIBNAME).$(SOEXT)
148	rm $(DESTDIR)$(LIBDIR)/$(SONAME)
149	rm $(DESTDIR)$(LIBDIR)/$(LIBNAME)
150
151clean:
152	rm -f *.o *.a tags test test_fast test_g \
153		http_parser.tar libhttp_parser.so.* \
154		url_parser url_parser_g parsertrace parsertrace_g \
155		*.exe *.exe.so
156
157contrib/url_parser.c:	http_parser.h
158contrib/parsertrace.c:	http_parser.h
159
160.PHONY: clean package test-run test-run-timed test-valgrind install install-strip uninstall
161