CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. -Wstrict-aliasing)
  116. endif ()
  117. if (CMAKE_C_COMPILER_ID MATCHES "^Clang")
  118. add_definitions(-Wno-initializer-overrides -Wno-tautological-constant-out-of-range-compare)
  119. endif ()
  120. endif ()
  121. # platform-specific stuff
  122. if (WIN32)
  123. add_definitions(-DBADVPN_USE_WINAPI -D_WIN32_WINNT=0x600 -DWIN32_LEAN_AND_MEAN)
  124. set(CMAKE_REQUIRED_DEFINITIONS "-D_WIN32_WINNT=0x600")
  125. check_symbol_exists(WSAID_WSASENDMSG "winsock2.h;mswsock.h" HAVE_MSW_1)
  126. check_symbol_exists(WSAID_WSARECVMSG "winsock2.h;mswsock.h" HAVE_MSW_2)
  127. check_symbol_exists(WSAID_ACCEPTEX "winsock2.h;mswsock.h" HAVE_MSW_3)
  128. check_symbol_exists(WSAID_GETACCEPTEXSOCKADDRS "winsock2.h;mswsock.h" HAVE_MSW_4)
  129. check_symbol_exists(WSAID_CONNECTEX "winsock2.h;mswsock.h" HAVE_MSW_5)
  130. set(CMAKE_REQUIRED_DEFINITIONS "")
  131. if (NOT (HAVE_MSW_1 AND HAVE_MSW_2 AND HAVE_MSW_3 AND HAVE_MSW_4 AND HAVE_MSW_5))
  132. add_definitions(-DBADVPN_USE_SHIPPED_MSWSOCK)
  133. check_type_size(WSAMSG HAVE_WSAMSG)
  134. if (NOT HAVE_WSAMSG)
  135. add_definitions(-DBADVPN_SHIPPED_MSWSOCK_DECLARE_WSAMSG)
  136. endif ()
  137. endif ()
  138. else ()
  139. set(BADVPN_THREADWORK_USE_PTHREAD 1)
  140. add_definitions(-DBADVPN_THREADWORK_USE_PTHREAD)
  141. link_libraries(rt)
  142. if (EMSCRIPTEN)
  143. add_definitions(-DBADVPN_EMSCRIPTEN)
  144. add_definitions(-DBADVPN_NO_PROCESS -DBADVPN_NO_UDEV -DBADVPN_NO_RANDOM)
  145. elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  146. add_definitions(-DBADVPN_LINUX)
  147. check_include_files(sys/signalfd.h HAVE_SYS_SIGNALFD_H)
  148. if (HAVE_SYS_SIGNALFD_H)
  149. add_definitions(-DBADVPN_USE_SIGNALFD)
  150. else ()
  151. add_definitions(-DBADVPN_USE_SELFPIPE)
  152. endif ()
  153. check_include_files(sys/epoll.h HAVE_SYS_EPOLL_H)
  154. if (HAVE_SYS_EPOLL_H)
  155. add_definitions(-DBADVPN_USE_EPOLL)
  156. else ()
  157. add_definitions(-DBADVPN_USE_POLL)
  158. endif ()
  159. check_include_files(linux/rfkill.h HAVE_LINUX_RFKILL_H)
  160. if (HAVE_LINUX_RFKILL_H)
  161. add_definitions(-DBADVPN_USE_LINUX_RFKILL)
  162. set(BADVPN_USE_LINUX_RFKILL 1)
  163. endif ()
  164. check_include_files(linux/input.h HAVE_LINUX_INPUT_H)
  165. if (HAVE_LINUX_INPUT_H)
  166. add_definitions(-DBADVPN_USE_LINUX_INPUT)
  167. set(BADVPN_USE_LINUX_INPUT 1)
  168. endif ()
  169. check_include_files(sys/inotify.h HAVE_SYS_INOTIFY_H)
  170. if (HAVE_SYS_INOTIFY_H)
  171. add_definitions(-DBADVPN_USE_INOTIFY)
  172. set(BADVPN_USE_INOTIFY 1)
  173. endif ()
  174. elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  175. add_definitions(-DBADVPN_FREEBSD)
  176. check_symbol_exists(kqueue "sys/types.h;sys/event.h;sys/time.h" HAVE_KQUEUE)
  177. if (NOT HAVE_KQUEUE)
  178. message(FATAL_ERROR "kqueue is required")
  179. endif ()
  180. add_definitions(-DBADVPN_USE_KEVENT)
  181. endif ()
  182. if (NOT DEFINED BADVPN_WITHOUT_CRYPTODEV)
  183. check_include_files(crypto/cryptodev.h HAVE_CRYPTO_CRYPTODEV_H)
  184. if (HAVE_CRYPTO_CRYPTODEV_H)
  185. add_definitions(-DBADVPN_USE_CRYPTODEV)
  186. elseif (DEFINED BADVPN_WITH_CRYPTODEV)
  187. message(FATAL_ERROR "crypto/cryptodev.h not found")
  188. endif ()
  189. endif ()
  190. endif ()
  191. # check for syslog
  192. check_include_files(syslog.h HAVE_SYSLOG_H)
  193. if (HAVE_SYSLOG_H)
  194. add_definitions(-DBADVPN_USE_SYSLOG)
  195. endif ()
  196. # add preprocessor definitions
  197. if (BIG_ENDIAN)
  198. add_definitions(-DBADVPN_BIG_ENDIAN)
  199. else ()
  200. add_definitions(-DBADVPN_LITTLE_ENDIAN)
  201. endif ()
  202. # install man pages
  203. install(
  204. FILES badvpn.7
  205. DESTINATION share/man/man7
  206. )
  207. # reset variables indicating whether we're building various libraries,
  208. # and set them in the respective CMakeLists files. This is used to disable
  209. # building examples and tests which require libraries that are not available.
  210. set(BUILDING_SECURITY 0)
  211. set(BUILDING_DHCPCLIENT 0)
  212. set(BUILDING_ARPPROBE 0)
  213. set(BUILDING_BKIO 0)
  214. set(BUILDING_PREDICATE 0)
  215. set(BUILDING_UDEVMONITOR 0)
  216. set(BUILDING_THREADWORK 0)
  217. set(BUILDING_RANDOM 0)
  218. # internal libraries
  219. add_subdirectory(base)
  220. add_subdirectory(system)
  221. add_subdirectory(flow)
  222. add_subdirectory(flowextra)
  223. if (OpenSSL_FOUND)
  224. set(BUILDING_SECURITY 1)
  225. add_subdirectory(security)
  226. endif ()
  227. if (NSS_FOUND)
  228. add_subdirectory(nspr_support)
  229. endif ()
  230. if (BUILD_CLIENT OR BUILDING_SECURITY)
  231. set(BUILDING_THREADWORK 1)
  232. add_subdirectory(threadwork)
  233. endif ()
  234. if (BUILD_CLIENT OR BUILD_TUN2SOCKS)
  235. add_subdirectory(tuntap)
  236. endif ()
  237. if (BUILD_SERVER)
  238. set(BUILDING_PREDICATE 1)
  239. add_subdirectory(predicate)
  240. endif ()
  241. if (BUILD_CLIENT OR BUILD_FLOODER)
  242. add_subdirectory(server_connection)
  243. endif ()
  244. if (BUILD_NCD AND NOT EMSCRIPTEN)
  245. set(BUILDING_DHCPCLIENT 1)
  246. set(BUILDING_ARPPROBE 1)
  247. set(BUILDING_UDEVMONITOR 1)
  248. set(BUILDING_RANDOM 1)
  249. add_subdirectory(stringmap)
  250. add_subdirectory(udevmonitor)
  251. add_subdirectory(dhcpclient)
  252. add_subdirectory(arpprobe)
  253. add_subdirectory(random)
  254. endif ()
  255. if (BUILD_TUN2SOCKS)
  256. add_subdirectory(socksclient)
  257. add_subdirectory(udpgw_client)
  258. add_subdirectory(lwip)
  259. endif ()
  260. if (BUILD_TUNCTL)
  261. add_subdirectory(tunctl)
  262. endif ()
  263. # example programs
  264. if (BUILD_EXAMPLES)
  265. add_subdirectory(examples)
  266. endif ()
  267. # tests
  268. if (BUILD_TESTS)
  269. add_subdirectory(tests)
  270. endif ()
  271. # server
  272. if (BUILD_SERVER)
  273. add_subdirectory(server)
  274. endif ()
  275. # client
  276. if (BUILD_CLIENT)
  277. add_subdirectory(client)
  278. endif ()
  279. # flooder
  280. if (BUILD_FLOODER)
  281. add_subdirectory(flooder)
  282. endif ()
  283. # tun2socks
  284. if (BUILD_TUN2SOCKS)
  285. add_subdirectory(tun2socks)
  286. endif ()
  287. # udpgw
  288. if (BUILD_UDPGW)
  289. add_subdirectory(udpgw)
  290. endif ()
  291. # ncd
  292. if (BUILD_NCD)
  293. add_subdirectory(ncd)
  294. if (NOT EMSCRIPTEN)
  295. add_subdirectory(ncd-request)
  296. endif ()
  297. endif ()
  298. # dostest
  299. if (BUILD_DOSTEST)
  300. add_subdirectory(dostest)
  301. endif ()
  302. message(STATUS "Building components:")
  303. # print what we're building and what not
  304. foreach (name ${BUILD_COMPONENTS})
  305. # to lower name
  306. string(TOLOWER "${name}" name_withspaces)
  307. # append spaces to name
  308. #while (TRUE)
  309. # string(LENGTH "${name_withspaces}" length)
  310. # if (NOT (length LESS 12))
  311. # break()
  312. # endif ()
  313. # set(name_withspaces "${name_withspaces} ")
  314. #endwhile ()
  315. # determine if we're building
  316. if (BUILD_${name})
  317. set(building "yes")
  318. else ()
  319. set(building "no")
  320. endif ()
  321. message(STATUS " ${name_withspaces} ${building}")
  322. endforeach ()