1
0

WindowsInstall.cmake 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 OR CYGWIN) # 只有windows需要执行该操作 (包括cygwin也需要处理.dll依赖的问题)
  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 OR CYGWIN)
  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. # 添加 target
  92. if (NOT TARGET import_build)
  93. add_custom_target(import_build ALL COMMENT "Copy import target.")
  94. endif()
  95. macro(set_copy_command target a b)
  96. add_custom_command(TARGET ${target} POST_BUILD
  97. COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${a}" "${b}"
  98. COMMENT "Copy ${a}.")
  99. endmacro()
  100. function(_wi_build_import_inline target run lib)
  101. if(WIN32 OR CYGWIN)
  102. if (CMAKE_BUILD_TYPE)
  103. string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
  104. else()
  105. set(_build_type DEBUG)
  106. endif()
  107. get_target_property(imp ${target} IMPORTED_IMPLIB)
  108. get_target_property(imp_t ${target} IMPORTED_IMPLIB_${_build_type})
  109. get_target_property(loc ${target} IMPORTED_LOCATION)
  110. get_target_property(loc_t ${target} IMPORTED_LOCATION_${_build_type})
  111. if(lib)
  112. if (imp)
  113. set_copy_command(import_build ${imp} ${lib})
  114. endif()
  115. if (imp_t)
  116. set_copy_command(import_build ${imp_t} ${lib})
  117. endif()
  118. endif()
  119. if(run)
  120. if (loc)
  121. set_copy_command(import_build ${loc} ${run})
  122. endif()
  123. if (loc_t)
  124. set_copy_command(import_build ${loc_t} ${run})
  125. endif()
  126. endif()
  127. endif()
  128. endfunction()
  129. function(wi_copy_import)
  130. cmake_parse_arguments(ii "" "RUNTIME;LIBRARY" "TARGETS" ${ARGN})
  131. if (NOT ii_RUNTIME)
  132. if (INSTALL_BINDIR)
  133. set(runtime ${INSTALL_BINDIR})
  134. else()
  135. set(runtime ${CMAKE_INSTALL_BINDIR})
  136. endif()
  137. else()
  138. set(runtime ${ii_RUNTIME})
  139. endif()
  140. if (NOT ii_LIBRARY)
  141. if (INSTALL_LIBRARY)
  142. set(library ${INSTALL_LIBRARY})
  143. else()
  144. set(library ${CMAKE_INSTALL_LIBDIR})
  145. endif()
  146. else()
  147. set(library ${ii_LIBRARY})
  148. endif()
  149. set(targets ${ii_TARGETS})
  150. foreach(tgt IN LISTS targets) # 不需要${}
  151. _wi_copy_import_inline(${tgt} ${runtime} ${library})
  152. endforeach()
  153. endfunction()
  154. function(wi_build_import)
  155. cmake_parse_arguments(ii "" "RUNTIME;LIBRARY" "TARGETS" ${ARGN})
  156. if (NOT ii_RUNTIME)
  157. if (INSTALL_BINDIR)
  158. set(runtime ${INSTALL_BINDIR})
  159. else()
  160. set(runtime ${CMAKE_INSTALL_BINDIR})
  161. endif()
  162. else()
  163. set(runtime ${ii_RUNTIME})
  164. endif()
  165. if (NOT ii_LIBRARY)
  166. if (INSTALL_LIBRARY)
  167. set(library ${INSTALL_LIBRARY})
  168. else()
  169. set(library ${CMAKE_INSTALL_LIBDIR})
  170. endif()
  171. else()
  172. set(library ${ii_LIBRARY})
  173. endif()
  174. set(targets ${ii_TARGETS})
  175. foreach(tgt IN LISTS targets) # 不需要${}
  176. _wi_build_import_inline(${tgt} ${runtime} ${library})
  177. endforeach()
  178. endfunction()
  179. # 安装install的bin目录(检查.dll并安装到指定位置)
  180. function(wi_install_dll_bin)
  181. if(WIN32 OR CYGWIN)
  182. cmake_parse_arguments(ii "" "RUNTIME" "DIRS" ${ARGN})
  183. if (NOT ii_RUNTIME)
  184. if (INSTALL_BINDIR)
  185. set(runtime ${INSTALL_BINDIR})
  186. else()
  187. set(runtime ${CMAKE_INSTALL_BINDIR})
  188. endif()
  189. else()
  190. set(runtime ${ii_RUNTIME})
  191. endif()
  192. set(dirs ${ii_DIRS})
  193. foreach(dir IN LISTS dirs)
  194. file(GLOB _dll # 遍历所有的.dll
  195. FOLLOW_SYMLINKS # 遍历link
  196. LIST_DIRECTORIES FALSE # 不记录列表
  197. CONFIGURE_DEPENDS
  198. "${dirs}/*.dll")
  199. if (_dll)
  200. install(FILES ${_dll} DESTINATION ${runtime})
  201. endif()
  202. endforeach()
  203. endif()
  204. endfunction()
  205. # 复制bin目录(检查.dll并复制到指定位置)
  206. function(wi_copy_dll_bin)
  207. if(WIN32 OR CYGWIN)
  208. cmake_parse_arguments(ii "" "RUNTIME" "DIRS" ${ARGN})
  209. if (NOT ii_RUNTIME)
  210. if (INSTALL_BINDIR)
  211. set(runtime ${INSTALL_BINDIR})
  212. else()
  213. set(runtime ${CMAKE_INSTALL_BINDIR})
  214. endif()
  215. else()
  216. set(runtime ${ii_RUNTIME})
  217. endif()
  218. set(dirs ${ii_DIRS})
  219. foreach(dir IN LISTS dirs)
  220. file(GLOB _dll # 遍历所有的.dll
  221. FOLLOW_SYMLINKS # 遍历link
  222. LIST_DIRECTORIES FALSE # 不记录列表
  223. CONFIGURE_DEPENDS
  224. "${dirs}/*.dll")
  225. if (_dll)
  226. file(COPY ${_dll} DESTINATION ${runtime} USE_SOURCE_PERMISSIONS)
  227. endif()
  228. endforeach()
  229. endif()
  230. endfunction()
  231. # 检查文件夹是否有exe, 若有则将其当作bin目录处理
  232. function(wi_install_dll_dir)
  233. if(WIN32 OR CYGWIN)
  234. cmake_parse_arguments(ii "" "RUNTIME" "DIRS" ${ARGN})
  235. if (NOT ii_RUNTIME)
  236. if (INSTALL_BINDIR)
  237. set(runtime ${INSTALL_BINDIR})
  238. else()
  239. set(runtime ${CMAKE_INSTALL_BINDIR})
  240. endif()
  241. else()
  242. set(runtime ${ii_RUNTIME})
  243. endif()
  244. set(dirs ${ii_DIRS})
  245. foreach(dir IN LISTS dirs)
  246. file(GLOB _exe
  247. LIST_DIRECTORIES FALSE # 不记录列表
  248. CONFIGURE_DEPENDS
  249. "${dir}/*.exe")
  250. if (_exe)
  251. wi_install_dll_bin(RUNTIME ${runtime} DIRS ${dir})
  252. endif()
  253. endforeach()
  254. endif()
  255. endfunction()
  256. # 检查文件夹是否有exe, 若有则将其当作bin目录处理
  257. function(wi_copy_dll_dir)
  258. if(WIN32 OR CYGWIN)
  259. cmake_parse_arguments(ii "" "RUNTIME" "DIRS" ${ARGN})
  260. if (NOT ii_RUNTIME)
  261. if (INSTALL_BINDIR)
  262. set(runtime ${INSTALL_BINDIR})
  263. else()
  264. set(runtime ${CMAKE_INSTALL_BINDIR})
  265. endif()
  266. else()
  267. set(runtime ${ii_RUNTIME})
  268. endif()
  269. set(dirs ${ii_DIRS})
  270. foreach(dir IN LISTS dirs)
  271. file(GLOB _exe
  272. LIST_DIRECTORIES FALSE # 不记录列表
  273. CONFIGURE_DEPENDS
  274. "${dir}/*.exe")
  275. if (_exe)
  276. wi_copy_dll_bin(RUNTIME ${runtime} DIRS ${dir})
  277. endif()
  278. endforeach()
  279. endif()
  280. endfunction()
  281. function(wi_find_cygwin1)
  282. if(CYGWIN)
  283. find_file(cygwin1_dll "cygwin1.dll" DOC "Find cygwin1.dll on windows.")
  284. if (NOT cygwin1_dll)
  285. message(FATAL_ERROR "The cygwin1.dll not found.")
  286. endif()
  287. add_library(CYGWIN::cygwin1 SHARED IMPORTED)
  288. set_target_properties(CYGWIN::cygwin1 PROPERTIES IMPORTED_LOCATION "${cygwin1_dll}")
  289. endif()
  290. endfunction()