Makefile 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #
  2. # Copyright (c) 2013 No Face Press, LLC
  3. # License http://opensource.org/licenses/mit-license.php MIT License
  4. #
  5. #
  6. # For help try, "make help"
  7. #
  8. include resources/Makefile.in-os
  9. CPROG = civetweb
  10. #CXXPROG = civetweb
  11. UNIT_TEST_PROG = civetweb_test
  12. BUILD_DIR = out
  13. # Installation directories by convention
  14. # http://www.gnu.org/prep/standards/html_node/Directory-Variables.html
  15. PREFIX = /usr/local
  16. EXEC_PREFIX = $(PREFIX)
  17. BINDIR = $(EXEC_PREFIX)/bin
  18. DATAROOTDIR = $(PREFIX)/share
  19. DOCDIR = $(DATAROOTDIR)/doc/$(CPROG)
  20. SYSCONFDIR = $(PREFIX)/etc
  21. HTMLDIR = $(DOCDIR)
  22. # build tools
  23. MKDIR = mkdir -p
  24. RMF = rm -f
  25. RMRF = rm -rf
  26. # desired configuration of the document root
  27. # never assume that the document_root actually
  28. # exists on the build machine. When building
  29. # a chroot, PREFIX if just a directory which
  30. # later becomes /.
  31. DOCUMENT_ROOT = $(HTMLDIR)
  32. PORTS = 8080
  33. BUILD_DIRS = $(BUILD_DIR) $(BUILD_DIR)/src $(BUILD_DIR)/resources
  34. LIB_SOURCES = src/civetweb.c
  35. LIB_INLINE = src/mod_lua.inl src/md5.inl
  36. APP_SOURCES = src/main.c
  37. WINDOWS_RESOURCES = resources/res.rc
  38. UNIT_TEST_SOURCES = test/unit_test.c
  39. SOURCE_DIRS =
  40. OBJECTS = $(LIB_SOURCES:.c=.o) $(APP_SOURCES:.c=.o)
  41. BUILD_RESOURCES =
  42. # The unit tests include the source files directly to get visibility to the
  43. # static functions. So we clear OBJECTS so that we don't try to build or link
  44. # with any external object. Later if we find WITH_LUA=1, we'll add lua objects
  45. # to this variable so we can run lua-specific unit tests.
  46. ifeq ($(MAKECMDGOALS), unit_test)
  47. OBJECTS =
  48. BUILD_DIRS += $(BUILD_DIR)/test
  49. endif
  50. # only set main compile options if none were chosen
  51. CFLAGS += -Wall -Wextra -Wshadow -Wformat-security -Winit-self -Wmissing-prototypes -Wsign-conversion -O2 -D$(TARGET_OS) -Iinclude $(COPT) -DUSE_STACK_SIZE=102400
  52. LIBS = -lpthread -lm
  53. ifdef WITH_DEBUG
  54. CFLAGS += -g -DDEBUG_ENABLED
  55. else
  56. CFLAGS += -DNDEBUG
  57. endif
  58. ifdef WITH_CPP
  59. OBJECTS += src/CivetServer.o
  60. LCC = $(CXX)
  61. else
  62. LCC = $(CC)
  63. endif
  64. ifdef WITH_LUA_SHARED
  65. WITH_LUA = 1
  66. endif
  67. ifdef WITH_LUA
  68. include resources/Makefile.in-lua
  69. endif
  70. ifdef WITH_IPV6
  71. CFLAGS += -DUSE_IPV6
  72. endif
  73. ifdef WITH_WEBSOCKET
  74. CFLAGS += -DUSE_WEBSOCKET
  75. ifdef WITH_LUA
  76. CFLAGS += -DUSE_TIMERS
  77. ifeq ($(TARGET_OS),LINUX)
  78. LIBS += -lrt
  79. endif
  80. endif
  81. endif
  82. ifdef CONFIG_FILE
  83. CFLAGS += -DCONFIG_FILE=\"$(CONFIG_FILE)\"
  84. endif
  85. ifdef CONFIG_FILE2
  86. CFLAGS += -DCONFIG_FILE2=\"$(CONFIG_FILE2)\"
  87. endif
  88. ifdef SSL_LIB
  89. CFLAGS += -DSSL_LIB=\"$(SSL_LIB)\"
  90. endif
  91. ifdef CRYPTO_LIB
  92. CFLAGS += -DCRYPTO_LIB=\"$(CRYPTO_LIB)\"
  93. endif
  94. BUILD_DIRS += $(addprefix $(BUILD_DIR)/, $(SOURCE_DIRS))
  95. BUILD_OBJECTS = $(addprefix $(BUILD_DIR)/, $(OBJECTS))
  96. MAIN_OBJECTS = $(addprefix $(BUILD_DIR)/, $(APP_SOURCES:.c=.o))
  97. LIB_OBJECTS = $(filter-out $(MAIN_OBJECTS), $(BUILD_OBJECTS))
  98. ifeq ($(TARGET_OS),LINUX)
  99. LIBS += -lrt -ldl
  100. CAN_INSTALL = 1
  101. endif
  102. ifeq ($(TARGET_OS),WIN32)
  103. MKDIR = mkdir
  104. RMF = del /q
  105. RMRF = rmdir /s /q
  106. endif
  107. ifdef WITH_LUA_SHARED
  108. LIBS += -llua5.2
  109. endif
  110. ifneq (, $(findstring mingw32, $(shell gcc -dumpmachine)))
  111. BUILD_RESOURCES = $(BUILD_DIR)/$(WINDOWS_RESOURCES:.rc=.o)
  112. LIBS := $(filter-out -lrt, $(LIBS)) -lws2_32 -lcomdlg32 -mwindows
  113. SHARED_LIB = dll
  114. else
  115. SHARED_LIB = so
  116. endif
  117. all: build
  118. help:
  119. @echo "make help show this message"
  120. @echo "make build compile"
  121. @echo "make install install on the system"
  122. @echo "make clean clean up the mess"
  123. @echo "make lib build a static library"
  124. @echo "make slib build a shared library"
  125. @echo "make unit_test build unit tests executable"
  126. @echo ""
  127. @echo " Make Options"
  128. @echo " WITH_LUA=1 build with Lua support; include Lua as static library"
  129. @echo " WITH_LUA_SHARED=1 build with Lua support; use dynamic linking to liblua5.2.so"
  130. @echo " WITH_DEBUG=1 build with GDB debug support"
  131. @echo " WITH_IPV6=1 with IPV6 support"
  132. @echo " WITH_WEBSOCKET=1 build with web socket support"
  133. @echo " WITH_CPP=1 build library with c++ classes"
  134. @echo " CONFIG_FILE=file use 'file' as the config file"
  135. @echo " CONFIG_FILE2=file use 'file' as the backup config file"
  136. @echo " DOCUMENT_ROOT=/path document root override when installing"
  137. @echo " PORTS=8080 listening ports override when installing"
  138. @echo " SSL_LIB=libssl.so.0 use versioned SSL library"
  139. @echo " CRYPTO_LIB=libcrypto.so.0 system versioned CRYPTO library"
  140. @echo " PREFIX=/usr/local sets the install directory"
  141. @echo " COPT='-DNO_SSL' method to insert compile flags"
  142. @echo ""
  143. @echo " Compile Flags"
  144. @echo " NDEBUG strip off all debug code"
  145. @echo " DEBUG build debug version (very noisy)"
  146. @echo " NO_CGI disable CGI support"
  147. @echo " NO_SSL disable SSL functionality"
  148. @echo " NO_SSL_DL link against system libssl library"
  149. @echo " NO_FILES do not serve files from a directory"
  150. @echo " MAX_REQUEST_SIZE maximum header size, default 16384"
  151. @echo ""
  152. @echo " Variables"
  153. @echo " TARGET_OS='$(TARGET_OS)'"
  154. @echo " CFLAGS='$(CFLAGS)'"
  155. @echo " CXXFLAGS='$(CXXFLAGS)'"
  156. @echo " LDFLAGS='$(LDFLAGS)'"
  157. @echo " CC='$(CC)'"
  158. @echo " CXX='$(CXX)'"
  159. build: $(CPROG) $(CXXPROG)
  160. unit_test: $(UNIT_TEST_PROG)
  161. ifeq ($(CAN_INSTALL),1)
  162. install: $(HTMLDIR)/index.html $(SYSCONFDIR)/civetweb.conf
  163. install -d -m 755 "$(DOCDIR)"
  164. install -m 644 *.md "$(DOCDIR)"
  165. install -d -m 755 "$(BINDIR)"
  166. install -m 755 $(CPROG) "$(BINDIR)/"
  167. # Install target we do not want to overwrite
  168. # as it may be an upgrade
  169. $(HTMLDIR)/index.html:
  170. install -d -m 755 "$(HTMLDIR)"
  171. install -m 644 resources/itworks.html $(HTMLDIR)/index.html
  172. install -m 644 resources/civetweb_64x64.png $(HTMLDIR)/
  173. # Install target we do not want to overwrite
  174. # as it may be an upgrade
  175. $(SYSCONFDIR)/civetweb.conf:
  176. install -d -m 755 "$(SYSCONFDIR)"
  177. install -m 644 resources/civetweb.conf "$(SYSCONFDIR)/"
  178. @sed -i 's#^document_root.*$$#document_root $(DOCUMENT_ROOT)#' "$(SYSCONFDIR)/civetweb.conf"
  179. @sed -i 's#^listening_ports.*$$#listening_ports $(PORTS)#' "$(SYSCONFDIR)/civetweb.conf"
  180. else
  181. install:
  182. @echo "Target not flagged for installation. Use CAN_INSTALL=1 to force"
  183. @echo "As a precaution only LINUX targets are set as installable."
  184. @echo "If the target is linux-like, use CAN_INSTALL=1 option."
  185. endif
  186. lib: lib$(CPROG).a
  187. slib: lib$(CPROG).$(SHARED_LIB)
  188. clean:
  189. $(RMRF) $(BUILD_DIR)
  190. $(eval version=$(shell grep "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g'))
  191. $(eval major=$(shell echo $(version) | cut -d'.' -f1))
  192. $(RMRF) lib$(CPROG).so
  193. $(RMRF) lib$(CPROG).so.$(major)
  194. $(RMRF) lib$(CPROG).so.$(version).0
  195. $(RMRF) $(CPROG)
  196. distclean: clean
  197. @$(RMRF) VS2012/Debug VS2012/*/Debug VS2012/*/*/Debug
  198. @$(RMRF) VS2012/Release VS2012/*/Release VS2012/*/*/Release
  199. $(RMF) $(CPROG) lib$(CPROG).so lib$(CPROG).a *.dmg *.msi *.exe lib$(CPROG).dll lib$(CPROG).dll.a
  200. $(RMF) $(UNIT_TEST_PROG)
  201. lib$(CPROG).a: CFLAGS += -fPIC
  202. lib$(CPROG).a: $(LIB_OBJECTS)
  203. @$(RMF) $@
  204. ar cq $@ $(LIB_OBJECTS)
  205. lib$(CPROG).so: CFLAGS += -fPIC
  206. lib$(CPROG).so: $(LIB_OBJECTS)
  207. $(eval version=$(shell grep "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g'))
  208. $(eval major=$(shell echo $(version) | cut -d'.' -f1))
  209. $(LCC) -shared -Wl,-soname,$@.$(major) -o $@.$(version).0 $(CFLAGS) $(LDFLAGS) $(LIB_OBJECTS)
  210. ln -s -f $@.$(major) $@
  211. ln -s -f $@.$(version).0 $@.$(major)
  212. lib$(CPROG).dll: CFLAGS += -fPIC
  213. lib$(CPROG).dll: $(LIB_OBJECTS)
  214. $(LCC) -shared -o $@ $(CFLAGS) $(LDFLAGS) $(LIB_OBJECTS) $(LIBS) -Wl,--out-implib,lib$(CPROG).dll.a
  215. $(UNIT_TEST_PROG): CFLAGS += -Isrc
  216. $(UNIT_TEST_PROG): $(LIB_SOURCES) $(LIB_INLINE) $(UNIT_TEST_SOURCES) $(BUILD_OBJECTS)
  217. $(LCC) -o $@ $(CFLAGS) $(LDFLAGS) $(UNIT_TEST_SOURCES) $(BUILD_OBJECTS) $(LIBS)
  218. $(CPROG): $(BUILD_OBJECTS) $(BUILD_RESOURCES)
  219. $(LCC) -o $@ $(CFLAGS) $(LDFLAGS) $(BUILD_OBJECTS) $(BUILD_RESOURCES) $(LIBS)
  220. $(CXXPROG): $(BUILD_OBJECTS)
  221. $(CXX) -o $@ $(CFLAGS) $(LDFLAGS) $(BUILD_OBJECTS) $(LIBS)
  222. $(BUILD_OBJECTS): $(BUILD_DIRS)
  223. $(BUILD_DIRS):
  224. -@$(MKDIR) "$@"
  225. $(BUILD_DIR)/%.o : %.cpp
  226. $(CXX) -c $(CFLAGS) $(CXXFLAGS) $< -o $@
  227. $(BUILD_DIR)/%.o : %.c
  228. $(CC) -c $(CFLAGS) $< -o $@
  229. $(BUILD_RESOURCES) : $(WINDOWS_RESOURCES)
  230. windres $< $@
  231. # This rules is used to keep the code formatted in a reasonable manor
  232. # For this to work astyle must be installed and in the path
  233. # http://sourceforge.net/projects/astyle
  234. indent:
  235. astyle --suffix=none --style=linux --indent=spaces=4 --lineend=linux include/*.h src/*.c src/*.cpp src/*.inl examples/*/*.c examples/*/*.cpp
  236. .PHONY: all help build install clean lib so