Home Explore Blog CI



neovim

3rd chunk of `src/nvim/CMakeLists.txt`
c6fa0e60df203b7418cc77d908f8834c0caca365677f8fc30000000100000fa0
  target_link_options(nvim_bin PRIVATE "-Wl,-export_dynamic")
elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
  target_link_libraries(main_lib INTERFACE pthread c++abi)
elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
  target_link_libraries(nvim_bin PRIVATE -lsocket)
endif()

check_c_compiler_flag(-Wimplicit-fallthrough HAVE_WIMPLICIT_FALLTHROUGH_FLAG)
if(HAVE_WIMPLICIT_FALLTHROUGH_FLAG)
  target_compile_options(main_lib INTERFACE -Wimplicit-fallthrough)
endif()

check_c_compiler_flag(-fdiagnostics-color=auto HAS_DIAG_COLOR_FLAG)
if(HAS_DIAG_COLOR_FLAG)
  if(CMAKE_GENERATOR MATCHES "Ninja")
    target_compile_options(main_lib INTERFACE -fdiagnostics-color=always)
  else()
    target_compile_options(main_lib INTERFACE -fdiagnostics-color=auto)
  endif()
endif()

target_compile_definitions(main_lib INTERFACE INCLUDE_GENERATED_DECLARATIONS)

# Remove --sort-common from linker flags, as this seems to cause bugs (see #2641, #3374).
# TODO: Figure out the root cause.
if(CMAKE_EXE_LINKER_FLAGS MATCHES "--sort-common" OR
   CMAKE_SHARED_LINKER_FLAGS MATCHES "--sort-common" OR
   CMAKE_MODULE_LINKER_FLAGS MATCHES "--sort-common")
  message(STATUS "Removing --sort-common from linker flags")
  string(REGEX REPLACE ",--sort-common(=[^,]+)?" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  string(REGEX REPLACE ",--sort-common(=[^,]+)?" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
  string(REGEX REPLACE ",--sort-common(=[^,]+)?" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}")

  # If no linker flags remain for a -Wl argument, remove it.
  # '-Wl$' will match LDFLAGS="-Wl,--sort-common",
  # '-Wl ' will match LDFLAGS="-Wl,--sort-common -Wl,..."
  string(REGEX REPLACE "-Wl($| )" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  string(REGEX REPLACE "-Wl($| )" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
  string(REGEX REPLACE "-Wl($| )" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}")
endif()

#-------------------------------------------------------------------------------
# Cmake options
#-------------------------------------------------------------------------------

if(ENABLE_ASAN_UBSAN)
  message(STATUS "Enabling address sanitizer and undefined behavior sanitizer for nvim.")
  if(NOT MSVC)
    if(CI_BUILD)
      # Try to recover from all sanitize issues so we get reports about all failures
      target_compile_options(nvim_bin PRIVATE -fsanitize-recover=all)
    else()
      target_compile_options(nvim_bin PRIVATE -fno-sanitize-recover=all)
    endif()
    target_compile_options(nvim_bin PRIVATE
      -fno-omit-frame-pointer
      -fno-optimize-sibling-calls
      -fsanitize=undefined)
  endif()

  target_compile_options(nvim_bin PRIVATE -fsanitize=address)
  target_link_libraries(nvim_bin PRIVATE -fsanitize=address -fsanitize=undefined)
  target_compile_definitions(nvim_bin PRIVATE ENABLE_ASAN_UBSAN)
endif()

if(ENABLE_MSAN)
  message(STATUS "Enabling memory sanitizer for nvim.")
  target_compile_options(nvim_bin PRIVATE
    -fsanitize=memory
    -fsanitize-memory-track-origins
    -fno-omit-frame-pointer
    -fno-optimize-sibling-calls)
  target_link_libraries(nvim_bin PRIVATE -fsanitize=memory -fsanitize-memory-track-origins)
endif()

if(ENABLE_TSAN)
  message(STATUS "Enabling thread sanitizer for nvim.")
  target_compile_options(nvim_bin PRIVATE -fsanitize=thread -fPIE)
  target_link_libraries(nvim_bin PRIVATE -fsanitize=thread)
endif()

option(CI_BUILD "CI, extra flags will be set" OFF)
if(CI_BUILD)
  message(STATUS "CI build enabled")
  if(MSVC)
    target_compile_options(main_lib INTERFACE -WX)
  else()
    target_compile_options(main_lib INTERFACE -Werror)
  endif()
endif()

option(ENABLE_IWYU "Run include-what-you-use with the compiler." OFF)
if(ENABLE_IWYU)
  find_program(IWYU_PRG NAMES include-what-you-use iwyu REQUIRED)
  set(iwyu_flags "${IWYU_PRG};")
  string(APPEND iwyu_flags "-Xiwyu;--no_default_mappings;")
  string(APPEND iwyu_flags "-Xiwyu;--no_fwd_decls;")
 

Title: CMake Options and Sanitizers Configuration
Summary
This section configures CMake options and sanitizers for Neovim. It sets platform-specific linker options, checks for compiler flags like `-Wimplicit-fallthrough` and `-fdiagnostics-color`, and defines preprocessor macros. It removes `--sort-common` from linker flags to avoid bugs. It enables address, undefined behavior, memory, and thread sanitizers if specified. It also configures options for CI builds and include-what-you-use.