diff options
author | dundargoc <gocdundar@gmail.com> | 2023-11-20 16:49:48 +0100 |
---|---|---|
committer | dundargoc <33953936+dundargoc@users.noreply.github.com> | 2023-11-21 22:18:21 +0100 |
commit | 78239f0bbcb2527165fbaa3172b50c99c6092136 (patch) | |
tree | 7e02592f4731d2ff7dc58cdcb4defe2f6402b98b | |
parent | b4fee208ef668c0dceebca12fe30c0f9d5cbf64d (diff) | |
download | rneovim-78239f0bbcb2527165fbaa3172b50c99c6092136.tar.gz rneovim-78239f0bbcb2527165fbaa3172b50c99c6092136.tar.bz2 rneovim-78239f0bbcb2527165fbaa3172b50c99c6092136.zip |
build: reorganize cmake files
Also add _GNU_SOURCE compiler definition for all non MSVC compilers.
Closes https://github.com/neovim/neovim/issues/26087.
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/nvim/CMakeLists.txt | 270 |
2 files changed, 147 insertions, 127 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d128ec6a8e..475a1a2c3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,10 @@ if(NOT CI_BUILD) set(CMAKE_INSTALL_MESSAGE NEVER) endif() +if(${CMAKE_VERSION} VERSION_LESS 3.20) + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +endif() + # Prefer our bundled versions of dependencies. if(DEFINED ENV{DEPS_BUILD_DIR}) set(DEPS_PREFIX "$ENV{DEPS_BUILD_DIR}/usr" CACHE PATH "Path prefix for finding dependencies") diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 792cfb894b..ed17c3dc8d 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -1,6 +1,15 @@ add_library(main_lib INTERFACE) add_executable(nvim main.c) +set_target_properties(nvim + PROPERTIES + EXPORT_COMPILE_COMMANDS ON + ENABLE_EXPORTS TRUE) + +#------------------------------------------------------------------------------- +# Dependencies +#------------------------------------------------------------------------------- + add_library(libuv INTERFACE) find_package(libuv CONFIG QUIET) if(TARGET libuv::uv_a) @@ -70,23 +79,11 @@ else() 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) - if(NOT IWYU_PRG) - message(FATAL_ERROR "ENABLE_IWYU is ON but include-what-you-use is not found!") - endif() - - set(iwyu_flags "${IWYU_PRG};") - string(APPEND iwyu_flags "-Xiwyu;--no_default_mappings;") - string(APPEND iwyu_flags "-Xiwyu;--mapping_file=${PROJECT_SOURCE_DIR}/cmake.config/iwyu/mapping.imp;") - string(APPEND iwyu_flags "-Xiwyu;--mapping_file=${PROJECT_SOURCE_DIR}/cmake.config/iwyu/gcc.libc.imp;") - string(APPEND iwyu_flags "-Xiwyu;--mapping_file=${PROJECT_SOURCE_DIR}/cmake.config/iwyu/gcc.symbols.imp") - - set_target_properties(nvim PROPERTIES C_INCLUDE_WHAT_YOU_USE "${iwyu_flags}") - target_compile_definitions(main_lib INTERFACE EXITFREE) -endif() +#------------------------------------------------------------------------------- +# Compiler and linker options +#------------------------------------------------------------------------------- +# Compiler specific options if(MSVC) target_compile_options(main_lib INTERFACE -W3) @@ -95,7 +92,31 @@ if(MSVC) target_compile_definitions(main_lib INTERFACE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE) target_sources(main_lib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/os/nvim.manifest) -else() +elseif(MINGW) + # Use POSIX compatible stdio in Mingw + target_compile_definitions(main_lib INTERFACE __USE_MINGW_ANSI_STDIO) + + # Enable wmain + target_link_libraries(nvim PRIVATE -municode) +elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU") + target_compile_options(main_lib INTERFACE -fno-common + $<$<CONFIG:Release>:-Wno-unused-result> + $<$<CONFIG:RelWithDebInfo>:-Wno-unused-result> + $<$<CONFIG:MinSizeRel>:-Wno-unused-result>) +elseif(CMAKE_C_COMPILER_ID MATCHES "Clang") + # On FreeBSD 64 math.h uses unguarded C11 extension, which taints clang + # 3.4.1 used there. + if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") + target_compile_options(main_lib INTERFACE -Wno-c11-extensions) + endif() + + # workaround for clang-11 on macOS, supported on later versions + if(NOT APPLE) + target_link_libraries(nvim PRIVATE -Wl,--no-undefined) + endif() +endif() + +if(NOT MSVC) target_compile_options(main_lib INTERFACE -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wvla -Wdouble-promotion @@ -104,55 +125,57 @@ else() -Wmissing-prototypes -fsigned-char) - if(CMAKE_C_COMPILER_ID STREQUAL "GNU") - target_compile_options(main_lib INTERFACE -fno-common - $<$<CONFIG:Release>:-Wno-unused-result> - $<$<CONFIG:RelWithDebInfo>:-Wno-unused-result> - $<$<CONFIG:MinSizeRel>:-Wno-unused-result>) - endif() + # 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() -# On FreeBSD 64 math.h uses unguarded C11 extension, which taints clang -# 3.4.1 used there. -if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND CMAKE_C_COMPILER_ID MATCHES "Clang") - target_compile_options(main_lib INTERFACE -Wno-c11-extensions) +if(WIN32) + target_compile_definitions(main_lib INTERFACE _WIN32_WINNT=0x0602 MSWIN) + target_link_libraries(main_lib INTERFACE netapi32) endif() -check_c_compiler_flag(-Wimplicit-fallthrough HAVE_WIMPLICIT_FALLTHROUGH_FLAG) -if(HAVE_WIMPLICIT_FALLTHROUGH_FLAG) - target_compile_options(main_lib INTERFACE -Wimplicit-fallthrough) +if(APPLE) + target_link_libraries(nvim PRIVATE "-framework CoreServices") + + # Actually export symbols - symbols may not be visible even though + # ENABLE_EXPORTS is set to true. See + # https://github.com/neovim/neovim/issues/25295 + set_target_properties(nvim PROPERTIES LINK_FLAGS "-Wl,-export_dynamic") endif() -option(ENABLE_COMPILER_SUGGESTIONS "Enable -Wsuggest compiler warnings" OFF) -if(ENABLE_COMPILER_SUGGESTIONS) - # Clang doesn't have -Wsuggest-attribute so check for each one. - check_c_compiler_flag(-Wsuggest-attribute=pure HAVE_WSUGGEST_ATTRIBUTE_PURE) - if(HAVE_WSUGGEST_ATTRIBUTE_PURE) - target_compile_options(main_lib INTERFACE -Wsuggest-attribute=pure) - endif() +if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") + target_link_libraries(main_lib INTERFACE pthread c++abi) +endif() - check_c_compiler_flag(-Wsuggest-attribute=const HAVE_WSUGGEST_ATTRIBUTE_CONST) - if(HAVE_WSUGGEST_ATTRIBUTE_CONST) - target_compile_options(main_lib INTERFACE -Wsuggest-attribute=const) - endif() +if(CMAKE_SYSTEM_NAME STREQUAL "SunOS") + target_link_libraries(nvim PRIVATE -lsocket) +endif() - check_c_compiler_flag(-Wsuggest-attribute=malloc HAVE_WSUGGEST_ATTRIBUTE_MALLOC) - if(HAVE_WSUGGEST_ATTRIBUTE_MALLOC) - target_compile_options(main_lib INTERFACE -Wsuggest-attribute=malloc) +if(UNIX) + target_link_libraries(main_lib INTERFACE m) + if (NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS") + target_link_libraries(main_lib INTERFACE util) endif() - check_c_compiler_flag(-Wsuggest-attribute=cold HAVE_WSUGGEST_ATTRIBUTE_COLD) - if(HAVE_WSUGGEST_ATTRIBUTE_COLD) - target_compile_options(main_lib INTERFACE -Wsuggest-attribute=cold) + # -fstack-protector breaks non Unix builds even in Mingw-w64 + 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() endif() endif() -if(MINGW) - # Use POSIX compatible stdio in Mingw - target_compile_definitions(main_lib INTERFACE __USE_MINGW_ANSI_STDIO) -endif() -if(WIN32) - target_compile_definitions(main_lib INTERFACE _WIN32_WINNT=0x0602 MSWIN) +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) @@ -164,16 +187,6 @@ if(HAS_DIAG_COLOR_FLAG) endif() 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() - target_compile_definitions(main_lib INTERFACE INCLUDE_GENERATED_DECLARATIONS) # Remove --sort-common from linker flags, as this seems to cause bugs (see #2641, #3374). @@ -194,18 +207,59 @@ if(CMAKE_EXE_LINKER_FLAGS MATCHES "--sort-common" OR string(REGEX REPLACE "-Wl($| )" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}") endif() -if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang") - if(CMAKE_SYSTEM_NAME STREQUAL "SunOS") - target_link_libraries(nvim PRIVATE -Wl,--no-undefined -lsocket) - # workaround for clang-11 on macOS, supported on later versions: - elseif(NOT APPLE) - target_link_libraries(nvim PRIVATE -Wl,--no-undefined) +#------------------------------------------------------------------------------- +# Cmake options +#------------------------------------------------------------------------------- + +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() - # 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) +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) + if(NOT IWYU_PRG) + message(FATAL_ERROR "ENABLE_IWYU is ON but include-what-you-use is not found!") + endif() + + set(iwyu_flags "${IWYU_PRG};") + string(APPEND iwyu_flags "-Xiwyu;--no_default_mappings;") + string(APPEND iwyu_flags "-Xiwyu;--mapping_file=${PROJECT_SOURCE_DIR}/cmake.config/iwyu/mapping.imp;") + string(APPEND iwyu_flags "-Xiwyu;--mapping_file=${PROJECT_SOURCE_DIR}/cmake.config/iwyu/gcc.libc.imp;") + string(APPEND iwyu_flags "-Xiwyu;--mapping_file=${PROJECT_SOURCE_DIR}/cmake.config/iwyu/gcc.symbols.imp") + + set_target_properties(nvim PROPERTIES C_INCLUDE_WHAT_YOU_USE "${iwyu_flags}") + target_compile_definitions(main_lib INTERFACE EXITFREE) +endif() + +option(ENABLE_COMPILER_SUGGESTIONS "Enable -Wsuggest compiler warnings" OFF) +if(ENABLE_COMPILER_SUGGESTIONS) + # Clang doesn't have -Wsuggest-attribute so check for each one. + check_c_compiler_flag(-Wsuggest-attribute=pure HAVE_WSUGGEST_ATTRIBUTE_PURE) + if(HAVE_WSUGGEST_ATTRIBUTE_PURE) + target_compile_options(main_lib INTERFACE -Wsuggest-attribute=pure) + endif() + + check_c_compiler_flag(-Wsuggest-attribute=const HAVE_WSUGGEST_ATTRIBUTE_CONST) + if(HAVE_WSUGGEST_ATTRIBUTE_CONST) + target_compile_options(main_lib INTERFACE -Wsuggest-attribute=const) + endif() + + check_c_compiler_flag(-Wsuggest-attribute=malloc HAVE_WSUGGEST_ATTRIBUTE_MALLOC) + if(HAVE_WSUGGEST_ATTRIBUTE_MALLOC) + target_compile_options(main_lib INTERFACE -Wsuggest-attribute=malloc) + endif() + + check_c_compiler_flag(-Wsuggest-attribute=cold HAVE_WSUGGEST_ATTRIBUTE_COLD) + if(HAVE_WSUGGEST_ATTRIBUTE_COLD) + target_compile_options(main_lib INTERFACE -Wsuggest-attribute=cold) + endif() endif() option(ENABLE_GCOV "Enable gcov support" OFF) @@ -220,34 +274,9 @@ if(ENABLE_GCOV) target_compile_definitions(main_lib INTERFACE USE_GCOV) endif() -if(WIN32) - if(MINGW) - # Enable wmain - target_link_libraries(nvim PRIVATE -municode) - endif() -elseif(APPLE) - target_link_libraries(nvim PRIVATE "-framework CoreServices") - - # Actually export symbols - symbols may not be visible even though - # ENABLE_EXPORTS is set to true. See - # https://github.com/neovim/neovim/issues/25295 - set_target_properties(nvim PROPERTIES LINK_FLAGS "-Wl,-export_dynamic") -endif() - -if(UNIX) - # -fstack-protector breaks non Unix builds even in Mingw-w64 - 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() - endif() -endif() +#------------------------------------------------------------------------------- +# Variables +#------------------------------------------------------------------------------- set(GENERATOR_DIR ${CMAKE_CURRENT_LIST_DIR}/generators) set(GENERATED_DIR ${PROJECT_BINARY_DIR}/src/nvim/auto) @@ -401,6 +430,10 @@ if(ENABLE_ASAN_UBSAN OR ENABLE_MSAN OR ENABLE_TSAN) target_compile_definitions(main_lib INTERFACE EXITFREE) endif() +#------------------------------------------------------------------------------- +# Header generation +#------------------------------------------------------------------------------- + get_target_property(prop main_lib INTERFACE_COMPILE_DEFINITIONS) foreach(gen_cdef DO_NOT_DEFINE_EMPTY_ATTRIBUTES ${prop}) if(NOT ${gen_cdef} MATCHES "INCLUDE_GENERATED_DECLARATIONS") @@ -417,8 +450,7 @@ foreach(target ${targets}) endforeach() if(APPLE AND CMAKE_OSX_SYSROOT) - list(APPEND gen_cflags "-isysroot") - list(APPEND gen_cflags "${CMAKE_OSX_SYSROOT}") + list(APPEND gen_cflags "-isysroot" "${CMAKE_OSX_SYSROOT}") endif() if(MSVC) list(APPEND gen_cflags -wd4003) @@ -629,21 +661,6 @@ foreach(hfile ${NVIM_GENERATED_FOR_HEADERS}) endif() endforeach() -if (CMAKE_SYSTEM_NAME MATCHES "OpenBSD") - target_link_libraries(main_lib INTERFACE pthread c++abi) -endif() - -if(WIN32) - target_link_libraries(main_lib INTERFACE netapi32) -endif() - -if(UNIX) - target_link_libraries(main_lib INTERFACE m) - if (NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS") - target_link_libraries(main_lib INTERFACE util) - endif() -endif() - if(PREFER_LUA) message(STATUS "luajit not used, skipping unit tests") else() @@ -663,15 +680,6 @@ target_sources(main_lib INTERFACE target_sources(nlua0 PUBLIC ${NLUA0_SOURCES}) -set_target_properties(nvim - PROPERTIES - EXPORT_COMPILE_COMMANDS ON - ENABLE_EXPORTS TRUE) - -if(${CMAKE_VERSION} VERSION_LESS 3.20) - set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -endif() - target_link_libraries(nvim PRIVATE main_lib PUBLIC libuv) install_helper(TARGETS nvim) if(MSVC) @@ -768,6 +776,10 @@ set_target_properties( target_compile_definitions(libnvim PRIVATE MAKE_LIB) target_link_libraries(libnvim PRIVATE main_lib PUBLIC libuv) +#------------------------------------------------------------------------------- +# Cmake options +#------------------------------------------------------------------------------- + if(ENABLE_ASAN_UBSAN) message(STATUS "Enabling address sanitizer and undefined behavior sanitizer for nvim.") if(NOT MSVC) @@ -800,6 +812,10 @@ elseif(ENABLE_TSAN) target_link_libraries(nvim PRIVATE -fsanitize=thread) endif() +#------------------------------------------------------------------------------- +# Lint +#------------------------------------------------------------------------------- + find_program(CLANG_TIDY_PRG clang-tidy) set(EXCLUDE_CLANG_TIDY typval_encode.c.h ui_events.in.h) if(WIN32) |