You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

459 lines
13 KiB

  1. cmake_minimum_required(VERSION 3.6)
  2. set(PROJECT_NAME shadowsocks-libev)
  3. set(RELEASE_DATE 2017-2-17)
  4. set(PROJECT_VERSION 3.0.2)
  5. project(${PROJECT_NAME})
  6. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11")
  7. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -c")
  8. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  9. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  10. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  11. #set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
  12. #set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  13. # -------------------------------------------------------------
  14. # Options
  15. option(BUILD_STATIC "Static link library to executable" ON)
  16. if (NOT CMAKE_BUILD_TYPE)
  17. set(CMAKE_BUILD_TYPE Debug)
  18. endif ()
  19. # Detect linux
  20. if (UNIX AND NOT APPLE)
  21. set(LINUX TRUE)
  22. endif ()
  23. # -------------------------------------------------------------
  24. # config.h
  25. # If we generate config.h by automake
  26. #include_directories(.)
  27. # Use cmake to generate config.h
  28. include(CheckIncludeFiles)
  29. include(CheckFunctionExists)
  30. include(CheckSymbolExists)
  31. include(CheckLibraryExists)
  32. include(CheckTypeSize)
  33. include(CheckCSourceCompiles)
  34. # Define if building universal (internal helper macro)
  35. # AC_APPLE_UNIVERSAL_BUILD
  36. set(CONNECT_IN_PROGRESS "EINPROGRESS")
  37. set(CONNECT_IN_PROGRESS "EINPROGRESS" CACHE STRING "")
  38. check_include_files(dlfcn.h HAVE_DLFCN_H)
  39. check_include_files(ev.h HAVE_EV_H)
  40. check_include_files(fcntl.h HAVE_FCNTL_H)
  41. check_function_exists(fork HAVE_FORK)
  42. check_function_exists(getpwnam_r HAVE_GETPWNAM_R)
  43. check_function_exists(inet_ntop HAVE_INET_NTOP)
  44. check_include_files(inttypes.h HAVE_INTTYPES_H)
  45. set(HAVE_IPv6 1)
  46. check_include_files(langinfo.h HAVE_LANGINFO_H)
  47. set(HAVE_LIBPCRE 1)
  48. check_library_exists(socket socket "" HAVE_LIBSOCKET)
  49. check_include_files(limits.h HAVE_LIMITS_H)
  50. check_include_files(linux/if.h HAVE_LINUX_IF_H)
  51. check_include_files(linux/netfilter_ipv4.h HAVE_LINUX_NETFILTER_IPV4_H)
  52. check_include_files(linux/netfilter_ipv6/ip6_tables.h HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
  53. check_include_files(locale.h HAVE_LOCALE_H)
  54. check_function_exists(malloc HAVE_MALLOC)
  55. check_include_files(memory.h HAVE_MEMORY_H)
  56. check_function_exists(memset HAVE_MEMSET)
  57. check_include_files(netdb.h HAVE_NETDB_H)
  58. check_include_files(netinet/in.h HAVE_NETINET_IN_H)
  59. check_include_files(net/if.h HAVE_NET_IF_H)
  60. check_include_files(pcre.h HAVE_PCRE_H)
  61. check_include_files(pcre/pcre.h HAVE_PCRE_PCRE_H)
  62. check_symbol_exists(PTHREAD_PRIO_INHERIT pthread.h HAVE_PTHREAD_PRIO_INHERIT)
  63. check_function_exists(select HAVE_SELECT)
  64. check_function_exists(setresuid HAVE_SETRESUID)
  65. check_function_exists(setreuid HAVE_SETREUID)
  66. check_function_exists(setrlimit HAVE_SETRLIMIT)
  67. check_function_exists(socket HAVE_SOCKET)
  68. check_include_files(stdint.h HAVE_STDINT_H)
  69. check_include_files(stdlib.h HAVE_STDLIB_H)
  70. check_function_exists(strerror HAVE_STRERROR)
  71. check_include_files(strings.h HAVE_STRINGS_H)
  72. check_include_files(string.h HAVE_STRING_H)
  73. check_include_files(sys/ioctl.h HAVE_SYS_IOCTL_H)
  74. check_include_files(sys/select.h HAVE_SYS_SELECT_H)
  75. check_include_files(sys/socket.h HAVE_SYS_SOCKET_H)
  76. check_include_files(sys/stat.h HAVE_SYS_STAT_H)
  77. check_include_files(sys/types.h HAVE_SYS_TYPES_H)
  78. check_include_files(sys/wait.h HAVE_SYS_WAIT_H)
  79. check_include_files(udns.h HAVE_UDNS_H)
  80. check_include_files(unistd.h HAVE_UNISTD_H)
  81. check_function_exists(fork HAVE_FORK)
  82. check_function_exists(vfork HAVE_VFORK)
  83. check_include_files(vfork.h HAVE_VFORK_H)
  84. if (HAVE_VFORK)
  85. set(HAVE_WORKING_VFORK 1)
  86. endif ()
  87. if (HAVE_FORK)
  88. set(HAVE_WORKING_FORK 1)
  89. endif ()
  90. # Define to the sub-directory where libtool stores uninstalled libraries.
  91. set(LT_OBJDIR ".libs/")
  92. set(NDEBUG 1)
  93. set(PACKAGE ${PROJECT_NAME})
  94. set(PACKAGE_BUGREPORT max.c.lv@gmail.com)
  95. set(PACKAGE_NAME ${PROJECT_NAME})
  96. #set(PACKAGE_VERSION ${PROJECT_VERSION})
  97. set(PACKAGE_VERSION 3.0.2)
  98. set(PACKAGE_STRING "${PROJECT_NAME} ${PACKAGE_VERSION}")
  99. set(PACKAGE_TARNAME ${PROJECT_NAME})
  100. set(PACKAGE_URL "")
  101. #message(${PACKAGE_NAME} - v${PACKAGE_VERSION} - v${PROJECT_VERSION})
  102. # PTHREAD_CREATE_JOINABLE
  103. # Define as the return type of signal handlers (`int' or `void').
  104. set(RETSIGTYPE void)
  105. # Define to the type of arg 1 for `select'.
  106. set(SELECT_TYPE_ARG1 int)
  107. # Define to the type of args 2, 3 and 4 for `select'.
  108. set(SELECT_TYPE_ARG234 "(fd_set *)")
  109. # Define to the type of arg 5 for `select'.
  110. set(SELECT_TYPE_ARG5 "(struct timeval *)")
  111. # Define to 1 if you have the ANSI C header files.
  112. set(STDC_HEADERS 1)
  113. check_include_files(sys/time.h time.h TIME_WITH_SYS_TIME)
  114. # If the compiler supports a TLS storage class define it to that here
  115. check_c_source_compiles("
  116. __thread int tls;
  117. int main(void) { return 0; }"
  118. HAVE_GCC_THREAD_LOCAL_STORAGE)
  119. if (HAVE_GCC_THREAD_LOCAL_STORAGE)
  120. set(TLS __thread)
  121. endif ()
  122. set(_ALL_SOURCE 1)
  123. set(_GNU_SOURCE 1)
  124. set(_POSIX_PTHREAD_SEMANTICS 1)
  125. set(_TANDEM_SOURCE 1)
  126. set(__EXTENSIONS__ 1)
  127. # USE_SYSTEM_SHARED_LIB
  128. set(VERSION ${PACKAGE_VERSION})
  129. # TODO WORDS_BIGENDIAN
  130. # _MINIX
  131. # _POSIX_1_SOURCE
  132. # _POSIX_SOURCE
  133. # _UINT8_T
  134. # Define to empty if `const' does not conform to ANSI C.
  135. # undef const
  136. # Define to `__inline__' or `__inline' if that's what the C compiler
  137. # calls it, or to nothing if 'inline' is not supported under any name.
  138. #ifndef __cplusplus
  139. #undef inline
  140. #endif
  141. # TODO Assume we got inline support
  142. # https://cmake.org/Wiki/CMakeTestInline
  143. # Define to the equivalent of the C99 'restrict' keyword, or to
  144. # nothing if this is not supported. Do not define if restrict is
  145. # supported directly.
  146. #define restrict __restrict
  147. if (NOT "c_restrict" IN_LIST CMAKE_C_COMPILE_FEATURES)
  148. message("No restrict")
  149. set(restrict __restrict)
  150. endif ()
  151. # Define to `int' if <sys/types.h> does not define.
  152. # undef pid_t
  153. # Define to the type of an unsigned integer type of width exactly 16 bits if
  154. # such a type exists and the standard includes do not define it.
  155. # undef uint16_t
  156. # Define to the type of an unsigned integer type of width exactly 8 bits if
  157. # such a type exists and the standard includes do not define it.
  158. # undef uint8_t
  159. set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h)
  160. check_type_size(pid_t PID_T)
  161. check_type_size(size_t SIZE_T)
  162. check_type_size(ssize_t SSIZE_T)
  163. set(CMAKE_EXTRA_INCLUDE_FILES)
  164. check_type_size(uint16_t UINT16_T)
  165. check_type_size(uint8_t UINT8_T)
  166. ## Inverse
  167. if (NOT HAVE_PID_T)
  168. set(pid_t int)
  169. endif ()
  170. if (NOT HAVE_SIZE_T)
  171. set(size_t "unsigned int")
  172. endif ()
  173. if (NOT HAVE_SSIZE_T)
  174. set(ssize_t int)
  175. endif ()
  176. if (NOT HAVE_UINT8_T)
  177. set(uint8_t "unsigned char")
  178. endif ()
  179. if (NOT HAVE_UINT16_T)
  180. set(uint16_t "unsigned short")
  181. endif ()
  182. # Define as `fork' if `vfork' does not work.
  183. if (NOT HAVE_WORKING_VFORK)
  184. set(vfork fork)
  185. endif ()
  186. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/src/config.h)
  187. add_definitions(-DHAVE_CONFIG_H)
  188. # -------------------------------------------------------------
  189. # Source
  190. set(SNI_SOURCE
  191. src/http.c
  192. src/tls.c
  193. src/rule.c
  194. )
  195. set(CRYPTO_SOURCE
  196. src/crypto.c
  197. src/aead.c
  198. src/stream.c
  199. src/base64.c
  200. )
  201. set(PLUGIN_SOURCE
  202. src/plugin.c
  203. )
  204. set(SS_LOCAL_SOURCE
  205. src/utils.c
  206. src/jconf.c
  207. src/json.c
  208. src/netutils.c
  209. src/udprelay.c
  210. src/cache.c
  211. src/acl.c
  212. src/local.c
  213. ${CRYPTO_SOURCE}
  214. ${PLUGIN_SOURCE}
  215. ${SNI_SOURCE}
  216. )
  217. set(SS_TUNNEL_SOURCE
  218. src/utils.c
  219. src/jconf.c
  220. src/json.c
  221. src/netutils.c
  222. src/udprelay.c
  223. src/cache.c
  224. src/tunnel.c
  225. ${CRYPTO_SOURCE}
  226. ${PLUGIN_SOURCE}
  227. )
  228. set(SS_SERVER_SOURCE
  229. src/utils.c
  230. src/jconf.c
  231. src/json.c
  232. src/netutils.c
  233. src/udprelay.c
  234. src/cache.c
  235. src/acl.c
  236. src/resolv.c
  237. src/server.c
  238. ${CRYPTO_SOURCE}
  239. ${PLUGIN_SOURCE}
  240. ${SNI_SOURCE}
  241. )
  242. set(SS_MANAGER_SOURCE
  243. src/utils.c
  244. src/jconf.c
  245. src/json.c
  246. src/netutils.c
  247. src/manager.c
  248. )
  249. set(SS_REDIR_SOURCE
  250. src/utils.c
  251. src/jconf.c
  252. src/json.c
  253. src/netutils.c
  254. src/udprelay.c
  255. src/cache.c
  256. src/redir.c
  257. ${CRYPTO_SOURCE}
  258. ${PLUGIN_SOURCE}
  259. ${SNI_SOURCE}
  260. )
  261. # We don't care about shared
  262. set(ENABLE_SHARED OFF)
  263. set(ENABLE_SHARED_EXECUTABLES OFF)
  264. set(ENABLE_STATIC ON)
  265. set(ENABLE_STATIC_EXECUTABLES ON)
  266. #set(CMAKE_MACOSX_RPATH TRUE)
  267. # We need libcork,libipset headers
  268. include_directories(libcork/include)
  269. include_directories(libipset/include)
  270. # Things we need to build libcork,libipset
  271. #link_directories(libcork/src)
  272. #link_directories(libipset/src)
  273. #add_subdirectory(libcork EXCLUDE_FROM_ALL)
  274. # So libipset can find cork
  275. #set( ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:libcork/src" )
  276. #add_subdirectory(libipset EXCLUDE_FROM_ALL)
  277. add_library(
  278. libcork STATIC
  279. libcork/src/libcork/cli/commands.c
  280. libcork/src/libcork/core/allocator.c
  281. libcork/src/libcork/core/error.c
  282. libcork/src/libcork/core/gc.c
  283. libcork/src/libcork/core/hash.c
  284. libcork/src/libcork/core/ip-address.c
  285. libcork/src/libcork/core/mempool.c
  286. libcork/src/libcork/core/timestamp.c
  287. libcork/src/libcork/core/u128.c
  288. libcork/src/libcork/core/version.c
  289. libcork/src/libcork/ds/array.c
  290. libcork/src/libcork/ds/bitset.c
  291. libcork/src/libcork/ds/buffer.c
  292. libcork/src/libcork/ds/dllist.c
  293. libcork/src/libcork/ds/file-stream.c
  294. libcork/src/libcork/ds/hash-table.c
  295. libcork/src/libcork/ds/managed-buffer.c
  296. libcork/src/libcork/ds/ring-buffer.c
  297. libcork/src/libcork/ds/slice.c
  298. libcork/src/libcork/posix/directory-walker.c
  299. libcork/src/libcork/posix/env.c
  300. libcork/src/libcork/posix/exec.c
  301. libcork/src/libcork/posix/files.c
  302. libcork/src/libcork/posix/process.c
  303. libcork/src/libcork/posix/subprocess.c
  304. libcork/src/libcork/pthreads/thread.c
  305. )
  306. target_compile_definitions(libcork PUBLIC -DCORK_API=CORK_LOCAL)
  307. set(LIBIPSET_SRC
  308. libipset/src/libipset/general.c
  309. libipset/src/libipset/bdd/assignments.c
  310. libipset/src/libipset/bdd/basics.c
  311. libipset/src/libipset/bdd/bdd-iterator.c
  312. libipset/src/libipset/bdd/expanded.c
  313. libipset/src/libipset/bdd/reachable.c
  314. libipset/src/libipset/bdd/read.c
  315. libipset/src/libipset/bdd/write.c
  316. libipset/src/libipset/map/allocation.c
  317. libipset/src/libipset/map/inspection.c
  318. libipset/src/libipset/map/ipv4_map.c
  319. libipset/src/libipset/map/ipv6_map.c
  320. libipset/src/libipset/map/storage.c
  321. libipset/src/libipset/set/allocation.c
  322. libipset/src/libipset/set/inspection.c
  323. libipset/src/libipset/set/ipv4_set.c
  324. libipset/src/libipset/set/ipv6_set.c
  325. libipset/src/libipset/set/iterator.c
  326. libipset/src/libipset/set/storage.c
  327. )
  328. add_library(libipset STATIC ${LIBIPSET_SRC})
  329. set_target_properties(libipset PROPERTIES
  330. OUTPUT_NAME ipset
  331. VERSION 1.1.0
  332. SOVERSION 1)
  333. target_link_libraries(libipset libcork)
  334. # Add our targets
  335. add_executable(ss-server ${SS_SERVER_SOURCE})
  336. add_executable(ss-tunnel ${SS_TUNNEL_SOURCE})
  337. add_executable(ss-manager ${SS_MANAGER_SOURCE})
  338. add_executable(ss-local ${SS_LOCAL_SOURCE})
  339. target_compile_definitions(ss-server PUBLIC -DMODULE_REMOTE)
  340. target_compile_definitions(ss-tunnel PUBLIC -DMODULE_TUNNEL)
  341. target_compile_definitions(ss-manager PUBLIC -DMODULE_MANAGER)
  342. target_compile_definitions(ss-local PUBLIC -DMODULE_LOCAL)
  343. if (BUILD_STATIC)
  344. message("Build static linked executable")
  345. set(BUILD_SHARED_LIBS OFF)
  346. find_library(LIBSODIUM libsodium.a)
  347. find_library(LIBMBEDTLS libmbedtls.a)
  348. find_library(LIBMBEDCRYPTO libmbedcrypto.a)
  349. find_library(LIBEV libev.a)
  350. find_library(LIBUDNS libudns.a)
  351. find_library(LIBPCRE libpcre.a)
  352. # Clang crt0 issues
  353. # https://github.com/skaht/Csu-85
  354. # https://bugs.llvm.org//show_bug.cgi?id=17801
  355. # set(CMAKE_EXE_LINKER_FLAGS "-static")
  356. else ()
  357. message("Build shared linked executable")
  358. find_library(LIBSODIUM sodium)
  359. find_library(LIBMBEDTLS mbedtls)
  360. find_library(LIBMBEDCRYPTO mbedcrypto)
  361. find_library(LIBEV ev)
  362. find_library(LIBUDNS udns)
  363. find_library(LIBPCRE pcre)
  364. endif ()
  365. list(APPEND DEPS ${LIBEV} ${LIBUDNS} ${LIBPCRE} ${LIBSODIUM} ${LIBMBEDTLS} ${LIBMBEDCRYPTO})
  366. target_link_libraries(ss-server libcork libipset ${DEPS})
  367. target_link_libraries(ss-tunnel libcork ${DEPS})
  368. target_link_libraries(ss-manager libcork ${LIBEV} ${LIBUDNS})
  369. target_link_libraries(ss-local libcork libipset ${DEPS})
  370. # redir need linux/* stuff
  371. if (LINUX)
  372. add_executable(ss-redir ${SS_REDIR_SOURCE})
  373. target_compile_definitions(ss-redir PUBLIC -DMODULE_REDIR)
  374. target_link_libraries(ss-redir libcork libipset ${DEPS})
  375. endif (LINUX)
  376. install(DIRECTORY DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
  377. install(TARGETS ss-server RUNTIME DESTINATION bin)
  378. install(TARGETS ss-tunnel RUNTIME DESTINATION bin)
  379. install(TARGETS ss-manager RUNTIME DESTINATION bin)
  380. install(TARGETS ss-local RUNTIME DESTINATION bin)
  381. if (LINUX)
  382. install(TARGETS ss-redir RUNTIME DESTINATION bin)
  383. endif (LINUX)