Ver Fonte

feat: CFEP添加在构建时安装IMPORT-LIBRARY的功能

SongZihuan há 3 anos atrás
pai
commit
f3381c2760
2 ficheiros alterados com 75 adições e 0 exclusões
  1. 74 0
      cmake/CMakeFindExternalProject/WindowsInstall.cmake
  2. 1 0
      deps/deps.cmake

+ 74 - 0
cmake/CMakeFindExternalProject/WindowsInstall.cmake

@@ -102,6 +102,52 @@ function(_wi_copy_import_inline target run lib)
     endif()
 endfunction()
 
+# 添加 target
+if (NOT TARGET import_build)
+    add_custom_target(import_build ALL COMMENT "Copy import target.")
+endif()
+
+macro(set_copy_command target a b)
+    add_custom_command(TARGET ${target} POST_BUILD
+                       COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${a}" "${b}"
+                       COMMENT "Copy ${a}.")
+endmacro()
+
+function(_wi_build_import_inline target run lib)
+    if(WIN32)  # 只有windows需要执行该操作
+        if (CMAKE_BUILD_TYPE)
+            string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
+        else()
+            set(_build_type DEBUG)
+        endif()
+
+        get_target_property(imp ${target} IMPORTED_IMPLIB)
+        get_target_property(imp_t ${target} IMPORTED_IMPLIB_${_build_type})
+
+        get_target_property(loc ${target} IMPORTED_LOCATION)
+        get_target_property(loc_t ${target} IMPORTED_LOCATION_${_build_type})
+
+        if(lib)
+            if (imp)
+                set_copy_command(import_build ${imp} ${lib})
+            endif()
+
+            if (imp_t)
+                set_copy_command(import_build ${imp_t} ${lib})
+            endif()
+        endif()
+
+        if(run)
+            if (loc)
+                set_copy_command(import_build ${loc} ${run})
+            endif()
+            if (loc_t)
+                set_copy_command(import_build ${loc_t} ${run})
+            endif()
+        endif()
+    endif()
+endfunction()
+
 function(wi_copy_import)
     cmake_parse_arguments(ii "" "RUNTIME;LIBRARY" "TARGETS" ${ARGN})
     if (NOT ii_RUNTIME)
@@ -130,6 +176,34 @@ function(wi_copy_import)
     endforeach()
 endfunction()
 
+function(wi_build_import)
+    cmake_parse_arguments(ii "" "RUNTIME;LIBRARY" "TARGETS" ${ARGN})
+    if (NOT ii_RUNTIME)
+        if (INSTALL_BINDIR)
+            set(runtime ${INSTALL_BINDIR})
+        else()
+            set(runtime ${CMAKE_INSTALL_BINDIR})
+        endif()
+    else()
+        set(runtime ${ii_RUNTIME})
+    endif()
+
+    if (NOT ii_LIBRARY)
+        if (INSTALL_LIBRARY)
+            set(library ${INSTALL_LIBRARY})
+        else()
+            set(library ${CMAKE_INSTALL_LIBDIR})
+        endif()
+    else()
+        set(library ${ii_LIBRARY})
+    endif()
+
+    set(targets ${ii_TARGETS})
+    foreach(tgt IN LISTS targets)  # 不需要${}
+        _wi_build_import_inline(${tgt} ${runtime} ${library})
+    endforeach()
+endfunction()
+
 # 安装install的bin目录(检查.dll并安装到指定位置)
 function(wi_install_dll_bin)
     if(WIN32)

+ 1 - 0
deps/deps.cmake

@@ -29,6 +29,7 @@ if (WIN32 AND NOT CYGWIN)
     get_target_property(_dlfcn_include dlfcn-win32::dl INTERFACE_INCLUDE_DIRECTORIES)  # INTERFACE_INCLUDE_DIRECTORIES
     find_path(dlfcn_h_file NAMES dlfcn.h HINTS ${_dlfcn_include} DOC "The directory of dlfcn.h" REQUIRED NO_DEFAULT_PATH)
     wi_copy_import(TARGETS dlfcn-win32::dl)
+    wi_build_import(TARGETS dlfcn-win32::dl)
     wi_install_import(TARGETS dlfcn-win32::dl)
     install(DIRECTORY "${dlfcn_h_file}/" DESTINATION ${INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h")  # 把目录下的内容安装到include
     cfep_install(dlfcn-win32 PREFIX ${deps_install_dir})