CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # The C API library
  2. set( LIB_TYPE "STATIC" )
  3. if (BUILD_SHARED_LIBS)
  4. set( LIB_TYPE "SHARED" )
  5. endif()
  6. add_library(civetweb-c-library ${LIB_TYPE} civetweb.c)
  7. set_target_properties(civetweb-c-library PROPERTIES
  8. OUTPUT_NAME "civetweb"
  9. VERSION ${CIVETWEB_VERSION}
  10. SOVERSION ${CIVETWEB_VERSION}
  11. )
  12. if (BUILD_SHARED_LIBS)
  13. target_compile_definitions(civetweb-c-library PRIVATE CIVETWEB_DLL_EXPORTS)
  14. endif()
  15. target_include_directories(
  16. civetweb-c-library PUBLIC
  17. ${PROJECT_SOURCE_DIR}/include)
  18. install(
  19. TARGETS civetweb-c-library
  20. ARCHIVE DESTINATION lib
  21. LIBRARY DESTINATION lib
  22. RUNTIME DESTINATION bin
  23. COMPONENT civetweb-c-library)
  24. install(FILES
  25. ${PROJECT_SOURCE_DIR}/include/civetweb.h
  26. DESTINATION include
  27. COMPONENT civetweb-c-library)
  28. # Need Windows sockets if available
  29. find_package(WinSock)
  30. if (WINSOCK_FOUND)
  31. target_link_libraries(civetweb-c-library WINSOCK::WINSOCK)
  32. endif()
  33. # We need threading
  34. find_package(Threads)
  35. target_link_libraries(civetweb-c-library ${CMAKE_THREAD_LIBS_INIT})
  36. # Need the realtime library if we're using timers
  37. find_package(LibRt)
  38. if (CIVETWEB_ENABLE_WEBSOCKETS AND CIVETWEB_ENABLE_LUA AND LIBRT_FOUND)
  39. target_link_libraries(civetweb-c-library LIBRT::LIBRT)
  40. endif()
  41. # We need to link OpenSSL if not dynamically loading
  42. if (CIVETWEB_ENABLE_SSL)
  43. if (CIVETWEB_ENABLE_SSL_DYNAMIC_LOADING)
  44. find_package(LibDl)
  45. if (LIBDL_FOUND)
  46. target_link_libraries(civetweb-c-library -ldl)
  47. endif()
  48. else()
  49. find_package(OpenSSL)
  50. include_directories(${OPENSSL_INCLUDE_DIR})
  51. message(STATUS "OpenSSL include directory: ${OPENSSL_INCLUDE_DIR}")
  52. target_link_libraries(civetweb-c-library ${OPENSSL_LIBRARIES})
  53. endif()
  54. endif()
  55. # If Lua support is needed we build some extra Lua libraries
  56. if (CIVETWEB_ENABLE_LUA)
  57. include(ExternalProject)
  58. # Determine if we should print to the output
  59. if (CIVETWEB_ENABLE_THIRD_PARTY_OUTPUT)
  60. set(THIRD_PARTY_LOGGING 0)
  61. else()
  62. set(THIRD_PARTY_LOGGING 1)
  63. endif()
  64. # If Lua is static we must build it from source
  65. if (NOT CIVETWEB_ENABLE_LUA_SHARED)
  66. if (LINUX)
  67. set(LUA_MAKE_TARGET linux)
  68. elseif(DARWIN)
  69. set(LUA_MAKE_TARGET macosx)
  70. elseif(FREEBSD)
  71. set(LUA_MAKE_TARGET freebsd)
  72. elseif(WINDOWS)
  73. set(LUA_MAKE_TARGET mingw)
  74. elseif(UNIX)
  75. set(LUA_MAKE_TARGET posix)
  76. else()
  77. set(LUA_MAKE_TARGET generic)
  78. endif()
  79. set(LUA_BUILD_COMMAND "${CMAKE_MAKE_PROGRAM};${LUA_MAKE_TARGET}")
  80. if (BUILD_SHARED_LIBS)
  81. set(LUA_BUILD_COMMAND "${LUA_BUILD_COMMAND};MYCFLAGS=-fPIC")
  82. endif()
  83. ExternalProject_Add(lua
  84. URL "http://www.lua.org/ftp/lua-${CIVETWEB_LUA_VERSION}.tar.gz"
  85. URL_MD5 ${CIVETWEB_LUA_MD5_HASH}
  86. PREFIX "${CIVETWEB_THIRD_PARTY_DIR}"
  87. CONFIGURE_COMMAND ""
  88. BUILD_COMMAND ${LUA_BUILD_COMMAND}
  89. BUILD_IN_SOURCE 1
  90. INSTALL_COMMAND make install "INSTALL_TOP=<INSTALL_DIR>"
  91. LOG_DOWNLOAD ${THIRD_PARTY_LOGGING}
  92. LOG_UPDATE ${THIRD_PARTY_LOGGING}
  93. LOG_CONFIGURE ${THIRD_PARTY_LOGGING}
  94. LOG_BUILD ${THIRD_PARTY_LOGGING}
  95. LOG_TEST ${THIRD_PARTY_LOGGING}
  96. LOG_INSTALL ${THIRD_PARTY_LOGGING})
  97. ExternalProject_Get_Property(lua INSTALL_DIR)
  98. set(LUA_INSTALL_DIR ${INSTALL_DIR})
  99. unset(INSTALL_DIR)
  100. link_directories("${LUA_INSTALL_DIR}/lib")
  101. include_directories("${LUA_INSTALL_DIR}/include")
  102. set(LUA_LIBRARIES "${LUA_INSTALL_DIR}/lib/liblua.a")
  103. add_dependencies(civetweb-c-library lua)
  104. else()
  105. find_package(Lua)
  106. endif()
  107. # Lua Filesystem Support
  108. string(REPLACE "." "_" LUA_FILESYSTEM_VERSION_UNDERSCORE ${CIVETWEB_LUA_FILESYSTEM_VERSION})
  109. ExternalProject_Add(luafilesystem
  110. URL "https://github.com/keplerproject/luafilesystem/archive/v_${LUA_FILESYSTEM_VERSION_UNDERSCORE}.tar.gz"
  111. URL_MD5 ${CIVETWEB_LUA_FILESYSTEM_MD5_HASH}
  112. PREFIX "${CIVETWEB_THIRD_PARTY_DIR}"
  113. PATCH_COMMAND ${CMAKE_COMMAND} -E copy
  114. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/luafilesystem/CMakeLists.txt" <SOURCE_DIR>/CMakeLists.txt
  115. CMAKE_ARGS
  116. "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
  117. "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>"
  118. LOG_DOWNLOAD ${THIRD_PARTY_LOGGING}
  119. LOG_UPDATE ${THIRD_PARTY_LOGGING}
  120. LOG_CONFIGURE ${THIRD_PARTY_LOGGING}
  121. LOG_BUILD ${THIRD_PARTY_LOGGING}
  122. LOG_TEST ${THIRD_PARTY_LOGGING}
  123. LOG_INSTALL ${THIRD_PARTY_LOGGING})
  124. ExternalProject_Get_Property(luafilesystem INSTALL_DIR)
  125. set(LUA_FILESYSTEM_INSTALL_DIR ${INSTALL_DIR})
  126. unset(INSTALL_DIR)
  127. link_directories("${LUA_FILESYSTEM_INSTALL_DIR}/lib")
  128. include_directories("${LUA_FILESYSTEM_INSTALL_DIR}/include")
  129. set(LUA_LIBRARIES "${LUA_LIBRARIES};${LUA_FILESYSTEM_INSTALL_DIR}/lib/libluafilesystem.a")
  130. add_dependencies(civetweb-c-library luafilesystem)
  131. # Lua SQLite Support
  132. if (${CIVETWEB_LUA_SQLITE_VERSION} VERSION_EQUAL "0.9.3")
  133. set(LUA_SQLITE_FILENAME lsqlite3_fsl09w.zip)
  134. elseif (${CIVETWEB_LUA_SQLITE_VERSION} VERSION_EQUAL "0.9.2")
  135. set(LUA_SQLITE_FILENAME lsqlite3_fsl09v.zip)
  136. elseif (${CIVETWEB_LUA_SQLITE_VERSION} VERSION_EQUAL "0.9.1")
  137. set(LUA_SQLITE_FILENAME lsqlite3_fsl09t.zip)
  138. else()
  139. message(FATAL_ERROR "The Lua SQLite archive filename is unknown for version ${CIVETWEB_LUA_SQLITE_VERSION}")
  140. endif()
  141. ExternalProject_Add(luasqlite
  142. URL "http://lua.sqlite.org/index.cgi/zip/${LUA_SQLITE_FILENAME}"
  143. URL_MD5 ${CIVETWEB_LUA_SQLITE_MD5_HASH}
  144. PREFIX "${CIVETWEB_THIRD_PARTY_DIR}"
  145. PATCH_COMMAND ${CMAKE_COMMAND} -E copy
  146. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/luasqlite/CMakeLists.txt" <SOURCE_DIR>/CMakeLists.txt
  147. CMAKE_ARGS
  148. "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
  149. "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>"
  150. LOG_DOWNLOAD ${THIRD_PARTY_LOGGING}
  151. LOG_UPDATE ${THIRD_PARTY_LOGGING}
  152. LOG_CONFIGURE ${THIRD_PARTY_LOGGING}
  153. LOG_BUILD ${THIRD_PARTY_LOGGING}
  154. LOG_TEST ${THIRD_PARTY_LOGGING}
  155. LOG_INSTALL ${THIRD_PARTY_LOGGING})
  156. ExternalProject_Get_Property(luasqlite INSTALL_DIR)
  157. set(LUA_SQLITE_INSTALL_DIR ${INSTALL_DIR})
  158. unset(INSTALL_DIR)
  159. link_directories("${LUA_SQLITE_INSTALL_DIR}/lib")
  160. set(LUA_LIBRARIES "${LUA_LIBRARIES};${LUA_SQLITE_INSTALL_DIR}/lib/libluasqlite.a")
  161. add_dependencies(civetweb-c-library luasqlite)
  162. # Lua XML Support
  163. if (${CIVETWEB_LUA_XML_VERSION} VERSION_EQUAL "1.8.0")
  164. set(LUA_XML_FILENAME LuaXML_130610.zip)
  165. elseif (${CIVETWEB_LUA_XML_VERSION} VERSION_EQUAL "1.7.4")
  166. set(LUA_XML_FILENAME LuaXML_101012.zip)
  167. else()
  168. message(FATAL_ERROR "The Lua XML archive filename is unknown for version ${CIVETWEB_LUA_XML_VERSION}")
  169. endif()
  170. ExternalProject_Add(luaxml
  171. # Old:
  172. # URL "http://viremo.eludi.net/LuaXML/${LUA_XML_FILENAME}"
  173. # URL_MD5 ${CIVETWEB_LUA_XML_MD5_HASH}
  174. # PREFIX "${CIVETWEB_THIRD_PARTY_DIR}"
  175. #
  176. # TODO: find current sourde
  177. # viremo.eludi.net does no longer exist
  178. # An extended version is available at https://github.com/n1tehawk/LuaXML
  179. # but the last commit there was in 2015
  180. #
  181. URL "https://github.com/n1tehawk/LuaXML/archive/v1.8.0.zip"
  182. PREFIX "${CIVETWEB_THIRD_PARTY_DIR}"
  183. #
  184. # TODO: fix this patch command (needs someone with deeper CMake know how)
  185. PATCH_COMMAND ${CMAKE_COMMAND} -E copy
  186. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/luaxml/CMakeLists.txt" <SOURCE_DIR>/CMakeLists.txt
  187. CMAKE_ARGS
  188. "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
  189. "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>"
  190. LOG_DOWNLOAD ${THIRD_PARTY_LOGGING}
  191. LOG_UPDATE ${THIRD_PARTY_LOGGING}
  192. LOG_CONFIGURE ${THIRD_PARTY_LOGGING}
  193. LOG_BUILD ${THIRD_PARTY_LOGGING}
  194. LOG_TEST ${THIRD_PARTY_LOGGING}
  195. LOG_INSTALL ${THIRD_PARTY_LOGGING})
  196. ExternalProject_Get_Property(luaxml INSTALL_DIR)
  197. set(LUA_XML_INSTALL_DIR ${INSTALL_DIR})
  198. unset(INSTALL_DIR)
  199. link_directories("${LUA_XML_INSTALL_DIR}/lib")
  200. set(LUA_LIBRARIES "${LUA_LIBRARIES};${LUA_XML_INSTALL_DIR}/lib/libluaxml.a")
  201. add_dependencies(civetweb-c-library luaxml)
  202. # SQLite Support
  203. string (REGEX MATCHALL "[0-9]+" SQLITE_VERSION_MATCHES ${CIVETWEB_SQLITE_VERSION})
  204. list(GET SQLITE_VERSION_MATCHES 0 SQLITE_VERSION_MAJOR)
  205. list(GET SQLITE_VERSION_MATCHES 1 SQLITE_VERSION_MINOR)
  206. list(GET SQLITE_VERSION_MATCHES 2 SQLITE_VERSION_PATCH)
  207. set(SQLITE_FILE_VERSION ${SQLITE_VERSION_MAJOR}0${SQLITE_VERSION_MINOR}0${SQLITE_VERSION_PATCH}00)
  208. ExternalProject_Add(sqlite
  209. URL "http://www.sqlite.org/2015/sqlite-amalgamation-${SQLITE_FILE_VERSION}.zip"
  210. URL_MD5 ${CIVETWEB_SQLITE_MD5_HASH}
  211. PREFIX "${CIVETWEB_THIRD_PARTY_DIR}"
  212. PATCH_COMMAND ${CMAKE_COMMAND} -E copy
  213. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/sqlite/CMakeLists.txt" <SOURCE_DIR>/CMakeLists.txt
  214. CMAKE_ARGS
  215. "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
  216. "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>"
  217. LOG_DOWNLOAD ${THIRD_PARTY_LOGGING}
  218. LOG_UPDATE ${THIRD_PARTY_LOGGING}
  219. LOG_CONFIGURE ${THIRD_PARTY_LOGGING}
  220. LOG_BUILD ${THIRD_PARTY_LOGGING}
  221. LOG_TEST ${THIRD_PARTY_LOGGING}
  222. LOG_INSTALL ${THIRD_PARTY_LOGGING})
  223. ExternalProject_Get_Property(sqlite INSTALL_DIR)
  224. set(SQLITE_INSTALL_DIR ${INSTALL_DIR})
  225. unset(INSTALL_DIR)
  226. link_directories("${SQLITE_INSTALL_DIR}/lib")
  227. include_directories("${SQLITE_INSTALL_DIR}/include")
  228. set(LUA_LIBRARIES "${LUA_LIBRARIES};${SQLITE_INSTALL_DIR}/lib/libsqlite.a")
  229. add_dependencies(civetweb-c-library sqlite)
  230. # Link all the Lua libraries
  231. target_link_libraries(civetweb-c-library ${LUA_LIBRARIES})
  232. endif()
  233. # The web server executable
  234. if (CIVETWEB_ENABLE_SERVER_EXECUTABLE)
  235. add_executable(civetweb-c-executable main.c)
  236. set_target_properties(civetweb-c-executable PROPERTIES
  237. OUTPUT_NAME "civetweb"
  238. )
  239. if (CIVETWEB_INSTALL_EXECUTABLE)
  240. install(
  241. TARGETS civetweb-c-executable
  242. ARCHIVE DESTINATION lib
  243. LIBRARY DESTINATION lib
  244. RUNTIME DESTINATION bin
  245. COMPONENT server)
  246. endif()
  247. if (BUILD_SHARED_LIBS)
  248. target_compile_definitions(civetweb-c-executable PRIVATE CIVETWEB_DLL_IMPORTS)
  249. endif()
  250. target_include_directories(
  251. civetweb-c-executable PUBLIC
  252. ${PROJECT_SOURCE_DIR}/include)
  253. target_link_libraries(civetweb-c-executable civetweb-c-library)
  254. if (LIBRT_FOUND)
  255. target_link_libraries(civetweb-c-executable LIBRT::LIBRT)
  256. endif()
  257. endif()
  258. if (CIVETWEB_ENABLE_LUA)
  259. add_library(lua-library third_party/lfs.c third_party/lsqlite3.c third_party/LuaXML_lib.c third_party/sqlite3.c)
  260. set_target_properties(lua-library PROPERTIES
  261. OUTPUT_NAME "lua-library"
  262. VERSION ${CIVETWEB_VERSION}
  263. SOVERSION ${CIVETWEB_VERSION}
  264. )
  265. target_include_directories(
  266. lua-library PUBLIC
  267. ${PROJECT_SOURCE_DIR}/src/third_party/lua-5.2.4)
  268. install(
  269. TARGETS lua-library
  270. ARCHIVE DESTINATION lib
  271. LIBRARY DESTINATION lib
  272. RUNTIME DESTINATION bin
  273. COMPONENT lua-library)
  274. endif()
  275. # The C++ API library
  276. if (CIVETWEB_ENABLE_CXX)
  277. add_library(civetweb-cpp ${LIB_TYPE} CivetServer.cpp)
  278. set_target_properties(civetweb-cpp PROPERTIES
  279. OUTPUT_NAME "civetweb-cpp"
  280. VERSION ${CIVETWEB_VERSION}
  281. SOVERSION ${CIVETWEB_VERSION}
  282. )
  283. if (BUILD_SHARED_LIBS)
  284. target_compile_definitions(civetweb-cpp PRIVATE CIVETWEB_DLL_EXPORTS)
  285. endif()
  286. target_include_directories(
  287. civetweb-cpp PUBLIC
  288. ${PROJECT_SOURCE_DIR}/include)
  289. install(
  290. TARGETS civetweb-cpp
  291. ARCHIVE DESTINATION lib
  292. LIBRARY DESTINATION lib
  293. RUNTIME DESTINATION bin
  294. COMPONENT civetweb-cpp)
  295. install(FILES
  296. ${PROJECT_SOURCE_DIR}/include/CivetServer.h
  297. DESTINATION include
  298. COMPONENT civetweb-cpp)
  299. endif()