Makefile 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 -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. endif
  76. ifdef CONFIG_FILE
  77. CFLAGS += -DCONFIG_FILE=\"$(CONFIG_FILE)\"
  78. endif
  79. ifdef CONFIG_FILE2
  80. CFLAGS += -DCONFIG_FILE2=\"$(CONFIG_FILE2)\"
  81. endif
  82. ifdef SSL_LIB
  83. CFLAGS += -DSSL_LIB=\"$(SSL_LIB)\"
  84. endif
  85. ifdef CRYPTO_LIB
  86. CFLAGS += -DCRYPTO_LIB=\"$(CRYPTO_LIB)\"
  87. endif
  88. BUILD_DIRS += $(addprefix $(BUILD_DIR)/, $(SOURCE_DIRS))
  89. BUILD_OBJECTS = $(addprefix $(BUILD_DIR)/, $(OBJECTS))
  90. MAIN_OBJECTS = $(addprefix $(BUILD_DIR)/, $(APP_SOURCES:.c=.o))
  91. LIB_OBJECTS = $(filter-out $(MAIN_OBJECTS), $(BUILD_OBJECTS))
  92. ifeq ($(TARGET_OS),LINUX)
  93. LIBS += -lrt -ldl
  94. CAN_INSTALL = 1
  95. endif
  96. ifeq ($(TARGET_OS),WIN32)
  97. MKDIR = mkdir
  98. RMF = del /q
  99. RMRF = rmdir /s /q
  100. endif
  101. ifdef WITH_LUA_SHARED
  102. LIBS += -llua5.2
  103. endif
  104. ifneq (, $(findstring mingw32, $(shell $(CC) -dumpmachine)))
  105. BUILD_RESOURCES = $(BUILD_DIR)/$(WINDOWS_RESOURCES:.rc=.o)
  106. LIBS += -lws2_32 -mwindows
  107. SHARED_LIB = dll
  108. else
  109. SHARED_LIB = so
  110. endif
  111. all: build
  112. help:
  113. @echo "make help show this message"
  114. @echo "make build compile"
  115. @echo "make install install on the system"
  116. @echo "make clean clean up the mess"
  117. @echo "make lib build a static library"
  118. @echo "make slib build a shared library"
  119. @echo "make unit_test build unit tests executable"
  120. @echo ""
  121. @echo " Make Options"
  122. @echo " WITH_LUA=1 build with Lua support; include Lua as static library"
  123. @echo " WITH_LUA_SHARED=1 build with Lua support; use dynamic linking to liblua5.2.so"
  124. @echo " WITH_DEBUG=1 build with GDB debug support"
  125. @echo " WITH_IPV6=1 with IPV6 support"
  126. @echo " WITH_WEBSOCKET=1 build with web socket support"
  127. @echo " WITH_CPP=1 build library with c++ classes"
  128. @echo " CONFIG_FILE=file use 'file' as the config file"
  129. @echo " CONFIG_FILE2=file use 'file' as the backup config file"
  130. @echo " DOCUMENT_ROOT=/path document root override when installing"
  131. @echo " PORTS=8080 listening ports override when installing"
  132. @echo " SSL_LIB=libssl.so.0 use versioned SSL library"
  133. @echo " CRYPTO_LIB=libcrypto.so.0 system versioned CRYPTO library"
  134. @echo " PREFIX=/usr/local sets the install directory"
  135. @echo " COPT='-DNO_SSL' method to insert compile flags"
  136. @echo ""
  137. @echo " Compile Flags"
  138. @echo " NDEBUG strip off all debug code"
  139. @echo " DEBUG build debug version (very noisy)"
  140. @echo " NO_CGI disable CGI support"
  141. @echo " NO_SSL disable SSL functionality"
  142. @echo " NO_SSL_DL link against system libssl library"
  143. @echo " NO_FILES do not serve files from a directory"
  144. @echo " MAX_REQUEST_SIZE maximum header size, default 16384"
  145. @echo ""
  146. @echo " Variables"
  147. @echo " TARGET_OS='$(TARGET_OS)'"
  148. @echo " CFLAGS='$(CFLAGS)'"
  149. @echo " CXXFLAGS='$(CXXFLAGS)'"
  150. @echo " LDFLAGS='$(LDFLAGS)'"
  151. @echo " CC='$(CC)'"
  152. @echo " CXX='$(CXX)'"
  153. build: $(CPROG) $(CXXPROG)
  154. unit_test: $(UNIT_TEST_PROG)
  155. ifeq ($(CAN_INSTALL),1)
  156. install: $(HTMLDIR)/index.html $(SYSCONFDIR)/civetweb.conf
  157. install -d -m 755 "$(DOCDIR)"
  158. install -m 644 *.md "$(DOCDIR)"
  159. install -d -m 755 "$(BINDIR)"
  160. install -m 755 $(CPROG) "$(BINDIR)/"
  161. # Install target we do not want to overwrite
  162. # as it may be an upgrade
  163. $(HTMLDIR)/index.html:
  164. install -d -m 755 "$(HTMLDIR)"
  165. install -m 644 resources/itworks.html $(HTMLDIR)/index.html
  166. install -m 644 resources/civetweb_64x64.png $(HTMLDIR)/
  167. # Install target we do not want to overwrite
  168. # as it may be an upgrade
  169. $(SYSCONFDIR)/civetweb.conf:
  170. install -d -m 755 "$(SYSCONFDIR)"
  171. install -m 644 resources/civetweb.conf "$(SYSCONFDIR)/"
  172. @sed -i 's#^document_root.*$$#document_root $(DOCUMENT_ROOT)#' "$(SYSCONFDIR)/civetweb.conf"
  173. @sed -i 's#^listening_ports.*$$#listening_ports $(PORTS)#' "$(SYSCONFDIR)/civetweb.conf"
  174. else
  175. install:
  176. @echo "Target not flagged for installation. Use CAN_INSTALL=1 to force"
  177. @echo "As a precaution only LINUX targets are set as installable."
  178. @echo "If the target is linux-like, use CAN_INSTALL=1 option."
  179. endif
  180. lib: lib$(CPROG).a
  181. slib: lib$(CPROG).$(SHARED_LIB)
  182. clean:
  183. $(RMRF) $(BUILD_DIR)
  184. $(eval version=$(shell grep "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g'))
  185. $(eval major=$(shell echo $(version) | cut -d'.' -f1))
  186. $(RMRF) lib$(CPROG).so
  187. $(RMRF) lib$(CPROG).so.$(major)
  188. $(RMRF) lib$(CPROG).so.$(version).0
  189. $(RMRF) $(CPROG)
  190. distclean: clean
  191. @$(RMRF) VS2012/Debug VS2012/*/Debug VS2012/*/*/Debug
  192. @$(RMRF) VS2012/Release VS2012/*/Release VS2012/*/*/Release
  193. $(RMF) $(CPROG) lib$(CPROG).so lib$(CPROG).a *.dmg *.msi *.exe lib$(CPROG).dll lib$(CPROG).dll.a
  194. $(RMF) $(UNIT_TEST_PROG)
  195. lib$(CPROG).a: CFLAGS += -fPIC
  196. lib$(CPROG).a: $(LIB_OBJECTS)
  197. @$(RMF) $@
  198. ar cq $@ $(LIB_OBJECTS)
  199. lib$(CPROG).so: CFLAGS += -fPIC
  200. lib$(CPROG).so: $(LIB_OBJECTS)
  201. $(eval version=$(shell grep "define CIVETWEB_VERSION" include/civetweb.h | sed 's|.*VERSION "\(.*\)"|\1|g'))
  202. $(eval major=$(shell echo $(version) | cut -d'.' -f1))
  203. $(LCC) -shared -Wl,-soname,$@.$(major) -o $@.$(version).0 $(CFLAGS) $(LDFLAGS) $(LIB_OBJECTS)
  204. ln -s -f $@.$(major) $@
  205. ln -s -f $@.$(version).0 $@.$(major)
  206. lib$(CPROG).dll: CFLAGS += -fPIC
  207. lib$(CPROG).dll: $(LIB_OBJECTS)
  208. $(LCC) -shared -o $@ $(CFLAGS) $(LDFLAGS) $(LIB_OBJECTS) $(LIBS) -Wl,--out-implib,lib$(CPROG).dll.a
  209. $(UNIT_TEST_PROG): CFLAGS += -Isrc
  210. $(UNIT_TEST_PROG): $(LIB_SOURCES) $(LIB_INLINE) $(UNIT_TEST_SOURCES) $(BUILD_OBJECTS)
  211. $(LCC) -o $@ $(CFLAGS) $(LDFLAGS) $(UNIT_TEST_SOURCES) $(BUILD_OBJECTS) $(LIBS)
  212. $(CPROG): $(BUILD_OBJECTS) $(BUILD_RESOURCES)
  213. $(LCC) -o $@ $(CFLAGS) $(LDFLAGS) $(BUILD_OBJECTS) $(BUILD_RESOURCES) $(LIBS)
  214. $(CXXPROG): $(BUILD_OBJECTS)
  215. $(CXX) -o $@ $(CFLAGS) $(LDFLAGS) $(BUILD_OBJECTS) $(LIBS)
  216. $(BUILD_OBJECTS): $(BUILD_DIRS)
  217. $(BUILD_DIRS):
  218. -@$(MKDIR) "$@"
  219. $(BUILD_DIR)/%.o : %.cpp
  220. $(CXX) -c $(CFLAGS) $(CXXFLAGS) $< -o $@
  221. $(BUILD_DIR)/%.o : %.c
  222. $(CC) -c $(CFLAGS) $< -o $@
  223. $(BUILD_RESOURCES) : $(WINDOWS_RESOURCES)
  224. windres $< $@
  225. # This rules is used to keep the code formatted in a reasonable manor
  226. # For this to work astyle must be installed and in the path
  227. # http://sourceforge.net/projects/astyle
  228. indent:
  229. astyle --suffix=none --style=linux --indent=spaces=4 --lineend=linux include/*.h src/*.c src/*.cpp src/*.inl examples/*/*.c examples/*/*.cpp
  230. .PHONY: all help build install clean lib so