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.

679 lines
23KB

  1. # CMAKE REFERENCE
  2. # intro: https://codingnest.com/basic-cmake/
  3. # best practices (3.0+): https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1
  4. # Version should match the tested CMAKE_URL in .travis.yml.
  5. cmake_minimum_required(VERSION 2.8.12)
  6. project(nvim C)
  7. if(POLICY CMP0065)
  8. cmake_policy(SET CMP0065 NEW)
  9. endif()
  10. if(POLICY CMP0060)
  11. cmake_policy(SET CMP0060 NEW)
  12. endif()
  13. # Point CMake at any custom modules we may ship
  14. list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
  15. # We don't support building in-tree.
  16. include(PreventInTreeBuilds)
  17. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  18. # Prefer our bundled versions of dependencies.
  19. if(DEFINED ENV{DEPS_BUILD_DIR})
  20. set(DEPS_PREFIX "$ENV{DEPS_BUILD_DIR}/usr" CACHE PATH "Path prefix for finding dependencies")
  21. else()
  22. set(DEPS_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/.deps/usr" CACHE PATH "Path prefix for finding dependencies")
  23. # When running from within CLion or Visual Studio,
  24. # build bundled dependencies automatically.
  25. if(NOT EXISTS ${DEPS_PREFIX}
  26. AND (DEFINED ENV{CLION_IDE}
  27. OR DEFINED ENV{VisualStudioEdition}))
  28. message(STATUS "Building dependencies...")
  29. set(DEPS_BUILD_DIR ${PROJECT_BINARY_DIR}/.deps)
  30. file(MAKE_DIRECTORY ${DEPS_BUILD_DIR})
  31. execute_process(
  32. COMMAND ${CMAKE_COMMAND} -G ${CMAKE_GENERATOR}
  33. -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
  34. -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
  35. -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
  36. -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
  37. -DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
  38. -DCMAKE_C_FLAGS_MINSIZEREL=${CMAKE_C_FLAGS_MINSIZEREL}
  39. -DCMAKE_C_FLAGS_RELWITHDEBINFO=${CMAKE_C_FLAGS_RELWITHDEBINFO}
  40. -DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}
  41. -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
  42. ${PROJECT_SOURCE_DIR}/third-party
  43. WORKING_DIRECTORY ${DEPS_BUILD_DIR})
  44. execute_process(
  45. COMMAND ${CMAKE_COMMAND} --build ${DEPS_BUILD_DIR}
  46. --config ${CMAKE_BUILD_TYPE})
  47. set(DEPS_PREFIX ${DEPS_BUILD_DIR}/usr)
  48. endif()
  49. endif()
  50. if(CMAKE_CROSSCOMPILING AND NOT UNIX)
  51. list(INSERT CMAKE_FIND_ROOT_PATH 0 ${DEPS_PREFIX})
  52. list(INSERT CMAKE_PREFIX_PATH 0 ${DEPS_PREFIX}/../host/bin)
  53. else()
  54. list(INSERT CMAKE_PREFIX_PATH 0 ${DEPS_PREFIX})
  55. set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${DEPS_PREFIX}/lib/pkgconfig")
  56. endif()
  57. # used for check_c_compiler_flag
  58. include(CheckCCompilerFlag)
  59. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  60. # CMake tries to treat /sw and /opt/local as extension of the system path, but
  61. # that doesn't really work out very well. Once you have a dependency that
  62. # resides there and have to add it as an include directory, then any other
  63. # dependency that could be satisfied from there must be--otherwise you can end
  64. # up with conflicting versions. So, let's make them more of a priority having
  65. # them be included as one of the first places to look for dependencies.
  66. list(APPEND CMAKE_PREFIX_PATH /sw /opt/local)
  67. # Work around some old, broken detection by CMake for knowing when to use the
  68. # isystem flag. Apple's compilers have supported this for quite some time
  69. # now.
  70. if(CMAKE_COMPILER_IS_GNUCC)
  71. set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
  72. endif()
  73. endif()
  74. if(WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  75. # Enable fixing case-insensitive filenames for Windows and Mac.
  76. set(USE_FNAME_CASE TRUE)
  77. endif()
  78. option(ENABLE_LIBINTL "enable libintl" ON)
  79. option(ENABLE_LIBICONV "enable libiconv" ON)
  80. if (MINGW)
  81. # Disable LTO by default as it may not compile
  82. # See https://github.com/Alexpux/MINGW-packages/issues/3516
  83. # and https://github.com/neovim/neovim/pull/8654#issuecomment-402316672
  84. option(ENABLE_LTO "enable link time optimization" OFF)
  85. else()
  86. option(ENABLE_LTO "enable link time optimization" ON)
  87. endif()
  88. message(STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}")
  89. # Build type.
  90. if(NOT CMAKE_BUILD_TYPE)
  91. message(STATUS "CMAKE_BUILD_TYPE not specified, default is 'Debug'")
  92. set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build" FORCE)
  93. else()
  94. message(STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
  95. endif()
  96. if(CMAKE_BUILD_TYPE MATCHES Debug)
  97. set(DEBUG 1)
  98. else()
  99. set(DEBUG 0)
  100. endif()
  101. # Set available build types for CMake GUIs.
  102. # Other build types can still be set by -DCMAKE_BUILD_TYPE=...
  103. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
  104. STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
  105. # If not in a git repo (e.g., a tarball) these tokens define the complete
  106. # version string, else they are combined with the result of `git describe`.
  107. set(NVIM_VERSION_MAJOR 0)
  108. set(NVIM_VERSION_MINOR 5)
  109. set(NVIM_VERSION_PATCH 0)
  110. set(NVIM_VERSION_PRERELEASE "-dev") # for package maintainers
  111. # API level
  112. set(NVIM_API_LEVEL 7) # Bump this after any API change.
  113. set(NVIM_API_LEVEL_COMPAT 0) # Adjust this after a _breaking_ API change.
  114. set(NVIM_API_PRERELEASE true)
  115. set(NVIM_VERSION_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
  116. # NVIM_VERSION_CFLAGS set further below.
  117. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  118. # Log level (MIN_LOG_LEVEL in log.h)
  119. if("${MIN_LOG_LEVEL}" MATCHES "^$")
  120. # Minimize logging for release-type builds.
  121. if(CMAKE_BUILD_TYPE STREQUAL "Release"
  122. OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"
  123. OR CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
  124. message(STATUS "MIN_LOG_LEVEL not specified, default is 3 (ERROR) for release builds")
  125. set(MIN_LOG_LEVEL 3)
  126. else()
  127. message(STATUS "MIN_LOG_LEVEL not specified, default is 1 (INFO)")
  128. set(MIN_LOG_LEVEL 1)
  129. endif()
  130. else()
  131. if(NOT MIN_LOG_LEVEL MATCHES "^[0-3]$")
  132. message(FATAL_ERROR "invalid MIN_LOG_LEVEL: " ${MIN_LOG_LEVEL})
  133. endif()
  134. message(STATUS "MIN_LOG_LEVEL=${MIN_LOG_LEVEL}")
  135. endif()
  136. # Default to -O2 on release builds.
  137. if(CMAKE_C_FLAGS_RELEASE MATCHES "-O3")
  138. message(STATUS "Replacing -O3 in CMAKE_C_FLAGS_RELEASE with -O2")
  139. string(REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
  140. endif()
  141. if(CMAKE_COMPILER_IS_GNUCC)
  142. check_c_compiler_flag(-Og HAS_OG_FLAG)
  143. else()
  144. set(HAS_OG_FLAG 0)
  145. endif()
  146. #
  147. # Build-type: RelWithDebInfo
  148. #
  149. if(HAS_OG_FLAG)
  150. set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -Og -g")
  151. endif()
  152. # We _want_ assertions in RelWithDebInfo build-type.
  153. if(CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES DNDEBUG)
  154. string(REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
  155. endif()
  156. # gcc 4.0+ sets _FORTIFY_SOURCE=2 automatically. This currently
  157. # does not work with Neovim due to some uses of dynamically-sized structures.
  158. # https://github.com/neovim/neovim/issues/223
  159. include(CheckCSourceCompiles)
  160. # Include the build type's default flags in the check for _FORTIFY_SOURCE,
  161. # otherwise we may incorrectly identify the level as acceptable and find out
  162. # later that it was not when optimizations were enabled. CFLAGS is applied
  163. # even though you don't see it in CMAKE_REQUIRED_FLAGS.
  164. set(INIT_FLAGS_NAME CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE})
  165. string(TOUPPER ${INIT_FLAGS_NAME} INIT_FLAGS_NAME)
  166. if(${INIT_FLAGS_NAME})
  167. set(CMAKE_REQUIRED_FLAGS "${${INIT_FLAGS_NAME}}")
  168. endif()
  169. # Include <string.h> because some toolchains define _FORTIFY_SOURCE=2 in
  170. # internal header files, which should in turn be #included by <string.h>.
  171. check_c_source_compiles("
  172. #include <string.h>
  173. #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 1
  174. #error \"_FORTIFY_SOURCE > 1\"
  175. #endif
  176. int
  177. main(void)
  178. {
  179. return 0;
  180. }
  181. " HAS_ACCEPTABLE_FORTIFY)
  182. if(NOT HAS_ACCEPTABLE_FORTIFY)
  183. message(STATUS "Unsupported _FORTIFY_SOURCE found, forcing _FORTIFY_SOURCE=1")
  184. # Extract possible prefix to _FORTIFY_SOURCE (e.g. -Wp,-D_FORTIFY_SOURCE).
  185. STRING(REGEX MATCH "[^\ ]+-D_FORTIFY_SOURCE" _FORTIFY_SOURCE_PREFIX "${CMAKE_C_FLAGS}")
  186. STRING(REPLACE "-D_FORTIFY_SOURCE" "" _FORTIFY_SOURCE_PREFIX "${_FORTIFY_SOURCE_PREFIX}" )
  187. if(NOT _FORTIFY_SOURCE_PREFIX STREQUAL "")
  188. message(STATUS "Detected _FORTIFY_SOURCE Prefix=${_FORTIFY_SOURCE_PREFIX}")
  189. endif()
  190. # -U in add_definitions doesn't end up in the correct spot, so we add it to
  191. # the flags variable instead.
  192. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FORTIFY_SOURCE_PREFIX}-U_FORTIFY_SOURCE ${_FORTIFY_SOURCE_PREFIX}-D_FORTIFY_SOURCE=1")
  193. endif()
  194. # Remove --sort-common from linker flags, as this seems to cause bugs (see #2641, #3374).
  195. # TODO: Figure out the root cause.
  196. if(CMAKE_EXE_LINKER_FLAGS MATCHES "--sort-common" OR
  197. CMAKE_SHARED_LINKER_FLAGS MATCHES "--sort-common" OR
  198. CMAKE_MODULE_LINKER_FLAGS MATCHES "--sort-common")
  199. message(STATUS "Removing --sort-common from linker flags")
  200. string(REGEX REPLACE ",--sort-common(=[^,]+)?" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  201. string(REGEX REPLACE ",--sort-common(=[^,]+)?" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
  202. string(REGEX REPLACE ",--sort-common(=[^,]+)?" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}")
  203. # If no linker flags remain for a -Wl argument, remove it.
  204. # '-Wl$' will match LDFLAGS="-Wl,--sort-common",
  205. # '-Wl ' will match LDFLAGS="-Wl,--sort-common -Wl,..."
  206. string(REGEX REPLACE "-Wl($| )" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  207. string(REGEX REPLACE "-Wl($| )" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
  208. string(REGEX REPLACE "-Wl($| )" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}")
  209. endif()
  210. check_c_source_compiles("
  211. #include <execinfo.h>
  212. int main(void)
  213. {
  214. void *trace[1];
  215. backtrace(trace, 1);
  216. return 0;
  217. }
  218. " HAVE_EXECINFO_BACKTRACE)
  219. check_c_source_compiles("
  220. int main(void)
  221. {
  222. int a = 42;
  223. __builtin_add_overflow(a, a, &a);
  224. __builtin_sub_overflow(a, a, &a);
  225. return 0;
  226. }
  227. " HAVE_BUILTIN_ADD_OVERFLOW)
  228. if(MSVC)
  229. # XXX: /W4 gives too many warnings. #3241
  230. add_compile_options(/W3)
  231. add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE)
  232. add_definitions(-DWIN32)
  233. else()
  234. add_compile_options(-Wall -Wextra -pedantic -Wno-unused-parameter
  235. -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion
  236. -Wmissing-prototypes)
  237. check_c_compiler_flag(-Wimplicit-fallthrough HAS_WIMPLICIT_FALLTHROUGH_FLAG)
  238. if(HAS_WIMPLICIT_FALLTHROUGH_FLAG)
  239. add_compile_options(-Wimplicit-fallthrough)
  240. endif()
  241. # On FreeBSD 64 math.h uses unguarded C11 extension, which taints clang
  242. # 3.4.1 used there.
  243. if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND CMAKE_C_COMPILER_ID MATCHES "Clang")
  244. add_compile_options(-Wno-c11-extensions)
  245. endif()
  246. endif()
  247. if(MINGW)
  248. # Use POSIX compatible stdio in Mingw
  249. add_definitions(-D__USE_MINGW_ANSI_STDIO)
  250. endif()
  251. if(WIN32)
  252. # Windows Vista is the minimum supported version
  253. add_definitions(-D_WIN32_WINNT=0x0600)
  254. endif()
  255. # OpenBSD's GCC (4.2.1) doesn't have -Wvla
  256. check_c_compiler_flag(-Wvla HAS_WVLA_FLAG)
  257. if(HAS_WVLA_FLAG)
  258. add_compile_options(-Wvla)
  259. endif()
  260. if(UNIX)
  261. # -fstack-protector breaks non Unix builds even in Mingw-w64
  262. check_c_compiler_flag(-fstack-protector-strong HAS_FSTACK_PROTECTOR_STRONG_FLAG)
  263. check_c_compiler_flag(-fstack-protector HAS_FSTACK_PROTECTOR_FLAG)
  264. if(HAS_FSTACK_PROTECTOR_STRONG_FLAG)
  265. add_compile_options(-fstack-protector-strong)
  266. link_libraries(-fstack-protector-strong)
  267. elseif(HAS_FSTACK_PROTECTOR_FLAG)
  268. add_compile_options(-fstack-protector --param ssp-buffer-size=4)
  269. link_libraries(-fstack-protector --param ssp-buffer-size=4)
  270. endif()
  271. endif()
  272. check_c_compiler_flag(-fdiagnostics-color=auto HAS_DIAG_COLOR_FLAG)
  273. if(HAS_DIAG_COLOR_FLAG)
  274. if(CMAKE_GENERATOR MATCHES "Ninja")
  275. add_compile_options(-fdiagnostics-color=always)
  276. else()
  277. add_compile_options(-fdiagnostics-color=auto)
  278. endif()
  279. endif()
  280. option(TRAVIS_CI_BUILD "Travis/sourcehut CI, extra flags will be set" OFF)
  281. if(TRAVIS_CI_BUILD)
  282. message(STATUS "Travis/sourcehut CI build enabled")
  283. add_compile_options(-Werror)
  284. if(DEFINED ENV{BUILD_32BIT})
  285. # Get some test coverage for unsigned char
  286. add_compile_options(-funsigned-char)
  287. endif()
  288. endif()
  289. option(LOG_LIST_ACTIONS "Add list actions logging" OFF)
  290. add_definitions(-DINCLUDE_GENERATED_DECLARATIONS)
  291. if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
  292. if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
  293. set(NO_UNDEFINED "-Wl,--no-undefined -lsocket")
  294. elseif(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  295. set(NO_UNDEFINED "-Wl,--no-undefined")
  296. endif()
  297. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${NO_UNDEFINED}")
  298. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${NO_UNDEFINED}")
  299. # For O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW flags on older systems
  300. # (pre POSIX.1-2008: glibc 2.11 and earlier). #4042
  301. # For ptsname(). #6743
  302. add_definitions(-D_GNU_SOURCE)
  303. endif()
  304. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SIZEOF_VOID_P EQUAL 8)
  305. # Required for luajit.
  306. set(CMAKE_EXE_LINKER_FLAGS
  307. "${CMAKE_EXE_LINKER_FLAGS} -pagezero_size 10000 -image_base 100000000")
  308. set(CMAKE_SHARED_LINKER_FLAGS
  309. "${CMAKE_SHARED_LINKER_FLAGS} -image_base 100000000")
  310. set(CMAKE_MODULE_LINKER_FLAGS
  311. "${CMAKE_MODULE_LINKER_FLAGS} -image_base 100000000")
  312. endif()
  313. include_directories("${PROJECT_BINARY_DIR}/config")
  314. include_directories("${PROJECT_SOURCE_DIR}/src")
  315. find_package(LibUV 1.28.0 REQUIRED)
  316. include_directories(SYSTEM ${LIBUV_INCLUDE_DIRS})
  317. find_package(Msgpack 1.0.0 REQUIRED)
  318. include_directories(SYSTEM ${MSGPACK_INCLUDE_DIRS})
  319. find_package(LibLUV 1.30.0 REQUIRED)
  320. include_directories(SYSTEM ${LIBLUV_INCLUDE_DIRS})
  321. find_package(UTF8PROC REQUIRED)
  322. include_directories(SYSTEM ${UTF8PROC_INCLUDE_DIRS})
  323. if(WIN32)
  324. add_definitions(-DUTF8PROC_STATIC)
  325. endif()
  326. # Note: The test lib requires LuaJIT; it will be skipped if LuaJIT is missing.
  327. option(PREFER_LUA "Prefer Lua over LuaJIT in the nvim executable." OFF)
  328. if(PREFER_LUA)
  329. find_package(Lua 5.1 REQUIRED)
  330. set(LUA_PREFERRED_INCLUDE_DIRS ${LUA_INCLUDE_DIR})
  331. set(LUA_PREFERRED_LIBRARIES ${LUA_LIBRARIES})
  332. # Passive (not REQUIRED): if LUAJIT_FOUND is not set, nvim-test is skipped.
  333. find_package(LuaJit)
  334. else()
  335. find_package(LuaJit REQUIRED)
  336. set(LUA_PREFERRED_INCLUDE_DIRS ${LUAJIT_INCLUDE_DIRS})
  337. set(LUA_PREFERRED_LIBRARIES ${LUAJIT_LIBRARIES})
  338. endif()
  339. list(APPEND CMAKE_REQUIRED_INCLUDES "${MSGPACK_INCLUDE_DIRS}")
  340. check_c_source_compiles("
  341. #include <msgpack.h>
  342. int
  343. main(void)
  344. {
  345. return MSGPACK_OBJECT_FLOAT32;
  346. }
  347. " MSGPACK_HAS_FLOAT32)
  348. list(REMOVE_ITEM CMAKE_REQUIRED_INCLUDES "${MSGPACK_INCLUDE_DIRS}")
  349. if(MSGPACK_HAS_FLOAT32)
  350. add_definitions(-DNVIM_MSGPACK_HAS_FLOAT32)
  351. endif()
  352. option(FEAT_TUI "Enable the Terminal UI" ON)
  353. if(FEAT_TUI)
  354. find_package(UNIBILIUM 2.0 REQUIRED)
  355. include_directories(SYSTEM ${UNIBILIUM_INCLUDE_DIRS})
  356. list(APPEND CMAKE_REQUIRED_INCLUDES "${UNIBILIUM_INCLUDE_DIRS}")
  357. list(APPEND CMAKE_REQUIRED_LIBRARIES "${UNIBILIUM_LIBRARIES}")
  358. check_c_source_compiles("
  359. #include <unibilium.h>
  360. int
  361. main(void)
  362. {
  363. return unibi_num_from_var(unibi_var_from_num(0));
  364. }
  365. " UNIBI_HAS_VAR_FROM)
  366. list(REMOVE_ITEM CMAKE_REQUIRED_INCLUDES "${UNIBILIUM_INCLUDE_DIRS}")
  367. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "${UNIBILIUM_LIBRARIES}")
  368. if(UNIBI_HAS_VAR_FROM)
  369. add_definitions(-DNVIM_UNIBI_HAS_VAR_FROM)
  370. endif()
  371. find_package(LibTermkey 0.18 REQUIRED)
  372. include_directories(SYSTEM ${LIBTERMKEY_INCLUDE_DIRS})
  373. endif()
  374. find_package(LIBVTERM 0.1 REQUIRED)
  375. include_directories(SYSTEM ${LIBVTERM_INCLUDE_DIRS})
  376. if(WIN32)
  377. find_package(Winpty 0.4.3 REQUIRED)
  378. include_directories(SYSTEM ${WINPTY_INCLUDE_DIRS})
  379. endif()
  380. option(CLANG_ASAN_UBSAN "Enable Clang address & undefined behavior sanitizer for nvim binary." OFF)
  381. option(CLANG_MSAN "Enable Clang memory sanitizer for nvim binary." OFF)
  382. option(CLANG_TSAN "Enable Clang thread sanitizer for nvim binary." OFF)
  383. if((CLANG_ASAN_UBSAN AND CLANG_MSAN)
  384. OR (CLANG_ASAN_UBSAN AND CLANG_TSAN)
  385. OR (CLANG_MSAN AND CLANG_TSAN))
  386. message(FATAL_ERROR "Sanitizers cannot be enabled simultaneously")
  387. endif()
  388. if((CLANG_ASAN_UBSAN OR CLANG_MSAN OR CLANG_TSAN) AND NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
  389. message(FATAL_ERROR "Sanitizers are only supported for Clang")
  390. endif()
  391. if(ENABLE_LIBICONV)
  392. find_package(Iconv REQUIRED)
  393. include_directories(SYSTEM ${Iconv_INCLUDE_DIRS})
  394. endif()
  395. if(ENABLE_LIBINTL)
  396. # LibIntl (not Intl) selects our FindLibIntl.cmake script. #8464
  397. find_package(LibIntl REQUIRED)
  398. include_directories(SYSTEM ${LibIntl_INCLUDE_DIRS})
  399. endif()
  400. # Determine platform's threading library. Set CMAKE_THREAD_PREFER_PTHREAD
  401. # explicitly to indicate a strong preference for pthread.
  402. set(CMAKE_THREAD_PREFER_PTHREAD ON)
  403. find_package(Threads REQUIRED)
  404. # Place targets in bin/ or lib/ for all build configurations
  405. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  406. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  407. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  408. foreach(CFGNAME ${CMAKE_CONFIGURATION_TYPES})
  409. string(TOUPPER ${CFGNAME} CFGNAME)
  410. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CFGNAME} ${CMAKE_BINARY_DIR}/bin)
  411. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CFGNAME} ${CMAKE_BINARY_DIR}/lib)
  412. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CFGNAME} ${CMAKE_BINARY_DIR}/lib)
  413. endforeach()
  414. # Find Lua interpreter
  415. include(LuaHelpers)
  416. set(LUA_DEPENDENCIES lpeg mpack bit)
  417. if(NOT LUA_PRG)
  418. foreach(CURRENT_LUA_PRG luajit lua5.1 lua5.2 lua)
  419. unset(_CHECK_LUA_PRG CACHE)
  420. unset(LUA_PRG_WORKS)
  421. find_program(_CHECK_LUA_PRG ${CURRENT_LUA_PRG})
  422. if(_CHECK_LUA_PRG)
  423. check_lua_deps(${_CHECK_LUA_PRG} "${LUA_DEPENDENCIES}" LUA_PRG_WORKS)
  424. if(LUA_PRG_WORKS)
  425. set(LUA_PRG "${_CHECK_LUA_PRG}" CACHE FILEPATH "Path to a program.")
  426. break()
  427. endif()
  428. endif()
  429. endforeach()
  430. unset(_CHECK_LUA_PRG CACHE)
  431. else()
  432. check_lua_deps(${LUA_PRG} "${LUA_DEPENDENCIES}" LUA_PRG_WORKS)
  433. endif()
  434. if(NOT LUA_PRG_WORKS)
  435. message(FATAL_ERROR "Failed to find a Lua 5.1-compatible interpreter")
  436. endif()
  437. message(STATUS "Using Lua interpreter: ${LUA_PRG}")
  438. # Setup busted.
  439. find_program(BUSTED_PRG NAMES busted busted.bat)
  440. find_program(BUSTED_LUA_PRG busted-lua)
  441. if(NOT BUSTED_OUTPUT_TYPE)
  442. set(BUSTED_OUTPUT_TYPE "nvim")
  443. endif()
  444. find_program(LUACHECK_PRG luacheck)
  445. find_program(FLAKE8_PRG flake8)
  446. find_program(GPERF_PRG gperf)
  447. include(InstallHelpers)
  448. file(GLOB MANPAGES
  449. RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
  450. man/nvim.1)
  451. install_helper(
  452. FILES ${MANPAGES}
  453. DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
  454. #
  455. # Go down the tree.
  456. #
  457. add_subdirectory(src/nvim)
  458. get_directory_property(NVIM_VERSION_CFLAGS DIRECTORY src/nvim DEFINITION NVIM_VERSION_CFLAGS)
  459. add_subdirectory(test/includes)
  460. add_subdirectory(config)
  461. add_subdirectory(test/functional/fixtures) # compile test programs
  462. add_subdirectory(runtime)
  463. get_directory_property(GENERATED_HELP_TAGS DIRECTORY runtime DEFINITION GENERATED_HELP_TAGS)
  464. if(WIN32)
  465. install_helper(
  466. FILES ${DEPS_PREFIX}/share/nvim-qt/runtime/plugin/nvim_gui_shim.vim
  467. DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/nvim-qt/runtime/plugin)
  468. endif()
  469. # Setup some test-related bits. We do this after going down the tree because we
  470. # need some of the targets.
  471. if(BUSTED_PRG)
  472. get_property(TEST_INCLUDE_DIRS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  473. PROPERTY INCLUDE_DIRECTORIES)
  474. # When running tests from 'ninja' we need to use the
  475. # console pool: to do so we need to use the USES_TERMINAL
  476. # option, but this is only available in CMake 3.2
  477. set(TEST_TARGET_ARGS)
  478. if(NOT (${CMAKE_VERSION} VERSION_LESS 3.2.0))
  479. list(APPEND TEST_TARGET_ARGS "USES_TERMINAL")
  480. endif()
  481. set(UNITTEST_PREREQS nvim-test unittest-headers)
  482. set(FUNCTIONALTEST_PREREQS nvim printenv-test printargs-test shell-test streams-test tty-test ${GENERATED_HELP_TAGS})
  483. set(BENCHMARK_PREREQS nvim tty-test)
  484. # Useful for automated build systems, if they want to manually run the tests.
  485. add_custom_target(unittest-prereqs
  486. DEPENDS ${UNITTEST_PREREQS})
  487. set_target_properties(unittest-prereqs PROPERTIES FOLDER test)
  488. add_custom_target(functionaltest-prereqs
  489. DEPENDS ${FUNCTIONALTEST_PREREQS})
  490. add_custom_target(benchmark-prereqs
  491. DEPENDS ${BENCHMARK_PREREQS})
  492. check_lua_module(${LUA_PRG} "ffi" LUA_HAS_FFI)
  493. if(LUA_HAS_FFI)
  494. add_custom_target(unittest
  495. COMMAND ${CMAKE_COMMAND}
  496. -DBUSTED_PRG=${BUSTED_PRG}
  497. -DLUA_PRG=${LUA_PRG}
  498. -DWORKING_DIR=${CMAKE_CURRENT_SOURCE_DIR}
  499. -DBUSTED_OUTPUT_TYPE=${BUSTED_OUTPUT_TYPE}
  500. -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
  501. -DBUILD_DIR=${CMAKE_BINARY_DIR}
  502. -DTEST_TYPE=unit
  503. -P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake
  504. DEPENDS ${UNITTEST_PREREQS}
  505. ${TEST_TARGET_ARGS})
  506. set_target_properties(unittest PROPERTIES FOLDER test)
  507. else()
  508. message(WARNING "disabling unit tests: no Luajit FFI in ${LUA_PRG}")
  509. endif()
  510. if(LUA_HAS_FFI)
  511. set(TEST_LIBNVIM_PATH $<TARGET_FILE:nvim-test>)
  512. else()
  513. set(TEST_LIBNVIM_PATH "")
  514. endif()
  515. configure_file(
  516. ${CMAKE_SOURCE_DIR}/test/config/paths.lua.in
  517. ${CMAKE_BINARY_DIR}/test/config/paths.lua.gen)
  518. file(GENERATE
  519. OUTPUT ${CMAKE_BINARY_DIR}/test/config/paths.lua
  520. INPUT ${CMAKE_BINARY_DIR}/test/config/paths.lua.gen)
  521. add_custom_target(functionaltest
  522. COMMAND ${CMAKE_COMMAND}
  523. -DBUSTED_PRG=${BUSTED_PRG}
  524. -DLUA_PRG=${LUA_PRG}
  525. -DNVIM_PRG=$<TARGET_FILE:nvim>
  526. -DWORKING_DIR=${CMAKE_CURRENT_SOURCE_DIR}
  527. -DBUSTED_OUTPUT_TYPE=${BUSTED_OUTPUT_TYPE}
  528. -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
  529. -DBUILD_DIR=${CMAKE_BINARY_DIR}
  530. -DTEST_TYPE=functional
  531. -P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake
  532. DEPENDS ${FUNCTIONALTEST_PREREQS}
  533. ${TEST_TARGET_ARGS})
  534. set_target_properties(functionaltest functionaltest-prereqs
  535. PROPERTIES FOLDER test)
  536. add_custom_target(benchmark
  537. COMMAND ${CMAKE_COMMAND}
  538. -DBUSTED_PRG=${BUSTED_PRG}
  539. -DLUA_PRG=${LUA_PRG}
  540. -DNVIM_PRG=$<TARGET_FILE:nvim>
  541. -DWORKING_DIR=${CMAKE_CURRENT_SOURCE_DIR}
  542. -DBUSTED_OUTPUT_TYPE=${BUSTED_OUTPUT_TYPE}
  543. -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
  544. -DBUILD_DIR=${CMAKE_BINARY_DIR}
  545. -DTEST_TYPE=benchmark
  546. -P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake
  547. DEPENDS ${BENCHMARK_PREREQS}
  548. ${TEST_TARGET_ARGS})
  549. set_target_properties(benchmark benchmark-prereqs PROPERTIES FOLDER test)
  550. endif()
  551. if(BUSTED_LUA_PRG)
  552. add_custom_target(functionaltest-lua
  553. COMMAND ${CMAKE_COMMAND}
  554. -DBUSTED_PRG=${BUSTED_LUA_PRG}
  555. -DLUA_PRG=${LUA_PRG}
  556. -DNVIM_PRG=$<TARGET_FILE:nvim>
  557. -DWORKING_DIR=${CMAKE_CURRENT_SOURCE_DIR}
  558. -DBUSTED_OUTPUT_TYPE=${BUSTED_OUTPUT_TYPE}
  559. -DTEST_DIR=${CMAKE_CURRENT_SOURCE_DIR}/test
  560. -DBUILD_DIR=${CMAKE_BINARY_DIR}
  561. -DTEST_TYPE=functional
  562. -P ${PROJECT_SOURCE_DIR}/cmake/RunTests.cmake
  563. DEPENDS ${FUNCTIONALTEST_PREREQS}
  564. ${TEST_TARGET_ARGS})
  565. set_target_properties(functionaltest-lua PROPERTIES FOLDER test)
  566. endif()
  567. if(LUACHECK_PRG)
  568. add_custom_target(lualint
  569. COMMAND ${LUACHECK_PRG} -q runtime/ src/ test/
  570. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  571. else()
  572. add_custom_target(lualint false
  573. COMMENT "lualint: LUACHECK_PRG not defined")
  574. endif()
  575. set(CPACK_PACKAGE_NAME "Neovim")
  576. set(CPACK_PACKAGE_VENDOR "neovim.io")
  577. set(CPACK_PACKAGE_VERSION ${NVIM_VERSION_MEDIUM})
  578. set(CPACK_PACKAGE_INSTALL_DIRECTORY "Neovim")
  579. # Set toplevel directory/installer name as Neovim
  580. set(CPACK_PACKAGE_FILE_NAME "Neovim")
  581. set(CPACK_TOPLEVEL_TAG "Neovim")
  582. set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
  583. set(CPACK_NSIS_MODIFY_PATH ON)
  584. set(CPACK_NSIS_ENABLE_UNINSTALL_BEFORE_INSTALL ON)
  585. include(CPack)