From 10860164778327c0009f6efc8e020308cadb13a2 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Sat, 13 May 2023 12:12:29 +0200 Subject: [PATCH] build: cmake cleanup - Simplify error checking when using execute_process. - Set BUILD_SHARED_LIBS to OFF when building dependencies. This is normally not needed, but msgpack interprets an unset BUILD_SHARED_LIBS to build a shared library, which is the opposite of the cmake behavior. - Move function check_lua_module to Util.cmake. - Remove unnecessary code. - Make variable naming more consistent --- CMakeLists.txt | 4 +++- cmake.deps/CMakeLists.txt | 5 +++-- cmake.deps/cmake/BuildLuarocks.cmake | 6 +++--- cmake.deps/cmake/BuildLuv.cmake | 9 ++++----- cmake.deps/cmake/BuildMsgpack.cmake | 8 -------- cmake.deps/cmake/CopyFilesGlob.cmake | 2 +- cmake/ConvertPo.cmake | 2 +- cmake/FindLibluv.cmake | 14 -------------- cmake/FindLibuv.cmake | 8 +------- cmake/FindLuajit.cmake | 3 --- cmake/FindLuv.cmake | 14 ++++++++++++++ cmake/InstallHelpers.cmake | 3 --- cmake/LuaHelpers.cmake | 14 -------------- cmake/RunTests.cmake | 2 +- cmake/Util.cmake | 11 +++++++++++ src/nvim/CMakeLists.txt | 16 ++++++++-------- 16 files changed, 50 insertions(+), 71 deletions(-) delete mode 100644 cmake/FindLibluv.cmake create mode 100644 cmake/FindLuv.cmake delete mode 100644 cmake/LuaHelpers.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 731bf09e2e..f1b17f66a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,9 +22,11 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") include(CheckCCompilerFlag) include(CheckCSourceCompiles) +include(CheckLibraryExists) include(FindPackageHandleStandardArgs) +include(GNUInstallDirs) + include(InstallHelpers) -include(LuaHelpers) include(PreventInTreeBuilds) include(Util) diff --git a/cmake.deps/CMakeLists.txt b/cmake.deps/CMakeLists.txt index bb86a671f2..2de4160176 100644 --- a/cmake.deps/CMakeLists.txt +++ b/cmake.deps/CMakeLists.txt @@ -9,7 +9,9 @@ endif() # Point CMake at any custom modules we may ship list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" "${PROJECT_SOURCE_DIR}/../cmake") +include(ExternalProject) include(CheckCCompilerFlag) + include(Util) set(DEPS_CMAKE_ARGS @@ -17,13 +19,13 @@ set(DEPS_CMAKE_ARGS -D CMAKE_C_STANDARD=99 -D CMAKE_GENERATOR=${CMAKE_GENERATOR} -D CMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} + -D BUILD_SHARED_LIBS=OFF -D CMAKE_POSITION_INDEPENDENT_CODE=ON -D CMAKE_FIND_FRAMEWORK=${CMAKE_FIND_FRAMEWORK}) set(DEPS_CMAKE_CACHE_ARGS -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHITECTURES}) set_default_buildtype() - get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) if(NOT isMultiConfig) list(APPEND DEPS_CMAKE_ARGS -D CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}) @@ -132,7 +134,6 @@ if(APPLE) message(STATUS "Using deployment target ${CMAKE_OSX_DEPLOYMENT_TARGET}") endif() -include(ExternalProject) set_directory_properties(PROPERTIES EP_PREFIX "${DEPS_BUILD_DIR}" CMAKE_CONFIGURE_DEPENDS deps.txt) diff --git a/cmake.deps/cmake/BuildLuarocks.cmake b/cmake.deps/cmake/BuildLuarocks.cmake index d45776f5b1..5725507c36 100644 --- a/cmake.deps/cmake/BuildLuarocks.cmake +++ b/cmake.deps/cmake/BuildLuarocks.cmake @@ -1,4 +1,4 @@ -# Luarocks recipe. Luarocks is only required when building Neovim. +# Luarocks recipe. Luarocks is only required when testing Neovim. # NOTE: LuaRocks rocks need to "DEPENDS" on the previous module, because # running luarocks in parallel will break, e.g. when some rocks have # the same dependency. @@ -31,7 +31,7 @@ if(UNIX) find_package(Luajit) if(LUAJIT_FOUND) list(APPEND LUAROCKS_OPTS - --with-lua-include=${LUAJIT_INCLUDE_DIRS} + --with-lua-include=${LUAJIT_INCLUDE_DIR} --with-lua-interpreter=luajit) endif() @@ -46,7 +46,7 @@ if(UNIX) OUTPUT_VARIABLE LUA_VERSION ERROR_VARIABLE ERR RESULT_VARIABLE RES) - if(NOT RES EQUAL 0) + if(RES) message(FATAL_ERROR "Could not get LUA_VERSION with ${LUA_EXE}: ${ERR}") endif() endif() diff --git a/cmake.deps/cmake/BuildLuv.cmake b/cmake.deps/cmake/BuildLuv.cmake index 08168c9804..7220333de1 100644 --- a/cmake.deps/cmake/BuildLuv.cmake +++ b/cmake.deps/cmake/BuildLuv.cmake @@ -2,7 +2,6 @@ set(LUV_CMAKE_ARGS -D LUA_BUILD_TYPE=System -D LUA_COMPAT53_DIR=${DEPS_BUILD_DIR}/src/lua-compat-5.3 -D WITH_SHARED_LIBUV=ON - -D BUILD_SHARED_LIBS=OFF -D BUILD_STATIC_LIBS=ON -D BUILD_MODULE=OFF) @@ -38,7 +37,7 @@ ExternalProject_Add(lua-compat-5.3 BUILD_COMMAND "" INSTALL_COMMAND "") -ExternalProject_Add(luv-static +ExternalProject_Add(luv DEPENDS lua-compat-5.3 URL ${LUV_URL} URL_HASH SHA256=${LUV_SHA256} @@ -49,10 +48,10 @@ ExternalProject_Add(luv-static CMAKE_CACHE_ARGS ${DEPS_CMAKE_CACHE_ARGS}) if(USE_BUNDLED_LUAJIT) - add_dependencies(luv-static luajit) + add_dependencies(luv luajit) elseif(USE_BUNDLED_LUA) - add_dependencies(luv-static lua) + add_dependencies(luv lua) endif() if(USE_BUNDLED_LIBUV) - add_dependencies(luv-static libuv) + add_dependencies(luv libuv) endif() diff --git a/cmake.deps/cmake/BuildMsgpack.cmake b/cmake.deps/cmake/BuildMsgpack.cmake index c9033be1ae..f63559e6b4 100644 --- a/cmake.deps/cmake/BuildMsgpack.cmake +++ b/cmake.deps/cmake/BuildMsgpack.cmake @@ -7,11 +7,3 @@ ExternalProject_Add(msgpack -D MSGPACK_BUILD_TESTS=OFF -D MSGPACK_BUILD_EXAMPLES=OFF CMAKE_CACHE_ARGS ${DEPS_CMAKE_CACHE_ARGS}) - -if (NOT MSVC) - add_custom_target(clean_shared_libraries_msgpack ALL - COMMAND ${CMAKE_COMMAND} - -D REMOVE_FILE_GLOB=${DEPS_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}*${CMAKE_SHARED_LIBRARY_SUFFIX}* - -P ${PROJECT_SOURCE_DIR}/cmake/RemoveFiles.cmake) - add_dependencies(clean_shared_libraries_msgpack msgpack) -endif() diff --git a/cmake.deps/cmake/CopyFilesGlob.cmake b/cmake.deps/cmake/CopyFilesGlob.cmake index 8950ead1e5..7d0f4fc289 100644 --- a/cmake.deps/cmake/CopyFilesGlob.cmake +++ b/cmake.deps/cmake/CopyFilesGlob.cmake @@ -14,7 +14,7 @@ execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${TO}) file(GLOB files ${FROM_GLOB}) foreach(file ${files}) execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${file} ${TO} RESULT_VARIABLE rv) - if(NOT rv EQUAL 0) + if(rv) message(FATAL_ERROR "Error copying ${file}") endif() endforeach() diff --git a/cmake/ConvertPo.cmake b/cmake/ConvertPo.cmake index 2282b96f56..202cd3fbb3 100644 --- a/cmake/ConvertPo.cmake +++ b/cmake/ConvertPo.cmake @@ -6,7 +6,7 @@ execute_process( OUTPUT_VARIABLE trans ERROR_VARIABLE err RESULT_VARIABLE res) -if(NOT res EQUAL 0) +if(res) message(FATAL_ERROR "iconv failed to run correctly: ${err}") endif() diff --git a/cmake/FindLibluv.cmake b/cmake/FindLibluv.cmake deleted file mode 100644 index d402b26447..0000000000 --- a/cmake/FindLibluv.cmake +++ /dev/null @@ -1,14 +0,0 @@ -find_path(LIBLUV_INCLUDE_DIR luv/luv.h) -find_library(LIBLUV_LIBRARY NAMES luv_a luv libluv_a) - -# Ubuntu-specific workaround to find system paths -function(ubuntu) - set(CMAKE_FIND_LIBRARY_PREFIXES "") - find_path(LIBLUV_INCLUDE_DIR luv/luv.h PATH_SUFFIXES lua5.1) - find_library(LIBLUV_LIBRARY NAMES luv PATH_SUFFIXES lua/5.1) -endfunction() -ubuntu() - -find_package_handle_standard_args(Libluv DEFAULT_MSG - LIBLUV_LIBRARY LIBLUV_INCLUDE_DIR) -mark_as_advanced(LIBLUV_INCLUDE_DIR LIBLUV_LIBRARY) diff --git a/cmake/FindLibuv.cmake b/cmake/FindLibuv.cmake index 0cf8da3061..fa1d51370f 100644 --- a/cmake/FindLibuv.cmake +++ b/cmake/FindLibuv.cmake @@ -1,13 +1,7 @@ find_path(LIBUV_INCLUDE_DIR uv.h) - -list(APPEND LIBUV_NAMES uv_a uv) - -find_library(LIBUV_LIBRARY NAMES ${LIBUV_NAMES}) +find_library(LIBUV_LIBRARY NAMES uv_a uv) set(LIBUV_LIBRARIES ${LIBUV_LIBRARY}) -set(LIBUV_INCLUDE_DIRS ${LIBUV_INCLUDE_DIR}) - -include(CheckLibraryExists) check_library_exists(dl dlopen "dlfcn.h" HAVE_LIBDL) if(HAVE_LIBDL) diff --git a/cmake/FindLuajit.cmake b/cmake/FindLuajit.cmake index 924e4c80d4..711c2c0c09 100644 --- a/cmake/FindLuajit.cmake +++ b/cmake/FindLuajit.cmake @@ -11,9 +11,6 @@ endif() find_library(LUAJIT_LIBRARY NAMES ${LUAJIT_NAMES}) -set(LUAJIT_LIBRARIES ${LUAJIT_LIBRARY}) -set(LUAJIT_INCLUDE_DIRS ${LUAJIT_INCLUDE_DIR}) - find_package_handle_standard_args(Luajit DEFAULT_MSG LUAJIT_LIBRARY LUAJIT_INCLUDE_DIR) diff --git a/cmake/FindLuv.cmake b/cmake/FindLuv.cmake new file mode 100644 index 0000000000..ebd74887ed --- /dev/null +++ b/cmake/FindLuv.cmake @@ -0,0 +1,14 @@ +find_path(LUV_INCLUDE_DIR luv/luv.h) +find_library(LUV_LIBRARY NAMES luv_a luv libluv_a) + +# Ubuntu-specific workaround to find system paths +function(ubuntu) + set(CMAKE_FIND_LIBRARY_PREFIXES "") + find_path(LUV_INCLUDE_DIR luv/luv.h PATH_SUFFIXES lua5.1) + find_library(LUV_LIBRARY NAMES luv PATH_SUFFIXES lua/5.1) +endfunction() +ubuntu() + +find_package_handle_standard_args(Luv DEFAULT_MSG + LUV_LIBRARY LUV_INCLUDE_DIR) +mark_as_advanced(LUV_INCLUDE_DIR LUV_LIBRARY) diff --git a/cmake/InstallHelpers.cmake b/cmake/InstallHelpers.cmake index 49d8692aae..63bf2bb73b 100644 --- a/cmake/InstallHelpers.cmake +++ b/cmake/InstallHelpers.cmake @@ -7,9 +7,6 @@ if(CMAKE_SYSTEM_NAME MATCHES "BSD" AND NOT DEFINED CMAKE_INSTALL_MANDIR) endif() endif() -# For $CMAKE_INSTALL_{DATAROOT,MAN, ...}DIR -include(GNUInstallDirs) - # This will create any directories that need to be created in the destination # path with the typical owner, group, and user permissions--independent of the # umask setting. diff --git a/cmake/LuaHelpers.cmake b/cmake/LuaHelpers.cmake deleted file mode 100644 index 00ecd1357f..0000000000 --- a/cmake/LuaHelpers.cmake +++ /dev/null @@ -1,14 +0,0 @@ -# -# Functions to help checking for a Lua interpreter -# - -# Check if a module is available in Lua -function(check_lua_module LUA_PRG_PATH MODULE RESULT_VAR) - execute_process(COMMAND ${LUA_PRG_PATH} -l "${MODULE}" -e "" - RESULT_VARIABLE module_missing) - if(module_missing) - set(${RESULT_VAR} False PARENT_SCOPE) - else() - set(${RESULT_VAR} True PARENT_SCOPE) - endif() -endfunction() diff --git a/cmake/RunTests.cmake b/cmake/RunTests.cmake index fe346661b5..1dcb6fb373 100644 --- a/cmake/RunTests.cmake +++ b/cmake/RunTests.cmake @@ -82,7 +82,7 @@ execute_process( file(GLOB RM_FILES ${BUILD_DIR}/Xtest_*) file(REMOVE_RECURSE ${RM_FILES}) -if(NOT res EQUAL 0) +if(res) message(STATUS "Tests exited non-zero: ${res}") if("${err}" STREQUAL "") message(STATUS "No output to stderr.") diff --git a/cmake/Util.cmake b/cmake/Util.cmake index e15b44d29a..b70f33a302 100644 --- a/cmake/Util.cmake +++ b/cmake/Util.cmake @@ -193,3 +193,14 @@ function(set_default_buildtype) endif() endif() endfunction() + +# Check if a module is available in Lua +function(check_lua_module LUA_PRG_PATH MODULE RESULT_VAR) + execute_process(COMMAND ${LUA_PRG_PATH} -l "${MODULE}" -e "" + RESULT_VARIABLE module_missing) + if(module_missing) + set(${RESULT_VAR} FALSE PARENT_SCOPE) + else() + set(${RESULT_VAR} TRUE PARENT_SCOPE) + endif() +endfunction() diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 3bb054946f..325b376b30 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -9,7 +9,7 @@ if(TARGET libuv::uv_a) else() # Fall back to find module for older libuv versions that don't provide config file find_package(Libuv 1.28.0 REQUIRED MODULE) - target_include_directories(libuv SYSTEM BEFORE INTERFACE ${LIBUV_INCLUDE_DIRS}) + target_include_directories(libuv SYSTEM BEFORE INTERFACE ${LIBUV_INCLUDE_DIR}) target_link_libraries(libuv INTERFACE ${LIBUV_LIBRARIES}) endif() @@ -21,9 +21,9 @@ elseif(APPLE) set_target_properties(nlua0 PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") endif() -find_package(Libluv 1.43.0 REQUIRED) -target_include_directories(main_lib SYSTEM BEFORE INTERFACE ${LIBLUV_INCLUDE_DIR}) -target_link_libraries(main_lib INTERFACE ${LIBLUV_LIBRARY}) +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(Lpeg REQUIRED) @@ -61,11 +61,11 @@ if(PREFER_LUA) find_package(Luajit) else() find_package(Luajit REQUIRED) - target_include_directories(main_lib SYSTEM BEFORE INTERFACE ${LUAJIT_INCLUDE_DIRS}) - target_link_libraries(main_lib INTERFACE ${LUAJIT_LIBRARIES}) - target_include_directories(nlua0 SYSTEM BEFORE PUBLIC ${LUAJIT_INCLUDE_DIRS}) + 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_LIBRARIES}) + target_link_libraries(nlua0 PUBLIC ${LUAJIT_LIBRARY}) endif() endif()