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