CMakeLists.txt 27 KB

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