From 3c9fd966f9697b1316746cca7d733e60c87a3da7 Mon Sep 17 00:00:00 2001 From: myd7349 Date: Mon, 28 Nov 2022 21:23:26 +0800 Subject: [PATCH] simplify --- CMakeLists.txt | 64 +++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea389b4..8d63c8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,23 +1,15 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12) -if(DEFINED PROJECT_NAME) - set(KCP_IS_TOP_PROJECT OFF) -else() - set(KCP_IS_TOP_PROJECT ON) -endif() - project(kcp LANGUAGES C) +include(CTest) +include(GNUInstallDirs) + cmake_policy(SET CMP0054 NEW) -option(KCP_BUILD_SHARED_LIBS "Build KCP as a shared lib" OFF) -option(KCP_BUILD_INSTALL "Build install target" ${KCP_IS_TOP_PROJECT}) -option(KCP_BUILD_TESTS "Build tests" ${KCP_IS_TOP_PROJECT}) - -if(KCP_BUILD_SHARED_LIBS) - if(WIN32) - set(exports_def_file "${CMAKE_CURRENT_BINARY_DIR}/exports.def") - set(exports_def_contents +if(BUILD_SHARED_LIBS AND WIN32) + set(exports_def_file "${CMAKE_CURRENT_BINARY_DIR}/exports.def") + set(exports_def_contents "EXPORTS ikcp_create ikcp_release @@ -38,39 +30,31 @@ if(KCP_BUILD_SHARED_LIBS) ikcp_getconv ") - file(WRITE "${exports_def_file}" "${exports_def_contents}") - add_library(kcp SHARED ikcp.h ikcp.c "${exports_def_file}") - else() - add_library(kcp SHARED ikcp.h ikcp.c) - endif() + file(WRITE "${exports_def_file}" "${exports_def_contents}") + add_library(kcp ikcp.c "${exports_def_file}") else() - add_library(kcp STATIC ikcp.h ikcp.c) + add_library(kcp ikcp.c) endif() -if(KCP_BUILD_INSTALL) - include(GNUInstallDirs) +install(FILES ikcp.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") - set_target_properties(kcp PROPERTIES PUBLIC_HEADER ikcp.h) +install(TARGETS kcp + EXPORT kcp-targets + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) - install(TARGETS kcp - EXPORT kcp-targets - PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} - ) +install(EXPORT kcp-targets + FILE kcp-config.cmake + NAMESPACE kcp:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kcp +) - install(EXPORT kcp-targets - FILE kcp-config.cmake - NAMESPACE kcp:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kcp - ) -endif() - -if(KCP_BUILD_TESTS) +if(BUILD_TESTING) enable_language(CXX) - + add_executable(kcp_test test.cpp) if(MSVC AND NOT (MSVC_VERSION LESS 1900)) target_compile_options(kcp_test PRIVATE /utf-8)