2
0

deps_install.cmake 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. VERBATIM)
  31. endmacro()
  32. function(_wi_build_import_inline target run)
  33. if(WIN32 OR CYGWIN)
  34. get_target_property(imp ${target} IMPORTED_IMPLIB)
  35. get_target_property(imp_debug ${target} IMPORTED_IMPLIB_DEBUG)
  36. get_target_property(imp_release ${target} IMPORTED_IMPLIB_RELEASE)
  37. get_target_property(loc ${target} IMPORTED_LOCATION)
  38. get_target_property(loc_debug ${target} IMPORTED_LOCATION_DEBUG)
  39. get_target_property(loc_release ${target} IMPORTED_LOCATION_RELEASE)
  40. if(run)
  41. foreach(tmp ${imp} ${imp_debug} ${imp_release} ${loc} ${loc_debug} ${loc_release})
  42. if (tmp AND tmp MATCHES ".+dll")
  43. set_copy_command(deps-copy ${tmp} ${run})
  44. endif()
  45. endforeach()
  46. endif()
  47. endif()
  48. endfunction()
  49. function(wi_install_import)
  50. cmake_parse_arguments(ii "" "RUNTIME" "TARGETS" ${ARGN})
  51. if (NOT ii_RUNTIME)
  52. if (INSTALL_BINDIR)
  53. set(runtime ${INSTALL_BINDIR})
  54. else()
  55. set(runtime ${CMAKE_INSTALL_BINDIR})
  56. endif()
  57. else()
  58. set(runtime ${ii_RUNTIME})
  59. endif()
  60. set(targets ${ii_TARGETS})
  61. foreach(tgt IN LISTS targets) # 不需要 ${}
  62. _wi_install_import_inline(${tgt} ${runtime})
  63. _wi_build_import_inline(${tgt} ${runtime})
  64. endforeach()
  65. endfunction()