Install.cmake 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # InstallSharedLibrary should be called once for each shared library in the libs directory.
  2. # This function installs the shared library in the correct places for each platform.
  3. #
  4. # If INSTALL_DESTINATIONS is not empty, it will install to each of the destinations in the list.
  5. # If INSTALL_DESTINATIONS is empty, this function does nothing.
  6. #
  7. # Arguments:
  8. # TARGET The name of the library target
  9. function(InstallSharedLibrary)
  10. if(NOT INSTALL_DESTINATIONS)
  11. return()
  12. endif()
  13. cmake_parse_arguments(
  14. INSTALL_SHARED_LIB
  15. ""
  16. "TARGET"
  17. ""
  18. ${ARGN}
  19. )
  20. # For readability
  21. set(shared_lib_target "${INSTALL_SHARED_LIB_TARGET}")
  22. message(STATUS "Install shared library: ${shared_lib_target}")
  23. foreach (destination ${INSTALL_DESTINATIONS})
  24. if(UNIX AND NOT APPLE)
  25. set(destination ${LINUX_INSTALL_SHARED_DESTINATION})
  26. endif()
  27. _InstallSharedTarget(
  28. TARGET ${shared_lib_target}
  29. DEST_PATH ${destination}
  30. )
  31. endforeach ()
  32. endfunction()
  33. # InstallFiles should be called to install files that are not targets.
  34. # This function installs the files in the correct places for each platform.
  35. #
  36. # If INSTALL_DESTINATIONS is not empty, it will install to each of the destinations in the list.
  37. # If INSTALL_DESTINATIONS is empty, this function does nothing.
  38. #
  39. # Arguments:
  40. # FILES The name of the files to install
  41. function( InstallFiles )
  42. if( NOT INSTALL_DESTINATIONS )
  43. return()
  44. endif()
  45. cmake_parse_arguments(
  46. INSTALL_FILES
  47. ""
  48. ""
  49. "FILES"
  50. ${ARGN}
  51. )
  52. # For readability
  53. set( files "${INSTALL_FILES_FILES}" )
  54. if( NOT files )
  55. message( WARNING "InstallFiles: no files specified" )
  56. return()
  57. endif()
  58. message( STATUS "Install files: ${files} to ${INSTALL_DESTINATIONS}")
  59. foreach( destination ${INSTALL_DESTINATIONS} )
  60. _InstallFiles(
  61. FILES ${files}
  62. DEST_PATH ${destination}
  63. )
  64. endforeach()
  65. endfunction()
  66. # InstallPlugins should be called once for each application.
  67. # This function installs the plugin types that are requested in TYPES to the path specified by DEST_FOLDER.
  68. # If it is a gl plugin with shaders, install the shaders to SHADER_DEST_FOLDER.
  69. #
  70. # Arguments:
  71. # DEST_FOLDER The name of the directory to install the plugins in.
  72. # DEST_PATH Path to DEST_FOLDER - note that on Windows we will modify this depending on CONFIGURATIONS
  73. # SHADER_DEST_FOLDER The name of the directory to install the shaders for the plugins.
  74. # SHADER_DEST_PATH Path to SHADER_DEST_FOLDER - note that on Windows we will modify this depending on CONFIGURATIONS
  75. # TYPES Semicolon-separated list of plugin types to install (valid: gl, io, standard). If not specified, install all.
  76. function( InstallPlugins )
  77. cmake_parse_arguments(
  78. INSTALL_PLUGINS
  79. ""
  80. "DEST_FOLDER;DEST_PATH;SHADER_DEST_FOLDER;SHADER_DEST_PATH"
  81. "TYPES"
  82. ${ARGN}
  83. )
  84. # Check the types we need to install
  85. set( VALID_TYPES "gl" "io" "standard" )
  86. # If TYPES was not specified, use all of them
  87. if( NOT INSTALL_PLUGINS_TYPES )
  88. set( INSTALL_PLUGINS_TYPES "${VALID_TYPES}" )
  89. else()
  90. foreach( type ${INSTALL_PLUGINS_TYPES} )
  91. if( NOT "${type}" IN_LIST VALID_TYPES )
  92. # In cmake 3.12:
  93. # list( JOIN VALID_TYPES ", " VALID_TYPES_STR )
  94. string( REPLACE ";" ", " VALID_TYPES_STR "${VALID_TYPES}" )
  95. message( FATAL_ERROR "InstallPlugins: Did not find proper TYPES. Valid values are: ${VALID_TYPES_STR}" )
  96. endif()
  97. endforeach()
  98. endif()
  99. message( STATUS "Install plugins" )
  100. message( STATUS " Types: ${INSTALL_PLUGINS_TYPES}" )
  101. # Check our destination path is valid
  102. if( NOT INSTALL_PLUGINS_DEST_PATH )
  103. message( FATAL_ERROR "InstallPlugins: DEST_PATH not specified" )
  104. endif()
  105. message( STATUS " Destination: ${INSTALL_PLUGINS_DEST_PATH}/${INSTALL_PLUGINS_DEST_FOLDER}" )
  106. # If we have gl plugins, check that our shader destination folder is valid
  107. if( "gl" IN_LIST VALID_TYPES )
  108. if( NOT INSTALL_PLUGINS_SHADER_DEST_PATH )
  109. message( FATAL_ERROR "InstallPlugins: SHADER_DEST_PATH not specified" )
  110. endif()
  111. message( STATUS " Shader Destination: ${INSTALL_PLUGINS_SHADER_DEST_PATH}/${INSTALL_PLUGINS_SHADER_DEST_FOLDER}" )
  112. endif()
  113. # Make CloudCompare/ccViewer depend on the plugins
  114. # so that when building CloudCompare/ccViewer the plugins also get built
  115. # instead of waiting for the `install` target to be ran for the plugins to get built
  116. add_dependencies(${PROJECT_NAME} ${CC_PLUGIN_TARGET_LIST})
  117. # Install the requested plugins in the DEST_FOLDER
  118. foreach( plugin_target ${CC_PLUGIN_TARGET_LIST} )
  119. get_target_property( plugin_type ${plugin_target} PLUGIN_TYPE )
  120. if( "${plugin_type}" IN_LIST INSTALL_PLUGINS_TYPES )
  121. message( STATUS " Install ${plugin_target} (${plugin_type})" )
  122. _InstallSharedTarget(
  123. TARGET ${plugin_target}
  124. DEST_PATH ${INSTALL_PLUGINS_DEST_PATH}
  125. DEST_FOLDER ${INSTALL_PLUGINS_DEST_FOLDER}
  126. )
  127. if( "${plugin_type}" STREQUAL "gl" )
  128. get_target_property( SHADER_FOLDER_NAME ${plugin_target} SHADER_FOLDER_NAME )
  129. get_target_property( SHADER_FOLDER_PATH ${plugin_target} SHADER_FOLDER_PATH )
  130. if( EXISTS "${SHADER_FOLDER_PATH}" )
  131. message( STATUS " + shader: ${SHADER_FOLDER_NAME} (${SHADER_FOLDER_PATH})" )
  132. get_target_property( shader_files ${plugin_target} SOURCES )
  133. list( FILTER shader_files INCLUDE REGEX ".*\.vert|frag" )
  134. _InstallFiles(
  135. FILES ${shader_files}
  136. DEST_PATH ${INSTALL_PLUGINS_SHADER_DEST_PATH}
  137. DEST_FOLDER ${INSTALL_PLUGINS_SHADER_DEST_FOLDER}/${SHADER_FOLDER_NAME}
  138. )
  139. endif()
  140. endif()
  141. endif()
  142. endforeach()
  143. endfunction()
  144. # _InstallSharedTarget should only be called by one of the functions above.
  145. # It was factored out to provide cmake < 3.13 a way to install shared libs.
  146. #
  147. # Arguments:
  148. # DEST_FOLDER The name of the directory to install the shared lib in.
  149. # DEST_PATH Path to DEST_FOLDER - note that on Windows we will modify this depending on CONFIGURATIONS
  150. # TARGET The name of the shared lib target
  151. function( _InstallSharedTarget )
  152. cmake_parse_arguments(
  153. INSTALL_SHARED_TARGET
  154. ""
  155. "DEST_FOLDER;DEST_PATH;TARGET"
  156. ""
  157. ${ARGN}
  158. )
  159. # For readability
  160. set( shared_target "${INSTALL_SHARED_TARGET_TARGET}" )
  161. set( full_path "${INSTALL_SHARED_TARGET_DEST_PATH}/${INSTALL_SHARED_TARGET_DEST_FOLDER}" )
  162. # Before CMake 3.13, install(TARGETS) would only accept targets created in the same directory scope
  163. # This makes it difficult to work with submodules.
  164. # This can be cleaned up when we move to a minimum CMake of 3.13 or higher
  165. # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/2152
  166. if ( ${CMAKE_VERSION} VERSION_LESS "3.13.0" )
  167. # Basic hack: construct the name of the dynamic library ("target_shared_lib") and install using
  168. # install(FILES) instead of install(TARGETS)
  169. if ( APPLE OR UNIX )
  170. set( lib_prefix "lib" )
  171. endif()
  172. if ( CMAKE_BUILD_TYPE STREQUAL "Debug" )
  173. get_target_property( lib_postfix ${shared_target} DEBUG_POSTFIX)
  174. endif()
  175. get_target_property( target_bin_dir ${shared_target} BINARY_DIR )
  176. set( target_shared_lib "${target_bin_dir}/${lib_prefix}${shared_target}${lib_postfix}${CMAKE_SHARED_LIBRARY_SUFFIX}" )
  177. copy_files( "${target_shared_lib}" "${full_path}" 1 )
  178. else()
  179. if( WIN32 )
  180. if( NOT CMAKE_CONFIGURATION_TYPES )
  181. install(
  182. TARGETS ${shared_target}
  183. RUNTIME DESTINATION ${full_path}
  184. )
  185. else()
  186. install(
  187. TARGETS ${shared_target}
  188. CONFIGURATIONS Debug
  189. RUNTIME DESTINATION ${INSTALL_SHARED_TARGET_DEST_PATH}_debug/${INSTALL_SHARED_TARGET_DEST_FOLDER}
  190. )
  191. install(
  192. TARGETS ${shared_target}
  193. CONFIGURATIONS Release
  194. RUNTIME DESTINATION ${full_path}
  195. )
  196. install(
  197. TARGETS ${shared_target}
  198. CONFIGURATIONS RelWithDebInfo
  199. RUNTIME DESTINATION ${INSTALL_SHARED_TARGET_DEST_PATH}_withDebInfo/${INSTALL_SHARED_TARGET_DEST_FOLDER}
  200. )
  201. endif()
  202. else()
  203. install( TARGETS ${shared_target}
  204. LIBRARY DESTINATION ${full_path}
  205. COMPONENT Runtime
  206. )
  207. endif()
  208. endif()
  209. endfunction()
  210. # _InstallFiles should only be called by one of the functions above.
  211. #
  212. # Arguments:
  213. # DEST_FOLDER The name of the directory to install the files in.
  214. # DEST_PATH Path to DEST_FOLDER - note that on Windows we will modify this depending on CONFIGURATIONS
  215. # FILES The name of the files to install
  216. function( _InstallFiles )
  217. cmake_parse_arguments(
  218. INSTALL_FILES
  219. ""
  220. "DEST_FOLDER;DEST_PATH"
  221. "FILES"
  222. ${ARGN}
  223. )
  224. # For readability
  225. set( files "${INSTALL_FILES_FILES}" )
  226. set( full_path "${INSTALL_FILES_DEST_PATH}/${INSTALL_FILES_DEST_FOLDER}" )
  227. if( WIN32 )
  228. if( NOT CMAKE_CONFIGURATION_TYPES )
  229. install(
  230. FILES ${files}
  231. DESTINATION "${full_path}"
  232. )
  233. else()
  234. install(
  235. FILES ${files}
  236. CONFIGURATIONS Debug
  237. DESTINATION "${INSTALL_FILES_DEST_PATH}_debug/${INSTALL_FILES_DEST_FOLDER}"
  238. )
  239. install(
  240. FILES ${files}
  241. CONFIGURATIONS Release
  242. RUNTIME DESTINATION ${full_path}
  243. )
  244. install(
  245. FILES ${files}
  246. CONFIGURATIONS RelWithDebInfo
  247. RUNTIME DESTINATION "${INSTALL_FILES_DEST_PATH}_withDebInfo/${INSTALL_FILES_DEST_FOLDER}"
  248. )
  249. endif()
  250. else()
  251. install(
  252. FILES ${files}
  253. DESTINATION "${full_path}"
  254. )
  255. endif()
  256. endfunction()