CMakeLists.txt 7.8 KB

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