CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. function(badvpn_add_library LIB_NAME LINK_BADVPN_LIBS LINK_SYS_LIBS LIB_SOURCES)
  221. set(BADVPN_LIBS_EXEC)
  222. set(BADVPN_LIBS_PLUGIN)
  223. foreach(LIB ${LINK_BADVPN_LIBS})
  224. list(APPEND BADVPN_LIBS_EXEC "${LIB}")
  225. list(APPEND BADVPN_LIBS_PLUGIN "${LIB}-plugin")
  226. endforeach()
  227. add_library("${LIB_NAME}" STATIC ${LIB_SOURCES})
  228. target_link_libraries("${LIB_NAME}" ${BADVPN_LIBS_EXEC} ${LINK_SYS_LIBS})
  229. add_library("${LIB_NAME}-plugin" STATIC ${LIB_SOURCES})
  230. target_link_libraries("${LIB_NAME}-plugin" ${BADVPN_LIBS_PLUGIN} ${LINK_SYS_LIBS})
  231. set_target_properties("${LIB_NAME}-plugin" PROPERTIES POSITION_INDEPENDENT_CODE YES)
  232. set_target_properties("${LIB_NAME}-plugin" PROPERTIES COMPILE_FLAGS "-fvisibility=hidden -DBADVPN_PLUGIN")
  233. endfunction()
  234. # internal libraries
  235. add_subdirectory(base)
  236. add_subdirectory(system)
  237. add_subdirectory(flow)
  238. add_subdirectory(flowextra)
  239. if (OpenSSL_FOUND)
  240. set(BUILDING_SECURITY 1)
  241. add_subdirectory(security)
  242. endif ()
  243. if (NSS_FOUND)
  244. add_subdirectory(nspr_support)
  245. endif ()
  246. if (BUILD_CLIENT OR BUILDING_SECURITY)
  247. set(BUILDING_THREADWORK 1)
  248. add_subdirectory(threadwork)
  249. endif ()
  250. if (BUILD_CLIENT OR BUILD_TUN2SOCKS)
  251. add_subdirectory(tuntap)
  252. endif ()
  253. if (BUILD_SERVER)
  254. set(BUILDING_PREDICATE 1)
  255. add_subdirectory(predicate)
  256. endif ()
  257. if (BUILD_CLIENT OR BUILD_FLOODER)
  258. add_subdirectory(server_connection)
  259. endif ()
  260. if (BUILD_NCD AND NOT EMSCRIPTEN)
  261. set(BUILDING_DHCPCLIENT 1)
  262. set(BUILDING_ARPPROBE 1)
  263. set(BUILDING_UDEVMONITOR 1)
  264. set(BUILDING_RANDOM 1)
  265. add_subdirectory(stringmap)
  266. add_subdirectory(udevmonitor)
  267. add_subdirectory(dhcpclient)
  268. add_subdirectory(arpprobe)
  269. add_subdirectory(random)
  270. endif ()
  271. if (BUILD_TUN2SOCKS)
  272. add_subdirectory(socksclient)
  273. add_subdirectory(udpgw_client)
  274. add_subdirectory(lwip)
  275. endif ()
  276. if (BUILD_TUNCTL)
  277. add_subdirectory(tunctl)
  278. endif ()
  279. # example programs
  280. if (BUILD_EXAMPLES)
  281. add_subdirectory(examples)
  282. endif ()
  283. # tests
  284. if (BUILD_TESTS)
  285. add_subdirectory(tests)
  286. endif ()
  287. # server
  288. if (BUILD_SERVER)
  289. add_subdirectory(server)
  290. endif ()
  291. # client
  292. if (BUILD_CLIENT)
  293. add_subdirectory(client)
  294. endif ()
  295. # flooder
  296. if (BUILD_FLOODER)
  297. add_subdirectory(flooder)
  298. endif ()
  299. # tun2socks
  300. if (BUILD_TUN2SOCKS)
  301. add_subdirectory(tun2socks)
  302. endif ()
  303. # udpgw
  304. if (BUILD_UDPGW)
  305. add_subdirectory(udpgw)
  306. endif ()
  307. # ncd
  308. if (BUILD_NCD)
  309. add_subdirectory(ncd)
  310. if (NOT EMSCRIPTEN)
  311. add_subdirectory(ncd-request)
  312. endif ()
  313. endif ()
  314. # dostest
  315. if (BUILD_DOSTEST)
  316. add_subdirectory(dostest)
  317. endif ()
  318. message(STATUS "Building components:")
  319. # print what we're building and what not
  320. foreach (name ${BUILD_COMPONENTS})
  321. # to lower name
  322. string(TOLOWER "${name}" name_withspaces)
  323. # append spaces to name
  324. #while (TRUE)
  325. # string(LENGTH "${name_withspaces}" length)
  326. # if (NOT (length LESS 12))
  327. # break()
  328. # endif ()
  329. # set(name_withspaces "${name_withspaces} ")
  330. #endwhile ()
  331. # determine if we're building
  332. if (BUILD_${name})
  333. set(building "yes")
  334. else ()
  335. set(building "no")
  336. endif ()
  337. message(STATUS " ${name_withspaces} ${building}")
  338. endforeach ()