CMakeLists.txt 24 KB

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