WindowsInstall.cmake 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. include_guard(GLOBAL) # 防止二次导入
  2. # 安装 dll
  3. function(_wi_install_import_inline target run)
  4. if(WIN32 OR CYGWIN)
  5. get_target_property(imp ${target} IMPORTED_IMPLIB)
  6. get_target_property(imp_debug ${target} IMPORTED_IMPLIB_DEBUG)
  7. get_target_property(imp_release ${target} IMPORTED_IMPLIB_RELEASE)
  8. get_target_property(loc ${target} IMPORTED_LOCATION)
  9. get_target_property(loc_debug ${target} IMPORTED_LOCATION_DEBUG)
  10. get_target_property(loc_release ${target} IMPORTED_LOCATION_RELEASE)
  11. if(run)
  12. foreach(tmp ${imp} ${imp_debug} ${imp_release} ${loc} ${loc_debug} ${loc_release})
  13. if (tmp AND tmp MATCHES ".+dll")
  14. install(FILES ${tmp} DESTINATION ${run})
  15. endif()
  16. endforeach()
  17. endif()
  18. endif()
  19. endfunction()
  20. # 拷贝dll
  21. # 添加 target
  22. if (NOT TARGET deps-copy)
  23. add_custom_target(deps-copy COMMENT "Copy import target.")
  24. endif()
  25. macro(set_copy_command target a b)
  26. add_custom_command(TARGET ${target} POST_BUILD
  27. COMMAND "${CMAKE_COMMAND}" "-E" "copy_if_different" "${a}" "${b}"
  28. COMMENT "Copy ${a} to ${b}."
  29. WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
  30. endmacro()
  31. function(_wi_build_import_inline target run)
  32. if(WIN32 OR CYGWIN)
  33. get_target_property(imp ${target} IMPORTED_IMPLIB)
  34. get_target_property(imp_debug ${target} IMPORTED_IMPLIB_DEBUG)
  35. get_target_property(imp_release ${target} IMPORTED_IMPLIB_RELEASE)
  36. get_target_property(loc ${target} IMPORTED_LOCATION)
  37. get_target_property(loc_debug ${target} IMPORTED_LOCATION_DEBUG)
  38. get_target_property(loc_release ${target} IMPORTED_LOCATION_RELEASE)
  39. if(run)
  40. foreach(tmp ${imp} ${imp_debug} ${imp_release} ${loc} ${loc_debug} ${loc_release})
  41. if (tmp AND tmp MATCHES ".+dll")
  42. set_copy_command(deps-copy ${tmp} ${run})
  43. endif()
  44. endforeach()
  45. endif()
  46. endif()
  47. endfunction()
  48. function(wi_install_import)
  49. cmake_parse_arguments(ii "" "RUNTIME" "TARGETS" ${ARGN})
  50. if (NOT ii_RUNTIME)
  51. if (INSTALL_BINDIR)
  52. set(runtime ${INSTALL_BINDIR})
  53. else()
  54. set(runtime ${CMAKE_INSTALL_BINDIR})
  55. endif()
  56. else()
  57. set(runtime ${ii_RUNTIME})
  58. endif()
  59. set(targets ${ii_TARGETS})
  60. foreach(tgt IN LISTS targets) # 不需要 ${}
  61. _wi_install_import_inline(${tgt} ${runtime})
  62. _wi_build_import_inline(${tgt} ${runtime})
  63. endforeach()
  64. endfunction()