CMakeLists.txt 11 KB

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