1
0

CMakeLists.txt 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # You may redistribute this program and/or modify it under the terms of
  2. # the GNU General Public License as published by the Free Software Foundation,
  3. # either version 3 of the License, or (at your option) any later version.
  4. #
  5. # This program is distributed in the hope that it will be useful,
  6. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. # GNU General Public License for more details.
  9. #
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. project(cjdns C)
  13. cmake_minimum_required(VERSION 2.8)
  14. message(${CMAKE_VERSION})
  15. if(CMAKE_BINARY_DIR STREQUAL ${CMAKE_SOURCE_DIR} AND NOT OPENWRT)
  16. message( FATAL_ERROR "type: git clean -df && mkdir build && cd build && cmake .. && make" )
  17. endif()
  18. if (OPENWRT)
  19. # OpenWRT messes around with the code quite a bit and makes the codestyle validator angry.
  20. set(NO_CODESTYLE TRUE)
  21. # OpenWRT's NACL is not built position independent.
  22. set(WITH_PIE FALSE)
  23. # Use the bundled NaCl because it's faster.
  24. #set(HAVE_NACL TRUE)
  25. endif()
  26. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
  27. ${CMAKE_SOURCE_DIR}/cmake/modules)
  28. # validation
  29. add_definitions(-Wall -Wextra -std=c99 -pedantic)
  30. if ("$ENV{NO_WERROR}" STREQUAL "")
  31. add_definitions(-Werror)
  32. endif()
  33. if(CMAKE_SYSTEM_NAME STREQUAL SunOS)
  34. set(ILLUMOS TRUE)
  35. set(SYSTEM Illumos)
  36. elseif(CMAKE_SYSTEM_NAME STREQUAL Linux)
  37. set(LINUX TRUE)
  38. set(SYSTEM Linux)
  39. elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
  40. set(APPLE TRUE)
  41. set(SYSTEM Darwin)
  42. elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
  43. set(FREEBSD TRUE)
  44. set(BSD TRUE)
  45. add_definitions(-D BSD=1)
  46. set(SYSTEM FreeBSD)
  47. elseif(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
  48. set(OPENBSD TRUE)
  49. set(BSD TRUE)
  50. add_definitions(-D BSD=1)
  51. set(SYSTEM OpenBSD)
  52. elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
  53. set(Windows TRUE)
  54. set(SYSTEM Windows)
  55. endif()
  56. add_definitions(-D ${SYSTEM}=1)
  57. if(Windows)
  58. # ISO C does not support the ‘I64’ ms_printf length modifier
  59. # but PRId64 is #defined as "I64d"
  60. add_definitions(-Wno-format)
  61. # SRWLOCK is vista or newer and needed by libuv
  62. add_definitions(-D_WIN32_WINNT=0x0600)
  63. # BufferAllocator.c:1:0: error: -fPIC ignored for target (all code is position independent)
  64. set(WITH_PIE OFF CACHE INTERNAL "" FORCE)
  65. endif()
  66. if(CMAKE_CROSSCOMPILING)
  67. if (NOT "$ENV{REMOTE_TEST_IP_PORT}" STREQUAL "")
  68. set(REMOTE_TEST_IP_PORT "$ENV{REMOTE_TEST_IP_PORT}")
  69. endif()
  70. endif()
  71. if(EXISTS ${CMAKE_SOURCE_DIR}/interface/ETHInterface_${SYSTEM}.c)
  72. add_definitions(-D HAS_ETH_INTERFACE=1)
  73. set(HAS_ETH_INTERFACE TRUE)
  74. endif()
  75. if(CMAKE_C_COMPILER MATCHES "(.*)-gcc")
  76. string(REGEX REPLACE "-gcc$" "" toolchain ${CMAKE_C_COMPILER})
  77. string(REGEX REPLACE ".*/" "" toolchain ${toolchain})
  78. message("detected toolchain ${toolchain}")
  79. set(TOOLCHAIN ${toolchain} CACHE STRING "Toolchain for compiling (prefix for -gcc, ... binaries)")
  80. else()
  81. set(TOOLCHAIN "" CACHE STRING "Toolchain for compiling (prefix for -gcc, ... binaries)")
  82. endif()
  83. if(TOOLCHAIN AND NOT CMAKE_C_COMPILER)
  84. set(CMAKE_C_COMPILER ${TOOLCHAIN}-gcc)
  85. endif()
  86. if(TOOLCHAIN AND NOT CMAKE_CXX_COMPILER)
  87. set(CMAKE_C_COMPILER ${TOOLCHAIN}-g++)
  88. endif()
  89. if(Windows AND TOOLCHAIN AND NOT CMAKE_CXX_COMPILER)
  90. set(CMAKE_C_COMPILER ${TOOLCHAIN}-windres)
  91. endif()
  92. message("c compiler: ${CMAKE_C_COMPILER}")
  93. message("c++ compiler: ${CMAKE_CXX_COMPILER}")
  94. if(Windows)
  95. message("rc compiler: ${CMAKE_RC_COMPILER}")
  96. endif()
  97. # This breaks logging since loggers are passed to functions
  98. # and only used if the log level is high.
  99. add_definitions(-Wno-unused-parameter)
  100. include(CheckCCompilerFlag)
  101. # There are a number of places in Admin where read() and write() are used
  102. # and they fail silently if anything goes wrong so the result is unused.
  103. check_c_compiler_flag(-Wno-unused-result HAS_NO_UNUSED_RESULT)
  104. if(HAS_NO_UNUSED_RESULT)
  105. add_definitions(-Wno-unused-result)
  106. endif()
  107. # GCC only sends a warning and not an error if it can't compile with stack canaries.
  108. set(CMAKE_REQUIRED_FLAGS "-Werror")
  109. check_c_compiler_flag(-fstack-protector-all HAS_F_STACK_PROTECTOR)
  110. if(NOT HAS_F_STACK_PROTECTOR)
  111. message("WARNING: Stack Smashing Protector is not available on this target.")
  112. message("SSP is an important security measure to minimize the risk of a vulnerability.")
  113. message("")
  114. else()
  115. add_definitions(
  116. # Broken GCC patch makes -fstack-protector-all not work
  117. # workaround is to give -fno-stack-protector first.
  118. # see: https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/691722
  119. -fno-stack-protector
  120. -fstack-protector-all
  121. -Wstack-protector
  122. )
  123. endif()
  124. OPTION(NO_CODESYTLE "No codestyle checks")
  125. # check codestyle
  126. if(NOT NO_CODESTYLE)
  127. set(checker ${CMAKE_SOURCE_DIR}/cmake/modules/CheckCodestyle.cmake)
  128. add_custom_command(OUTPUT check.codestyle
  129. COMMAND ${CMAKE_COMMAND}
  130. -DCSD=${CMAKE_SOURCE_DIR} -DSKIP=CMakeFiles -P ${checker}
  131. )
  132. add_custom_target(check_output ALL DEPENDS check.codestyle)
  133. endif()
  134. check_c_compiler_flag(-fcatch-undefined-behavior HAS_CATCH_UNDEFINED)
  135. if(HAS_CATCH_UNDEFINED)
  136. add_definitions(-fcatch-undefined-behavior)
  137. endif()
  138. # __builtin_constant_p()
  139. include(CheckCSourceCompiles)
  140. check_c_source_compiles("int main() { return __builtin_constant_p(0);}" HAS_BUILTIN_CONSTANT_P)
  141. if (HAS_BUILTIN_CONSTANT_P)
  142. add_definitions("-D HAS_BUILTIN_CONSTANT_P")
  143. endif()
  144. OPTION(WITH_PIE "Build a position independent executable" ON)
  145. if (NOT "$ENV{NO_PIE}" STREQUAL "")
  146. set(WITH_PIE FALSE)
  147. endif()
  148. if(WITH_PIE)
  149. message("Building a position independent executable (ASLR security enabled).")
  150. message("NOTE: PIE executables cannot run in a debugger.")
  151. message("")
  152. add_definitions(-fPIE)
  153. set(PIE "-pie")
  154. else()
  155. message("Building a position dependent executable (ASLR security disabled).")
  156. set(PIE "")
  157. endif()
  158. if ("$ENV{NO_DEBUG}" STREQUAL "")
  159. add_definitions(-g)
  160. endif()
  161. set(LDFLAGS ${CMAKE_EXE_LINKER_FLAGS})
  162. if(Windows)
  163. elseif(ILLUMOS)
  164. # TODO make this less ugly
  165. set(LDFLAGS "${LDFLAGS} ${PIE} -lsocket")
  166. elseif(APPLE)
  167. # apple seems to set noexecstack by default and not support relro.
  168. set(LDFLAGS "${LDFLAGS} ${PIE}")
  169. else()
  170. set(LDFLAGS "${LDFLAGS} ${PIE} -Wl,-z,relro,-z,now,-z,noexecstack")
  171. endif()
  172. # logging
  173. SET(Log_LEVEL DEBUG CACHE STRING "Log level (KEYS, DEBUG, INFO, WARN, ERROR, CRITICAL)")
  174. if (NOT "$ENV{Log_LEVEL}" STREQUAL "")
  175. set(Log_LEVEL "$ENV{Log_LEVEL}")
  176. endif()
  177. if (${Log_LEVEL} STREQUAL "KEYS")
  178. message("\n\nEXPECT TO SEE PRIVATE KEYS PRINTED IN YOUR LOGS!\n\n")
  179. endif (${Log_LEVEL} STREQUAL "KEYS")
  180. add_definitions("-D Log_${Log_LEVEL}")
  181. # vrooooooom
  182. #add_definitions(-O2)
  183. if (NOT $ENV{GCOV} STREQUAL "")
  184. add_definitions(-fprofile-arcs -ftest-coverage)
  185. set(LDFLAGS "${LDFLAGS} -fprofile-arcs -ftest-coverage")
  186. endif()
  187. #IF(NOT CMAKE_BUILD_TYPE)
  188. # default to RelWithDebInfo (-g -O2)
  189. # SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
  190. # "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
  191. # FORCE)
  192. #ENDIF(NOT CMAKE_BUILD_TYPE)
  193. OPTION(WITH_LTO "Building with link time optimization.")
  194. if(WITH_LTO)
  195. add_definitions(-flto)
  196. set(LDFLAGS "${LDFLAGS} -flto -fuse-linker-plugin -fwhole-program")
  197. endif()
  198. # Max direct connections to the switch.
  199. SET(CJDNS_MAX_PEERS 256 CACHE STRING "maximum number of peers")
  200. add_definitions("-D CJDNS_MAX_PEERS=${CJDNS_MAX_PEERS}")
  201. # make sure casts are done properly.
  202. add_definitions("-D Identity_CHECK=1")
  203. # add in a billion checks which slow things down but catch bugs.
  204. add_definitions("-D PARANOIA=1")
  205. if (NOT $ENV{DUMB_CLIENT} STREQUAL "")
  206. add_definitions("-D RouterModule_DUMB_CLIENT=1")
  207. endif ()
  208. include_directories(${CMAKE_SOURCE_DIR})
  209. #if NEON is specified then we will force NACL to use NEON.
  210. if (NOT $ENV{NEON} STREQUAL "")
  211. set(NEON TRUE)
  212. endif ()
  213. # Look for ARM NEON if it's available
  214. find_package(NEON)
  215. if (NEON)
  216. # needed because the NEON asm files do not support
  217. # THUMB and linking to them causes bad things.
  218. add_definitions(-marm)
  219. endif()
  220. if(HAVE_LIBUV)
  221. find_package(Libuv REQUIRED)
  222. else()
  223. find_package(LibuvBundled REQUIRED)
  224. endif()
  225. if(HAVE_NACL)
  226. find_package(NACL REQUIRED)
  227. elseif(HAVE_SODIUM)
  228. find_package(Sodium REQUIRED)
  229. else()
  230. if (NOT $ENV{NO_EXTERNAL_NACL})
  231. find_package(NACL)
  232. endif ()
  233. find_package(NACLBundled REQUIRED)
  234. endif()
  235. include_directories(${NACL_INCLUDE_DIRS})
  236. set(CMAKE_EXE_LINKER_FLAGS "${LDFLAGS}")
  237. add_subdirectory(admin)
  238. add_subdirectory(benc)
  239. add_subdirectory(crypto)
  240. add_subdirectory(dht)
  241. add_subdirectory(interface)
  242. add_subdirectory(io)
  243. add_subdirectory(memory)
  244. add_subdirectory(util)
  245. add_subdirectory(switch)
  246. add_subdirectory(net)
  247. add_subdirectory(exception)
  248. add_subdirectory(tunnel)
  249. add_executable(cleanconfig
  250. cleanconfig.c
  251. )
  252. target_link_libraries(cleanconfig
  253. cjdbenc_JsonBencSerializer
  254. cjdbenc
  255. cjdmemory
  256. cjdio
  257. )
  258. add_executable(benc2json
  259. benc2json.c
  260. )
  261. target_link_libraries(benc2json
  262. cjdbenc_StandardBencSerializer
  263. cjdbenc_JsonBencSerializer
  264. cjdbenc
  265. cjdmemory
  266. cjdio
  267. )
  268. add_executable(privatetopublic
  269. privatetopublic.c
  270. )
  271. target_link_libraries(privatetopublic
  272. util
  273. crypto
  274. )
  275. add_executable(publictoip6
  276. publictoip6.c
  277. )
  278. target_link_libraries(publictoip6
  279. util
  280. cjdns-crypto-key
  281. )
  282. enable_testing()
  283. add_subdirectory(test)