WindowsInstall.cmake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. include_guard(GLOBAL) # 防止二次导入
  2. # 安装 dll
  3. function(_wi_install_import_inline target run lib)
  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(imp ${target} IMPORTED_IMPLIB)
  11. get_target_property(imp_t ${target} IMPORTED_IMPLIB_${_build_type})
  12. get_target_property(loc ${target} IMPORTED_LOCATION)
  13. get_target_property(loc_t ${target} IMPORTED_LOCATION_${_build_type})
  14. if(lib)
  15. if (imp)
  16. install(FILES ${imp} DESTINATION ${lib})
  17. endif()
  18. if (imp_t)
  19. install(FILES ${imp_t} DESTINATION ${lib})
  20. endif()
  21. endif()
  22. if(run)
  23. if (loc)
  24. install(FILES ${loc} DESTINATION ${run})
  25. endif()
  26. if (loc_t)
  27. install(FILES ${loc_t} DESTINATION ${run})
  28. endif()
  29. endif()
  30. endif()
  31. endfunction()
  32. # 拷贝dll
  33. # 添加 target
  34. if (NOT TARGET import_build)
  35. add_custom_target(import_build ALL COMMENT "Copy import target.")
  36. endif()
  37. macro(set_copy_command target a b)
  38. add_custom_command(TARGET ${target} POST_BUILD
  39. COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${a}" "${b}"
  40. COMMENT "Copy ${a}.")
  41. endmacro()
  42. function(_wi_build_import_inline target run lib)
  43. if(WIN32 OR CYGWIN)
  44. if (CMAKE_BUILD_TYPE)
  45. string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
  46. else()
  47. set(_build_type DEBUG)
  48. endif()
  49. get_target_property(imp ${target} IMPORTED_IMPLIB)
  50. get_target_property(imp_t ${target} IMPORTED_IMPLIB_${_build_type})
  51. get_target_property(loc ${target} IMPORTED_LOCATION)
  52. get_target_property(loc_t ${target} IMPORTED_LOCATION_${_build_type})
  53. if(lib)
  54. if (imp)
  55. set_copy_command(import_build ${imp} ${lib})
  56. endif()
  57. if (imp_t)
  58. set_copy_command(import_build ${imp_t} ${lib})
  59. endif()
  60. endif()
  61. if(run)
  62. if (loc)
  63. set_copy_command(import_build ${loc} ${run})
  64. endif()
  65. if (loc_t)
  66. set_copy_command(import_build ${loc_t} ${run})
  67. endif()
  68. endif()
  69. endif()
  70. endfunction()
  71. function(wi_install_import)
  72. cmake_parse_arguments(ii "" "RUNTIME;LIBRARY" "TARGETS" ${ARGN})
  73. if (NOT ii_RUNTIME)
  74. if (INSTALL_BINDIR)
  75. set(runtime ${INSTALL_BINDIR})
  76. else()
  77. set(runtime ${CMAKE_INSTALL_BINDIR})
  78. endif()
  79. else()
  80. set(runtime ${ii_RUNTIME})
  81. endif()
  82. if (NOT ii_LIBRARY)
  83. if (INSTALL_LIBRARY)
  84. set(library ${INSTALL_LIBRARY})
  85. else()
  86. set(library ${CMAKE_INSTALL_LIBDIR})
  87. endif()
  88. else()
  89. set(library ${ii_LIBRARY})
  90. endif()
  91. set(targets ${ii_TARGETS})
  92. foreach(tgt IN LISTS targets) # 不需要 ${}
  93. _wi_install_import_inline(${tgt} ${runtime} ${library})
  94. _wi_build_import_inline(${tgt} ${runtime} ${library})
  95. endforeach()
  96. endfunction()