neovim/third-party/cmake/BuildLuv.cmake
Thiago de Arruda 4ae7acd152 Add luv as a third party dependency
Luv is a simple lua binding to libuv, which is now used by neovim lua client.
The bundled luv installation a bit different from other dependencies in that it
is installed two times:

- The "BuildLuv.cmake" script downloads and installs a static version of luv
  using its normal cmake build script. This static version will be used later.
- Luv default rockspec is replaced with the alternate under the "rockspecs"
  directory(the alternate rockspec plays nicer with neovim build system)
- The alternate rockspec is used to build/install the lua module and make it
  available to lua scripts.
2016-04-11 23:07:52 -03:00

91 lines
3.1 KiB
CMake

include(CMakeParseArguments)
# BuildLuv(PATCH_COMMAND ... CONFIGURE_COMMAND ... BUILD_COMMAND ... INSTALL_COMMAND ...)
# Reusable function to build luv, wraps ExternalProject_Add.
# Failing to pass a command argument will result in no command being run
function(BuildLuv)
cmake_parse_arguments(_luv
""
""
"PATCH_COMMAND;CONFIGURE_COMMAND;BUILD_COMMAND;INSTALL_COMMAND"
${ARGN})
if(NOT _luv_CONFIGURE_COMMAND AND NOT _luv_BUILD_COMMAND
AND NOT _luv_INSTALL_COMMAND)
message(FATAL_ERROR "Must pass at least one of CONFIGURE_COMMAND, BUILD_COMMAND, INSTALL_COMMAND")
endif()
ExternalProject_Add(luv-static
PREFIX ${DEPS_BUILD_DIR}
URL ${LUV_URL}
DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/luv
DOWNLOAD_COMMAND ${CMAKE_COMMAND}
-DPREFIX=${DEPS_BUILD_DIR}
-DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/luv
-DURL=${LUV_URL}
-DEXPECTED_SHA256=${LUV_SHA256}
-DTARGET=luv
-DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake
PATCH_COMMAND "${_luv_PATCH_COMMAND}"
CONFIGURE_COMMAND "${_luv_CONFIGURE_COMMAND}"
BUILD_COMMAND "${_luv_BUILD_COMMAND}"
INSTALL_COMMAND "${_luv_INSTALL_COMMAND}")
endfunction()
set(LUV_SRC_DIR ${DEPS_BUILD_DIR}/src/luv)
set(LUV_INCLUDE_FLAGS
"-I${DEPS_INSTALL_DIR}/include -I${DEPS_INSTALL_DIR}/include/luajit-2.0")
set(LUV_PATCH_COMMAND
${CMAKE_COMMAND} -DLUV_SRC_DIR=${LUV_SRC_DIR}
-P ${PROJECT_SOURCE_DIR}/cmake/PatchLuv.cmake)
set(LUV_CONFIGURE_COMMAND_COMMON
${CMAKE_COMMAND} ${LUV_SRC_DIR}
-DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR}
-DLUA_BUILD_TYPE=System
-DWITH_SHARED_LIBUV=ON
-DBUILD_SHARED_LIBS=OFF
-DBUILD_MODULE=OFF)
if(MINGW AND CMAKE_CROSSCOMPILING)
get_filename_component(TOOLCHAIN ${CMAKE_TOOLCHAIN_FILE} REALPATH)
set(LUV_CONFIGURE_COMMAND
${LUV_CONFIGURE_COMMAND_COMMON}
# Pass toolchain
-DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN}
"-DCMAKE_C_FLAGS:STRING=${LUV_INCLUDE_FLAGS} -D_WIN32_WINNT=0x0600"
# Hack to avoid -rdynamic in Mingw
-DCMAKE_SHARED_LIBRARY_LINK_C_FLAGS="")
elseif(MSVC)
set(LUV_CONFIGURE_COMMAND
${LUV_CONFIGURE_COMMAND_COMMON}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
# Same as Unix without fPIC
"-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1} ${LUV_INCLUDE_FLAGS}"
# Make sure we use the same generator, otherwise we may
# accidentaly end up using different MSVC runtimes
-DCMAKE_GENERATOR=${CMAKE_GENERATOR}
# Use static runtime
-DCMAKE_C_FLAGS_DEBUG="-MTd"
-DCMAKE_C_FLAGS_RELEASE="-MT")
else()
set(LUV_CONFIGURE_COMMAND
${LUV_CONFIGURE_COMMAND_COMMON}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
"-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1} ${LUV_INCLUDE_FLAGS} -fPIC")
endif()
set(LUV_BUILD_COMMAND ${CMAKE_COMMAND} --build .)
set(LUV_INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install)
BuildLuv(PATCH_COMMAND ${LUV_PATCH_COMMAND}
CONFIGURE_COMMAND ${LUV_CONFIGURE_COMMAND}
BUILD_COMMAND ${LUV_BUILD_COMMAND}
INSTALL_COMMAND ${LUV_INSTALL_COMMAND})
list(APPEND THIRD_PARTY_DEPS luv-static)
add_dependencies(luv-static luajit)
add_dependencies(luv-static libuv)