Home Explore Blog CI



neovim

1st chunk of `src/nvim/CMakeLists.txt`
e08204adb9159d23fd1cb697af2a8449e277aaa98215b3150000000100000fa1
add_library(main_lib INTERFACE)

# Internally we need to make a distinction between "nvim without runtime files"
# (nvim_bin) and "nvim with runtime files" (nvim).
add_executable(nvim_bin EXCLUDE_FROM_ALL)

set_target_properties(nvim_bin
  PROPERTIES
  EXPORT_COMPILE_COMMANDS ON
  ENABLE_EXPORTS TRUE
  OUTPUT_NAME nvim)

#-------------------------------------------------------------------------------
# Dependencies
#-------------------------------------------------------------------------------

add_library(nlua0 MODULE)
if(WIN32)
  target_compile_definitions(nlua0 PUBLIC LUA_BUILD_AS_DLL LUA_LIB)
  set_target_properties(nlua0 PROPERTIES ENABLE_EXPORTS TRUE)
elseif(APPLE)
  target_link_options(nlua0 PRIVATE -undefined dynamic_lookup)
endif()

# TODO(dundargoc): unittest stops working if I create an pseudo-imported
# library "luv" as with the other dependencies. Figure out why and fix.
find_package(Luv 1.43.0 REQUIRED)
target_include_directories(main_lib SYSTEM BEFORE INTERFACE ${LUV_INCLUDE_DIR})
target_link_libraries(main_lib INTERFACE ${LUV_LIBRARY})

find_package(Iconv REQUIRED)
find_package(Libuv 1.28.0 REQUIRED)
find_package(Lpeg REQUIRED)
find_package(Treesitter 0.25.0 REQUIRED)
find_package(Unibilium 2.0 REQUIRED)
find_package(UTF8proc REQUIRED)

target_link_libraries(main_lib INTERFACE
  iconv
  lpeg
  treesitter
  unibilium
  utf8proc)
target_link_libraries(nlua0 PUBLIC lpeg)

if(ENABLE_LIBINTL)
  find_package(Libintl REQUIRED) # Libintl (not Intl) selects our FindLibintl.cmake script. #8464
  target_link_libraries(main_lib INTERFACE libintl)
endif()

if(ENABLE_WASMTIME)
  find_package(Wasmtime 29.0.1 EXACT REQUIRED)
  target_link_libraries(main_lib INTERFACE wasmtime)
  target_compile_definitions(nvim_bin PRIVATE HAVE_WASMTIME)
endif()

# The unit test lib requires LuaJIT; it will be skipped if LuaJIT is missing.
option(PREFER_LUA "Prefer Lua over LuaJIT in the nvim executable." OFF)
if(PREFER_LUA)
  find_package(Lua 5.1 EXACT REQUIRED)
  target_include_directories(main_lib SYSTEM BEFORE INTERFACE ${LUA_INCLUDE_DIR})
  target_include_directories(nlua0 SYSTEM BEFORE PUBLIC ${LUA_INCLUDE_DIR})
  target_link_libraries(main_lib INTERFACE ${LUA_LIBRARIES})
  # Passive (not REQUIRED): if LUAJIT_FOUND is not set, fixtures for unittests is skipped.
  find_package(Luajit)
else()
  find_package(Luajit REQUIRED)
  target_include_directories(main_lib SYSTEM BEFORE INTERFACE ${LUAJIT_INCLUDE_DIR})
  target_link_libraries(main_lib INTERFACE ${LUAJIT_LIBRARY})
  target_include_directories(nlua0 SYSTEM BEFORE PUBLIC ${LUAJIT_INCLUDE_DIR})
  if(WIN32)
    target_link_libraries(nlua0 PUBLIC ${LUAJIT_LIBRARY})
  endif()
endif()

#-------------------------------------------------------------------------------
# Compiler and linker options
#-------------------------------------------------------------------------------

if(NOT MSVC)
  target_compile_options(main_lib INTERFACE -Wall -Wextra -pedantic -Wno-unused-parameter
    -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wvla
    -Wdouble-promotion
    -Wmissing-noreturn
    -Wmissing-format-attribute
    -Wmissing-prototypes
    -fsigned-char)

  # For O_CLOEXEC, O_DIRECTORY, and O_NOFOLLOW flags on older systems
  # (pre POSIX.1-2008: glibc 2.11 and earlier). #4042
  # For ptsname(). #6743
  target_compile_definitions(main_lib INTERFACE _GNU_SOURCE)
endif()

# -fstack-protector breaks Mingw-w64 builds
if(NOT MINGW)
  check_c_compiler_flag(-fstack-protector-strong HAS_FSTACK_PROTECTOR_STRONG_FLAG)
  if(HAS_FSTACK_PROTECTOR_STRONG_FLAG)
    target_compile_options(main_lib INTERFACE -fstack-protector-strong)
    target_link_libraries(main_lib INTERFACE -fstack-protector-strong)
  else()
    check_c_compiler_flag(-fstack-protector HAS_FSTACK_PROTECTOR_FLAG)
    if(HAS_FSTACK_PROTECTOR_FLAG)
      target_compile_options(main_lib INTERFACE -fstack-protector --param ssp-buffer-size=4)
      target_link_libraries(main_lib INTERFACE -fstack-protector --param ssp-buffer-size=4)
    endif()

Title: CMake Configuration for Nvim: Dependencies and Compiler Options
Summary
This section of the CMakeLists.txt configures the dependencies for Nvim, including libraries like Luv, Iconv, Libuv, Lpeg, Treesitter, Unibilium, UTF8proc, and optionally Libintl and Wasmtime. It also handles the choice between Lua and LuaJIT. Furthermore, it sets compiler and linker options, including warnings, standards, and stack protection flags, tailored for different platforms (Windows, Apple, etc.) while avoiding issues with Mingw.