CMakeLists.txt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. cmake_minimum_required(VERSION 2.8)
  2. project(BADVPN C)
  3. set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
  4. include(TestBigEndian)
  5. include(CheckIncludeFiles)
  6. include(CheckSymbolExists)
  7. include(CheckTypeSize)
  8. set(BUILD_COMPONENTS)
  9. macro (build_switch name default)
  10. if (NOT DEFINED BUILD_${name})
  11. if (BUILD_NOTHING_BY_DEFAULT)
  12. set(BUILD_${name} 0)
  13. else ()
  14. set(BUILD_${name} "${default}")
  15. endif ()
  16. endif ()
  17. list(APPEND BUILD_COMPONENTS "${name}")
  18. endmacro ()
  19. # define build defaults
  20. build_switch(EXAMPLES 1)
  21. build_switch(TESTS 1)
  22. build_switch(SERVER 1)
  23. build_switch(CLIENT 1)
  24. build_switch(FLOODER 1)
  25. build_switch(TUN2SOCKS 1)
  26. build_switch(UDPGW 1)
  27. build_switch(NCD 1)
  28. if (BUILD_NCD AND NOT (CMAKE_SYSTEM_NAME STREQUAL "Linux"))
  29. message(FATAL_ERROR, "NCD is only available on Linux")
  30. endif ()
  31. if (BUILD_CLIENT OR BUILD_SERVER OR BUILD_NCD)
  32. find_package(OpenSSL REQUIRED)
  33. set(LIBCRYPTO_INCLUDE_DIRS "${OpenSSL_INCLUDE_DIRS}")
  34. set(LIBCRYPTO_LIBRARY_DIRS "${OpenSSL_LIBRARY_DIRS}")
  35. set(LIBCRYPTO_LIBRARIES "${OpenSSL_LIBRARIES}")
  36. endif ()
  37. if (BUILD_SERVER OR BUILD_CLIENT OR BUILD_FLOODER)
  38. find_package(NSPR REQUIRED)
  39. find_package(NSS REQUIRED)
  40. endif ()
  41. include_directories(
  42. ${CMAKE_CURRENT_SOURCE_DIR}
  43. ${LIBCRYPTO_INCLUDE_DIRS}
  44. ${NSPR_INCLUDE_DIRS}
  45. ${NSS_INCLUDE_DIRS}
  46. ${FUSE_INCLUDE_DIRS}
  47. lwip/custom
  48. lwip/src/include
  49. lwip/src/include/ipv4
  50. )
  51. link_directories(
  52. ${LIBCRYPTO_LIBRARY_DIRS}
  53. ${NSPR_LIBRARY_DIRS}
  54. ${NSS_LIBRARY_DIRS}
  55. ${FUSE_LIBRARY_DIRS}
  56. )
  57. add_definitions(
  58. ${FUSE_CFLAGS}
  59. )
  60. test_big_endian(BIG_ENDIAN)
  61. check_type_size(int INT_SIZE)
  62. if (NOT (INT_SIZE GREATER "3"))
  63. message(FATAL_ERROR "int must be at least 32 bits")
  64. endif ()
  65. check_type_size(size_t SIZE_SIZE)
  66. if (NOT (SIZE_SIZE GREATER INT_SIZE OR SIZE_SIZE EQUAL INT_SIZE))
  67. message(FATAL_ERROR "size_t must be greater or equal than int")
  68. endif ()
  69. add_definitions(-std=gnu99 -Werror=implicit-function-declaration -Wno-unused-value -Wno-parentheses -Wno-switch-enum -Wredundant-decls)
  70. # platform-specific stuff
  71. if (WIN32)
  72. add_definitions(-DBADVPN_USE_WINAPI -D_WIN32_WINNT=0x600 -DWIN32_LEAN_AND_MEAN)
  73. set(CMAKE_REQUIRED_DEFINITIONS "-D_WIN32_WINNT=0x600")
  74. check_symbol_exists(WSAID_WSASENDMSG "mswsock.h" HAVE_MSW_1)
  75. check_symbol_exists(WSAID_WSARECVMSG "mswsock.h" HAVE_MSW_2)
  76. check_symbol_exists(WSAID_ACCEPTEX "mswsock.h" HAVE_MSW_3)
  77. check_symbol_exists(WSAID_GETACCEPTEXSOCKADDRS "mswsock.h" HAVE_MSW_4)
  78. check_symbol_exists(WSAID_CONNECTEX "mswsock.h" HAVE_MSW_5)
  79. set(CMAKE_REQUIRED_DEFINITIONS "")
  80. if (NOT (HAVE_MSW_1 AND HAVE_MSW_2 AND HAVE_MSW_3 AND HAVE_MSW_4 AND HAVE_MSW_5))
  81. add_definitions(-DBADVPN_USE_SHIPPED_MSWSOCK)
  82. check_type_size(WSAMSG HAVE_WSAMSG)
  83. if (NOT HAVE_WSAMSG)
  84. add_definitions(-DBADVPN_SHIPPED_MSWSOCK_DECLARE_WSAMSG)
  85. endif ()
  86. endif ()
  87. else ()
  88. set(BADVPN_THREADWORK_USE_PTHREAD 1)
  89. add_definitions(-DBADVPN_THREADWORK_USE_PTHREAD)
  90. link_libraries(rt)
  91. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  92. add_definitions(-DBADVPN_LINUX)
  93. check_include_files(sys/signalfd.h HAVE_SYS_SIGNALFD_H)
  94. if (HAVE_SYS_SIGNALFD_H)
  95. add_definitions(-DBADVPN_USE_SIGNALFD)
  96. else ()
  97. add_definitions(-DBADVPN_USE_SELFPIPE)
  98. endif ()
  99. check_include_files(sys/epoll.h HAVE_SYS_EPOLL_H)
  100. if (HAVE_SYS_EPOLL_H)
  101. add_definitions(-DBADVPN_USE_EPOLL)
  102. else ()
  103. add_definitions(-DBADVPN_USE_POLL)
  104. endif ()
  105. check_include_files(linux/rfkill.h HAVE_LINUX_RFKILL_H)
  106. if (HAVE_LINUX_RFKILL_H)
  107. add_definitions(-DBADVPN_USE_LINUX_RFKILL)
  108. set(BADVPN_USE_LINUX_RFKILL 1)
  109. endif ()
  110. check_include_files(linux/input.h HAVE_LINUX_INPUT_H)
  111. if (HAVE_LINUX_INPUT_H)
  112. add_definitions(-DBADVPN_USE_LINUX_INPUT)
  113. set(BADVPN_USE_LINUX_INPUT 1)
  114. endif ()
  115. check_include_files(sys/inotify.h HAVE_SYS_INOTIFY_H)
  116. if (HAVE_SYS_INOTIFY_H)
  117. add_definitions(-DBADVPN_USE_INOTIFY)
  118. set(BADVPN_USE_INOTIFY 1)
  119. endif ()
  120. elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  121. add_definitions(-DBADVPN_FREEBSD)
  122. check_symbol_exists(kqueue "sys/types.h;sys/event.h;sys/time.h" HAVE_KQUEUE)
  123. if (NOT HAVE_KQUEUE)
  124. message(FATAL_ERROR "kqueue is required")
  125. endif ()
  126. add_definitions(-DBADVPN_USE_KEVENT)
  127. endif ()
  128. if (NOT DEFINED BADVPN_WITHOUT_CRYPTODEV)
  129. check_include_files(crypto/cryptodev.h HAVE_CRYPTO_CRYPTODEV_H)
  130. if (HAVE_CRYPTO_CRYPTODEV_H)
  131. add_definitions(-DBADVPN_USE_CRYPTODEV)
  132. elseif (DEFINED BADVPN_WITH_CRYPTODEV)
  133. message(FATAL_ERROR "crypto/cryptodev.h not found")
  134. endif ()
  135. endif ()
  136. endif ()
  137. # add preprocessor definitions
  138. if (BIG_ENDIAN)
  139. add_definitions(-DBADVPN_BIG_ENDIAN)
  140. else ()
  141. add_definitions(-DBADVPN_LITTLE_ENDIAN)
  142. endif ()
  143. # install man pages
  144. install(
  145. FILES badvpn.7
  146. DESTINATION share/man/man7
  147. )
  148. # reset variables indicating whether we're building various libraries,
  149. # and set them in the respective CMakeLists files. This is used to disable
  150. # building examples and tests which require libraries that are not available.
  151. set(BUILDING_SECURITY 0)
  152. set(BUILDING_DHCPCLIENT 0)
  153. set(BUILDING_BKIO 0)
  154. set(BUILDING_PREDICATE 0)
  155. set(BUILDING_UDEVMONITOR 0)
  156. set(BUILDING_THREADWORK 0)
  157. # internal libraries
  158. add_subdirectory(base)
  159. add_subdirectory(system)
  160. add_subdirectory(flow)
  161. add_subdirectory(flowextra)
  162. if (OpenSSL_FOUND)
  163. set(BUILDING_SECURITY 1)
  164. add_subdirectory(security)
  165. endif ()
  166. if (NSS_FOUND)
  167. add_subdirectory(nspr_support)
  168. endif ()
  169. if (BUILD_CLIENT OR BUILDING_SECURITY)
  170. set(BUILDING_THREADWORK 1)
  171. add_subdirectory(threadwork)
  172. endif ()
  173. if (BUILD_CLIENT OR BUILD_TUN2SOCKS)
  174. add_subdirectory(tuntap)
  175. endif ()
  176. if (BUILD_SERVER)
  177. set(BUILDING_PREDICATE 1)
  178. add_subdirectory(predicate)
  179. endif ()
  180. if (BUILD_CLIENT OR BUILD_FLOODER)
  181. add_subdirectory(server_connection)
  182. endif ()
  183. if (BUILD_NCD)
  184. set(BUILDING_DHCPCLIENT 1)
  185. set(BUILDING_UDEVMONITOR 1)
  186. add_subdirectory(stringmap)
  187. add_subdirectory(udevmonitor)
  188. add_subdirectory(dhcpclient)
  189. endif ()
  190. if (BUILD_TUN2SOCKS)
  191. add_subdirectory(socksclient)
  192. add_subdirectory(udpgw_client)
  193. add_subdirectory(lwip)
  194. endif ()
  195. # example programs
  196. if (BUILD_EXAMPLES)
  197. add_subdirectory(examples)
  198. endif ()
  199. # tests
  200. if (BUILD_TESTS)
  201. add_subdirectory(tests)
  202. endif ()
  203. # server
  204. if (BUILD_SERVER)
  205. add_subdirectory(server)
  206. endif ()
  207. # client
  208. if (BUILD_CLIENT)
  209. add_subdirectory(client)
  210. endif ()
  211. # flooder
  212. if (BUILD_FLOODER)
  213. add_subdirectory(flooder)
  214. endif ()
  215. # tun2socks
  216. if (BUILD_TUN2SOCKS)
  217. add_subdirectory(tun2socks)
  218. endif ()
  219. # udpgw
  220. if (BUILD_UDPGW)
  221. add_subdirectory(udpgw)
  222. endif ()
  223. # ncd
  224. if (BUILD_NCD)
  225. add_subdirectory(ncd)
  226. endif ()
  227. message(STATUS "Building components:")
  228. # print what we're building and what not
  229. foreach (name ${BUILD_COMPONENTS})
  230. # to lower name
  231. string(TOLOWER "${name}" name_withspaces)
  232. # append spaces to name
  233. #while (TRUE)
  234. # string(LENGTH "${name_withspaces}" length)
  235. # if (NOT (length LESS 12))
  236. # break()
  237. # endif ()
  238. # set(name_withspaces "${name_withspaces} ")
  239. #endwhile ()
  240. # determine if we're building
  241. if (BUILD_${name})
  242. set(building "yes")
  243. else ()
  244. set(building "no")
  245. endif ()
  246. message(STATUS " ${name_withspaces} ${building}")
  247. endforeach ()