Makefile 9.4 KB

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