CMakeLists.txt 26 KB

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