CMakeLists.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. if (MSVC)
  94. add_definitions(/TP -D_CRT_SECURE_NO_WARNINGS /wd4065 /wd4018 /wd4533 /wd4244 /wd4102)
  95. else ()
  96. add_definitions(-std=gnu99 -Werror=implicit-function-declaration -Wno-unused-value -Wno-parentheses -Wno-switch-enum -Wno-switch -Wredundant-decls)
  97. endif ()
  98. # platform-specific stuff
  99. if (WIN32)
  100. add_definitions(-DBADVPN_USE_WINAPI -D_WIN32_WINNT=0x600 -DWIN32_LEAN_AND_MEAN)
  101. set(CMAKE_REQUIRED_DEFINITIONS "-D_WIN32_WINNT=0x600")
  102. check_symbol_exists(WSAID_WSASENDMSG "winsock2.h;mswsock.h" HAVE_MSW_1)
  103. check_symbol_exists(WSAID_WSARECVMSG "winsock2.h;mswsock.h" HAVE_MSW_2)
  104. check_symbol_exists(WSAID_ACCEPTEX "winsock2.h;mswsock.h" HAVE_MSW_3)
  105. check_symbol_exists(WSAID_GETACCEPTEXSOCKADDRS "winsock2.h;mswsock.h" HAVE_MSW_4)
  106. check_symbol_exists(WSAID_CONNECTEX "winsock2.h;mswsock.h" HAVE_MSW_5)
  107. set(CMAKE_REQUIRED_DEFINITIONS "")
  108. if (NOT (HAVE_MSW_1 AND HAVE_MSW_2 AND HAVE_MSW_3 AND HAVE_MSW_4 AND HAVE_MSW_5))
  109. add_definitions(-DBADVPN_USE_SHIPPED_MSWSOCK)
  110. check_type_size(WSAMSG HAVE_WSAMSG)
  111. if (NOT HAVE_WSAMSG)
  112. add_definitions(-DBADVPN_SHIPPED_MSWSOCK_DECLARE_WSAMSG)
  113. endif ()
  114. endif ()
  115. else ()
  116. set(BADVPN_THREADWORK_USE_PTHREAD 1)
  117. add_definitions(-DBADVPN_THREADWORK_USE_PTHREAD)
  118. link_libraries(rt)
  119. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  120. add_definitions(-DBADVPN_LINUX)
  121. check_include_files(sys/signalfd.h HAVE_SYS_SIGNALFD_H)
  122. if (HAVE_SYS_SIGNALFD_H)
  123. add_definitions(-DBADVPN_USE_SIGNALFD)
  124. else ()
  125. add_definitions(-DBADVPN_USE_SELFPIPE)
  126. endif ()
  127. check_include_files(sys/epoll.h HAVE_SYS_EPOLL_H)
  128. if (HAVE_SYS_EPOLL_H)
  129. add_definitions(-DBADVPN_USE_EPOLL)
  130. else ()
  131. add_definitions(-DBADVPN_USE_POLL)
  132. endif ()
  133. check_include_files(linux/rfkill.h HAVE_LINUX_RFKILL_H)
  134. if (HAVE_LINUX_RFKILL_H)
  135. add_definitions(-DBADVPN_USE_LINUX_RFKILL)
  136. set(BADVPN_USE_LINUX_RFKILL 1)
  137. endif ()
  138. check_include_files(linux/input.h HAVE_LINUX_INPUT_H)
  139. if (HAVE_LINUX_INPUT_H)
  140. add_definitions(-DBADVPN_USE_LINUX_INPUT)
  141. set(BADVPN_USE_LINUX_INPUT 1)
  142. endif ()
  143. check_include_files(sys/inotify.h HAVE_SYS_INOTIFY_H)
  144. if (HAVE_SYS_INOTIFY_H)
  145. add_definitions(-DBADVPN_USE_INOTIFY)
  146. set(BADVPN_USE_INOTIFY 1)
  147. endif ()
  148. elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  149. add_definitions(-DBADVPN_FREEBSD)
  150. check_symbol_exists(kqueue "sys/types.h;sys/event.h;sys/time.h" HAVE_KQUEUE)
  151. if (NOT HAVE_KQUEUE)
  152. message(FATAL_ERROR "kqueue is required")
  153. endif ()
  154. add_definitions(-DBADVPN_USE_KEVENT)
  155. endif ()
  156. if (NOT DEFINED BADVPN_WITHOUT_CRYPTODEV)
  157. check_include_files(crypto/cryptodev.h HAVE_CRYPTO_CRYPTODEV_H)
  158. if (HAVE_CRYPTO_CRYPTODEV_H)
  159. add_definitions(-DBADVPN_USE_CRYPTODEV)
  160. elseif (DEFINED BADVPN_WITH_CRYPTODEV)
  161. message(FATAL_ERROR "crypto/cryptodev.h not found")
  162. endif ()
  163. endif ()
  164. endif ()
  165. # add preprocessor definitions
  166. if (BIG_ENDIAN)
  167. add_definitions(-DBADVPN_BIG_ENDIAN)
  168. else ()
  169. add_definitions(-DBADVPN_LITTLE_ENDIAN)
  170. endif ()
  171. # install man pages
  172. install(
  173. FILES badvpn.7
  174. DESTINATION share/man/man7
  175. )
  176. # reset variables indicating whether we're building various libraries,
  177. # and set them in the respective CMakeLists files. This is used to disable
  178. # building examples and tests which require libraries that are not available.
  179. set(BUILDING_SECURITY 0)
  180. set(BUILDING_DHCPCLIENT 0)
  181. set(BUILDING_ARPPROBE 0)
  182. set(BUILDING_BKIO 0)
  183. set(BUILDING_PREDICATE 0)
  184. set(BUILDING_UDEVMONITOR 0)
  185. set(BUILDING_THREADWORK 0)
  186. # internal libraries
  187. add_subdirectory(base)
  188. add_subdirectory(system)
  189. add_subdirectory(flow)
  190. add_subdirectory(flowextra)
  191. if (OpenSSL_FOUND)
  192. set(BUILDING_SECURITY 1)
  193. add_subdirectory(security)
  194. endif ()
  195. if (NSS_FOUND)
  196. add_subdirectory(nspr_support)
  197. endif ()
  198. if (BUILD_CLIENT OR BUILDING_SECURITY)
  199. set(BUILDING_THREADWORK 1)
  200. add_subdirectory(threadwork)
  201. endif ()
  202. if (BUILD_CLIENT OR BUILD_TUN2SOCKS)
  203. add_subdirectory(tuntap)
  204. endif ()
  205. if (BUILD_SERVER)
  206. set(BUILDING_PREDICATE 1)
  207. add_subdirectory(predicate)
  208. endif ()
  209. if (BUILD_CLIENT OR BUILD_FLOODER)
  210. add_subdirectory(server_connection)
  211. endif ()
  212. if (BUILD_NCD)
  213. set(BUILDING_DHCPCLIENT 1)
  214. set(BUILDING_ARPPROBE 1)
  215. set(BUILDING_UDEVMONITOR 1)
  216. add_subdirectory(stringmap)
  217. add_subdirectory(udevmonitor)
  218. add_subdirectory(dhcpclient)
  219. add_subdirectory(arpprobe)
  220. endif ()
  221. if (BUILD_TUN2SOCKS)
  222. add_subdirectory(socksclient)
  223. add_subdirectory(udpgw_client)
  224. add_subdirectory(lwip)
  225. endif ()
  226. if (BUILD_TUNCTL)
  227. add_subdirectory(tunctl)
  228. endif ()
  229. # example programs
  230. if (BUILD_EXAMPLES)
  231. add_subdirectory(examples)
  232. endif ()
  233. # tests
  234. if (BUILD_TESTS)
  235. add_subdirectory(tests)
  236. endif ()
  237. # server
  238. if (BUILD_SERVER)
  239. add_subdirectory(server)
  240. endif ()
  241. # client
  242. if (BUILD_CLIENT)
  243. add_subdirectory(client)
  244. endif ()
  245. # flooder
  246. if (BUILD_FLOODER)
  247. add_subdirectory(flooder)
  248. endif ()
  249. # tun2socks
  250. if (BUILD_TUN2SOCKS)
  251. add_subdirectory(tun2socks)
  252. endif ()
  253. # udpgw
  254. if (BUILD_UDPGW)
  255. add_subdirectory(udpgw)
  256. endif ()
  257. # ncd
  258. if (BUILD_NCD)
  259. add_subdirectory(ncd)
  260. add_subdirectory(ncd-request)
  261. endif ()
  262. message(STATUS "Building components:")
  263. # print what we're building and what not
  264. foreach (name ${BUILD_COMPONENTS})
  265. # to lower name
  266. string(TOLOWER "${name}" name_withspaces)
  267. # append spaces to name
  268. #while (TRUE)
  269. # string(LENGTH "${name_withspaces}" length)
  270. # if (NOT (length LESS 12))
  271. # break()
  272. # endif ()
  273. # set(name_withspaces "${name_withspaces} ")
  274. #endwhile ()
  275. # determine if we're building
  276. if (BUILD_${name})
  277. set(building "yes")
  278. else ()
  279. set(building "no")
  280. endif ()
  281. message(STATUS " ${name_withspaces} ${building}")
  282. endforeach ()