CMakeLists.txt 10 KB

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