From: Lucian Petrut Date: Wed, 5 Feb 2020 14:54:16 +0000 (+0000) Subject: cmake: [win32] Update cmake files X-Git-Tag: v15.1.1~328^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d15481f1a50d29b1afd6de7a2e55965a3d1f3c44;p=ceph.git cmake: [win32] Update cmake files We'll update the cmake files in order to be able to build ceph components for Windows targets. Cross compiling using MINGW is the easiest approach for now. Subsequently, we'll add support for Clang and MSVC. This patch provides the following changes: * include winsock2.h, which provides ntohl on Windows * avoid unsupported compiler flags when using msvc * add a custom toolchain file for mingw * update install command for ceph-common in order to work with mingw * avoid running test sample when cross compiling * link against the ws_32 lib * set argeted Windows version * skip yasm checks when targeting Windows * allow multiple redefinitions when using mingw, picking the first one. this is a workaround for a mingw TLS bug: https://sourceforge.net/p/mingw-w64/bugs/816/ Signed-off-by: Alin Gabriel Serdean Signed-off-by: Lucian Petrut --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a5c6f7e3697a..fb14e66745a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,19 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") FIND_PACKAGE(Threads) endif(CMAKE_SYSTEM_NAME MATCHES "Linux") +if(WIN32) + # The Windows headers (e.g. coming from mingw or the Windows SDK) check + # the targeted Windows version. The availability of certain functions and + # structures will depend on it. + set(WIN32_WINNT "0x0A00" CACHE STRING "Targeted Windows version.") + add_definitions(-D_WIN32_WINNT=${WIN32_WINNT}) +endif() + +if(MINGW) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-allow-multiple-definition") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-allow-multiple-definition") +endif() + option(WITH_CCACHE "Build with ccache.") if(WITH_CCACHE) find_program(CCACHE_FOUND ccache) diff --git a/cmake/modules/CephChecks.cmake b/cmake/modules/CephChecks.cmake index 1a46e88876df..23687283a7c6 100644 --- a/cmake/modules/CephChecks.cmake +++ b/cmake/modules/CephChecks.cmake @@ -92,14 +92,23 @@ CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec sys/stat.h CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtimespec.tv_nsec sys/stat.h HAVE_STAT_ST_MTIMESPEC_TV_NSEC LANGUAGE C) -if(CMAKE_SYSTEM_PROCESSOR STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR) +if(NOT CMAKE_CROSSCOMPILING) include(CheckCXXSourceRuns) cmake_push_check_state() set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++17") + if(WIN32) + set(CMAKE_REQUIRED_LIBRARIES ws2_32) + endif() + check_cxx_source_runs(" #include #include + +#ifdef _WIN32 +#include +#else #include +#endif uint32_t load(char* p, size_t offset) { @@ -129,9 +138,9 @@ int main(int argc, char **argv) if(NOT HAVE_UNALIGNED_ACCESS) message(FATAL_ERROR "Unaligned access is required") endif() -else(CMAKE_SYSTEM_PROCESSOR STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR) +else(NOT CMAKE_CROSSCOMPILING) message(STATUS "Assuming unaligned access is supported") -endif(CMAKE_SYSTEM_PROCESSOR STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR) +endif(NOT CMAKE_CROSSCOMPILING) # should use LINK_OPTIONS instead of LINK_LIBRARIES, if we can use cmake v3.14+ try_compile(HAVE_LINK_VERSION_SCRIPT diff --git a/cmake/toolchains/mingw32.cmake b/cmake/toolchains/mingw32.cmake new file mode 100644 index 000000000000..e363a6897a20 --- /dev/null +++ b/cmake/toolchains/mingw32.cmake @@ -0,0 +1,24 @@ +set(CMAKE_SYSTEM_NAME Windows) +set(TOOLCHAIN_PREFIX x86_64-w64-mingw32) +set(CMAKE_SYSTEM_PROCESSOR x86_64) + +# We'll need to use posix threads in order to use +# C++11 features, such as std::thread. +set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc-posix) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++-posix) +set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres) + +set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} /usr/lib/gcc/${TOOLCHAIN_PREFIX}/7.3-posix) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +# TODO: consider switching this to "ONLY". The issue with +# that is that all our libs should then be under +# CMAKE_FIND_ROOT_PATH and CMAKE_PREFIX_PATH would be ignored. +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) + +# Some functions (e.g. localtime_r) will not be available unless we set +# the following flag. +add_definitions(-D_POSIX=1) +add_definitions(-D_POSIX_C_SOURCE=1) +add_definitions(-D_POSIX_=1) +add_definitions(-D_POSIX_THREADS=1) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e3c70e3f27af..5f370b8dbbbe 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,14 +29,21 @@ if(LINUX) add_definitions("-D_GNU_SOURCE") endif() -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wtype-limits -Wignored-qualifiers -Winit-self") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith -Werror=format-security -fno-strict-aliasing -fsigned-char") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing -fsigned-char") + +if(NOT MSVC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wtype-limits -Wignored-qualifiers") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith -Werror=format-security") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winit-self -Wno-unknown-pragmas") + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-ignored-qualifiers") +endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth-1024") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-ignored-qualifiers") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unknown-pragmas") + CHECK_CXX_COMPILER_FLAG("-Wpessimizing-move" COMPILER_SUPPORTS_PESSIMIZING_MOVE) if(COMPILER_SUPPORTS_PESSIMIZING_MOVE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpessimizing-move") @@ -46,7 +53,12 @@ if(COMPILER_SUPPORTS_REDUNDANT_MOVE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wredundant-move") endif() if(CMAKE_CXX_COMPILER_ID STREQUAL GNU) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -rdynamic") + if(MINGW) + # The MINGW headers are missing some "const" qualifiers. + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fpermissive") + else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -rdynamic") + endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wstrict-null-sentinel -Woverloaded-virtual") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-new-ttp-matching") # cmake does not add '-pie' for executables even if @@ -122,10 +134,14 @@ if(HAVE_INTEL) set(object_format "elf64") endif() set(CMAKE_ASM_FLAGS "-f ${object_format}") - include(CheckYasm) - check_yasm_support(${object_format} - HAVE_GOOD_YASM_ELF64 - HAVE_BETTER_YASM_ELF64) + if(NOT WIN32) + # The native tools might be located even when cross compiling, which + # might not work in this case (especially when targeting Windows). + include(CheckYasm) + check_yasm_support(${object_format} + HAVE_GOOD_YASM_ELF64 + HAVE_BETTER_YASM_ELF64) + endif() endif() @@ -391,6 +407,10 @@ if(WITH_DPDK) list(APPEND ceph_common_deps common_async_dpdk) endif() +if(WIN32) + list(APPEND ceph_common_deps ws2_32 mswsock) +endif() + if(WITH_BLUESTORE_PMEM OR WITH_RBD_RWL) if(WITH_SYSTEM_PMDK) if(WITH_BLUESTORE_PMEM) @@ -428,11 +448,18 @@ if(NOT APPLE AND NOT FREEBSD) PROPERTY LINK_FLAGS "-Wl,-Bsymbolic -Wl,-Bsymbolic-functions") endif() -install( - TARGETS ceph-common - LIBRARY - DESTINATION ${CEPH_INSTALL_PKGLIBDIR} - NAMELINK_SKIP) +if(MINGW) + install( + TARGETS ceph-common + RUNTIME + DESTINATION ${CEPH_INSTALL_PKGLIBDIR}) +else() + install( + TARGETS ceph-common + LIBRARY + DESTINATION ${CEPH_INSTALL_PKGLIBDIR} + NAMELINK_SKIP) +endif() if(${WITH_LTTNG}) add_subdirectory(tracing)