CMakeLists.txt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 default)
  10. if (NOT DEFINED BUILD_${name})
  11. if (BUILD_NOTHING_BY_DEFAULT)
  12. set(BUILD_${name} 0)
  13. else ()
  14. set(BUILD_${name} "${default}")
  15. endif ()
  16. endif ()
  17. list(APPEND BUILD_COMPONENTS "${name}")
  18. endmacro ()
  19. # define build defaults
  20. build_switch(EXAMPLES 1)
  21. build_switch(TESTS 1)
  22. build_switch(SERVER 1)
  23. build_switch(CLIENT 1)
  24. build_switch(FLOODER 1)
  25. build_switch(TUN2SOCKS 1)
  26. build_switch(UDPGW 1)
  27. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  28. build_switch(NCD 1)
  29. else ()
  30. build_switch(NCD 0)
  31. endif ()
  32. if (BUILD_NCD AND NOT (CMAKE_SYSTEM_NAME STREQUAL "Linux"))
  33. message(FATAL_ERROR "NCD is only available on Linux")
  34. endif ()
  35. if (BUILD_CLIENT OR BUILD_SERVER OR BUILD_NCD)
  36. find_package(OpenSSL REQUIRED)
  37. set(LIBCRYPTO_INCLUDE_DIRS "${OpenSSL_INCLUDE_DIRS}")
  38. set(LIBCRYPTO_LIBRARY_DIRS "${OpenSSL_LIBRARY_DIRS}")
  39. set(LIBCRYPTO_LIBRARIES "${OpenSSL_LIBRARIES}")
  40. endif ()
  41. if (BUILD_SERVER OR BUILD_CLIENT OR BUILD_FLOODER)
  42. find_package(NSPR REQUIRED)
  43. find_package(NSS REQUIRED)
  44. endif ()
  45. include_directories(
  46. ${CMAKE_CURRENT_SOURCE_DIR}
  47. ${LIBCRYPTO_INCLUDE_DIRS}
  48. ${NSPR_INCLUDE_DIRS}
  49. ${NSS_INCLUDE_DIRS}
  50. ${FUSE_INCLUDE_DIRS}
  51. lwip/custom
  52. lwip/src/include
  53. lwip/src/include/ipv4
  54. )
  55. link_directories(
  56. ${LIBCRYPTO_LIBRARY_DIRS}
  57. ${NSPR_LIBRARY_DIRS}
  58. ${NSS_LIBRARY_DIRS}
  59. ${FUSE_LIBRARY_DIRS}
  60. )
  61. add_definitions(
  62. ${FUSE_CFLAGS}
  63. )
  64. test_big_endian(BIG_ENDIAN)
  65. check_type_size(int INT_SIZE)
  66. if (NOT (INT_SIZE GREATER "3"))
  67. message(FATAL_ERROR "int must be at least 32 bits")
  68. endif ()
  69. check_type_size(size_t SIZE_SIZE)
  70. if (NOT (SIZE_SIZE GREATER INT_SIZE OR SIZE_SIZE EQUAL INT_SIZE))
  71. message(FATAL_ERROR "size_t must be greater or equal than int")
  72. endif ()
  73. add_definitions(-std=gnu99 -Werror=implicit-function-declaration -Wno-unused-value -Wno-parentheses -Wno-switch-enum -Wredundant-decls)
  74. # platform-specific stuff
  75. if (WIN32)
  76. add_definitions(-DBADVPN_USE_WINAPI -D_WIN32_WINNT=0x600 -DWIN32_LEAN_AND_MEAN)
  77. set(CMAKE_REQUIRED_DEFINITIONS "-D_WIN32_WINNT=0x600")
  78. check_symbol_exists(WSAID_WSASENDMSG "mswsock.h" HAVE_MSW_1)
  79. check_symbol_exists(WSAID_WSARECVMSG "mswsock.h" HAVE_MSW_2)
  80. check_symbol_exists(WSAID_ACCEPTEX "mswsock.h" HAVE_MSW_3)
  81. check_symbol_exists(WSAID_GETACCEPTEXSOCKADDRS "mswsock.h" HAVE_MSW_4)
  82. check_symbol_exists(WSAID_CONNECTEX "mswsock.h" HAVE_MSW_5)
  83. set(CMAKE_REQUIRED_DEFINITIONS "")
  84. if (NOT (HAVE_MSW_1 AND HAVE_MSW_2 AND HAVE_MSW_3 AND HAVE_MSW_4 AND HAVE_MSW_5))
  85. add_definitions(-DBADVPN_USE_SHIPPED_MSWSOCK)
  86. check_type_size(WSAMSG HAVE_WSAMSG)
  87. if (NOT HAVE_WSAMSG)
  88. add_definitions(-DBADVPN_SHIPPED_MSWSOCK_DECLARE_WSAMSG)
  89. endif ()
  90. endif ()
  91. else ()
  92. set(BADVPN_THREADWORK_USE_PTHREAD 1)
  93. add_definitions(-DBADVPN_THREADWORK_USE_PTHREAD)
  94. link_libraries(rt)
  95. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  96. add_definitions(-DBADVPN_LINUX)
  97. check_include_files(sys/signalfd.h HAVE_SYS_SIGNALFD_H)
  98. if (HAVE_SYS_SIGNALFD_H)
  99. add_definitions(-DBADVPN_USE_SIGNALFD)
  100. else ()
  101. add_definitions(-DBADVPN_USE_SELFPIPE)
  102. endif ()
  103. check_include_files(sys/epoll.h HAVE_SYS_EPOLL_H)
  104. if (HAVE_SYS_EPOLL_H)
  105. add_definitions(-DBADVPN_USE_EPOLL)
  106. else ()
  107. add_definitions(-DBADVPN_USE_POLL)
  108. endif ()
  109. check_include_files(linux/rfkill.h HAVE_LINUX_RFKILL_H)
  110. if (HAVE_LINUX_RFKILL_H)
  111. add_definitions(-DBADVPN_USE_LINUX_RFKILL)
  112. set(BADVPN_USE_LINUX_RFKILL 1)
  113. endif ()
  114. check_include_files(linux/input.h HAVE_LINUX_INPUT_H)
  115. if (HAVE_LINUX_INPUT_H)
  116. add_definitions(-DBADVPN_USE_LINUX_INPUT)
  117. set(BADVPN_USE_LINUX_INPUT 1)
  118. endif ()
  119. check_include_files(sys/inotify.h HAVE_SYS_INOTIFY_H)
  120. if (HAVE_SYS_INOTIFY_H)
  121. add_definitions(-DBADVPN_USE_INOTIFY)
  122. set(BADVPN_USE_INOTIFY 1)
  123. endif ()
  124. elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  125. add_definitions(-DBADVPN_FREEBSD)
  126. check_symbol_exists(kqueue "sys/types.h;sys/event.h;sys/time.h" HAVE_KQUEUE)
  127. if (NOT HAVE_KQUEUE)
  128. message(FATAL_ERROR "kqueue is required")
  129. endif ()
  130. add_definitions(-DBADVPN_USE_KEVENT)
  131. endif ()
  132. if (NOT DEFINED BADVPN_WITHOUT_CRYPTODEV)
  133. check_include_files(crypto/cryptodev.h HAVE_CRYPTO_CRYPTODEV_H)
  134. if (HAVE_CRYPTO_CRYPTODEV_H)
  135. add_definitions(-DBADVPN_USE_CRYPTODEV)
  136. elseif (DEFINED BADVPN_WITH_CRYPTODEV)
  137. message(FATAL_ERROR "crypto/cryptodev.h not found")
  138. endif ()
  139. endif ()
  140. endif ()
  141. # add preprocessor definitions
  142. if (BIG_ENDIAN)
  143. add_definitions(-DBADVPN_BIG_ENDIAN)
  144. else ()
  145. add_definitions(-DBADVPN_LITTLE_ENDIAN)
  146. endif ()
  147. # install man pages
  148. install(
  149. FILES badvpn.7
  150. DESTINATION share/man/man7
  151. )
  152. # reset variables indicating whether we're building various libraries,
  153. # and set them in the respective CMakeLists files. This is used to disable
  154. # building examples and tests which require libraries that are not available.
  155. set(BUILDING_SECURITY 0)
  156. set(BUILDING_DHCPCLIENT 0)
  157. set(BUILDING_ARPPROBE 0)
  158. set(BUILDING_BKIO 0)
  159. set(BUILDING_PREDICATE 0)
  160. set(BUILDING_UDEVMONITOR 0)
  161. set(BUILDING_THREADWORK 0)
  162. # internal libraries
  163. add_subdirectory(base)
  164. add_subdirectory(system)
  165. add_subdirectory(flow)
  166. add_subdirectory(flowextra)
  167. if (OpenSSL_FOUND)
  168. set(BUILDING_SECURITY 1)
  169. add_subdirectory(security)
  170. endif ()
  171. if (NSS_FOUND)
  172. add_subdirectory(nspr_support)
  173. endif ()
  174. if (BUILD_CLIENT OR BUILDING_SECURITY)
  175. set(BUILDING_THREADWORK 1)
  176. add_subdirectory(threadwork)
  177. endif ()
  178. if (BUILD_CLIENT OR BUILD_TUN2SOCKS)
  179. add_subdirectory(tuntap)
  180. endif ()
  181. if (BUILD_SERVER)
  182. set(BUILDING_PREDICATE 1)
  183. add_subdirectory(predicate)
  184. endif ()
  185. if (BUILD_CLIENT OR BUILD_FLOODER)
  186. add_subdirectory(server_connection)
  187. endif ()
  188. if (BUILD_NCD)
  189. set(BUILDING_DHCPCLIENT 1)
  190. set(BUILDING_ARPPROBE 1)
  191. set(BUILDING_UDEVMONITOR 1)
  192. add_subdirectory(stringmap)
  193. add_subdirectory(udevmonitor)
  194. add_subdirectory(dhcpclient)
  195. add_subdirectory(arpprobe)
  196. endif ()
  197. if (BUILD_TUN2SOCKS)
  198. add_subdirectory(socksclient)
  199. add_subdirectory(udpgw_client)
  200. add_subdirectory(lwip)
  201. endif ()
  202. # example programs
  203. if (BUILD_EXAMPLES)
  204. add_subdirectory(examples)
  205. endif ()
  206. # tests
  207. if (BUILD_TESTS)
  208. add_subdirectory(tests)
  209. endif ()
  210. # server
  211. if (BUILD_SERVER)
  212. add_subdirectory(server)
  213. endif ()
  214. # client
  215. if (BUILD_CLIENT)
  216. add_subdirectory(client)
  217. endif ()
  218. # flooder
  219. if (BUILD_FLOODER)
  220. add_subdirectory(flooder)
  221. endif ()
  222. # tun2socks
  223. if (BUILD_TUN2SOCKS)
  224. add_subdirectory(tun2socks)
  225. endif ()
  226. # udpgw
  227. if (BUILD_UDPGW)
  228. add_subdirectory(udpgw)
  229. endif ()
  230. # ncd
  231. if (BUILD_NCD)
  232. add_subdirectory(ncd)
  233. endif ()
  234. message(STATUS "Building components:")
  235. # print what we're building and what not
  236. foreach (name ${BUILD_COMPONENTS})
  237. # to lower name
  238. string(TOLOWER "${name}" name_withspaces)
  239. # append spaces to name
  240. #while (TRUE)
  241. # string(LENGTH "${name_withspaces}" length)
  242. # if (NOT (length LESS 12))
  243. # break()
  244. # endif ()
  245. # set(name_withspaces "${name_withspaces} ")
  246. #endwhile ()
  247. # determine if we're building
  248. if (BUILD_${name})
  249. set(building "yes")
  250. else ()
  251. set(building "no")
  252. endif ()
  253. message(STATUS " ${name_withspaces} ${building}")
  254. endforeach ()