aboutsummaryrefslogtreecommitdiff
path: root/cmake
diff options
context:
space:
mode:
authordundargoc <33953936+dundargoc@users.noreply.github.com>2023-01-10 18:49:57 +0100
committerGitHub <noreply@github.com>2023-01-10 18:49:57 +0100
commit438b4361cc761a2950689668f008cfe06c1510f7 (patch)
treeb950de8e32fa68e35d8287ac4cafe12754094bf8 /cmake
parent9b1112cf48238260b170b8763b18a02a58159c2a (diff)
downloadrneovim-438b4361cc761a2950689668f008cfe06c1510f7.tar.gz
rneovim-438b4361cc761a2950689668f008cfe06c1510f7.tar.bz2
rneovim-438b4361cc761a2950689668f008cfe06c1510f7.zip
build: use modern cmake (#21589)
Replace old-school cmake with the so-called "Modern CMake", meaning preferring using targets and properties over directory settings and variables. This allows greater flexibility, robustness and clarity over how the code works. The following deprecated commands will be replaced with their modern alternatives that operates on a specific target, rather than all targets in the current directory: - add_compile_options -> target_compile_options - include_directories -> target_include_directories - link_libraries -> target_link_libraries - add_definitions -> target_compile_definitions There are mainly four main targets that we currently use: nvim, libnvim, nvim-test (used by unittests) and ${texe} (used by check-single-includes). The goal is to explicitly define the dependencies of each target fully, rather than having everything be dependent on everything else.
Diffstat (limited to 'cmake')
-rw-r--r--cmake/FindIconv.cmake3
-rw-r--r--cmake/GetCompileFlags.cmake108
2 files changed, 46 insertions, 65 deletions
diff --git a/cmake/FindIconv.cmake b/cmake/FindIconv.cmake
index 1d0164dae9..63290fe889 100644
--- a/cmake/FindIconv.cmake
+++ b/cmake/FindIconv.cmake
@@ -1,3 +1,6 @@
+# TODO(dundargoc): FindIconv is shipped by default on cmake version 3.11+. This
+# file can be removed once we decide to upgrade minimum cmake version.
+
# - Try to find iconv
# Once done, this will define
#
diff --git a/cmake/GetCompileFlags.cmake b/cmake/GetCompileFlags.cmake
index 3b027690f8..9b3c053871 100644
--- a/cmake/GetCompileFlags.cmake
+++ b/cmake/GetCompileFlags.cmake
@@ -1,79 +1,57 @@
function(get_compile_flags _compile_flags)
- # Create template akin to CMAKE_C_COMPILE_OBJECT.
- set(compile_flags "<CMAKE_C_COMPILER> <CFLAGS> <BUILD_TYPE_CFLAGS> <COMPILE_OPTIONS><COMPILE_DEFINITIONS> <INCLUDES>")
+ string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
+ set(compile_flags ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${build_type}})
- string(REPLACE
- "<CMAKE_C_COMPILER>"
- "${CMAKE_C_COMPILER}"
- compile_flags
- "${compile_flags}")
+ # Get flags set by target_compile_options().
+ get_target_property(opt main_lib INTERFACE_COMPILE_OPTIONS)
+ if(opt)
+ list(APPEND compile_flags ${opt})
+ endif()
- # Get flags set by add_definitions().
- get_property(compile_definitions DIRECTORY PROPERTY COMPILE_DEFINITIONS)
- get_target_property(compile_definitions_target nvim COMPILE_DEFINITIONS)
- if(compile_definitions_target)
- list(APPEND compile_definitions ${compile_definitions_target})
- list(REMOVE_DUPLICATES compile_definitions)
+ get_target_property(opt nvim COMPILE_OPTIONS)
+ if(opt)
+ list(APPEND compile_flags ${opt})
endif()
- # NOTE: list(JOIN) requires CMake 3.12, string(CONCAT) requires CMake 3.
- string(REPLACE ";" " -D" compile_definitions "${compile_definitions}")
- if(compile_definitions)
- set(compile_definitions " -D${compile_definitions}")
+
+ # Get flags set by target_compile_definitions().
+ get_target_property(defs main_lib INTERFACE_COMPILE_DEFINITIONS)
+ if(defs)
+ foreach(def ${defs})
+ list(APPEND compile_flags "-D${def}")
+ endforeach()
endif()
- string(REPLACE
- "<COMPILE_DEFINITIONS>"
- "${compile_definitions}"
- compile_flags
- "${compile_flags}")
- # Get flags set by add_compile_options().
- get_property(compile_options DIRECTORY PROPERTY COMPILE_OPTIONS)
- get_target_property(compile_options_target nvim COMPILE_OPTIONS)
- if(compile_options_target)
- list(APPEND compile_options ${compile_options_target})
- list(REMOVE_DUPLICATES compile_options)
+ get_target_property(defs nvim COMPILE_DEFINITIONS)
+ if(defs)
+ foreach(def ${defs})
+ list(APPEND compile_flags "-D${def}")
+ endforeach()
endif()
- # NOTE: list(JOIN) requires CMake 3.12.
- string(REPLACE ";" " " compile_options "${compile_options}")
- string(REPLACE
- "<COMPILE_OPTIONS>"
- "${compile_options}"
- compile_flags
- "${compile_flags}")
- # Get general C flags.
- string(REPLACE
- "<CFLAGS>"
- "${CMAKE_C_FLAGS}"
- compile_flags
- "${compile_flags}")
+ # Get include directories.
+ get_target_property(dirs main_lib INTERFACE_INCLUDE_DIRECTORIES)
+ if(dirs)
+ foreach(dir ${dirs})
+ list(APPEND compile_flags "-I${dir}")
+ endforeach()
+ endif()
- # Get C flags specific to build type.
- string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
- string(REPLACE
- "<BUILD_TYPE_CFLAGS>"
- "${CMAKE_C_FLAGS_${build_type}}"
- compile_flags
- "${compile_flags}")
+ get_target_property(dirs main_lib INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
+ if(dirs)
+ foreach(dir ${dirs})
+ list(APPEND compile_flags "-I${dir}")
+ endforeach()
+ endif()
- # Get include directories.
- get_property(include_directories_list DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
- list(REMOVE_DUPLICATES include_directories_list)
- foreach(include_directory ${include_directories_list})
- set(include_directories "${include_directories} -I${include_directory}")
- endforeach()
- string(REPLACE
- "<INCLUDES>"
- "${include_directories}"
- compile_flags
- "${compile_flags}")
+ get_target_property(dirs nvim INCLUDE_DIRECTORIES)
+ if(dirs)
+ foreach(dir ${dirs})
+ list(APPEND compile_flags "-I${dir}")
+ endforeach()
+ endif()
- # Clean duplicate whitespace.
- string(REPLACE
- " "
- " "
- compile_flags
- "${compile_flags}")
+ list(REMOVE_DUPLICATES compile_flags)
+ string(REPLACE ";" " " compile_flags "${compile_flags}")
set(${_compile_flags} "${compile_flags}" PARENT_SCOPE)
endfunction()