WindowsInstall.cmake 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. include_guard(GLOBAL) # 防止二次导入
  2. # 安装 dll
  3. function(_wi_install_import_inline target run)
  4. if(WIN32 OR CYGWIN) # 只有windows需要执行该操作 (包括cygwin也需要处理.dll依赖的问题)
  5. if (CMAKE_BUILD_TYPE)
  6. string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
  7. else()
  8. set(_build_type DEBUG)
  9. endif()
  10. get_target_property(loc ${target} IMPORTED_LOCATION)
  11. get_target_property(loc_t ${target} IMPORTED_LOCATION_${_build_type})
  12. if(run)
  13. if (loc)
  14. install(FILES ${loc} DESTINATION ${run})
  15. endif()
  16. if (loc_t)
  17. install(FILES ${loc_t} DESTINATION ${run})
  18. endif()
  19. endif()
  20. endif()
  21. endfunction()
  22. # 拷贝dll
  23. # 添加 target
  24. if (NOT TARGET deps-copy)
  25. add_custom_target(deps-copy COMMENT "Copy import target.")
  26. endif()
  27. macro(set_copy_command target a b)
  28. add_custom_command(TARGET ${target} POST_BUILD
  29. COMMAND "${CMAKE_COMMAND}" "-E" "copy_if_different" "${a}" "${b}"
  30. COMMENT "Copy ${a} to ${b}."
  31. WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
  32. endmacro()
  33. function(_wi_build_import_inline target run lib)
  34. if(WIN32 OR CYGWIN)
  35. if (CMAKE_BUILD_TYPE)
  36. string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
  37. else()
  38. set(_build_type DEBUG)
  39. endif()
  40. get_target_property(imp ${target} IMPORTED_IMPLIB)
  41. get_target_property(imp_t ${target} IMPORTED_IMPLIB_${_build_type})
  42. get_target_property(loc ${target} IMPORTED_LOCATION)
  43. get_target_property(loc_t ${target} IMPORTED_LOCATION_${_build_type})
  44. if(lib)
  45. if (imp AND imp MATCHES ".+dll")
  46. set_copy_command(deps-copy ${imp} ${lib})
  47. endif()
  48. if (imp_t AND imp_t MATCHES ".+dll")
  49. set_copy_command(deps-copy ${imp_t} ${lib})
  50. endif()
  51. endif()
  52. if(run)
  53. if (loc AND loc MATCHES ".+dll")
  54. set_copy_command(deps-copy ${loc} ${run})
  55. endif()
  56. if (loc_t AND loc_t MATCHES ".+dll")
  57. set_copy_command(deps-copy ${loc_t} ${run})
  58. endif()
  59. endif()
  60. endif()
  61. endfunction()
  62. function(wi_install_import)
  63. cmake_parse_arguments(ii "" "RUNTIME;LIBRARY" "TARGETS" ${ARGN})
  64. if (NOT ii_RUNTIME)
  65. if (INSTALL_BINDIR)
  66. set(runtime ${INSTALL_BINDIR})
  67. else()
  68. set(runtime ${CMAKE_INSTALL_BINDIR})
  69. endif()
  70. else()
  71. set(runtime ${ii_RUNTIME})
  72. endif()
  73. if (NOT ii_LIBRARY)
  74. if (INSTALL_LIBRARY)
  75. set(library ${INSTALL_LIBRARY})
  76. else()
  77. set(library ${CMAKE_INSTALL_LIBDIR})
  78. endif()
  79. else()
  80. set(library ${ii_LIBRARY})
  81. endif()
  82. set(targets ${ii_TARGETS})
  83. foreach(tgt IN LISTS targets) # 不需要 ${}
  84. _wi_install_import_inline(${tgt} ${runtime} ${library})
  85. _wi_build_import_inline(${tgt} ${runtime} ${library})
  86. endforeach()
  87. endfunction()