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

CMakeLists.txt 23KB

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