CMakeLists.txt 25 KB

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