CMakeLists.txt 10 KB

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