CMakeLists.txt 9.1 KB

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