From db7fdcd0bab8d5333bc89ab90ff256fcbdccde96 Mon Sep 17 00:00:00 2001 From: Rui Abreu Ferreira Date: Sun, 20 Dec 2015 04:50:20 +0000 Subject: [PATCH] MSVC: Build third-party dependencies as release DLLs Using /MT was causing issues when building luarocks, revert it, use the dynammic runtime and generate release DLLs for the dependencies. Some refactoring was required because for linking cmake looks for the import libraries (.lib) but on runtime executables we need the .dll files to be in the same folder. The DLLs are placed in the bin/ folder in order for nvim.exe to run during the build and tests. The install target installs the DLLs with the nvim binary - uses GetPrerequisites to find runtime DLLs. Some minor issues that required adjustments: - [MSVC] FindMsgpack.cmake now looks for msgpack_import.lib instead of msgpack.lib - The lua-client fails to find libuv.lib, instead it looks for uv.lib, added second copy of the file to the install command. - [MSVC] CMAKE_BUILD_TYPE affects the output paths, default to Release. Part of these changes are credited to @jasonwilliams200OK who fixed the third-party recipes to consistently use the same build type. --- cmake/FindMsgpack.cmake | 7 ++++++- cmake/WindowsDllCopy.cmake | 30 +++++++++++++++++++++++++++ src/nvim/CMakeLists.txt | 15 ++++++++++++++ third-party/CMakeLists.txt | 6 ++++++ third-party/cmake/BuildLibuv.cmake | 14 +++++++++---- third-party/cmake/BuildLuajit.cmake | 5 ++--- third-party/cmake/BuildLuarocks.cmake | 6 ++---- third-party/cmake/BuildMsgpack.cmake | 15 ++++++++------ 8 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 cmake/WindowsDllCopy.cmake diff --git a/cmake/FindMsgpack.cmake b/cmake/FindMsgpack.cmake index 015737d658..8881a34332 100644 --- a/cmake/FindMsgpack.cmake +++ b/cmake/FindMsgpack.cmake @@ -42,7 +42,12 @@ if(MSGPACK_USE_STATIC) "${CMAKE_STATIC_LIBRARY_PREFIX}msgpack${CMAKE_STATIC_LIBRARY_SUFFIX}") endif() -list(APPEND MSGPACK_NAMES msgpackc msgpack) +if(MSVC) + # The import library for the msgpack DLL has a different name + list(APPEND MSGPACK_NAMES msgpack_import) +else() + list(APPEND MSGPACK_NAMES msgpackc msgpack) +endif() find_library(MSGPACK_LIBRARY NAMES ${MSGPACK_NAMES} # Check each directory for all names to avoid using headers/libraries from diff --git a/cmake/WindowsDllCopy.cmake b/cmake/WindowsDllCopy.cmake new file mode 100644 index 0000000000..fbbabf3a0c --- /dev/null +++ b/cmake/WindowsDllCopy.cmake @@ -0,0 +1,30 @@ +# In Windows we need to find dependency DLLs and install them along with our +# binaries. This script uses the following variables: +# +# - BINARY: The binary file whose dependencies need to be installed +# - DST: The destination path +# - CMAKE_PREFIX_PATH: A list of directories to search for dependencies + +if(NOT DEFINED BINARY) + message(FATAL_ERROR "Missing required argument -DBINARY=") +endif() +if(NOT DEFINED DST) + message(FATAL_ERROR "Missing required arguments -DDST=") +endif() +if(NOT DEFINED CMAKE_PREFIX_PATH) + message(FATAL_ERROR "Missing required arguments -DCMAKE_PREFIX_PATH=") +endif() + +include(GetPrerequisites) +get_prerequisites(${BINARY} DLLS 1 1 "" "${CMAKE_PREFIX_PATH}") +foreach(DLL_NAME ${DLLS}) + find_program(DLL_PATH ${DLL_NAME}) + if(NOT DLL_PATH) + message(FATAL_ERROR "Unable to find dependency ${DLL_NAME}") + endif() + + message("Copying ${DLL_NAME} to ${DST}") + execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${DLL_PATH} ${DST}) + unset(DLL_PATH CACHE) +endforeach() + diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index d80add2835..5c4df1be0b 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -259,6 +259,21 @@ add_executable(nvim ${NEOVIM_GENERATED_SOURCES} ${NEOVIM_SOURCES} target_link_libraries(nvim ${NVIM_EXEC_LINK_LIBRARIES}) install_helper(TARGETS nvim) +if(WIN32) + # Copy DLLs to bin/ and install them along with nvim + add_custom_target(nvim_dll_deps ALL DEPENDS nvim + COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/windows_runtime_deps + COMMAND ${CMAKE_COMMAND} + "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" + -DBINARY="${PROJECT_BINARY_DIR}/bin/nvim${CMAKE_EXECUTABLE_SUFFIX}" + -DDST=${PROJECT_BINARY_DIR}/windows_runtime_deps + -P ${PROJECT_SOURCE_DIR}/cmake/WindowsDllCopy.cmake + COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_BINARY_DIR}/windows_runtime_deps/ + ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) + install(DIRECTORY ${PROJECT_BINARY_DIR}/windows_runtime_deps/ + DESTINATION ${CMAKE_INSTALL_BINDIR}) +endif() + if(CLANG_ASAN_UBSAN) message(STATUS "Enabling Clang address sanitizer and undefined behavior sanitizer for nvim.") check_c_compiler_flag(-fno-sanitize-recover=all SANITIZE_RECOVER_ALL) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index 9fc1b2eb36..7eba17aba3 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -5,6 +5,12 @@ project(NEOVIM_DEPS) # Point CMake at any custom modules we may ship list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") +# In Windows/MSVC CMAKE_BUILD_TYPE changes the paths/linking of the build +# recipes (libuv, msgpack), make sure it is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + set(DEPS_INSTALL_DIR "${CMAKE_BINARY_DIR}/usr" CACHE PATH "Dependencies install directory.") set(DEPS_BIN_DIR "${DEPS_INSTALL_DIR}/bin" CACHE PATH "Dependencies binary install directory.") set(DEPS_LIB_DIR "${DEPS_INSTALL_DIR}/lib" CACHE PATH "Dependencies library install directory.") diff --git a/third-party/cmake/BuildLibuv.cmake b/third-party/cmake/BuildLibuv.cmake index 1d8d69e64e..30fae44b74 100644 --- a/third-party/cmake/BuildLibuv.cmake +++ b/third-party/cmake/BuildLibuv.cmake @@ -70,11 +70,17 @@ elseif(WIN32 AND MSVC) else() set(VS_ARCH x64) endif() - BuildLibuv( - # By default this creates Debug builds - BUILD_COMMAND set PYTHON=${PYTHON_EXECUTABLE} COMMAND ${DEPS_BUILD_DIR}/src/libuv/vcbuild.bat static debug ${VS_ARCH} + string(TOLOWER ${CMAKE_BUILD_TYPE} LOWERCASE_BUILD_TYPE) + set(UV_OUTPUT_DIR ${DEPS_BUILD_DIR}/src/libuv/${CMAKE_BUILD_TYPE}) + BuildLibUv( + BUILD_COMMAND set PYTHON=${PYTHON_EXECUTABLE} COMMAND ${DEPS_BUILD_DIR}/src/libuv/vcbuild.bat shared ${LOWERCASE_BUILD_TYPE} ${VS_ARCH} INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/lib - COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/libuv/Debug/lib/libuv.lib ${DEPS_INSTALL_DIR}/lib + COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/bin + COMMAND ${CMAKE_COMMAND} -E copy ${UV_OUTPUT_DIR}/libuv.lib ${DEPS_INSTALL_DIR}/lib + # Some applications (lua-client/luarocks) look for uv.lib instead of libuv.lib + COMMAND ${CMAKE_COMMAND} -E copy ${UV_OUTPUT_DIR}/libuv.lib ${DEPS_INSTALL_DIR}/lib/uv.lib + COMMAND ${CMAKE_COMMAND} -E copy ${UV_OUTPUT_DIR}/libuv.dll ${DEPS_INSTALL_DIR}/bin/ + COMMAND ${CMAKE_COMMAND} -E copy ${UV_OUTPUT_DIR}/libuv.dll ${DEPS_INSTALL_DIR}/bin/uv.dll COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include COMMAND ${CMAKE_COMMAND} -E copy_directory ${DEPS_BUILD_DIR}/src/libuv/include ${DEPS_INSTALL_DIR}/include) diff --git a/third-party/cmake/BuildLuajit.cmake b/third-party/cmake/BuildLuajit.cmake index 83aceecb59..de92b32a74 100644 --- a/third-party/cmake/BuildLuajit.cmake +++ b/third-party/cmake/BuildLuajit.cmake @@ -40,7 +40,6 @@ set(INSTALLCMD_UNIX ${MAKE_PRG} CFLAGS=-fPIC CFLAGS+=-DLUA_USE_APICHECK CFLAGS+=-DLUA_USE_ASSERT CCDEBUG+=-g - BUILDMODE=static Q= install) @@ -74,10 +73,10 @@ elseif(MINGW AND CMAKE_CROSSCOMPILING) elseif(WIN32 AND MSVC) BuildLuaJit( - BUILD_COMMAND ${CMAKE_COMMAND} -E chdir ${DEPS_BUILD_DIR}/src/luajit/src ${DEPS_BUILD_DIR}/src/luajit/src/msvcbuild.bat static + BUILD_COMMAND ${CMAKE_COMMAND} -E chdir ${DEPS_BUILD_DIR}/src/luajit/src ${DEPS_BUILD_DIR}/src/luajit/src/msvcbuild.bat INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/bin COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/luajit.exe ${DEPS_INSTALL_DIR}/bin - COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.lib ${DEPS_INSTALL_DIR}/bin + COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.dll ${DEPS_INSTALL_DIR}/bin COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/lib COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.lib ${DEPS_INSTALL_DIR}/lib COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.0 diff --git a/third-party/cmake/BuildLuarocks.cmake b/third-party/cmake/BuildLuarocks.cmake index 0041bb1a03..2c3b986c69 100644 --- a/third-party/cmake/BuildLuarocks.cmake +++ b/third-party/cmake/BuildLuarocks.cmake @@ -46,11 +46,9 @@ endfunction() set(LUAROCKS_BINARY ${HOSTDEPS_BIN_DIR}/luarocks) # Arguments for calls to 'luarocks build' -if(MSVC) - # In native Win32 don't pass the compiler/linker to luarocks, the bundled +if(NOT MSVC) + # In MSVC don't pass the compiler/linker to luarocks, the bundled # version already knows, and passing them here breaks the build - set(LUAROCKS_BUILDARGS CFLAGS=/MT) -else() set(LUAROCKS_BUILDARGS CC=${HOSTDEPS_C_COMPILER} LD=${HOSTDEPS_C_COMPILER}) endif() diff --git a/third-party/cmake/BuildMsgpack.cmake b/third-party/cmake/BuildMsgpack.cmake index 4b6b361e85..211a2f72aa 100644 --- a/third-party/cmake/BuildMsgpack.cmake +++ b/third-party/cmake/BuildMsgpack.cmake @@ -37,10 +37,11 @@ set(MSGPACK_CONFIGURE_COMMAND ${CMAKE_COMMAND} ${DEPS_BUILD_DIR}/src/msgpack -DMSGPACK_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1} -fPIC") -set(MSGPACK_BUILD_COMMAND ${CMAKE_COMMAND} --build .) -set(MSGPACK_INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install) +set(MSGPACK_BUILD_COMMAND ${CMAKE_COMMAND} --build . --config ${CMAKE_BUILD_TYPE}) +set(MSGPACK_INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config ${CMAKE_BUILD_TYPE}) if(MINGW AND CMAKE_CROSSCOMPILING) get_filename_component(TOOLCHAIN ${CMAKE_TOOLCHAIN_FILE} REALPATH) @@ -50,6 +51,7 @@ if(MINGW AND CMAKE_CROSSCOMPILING) -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR} # Pass toolchain -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} # Hack to avoid -rdynamic in Mingw -DCMAKE_SHARED_LIBRARY_LINK_C_FLAGS="") elseif(MSVC) @@ -60,12 +62,13 @@ elseif(MSVC) -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1}" + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} # 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") + -DCMAKE_GENERATOR=${CMAKE_GENERATOR}) + # Place the DLL in the bin folder + set(MSGPACK_INSTALL_COMMAND ${MSGPACK_INSTALL_COMMAND} + COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_INSTALL_DIR}/lib/msgpack.dll ${DEPS_INSTALL_DIR}/bin) endif() BuildMsgpack(CONFIGURE_COMMAND ${MSGPACK_CONFIGURE_COMMAND}