1#
2# Copyright (c) 2013 No Face Press, LLC
3# License http://opensource.org/licenses/mit-license.php MIT License
4#
5
6#
7# For help try, "make help"
8#
9
10include resources/Makefile.in-os
11
12CPROG = civetweb
13#CXXPROG = civetweb
14UNIT_TEST_PROG = civetweb_test
15
16BUILD_DIR = out
17
18# Installation directories by convention
19# http://www.gnu.org/prep/standards/html_node/Directory-Variables.html
20PREFIX ?= /usr/local
21EXEC_PREFIX = $(PREFIX)
22BINDIR = $(EXEC_PREFIX)/bin
23DATAROOTDIR = $(PREFIX)/share
24DOCDIR = $(DATAROOTDIR)/doc/$(CPROG)
25SYSCONFDIR ?= $(PREFIX)/etc
26HTMLDIR = $(DOCDIR)
27INCLUDEDIR = $(DESTDIR)$(PREFIX)/include
28LIBDIR = $(DESTDIR)$(EXEC_PREFIX)/lib
29PID_FILE ?= /var/run/$(CPROG).pid
30
31# build tools
32MKDIR = mkdir -p
33RMF = rm -f
34RMRF = rm -rf
35
36# desired configuration of the document root
37# never assume that the document_root actually
38# exists on the build machine.  When building
39# a chroot, PREFIX if just a directory which
40# later becomes /.
41DOCUMENT_ROOT = $(HTMLDIR)
42PORTS = 8080
43USE_STACK_SIZE ?= 102400
44
45BUILD_DIRS = $(BUILD_DIR) $(BUILD_DIR)/src $(BUILD_DIR)/resources
46
47LIB_SOURCES = src/civetweb.c
48LIB_INLINE  = src/mod_lua.inl src/md5.inl
49APP_SOURCES = src/main.c
50WINDOWS_RESOURCES = resources/res.rc
51UNIT_TEST_SOURCES = test/unit_test.c
52SOURCE_DIRS =
53
54OBJECTS = $(LIB_SOURCES:.c=.o) $(APP_SOURCES:.c=.o)
55HEADERS = include/civetweb.h
56BUILD_RESOURCES =
57
58# The unit tests include the source files directly to get visibility to the
59# static functions.  So we clear OBJECTS so that we don't try to build or link
60# with any external object.  Later if we find WITH_LUA=1, we'll add lua objects
61# to this variable so we can run lua-specific unit tests.
62ifeq ($(MAKECMDGOALS), unit_test)
63OBJECTS =
64BUILD_DIRS += $(BUILD_DIR)/test
65endif
66
67# only set main compile options if none were chosen
68CFLAGS += -Wall -Wextra -Wshadow -Wformat-security -Winit-self -Wmissing-prototypes -D$(TARGET_OS) -Iinclude $(COPT) -DUSE_STACK_SIZE=$(USE_STACK_SIZE)
69
70LIBS = -lpthread -lm
71
72ifdef WITH_DEBUG
73  CFLAGS += -g -DDEBUG
74else
75  CFLAGS += -O2 -DNDEBUG
76endif
77
78ifdef WITH_CPP
79  OBJECTS += src/CivetServer.o
80  HEADERS += include/CivetServer.h
81  LCC = $(CXX)
82else
83  LCC = $(CC)
84endif
85
86ifdef WITH_ALL
87  WITH_WEBSOCKET = 1
88  WITH_IPV6 = 1
89  WITH_LUA = 1
90  WITH_DUKTAPE = 1
91  WITH_SERVER_STATS = 1
92  WITH_ZLIB = 1
93  WITH_EXPERIMENTAL = 1
94  #WITH_CPP is not defined, ALL means only real features, not wrappers
95endif
96
97# Use Lua?
98ifdef WITH_LUA_VERSION
99  WITH_LUA = 1
100endif
101
102ifdef WITH_LUA_SHARED
103  WITH_LUA = 1
104endif
105
106ifdef WITH_LUAJIT_SHARED
107  WITH_LUA_SHARED = 1
108  WITH_LUA = 1
109  WITH_LUA_VERSION = 501
110endif
111
112ifdef WITH_LUA
113  include resources/Makefile.in-lua
114endif
115
116# Use Duktape?
117ifdef WITH_SSJS
118  WITH_DUKTAPE = 1
119endif
120
121ifdef WITH_DUKTAPE_VERSION
122  WITH_DUKTAPE = 1
123endif
124
125ifdef WITH_DUKTAPE_SHARED
126  WITH_DUKTAPE = 1
127endif
128
129ifdef WITH_DUKTAPE
130  include resources/Makefile.in-duktape
131endif
132
133# Use zlib?
134ifdef WITH_COMPRESSION
135  WITH_ZLIB = 1
136endif
137
138ifdef WITH_ZLIB
139  LIBS += -lz
140  CFLAGS += -DUSE_ZLIB
141endif
142
143# Other features
144ifdef WITH_EXPERIMENTAL
145  CFLAGS += -DMG_EXPERIMENTAL_INTERFACES
146endif
147
148ifdef WITH_IPV6
149  CFLAGS += -DUSE_IPV6
150endif
151
152ifdef WITH_WEBSOCKET
153  CFLAGS += -DUSE_WEBSOCKET
154endif
155ifdef WITH_WEBSOCKETS
156  CFLAGS += -DUSE_WEBSOCKET
157endif
158
159ifdef WITH_SERVER_STAT
160  CFLAGS += -DUSE_SERVER_STATS
161endif
162ifdef WITH_SERVER_STATS
163  CFLAGS += -DUSE_SERVER_STATS
164endif
165
166ifdef WITH_DAEMONIZE
167  CFLAGS += -DDAEMONIZE -DPID_FILE=\"$(PID_FILE)\"
168endif
169
170# File names
171ifdef CONFIG_FILE
172  CFLAGS += -DCONFIG_FILE=\"$(CONFIG_FILE)\"
173endif
174
175ifdef CONFIG_FILE2
176  CFLAGS += -DCONFIG_FILE2=\"$(CONFIG_FILE2)\"
177endif
178
179ifdef SSL_LIB
180  CFLAGS += -DSSL_LIB=\"$(SSL_LIB)\"
181endif
182
183ifdef CRYPTO_LIB
184  CFLAGS += -DCRYPTO_LIB=\"$(CRYPTO_LIB)\"
185endif
186
187BUILD_DIRS += $(addprefix $(BUILD_DIR)/, $(SOURCE_DIRS))
188BUILD_OBJECTS = $(addprefix $(BUILD_DIR)/, $(OBJECTS))
189MAIN_OBJECTS = $(addprefix $(BUILD_DIR)/, $(APP_SOURCES:.c=.o))
190LIB_OBJECTS = $(filter-out $(MAIN_OBJECTS), $(BUILD_OBJECTS))
191
192ifeq ($(TARGET_OS),LINUX)
193  LIBS += -lrt -ldl
194  CAN_INSTALL = 1
195endif
196
197ifeq ($(TARGET_OS),WIN32)
198  MKDIR = mkdir
199  RMF = del /q
200  RMRF = rmdir /s /q
201endif
202
203ifdef WITH_LUAJIT_SHARED
204  LIBS += -lluajit-5.1
205else
206ifdef WITH_LUA_SHARED
207  LIBS += $(LUA_SHARED_LIB_FLAG)
208endif
209endif
210
211ifneq (, $(findstring mingw32, $(shell $(CC) -dumpmachine)))
212  BUILD_RESOURCES = $(BUILD_DIR)/$(WINDOWS_RESOURCES:.rc=.o)
213  LIBS += -lws2_32 -mwindows
214  SHARED_LIB = dll
215else
216  SHARED_LIB = so
217endif
218
219all: build
220
221help:
222	@echo "make help                show this message"
223	@echo "make build               compile"
224	@echo "make install             install on the system"
225	@echo "make clean               clean up the mess"
226	@echo "make install-headers     install headers"
227	@echo "make lib                 build a static library"
228	@echo "make install-lib         install the static library"
229	@echo "make slib                build a shared library"
230	@echo "make install-slib        install the shared library"
231	@echo "make unit_test           build unit tests executable"
232	@echo ""
233	@echo " Make Options"
234	@echo "   WITH_LUA=1            build with Lua support; include Lua as static library"
235	@echo "   WITH_LUA_SHARED=1     build with Lua support; use dynamic linking to liblua5.2.so"
236	@echo "   WITH_LUA_VERSION=502  build with Lua 5.2.x (501 for Lua 5.1.x to 503 for 5.3.x)"
237	@echo "   WITH_DUKTAPE=1        build with Duktape support; include as static library"
238	@echo "   WITH_DUKTAPE_SHARED=1 build with Duktape support; use libduktape1.3.so"
239	@echo "   WITH_DUKTAPE_VERSION=108 build with Duktape 1.8.x"
240	@echo "   WITH_DEBUG=1          build with GDB debug support"
241	@echo "   WITH_IPV6=1           with IPV6 support"
242	@echo "   WITH_WEBSOCKET=1      build with web socket support"
243	@echo "   WITH_SERVER_STATS=1   build includes support for server statistics"
244	@echo "   WITH_ZLIB=1           build includes support for on-the-fly compression using zlib"
245	@echo "   WITH_CPP=1            build library with c++ classes"
246	@echo "   WITH_EXPERIMENTAL=1   build with experimental features"
247	@echo "   WITH_DAEMONIZE=1      build with daemonize."
248	@echo "   PID_FILE=/path        PID file path of daemon."
249	@echo "   CONFIG_FILE=file      use 'file' as the config file"
250	@echo "   CONFIG_FILE2=file     use 'file' as the backup config file"
251	@echo "   DOCUMENT_ROOT=/path   document root override when installing"
252	@echo "   PORTS=8080            listening ports override when installing"
253	@echo "   SSL_LIB=libssl.so.0   use versioned SSL library"
254	@echo "   CRYPTO_LIB=libcrypto.so.0 system versioned CRYPTO library"
255	@echo "   PREFIX=/usr/local     sets the install directory"
256	@echo "   COPT='-DNO_SSL'       method to insert compile flags"
257	@echo ""
258	@echo " Compile Flags"
259	@echo "   NDEBUG                strip off all debug code"
260	@echo "   DEBUG                 build debug version (very noisy)"
261	@echo "   NO_CGI                disable CGI support"
262	@echo "   NO_SSL                disable SSL functionality"
263	@echo "   NO_SSL_DL             link against system libssl library"
264	@echo "   NO_FILES              do not serve files from a directory"
265	@echo "   NO_CACHING            disable caching (useful for systems without timegm())"
266	@echo ""
267	@echo " Variables"
268	@echo "   TARGET_OS='$(TARGET_OS)'"
269	@echo "   CFLAGS='$(CFLAGS)'"
270	@echo "   CXXFLAGS='$(CXXFLAGS)'"
271	@echo "   LDFLAGS='$(LDFLAGS)'"
272	@echo "   CC='$(CC)'"
273	@echo "   CXX='$(CXX)'"
274
275build: $(CPROG) $(CXXPROG)
276
277unit_test: $(UNIT_TEST_PROG)
278
279ifeq ($(CAN_INSTALL),1)
280install: $(HTMLDIR)/index.html $(SYSCONFDIR)/civetweb.conf
281	install -d -m 755  "$(DOCDIR)"
282	install -m 644 *.md "$(DOCDIR)"
283	install -d -m 755 "$(BINDIR)"
284	install -m 755 $(CPROG) "$(BINDIR)/"
285
286install-headers:
287	install -m 644 $(HEADERS) "$(INCLUDEDIR)"
288
289install-lib: lib$(CPROG).a
290	install -m 644 $< "$(LIBDIR)"
291
292install-slib: lib$(CPROG).so
293	$(eval version=$(shell grep -w "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g'))
294	$(eval major=$(shell echo $(version) | cut -d'.' -f1))
295	install -m 644 $< "$(LIBDIR)"
296	install -m 777 $<.$(major) "$(LIBDIR)"
297	install -m 777 $<.$(version).0 "$(LIBDIR)"
298
299# Install target we do not want to overwrite
300# as it may be an upgrade
301$(HTMLDIR)/index.html:
302	install -d -m 755  "$(HTMLDIR)"
303	install -m 644 resources/itworks.html $(HTMLDIR)/index.html
304	install -m 644 resources/civetweb_64x64.png $(HTMLDIR)/
305
306# Install target we do not want to overwrite
307# as it may be an upgrade
308$(SYSCONFDIR)/civetweb.conf:
309	install -d -m 755  "$(SYSCONFDIR)"
310	install -m 644 resources/civetweb.conf  "$(SYSCONFDIR)/"
311	@sed -i 's#^document_root.*$$#document_root $(DOCUMENT_ROOT)#' "$(SYSCONFDIR)/civetweb.conf"
312	@sed -i 's#^listening_ports.*$$#listening_ports $(PORTS)#' "$(SYSCONFDIR)/civetweb.conf"
313
314else
315install:
316	@echo "Target not flagged for installation.  Use CAN_INSTALL=1 to force"
317	@echo "As a precaution only LINUX targets are set as installable."
318	@echo "If the target is linux-like, use CAN_INSTALL=1 option."
319endif
320
321lib: lib$(CPROG).a
322
323slib: lib$(CPROG).$(SHARED_LIB)
324
325clean:
326	$(RMRF) $(BUILD_DIR)
327	$(eval version=$(shell grep -w "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g'))
328	$(eval major=$(shell echo $(version) | cut -d'.' -f1))
329	$(RMRF) lib$(CPROG).a
330	$(RMRF) lib$(CPROG).so
331	$(RMRF) lib$(CPROG).so.$(major)
332	$(RMRF) lib$(CPROG).so.$(version).0
333	$(RMRF) $(CPROG)
334	$(RMF) $(UNIT_TEST_PROG)
335
336distclean: clean
337	@$(RMRF) VS2012/Debug VS2012/*/Debug  VS2012/*/*/Debug
338	@$(RMRF) VS2012/Release VS2012/*/Release  VS2012/*/*/Release
339	$(RMF) $(CPROG) lib$(CPROG).so lib$(CPROG).a *.dmg *.msi *.exe lib$(CPROG).dll lib$(CPROG).dll.a
340	$(RMF) $(UNIT_TEST_PROG)
341
342$(LIB_OBJECTS): CFLAGS += -fPIC
343
344lib$(CPROG).a: CFLAGS += -fPIC
345lib$(CPROG).a: $(LIB_OBJECTS)
346	@$(RMF) $@
347	ar cq $@ $(LIB_OBJECTS)
348
349lib$(CPROG).so: CFLAGS += -fPIC
350lib$(CPROG).so: $(LIB_OBJECTS)
351	$(eval version=$(shell grep -w "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g'))
352	$(eval major=$(shell echo $(version) | cut -d'.' -f1))
353	$(LCC) -shared -Wl,-soname,$@.$(major) -o $@.$(version).0 $(CFLAGS) $(LDFLAGS) $(LIB_OBJECTS)
354	ln -s -f $@.$(major) $@
355	ln -s -f $@.$(version).0 $@.$(major)
356
357lib$(CPROG).dll: CFLAGS += -fPIC
358lib$(CPROG).dll: $(LIB_OBJECTS)
359	$(LCC) -shared -o $@ $(CFLAGS) $(LDFLAGS) $(LIB_OBJECTS) $(LIBS) -Wl,--out-implib,lib$(CPROG).dll.a
360
361$(UNIT_TEST_PROG): CFLAGS += -Isrc -g
362$(UNIT_TEST_PROG): $(LIB_SOURCES) $(LIB_INLINE) $(UNIT_TEST_SOURCES) $(BUILD_OBJECTS)
363	$(LCC) -o $@ $(CFLAGS) $(LDFLAGS) $(UNIT_TEST_SOURCES) $(BUILD_OBJECTS) $(LIBS)
364
365$(CPROG): $(BUILD_OBJECTS) $(BUILD_RESOURCES)
366	$(LCC) -o $@ $(CFLAGS) $(LDFLAGS) $(BUILD_OBJECTS) $(BUILD_RESOURCES) $(LIBS)
367
368$(CXXPROG): $(BUILD_OBJECTS)
369	$(CXX) -o $@ $(CFLAGS) $(LDFLAGS) $(BUILD_OBJECTS) $(LIBS)
370
371$(BUILD_OBJECTS): $(BUILD_DIRS)
372
373$(BUILD_DIRS):
374	-@$(MKDIR) "$@"
375
376$(BUILD_DIR)/%.o : %.cpp
377	$(CXX) -c $(CFLAGS) $(CXXFLAGS) $< -o $@
378
379$(BUILD_DIR)/%.o : %.c
380	$(CC) -c $(CFLAGS) $< -o $@
381
382$(BUILD_RESOURCES) : $(WINDOWS_RESOURCES)
383	windres $(WINDRES_FLAGS) $< $@
384
385# This rules is used to keep the code formatted in a reasonable manor
386# For this to work astyle must be installed and in the path
387# http://sourceforge.net/projects/astyle
388indent:
389	astyle --suffix=none --style=linux --indent=spaces=4 --lineend=linux  include/*.h src/*.c src/*.cpp src/*.inl examples/*/*.c  examples/*/*.cpp
390
391.PHONY: all help build install clean lib so
392
393