WindowsInstall.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #[[
  2. 文件名: WindowsInstall.cmake
  3. windows下安装程序
  4. 因为windows需要复制动态库到指定位置, 因此需要特殊的安装程序
  5. ]]
  6. # 找到导入库的.dll和.lib并添加install
  7. function(_wi_install_import_inline target run lib)
  8. if(WIN32) # 只有windows需要执行该操作
  9. if (CMAKE_BUILD_TYPE)
  10. string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
  11. else()
  12. set(_build_type DEBUG)
  13. endif()
  14. get_target_property(imp ${target} IMPORTED_IMPLIB)
  15. get_target_property(imp_t ${target} IMPORTED_IMPLIB_${_build_type})
  16. get_target_property(loc ${target} IMPORTED_LOCATION)
  17. get_target_property(loc_t ${target} IMPORTED_LOCATION_${_build_type})
  18. if(lib)
  19. if (imp)
  20. install(FILES ${imp} DESTINATION ${lib})
  21. endif()
  22. if (imp_t)
  23. install(FILES ${imp_t} DESTINATION ${lib})
  24. endif()
  25. endif()
  26. if(run)
  27. if (loc)
  28. install(FILES ${loc} DESTINATION ${run})
  29. endif()
  30. if (loc_t)
  31. install(FILES ${loc_t} DESTINATION ${run})
  32. endif()
  33. endif()
  34. endif()
  35. endfunction()
  36. function(wi_install_import)
  37. cmake_parse_arguments(ii "" "RUNTIME;LIBRARY" "TARGETS" ${ARGN})
  38. if (NOT ii_RUNTIME)
  39. if (INSTALL_BINDIR)
  40. set(runtime ${INSTALL_BINDIR})
  41. else()
  42. set(runtime ${CMAKE_INSTALL_BINDIR})
  43. endif()
  44. else()
  45. set(runtime ${ii_RUNTIME})
  46. endif()
  47. if (NOT ii_LIBRARY)
  48. if (INSTALL_LIBRARY)
  49. set(library ${INSTALL_LIBRARY})
  50. else()
  51. set(library ${CMAKE_INSTALL_LIBDIR})
  52. endif()
  53. else()
  54. set(library ${ii_LIBRARY})
  55. endif()
  56. set(targets ${ii_TARGETS})
  57. foreach(tgt IN LISTS targets) # 不需要 ${}
  58. _wi_install_import_inline(${tgt} ${runtime} ${library})
  59. endforeach()
  60. endfunction()
  61. # 找到导入库的.dll和.lib并复制到指定的目录
  62. function(_wi_copy_import_inline target run lib)
  63. if(WIN32) # 只有windows需要执行该操作
  64. if (CMAKE_BUILD_TYPE)
  65. string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
  66. else()
  67. set(_build_type DEBUG)
  68. endif()
  69. get_target_property(imp ${target} IMPORTED_IMPLIB)
  70. get_target_property(imp_t ${target} IMPORTED_IMPLIB_${_build_type})
  71. get_target_property(loc ${target} IMPORTED_LOCATION)
  72. get_target_property(loc_t ${target} IMPORTED_LOCATION_${_build_type})
  73. if(lib)
  74. if (imp)
  75. file(COPY ${imp} DESTINATION ${lib} USE_SOURCE_PERMISSIONS)
  76. endif()
  77. if (imp_t)
  78. file(COPY ${imp_t} DESTINATION ${lib} USE_SOURCE_PERMISSIONS)
  79. endif()
  80. endif()
  81. if(run)
  82. if (loc)
  83. file(COPY ${loc} DESTINATION ${run} USE_SOURCE_PERMISSIONS)
  84. endif()
  85. if (loc_t)
  86. file(COPY ${loc_t} DESTINATION ${run} USE_SOURCE_PERMISSIONS)
  87. endif()
  88. endif()
  89. endif()
  90. endfunction()
  91. function(wi_copy_import)
  92. cmake_parse_arguments(ii "" "RUNTIME;LIBRARY" "TARGETS" ${ARGN})
  93. if (NOT ii_RUNTIME)
  94. if (INSTALL_BINDIR)
  95. set(runtime ${INSTALL_BINDIR})
  96. else()
  97. set(runtime ${CMAKE_INSTALL_BINDIR})
  98. endif()
  99. else()
  100. set(runtime ${ii_RUNTIME})
  101. endif()
  102. if (NOT ii_LIBRARY)
  103. if (INSTALL_LIBRARY)
  104. set(library ${INSTALL_LIBRARY})
  105. else()
  106. set(library ${CMAKE_INSTALL_LIBDIR})
  107. endif()
  108. else()
  109. set(library ${ii_LIBRARY})
  110. endif()
  111. set(targets ${ii_TARGETS})
  112. foreach(tgt IN LISTS targets) # 不需要${}
  113. _wi_copy_import_inline(${tgt} ${runtime} ${library})
  114. endforeach()
  115. endfunction()
  116. # 安装install的bin目录(检查.dll并安装到指定位置)
  117. function(wi_install_dll_bin)
  118. if(WIN32)
  119. cmake_parse_arguments(ii "" "RUNTIME" "DIRS" ${ARGN})
  120. if (NOT ii_RUNTIME)
  121. if (INSTALL_BINDIR)
  122. set(runtime ${INSTALL_BINDIR})
  123. else()
  124. set(runtime ${CMAKE_INSTALL_BINDIR})
  125. endif()
  126. else()
  127. set(runtime ${ii_RUNTIME})
  128. endif()
  129. set(dirs ${ii_DIRS})
  130. foreach(dir IN LISTS dirs)
  131. file(GLOB_RECURSE _dll # 遍历所有的.dll
  132. FOLLOW_SYMLINKS # 遍历link
  133. LIST_DIRECTORIES FALSE # 不记录列表
  134. CONFIGURE_DEPENDS
  135. "${dirs}/*.dll")
  136. if (_dll)
  137. install(FILES ${_dll} DESTINATION ${runtime})
  138. endif()
  139. endforeach()
  140. endif()
  141. endfunction()
  142. # 复制bin目录(检查.dll并复制到指定位置)
  143. function(wi_copy_dll_bin)
  144. if(WIN32)
  145. cmake_parse_arguments(ii "" "RUNTIME" "DIRS" ${ARGN})
  146. if (NOT ii_RUNTIME)
  147. if (INSTALL_BINDIR)
  148. set(runtime ${INSTALL_BINDIR})
  149. else()
  150. set(runtime ${CMAKE_INSTALL_BINDIR})
  151. endif()
  152. else()
  153. set(runtime ${ii_RUNTIME})
  154. endif()
  155. set(dirs ${ii_DIRS})
  156. foreach(dir IN LISTS dirs)
  157. file(GLOB_RECURSE _dll # 遍历所有的.dll
  158. FOLLOW_SYMLINKS # 遍历link
  159. LIST_DIRECTORIES FALSE # 不记录列表
  160. CONFIGURE_DEPENDS
  161. "${dirs}/*.dll")
  162. if (_dll)
  163. file(COPY ${_dll} DESTINATION ${runtime} USE_SOURCE_PERMISSIONS)
  164. endif()
  165. endforeach()
  166. endif()
  167. endfunction()
  168. # 检查文件夹是否有exe, 若有则将其当作bin目录处理
  169. function(wi_install_dll_dir)
  170. if(WIN32)
  171. cmake_parse_arguments(ii "" "RUNTIME" "DIRS" ${ARGN})
  172. if (NOT ii_RUNTIME)
  173. if (INSTALL_BINDIR)
  174. set(runtime ${INSTALL_BINDIR})
  175. else()
  176. set(runtime ${CMAKE_INSTALL_BINDIR})
  177. endif()
  178. else()
  179. set(runtime ${ii_RUNTIME})
  180. endif()
  181. set(dirs ${ii_DIRS})
  182. foreach(dir IN LISTS dirs)
  183. file(GLOB _exe
  184. LIST_DIRECTORIES FALSE # 不记录列表
  185. CONFIGURE_DEPENDS
  186. "${dir}/*.exe")
  187. if (_exe)
  188. wi_install_dll_bin(RUNTIME ${runtime} DIRS ${dir})
  189. endif()
  190. endforeach()
  191. endif()
  192. endfunction()
  193. # 检查文件夹是否有exe, 若有则将其当作bin目录处理
  194. function(wi_copy_dll_dir)
  195. if(WIN32)
  196. cmake_parse_arguments(ii "" "RUNTIME" "DIRS" ${ARGN})
  197. if (NOT ii_RUNTIME)
  198. if (INSTALL_BINDIR)
  199. set(runtime ${INSTALL_BINDIR})
  200. else()
  201. set(runtime ${CMAKE_INSTALL_BINDIR})
  202. endif()
  203. else()
  204. set(runtime ${ii_RUNTIME})
  205. endif()
  206. set(dirs ${ii_DIRS})
  207. foreach(dir IN LISTS dirs)
  208. file(GLOB _exe
  209. LIST_DIRECTORIES FALSE # 不记录列表
  210. CONFIGURE_DEPENDS
  211. "${dir}/*.exe")
  212. if (_exe)
  213. wi_copy_dll_bin(RUNTIME ${runtime} DIRS ${dir})
  214. endif()
  215. endforeach()
  216. endif()
  217. endfunction()