CMakeLists.txt 8.4 KB

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