InstallHelpers.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Fix CMAKE_INSTALL_MANDIR on BSD before including GNUInstallDirs. #6771
  2. if(CMAKE_SYSTEM_NAME MATCHES "BSD" AND NOT DEFINED CMAKE_INSTALL_MANDIR)
  3. if(DEFINED ENV{MANPREFIX})
  4. set(CMAKE_INSTALL_MANDIR "$ENV{MANPREFIX}/man")
  5. elseif(CMAKE_INSTALL_PREFIX MATCHES "^/usr/local$")
  6. set(CMAKE_INSTALL_MANDIR "man")
  7. endif()
  8. endif()
  9. # For $CMAKE_INSTALL_{DATAROOT,MAN, ...}DIR
  10. include(GNUInstallDirs)
  11. # This will create any directories that need to be created in the destination
  12. # path with the typical owner, group, and user permissions--independent of the
  13. # umask setting.
  14. function(create_install_dir_with_perms)
  15. cmake_parse_arguments(_install_dir
  16. ""
  17. "DESTINATION"
  18. "DIRECTORY_PERMISSIONS"
  19. ${ARGN}
  20. )
  21. if(NOT _install_dir_DESTINATION)
  22. message(FATAL_ERROR "Must specify DESTINATION")
  23. endif()
  24. if(NOT _install_dir_DIRECTORY_PERMISSIONS)
  25. set(_install_dir_DIRECTORY_PERMISSIONS
  26. OWNER_READ OWNER_WRITE OWNER_EXECUTE
  27. GROUP_READ GROUP_EXECUTE
  28. WORLD_READ WORLD_EXECUTE)
  29. endif()
  30. install(CODE
  31. "
  32. set(_current_dir \"\${CMAKE_INSTALL_PREFIX}/${_install_dir_DESTINATION}\")
  33. set(_dir_permissions \"${_install_dir_DIRECTORY_PERMISSIONS}\")
  34. set(_parent_dirs)
  35. set(_prev_dir)
  36. # Explicitly prepend DESTDIR when using EXISTS.
  37. # file(INSTALL ...) implicitly respects DESTDIR, but EXISTS does not.
  38. while(NOT EXISTS \$ENV{DESTDIR}\${_current_dir} AND NOT \${_prev_dir} STREQUAL \${_current_dir})
  39. list(APPEND _parent_dirs \${_current_dir})
  40. set(_prev_dir \${_current_dir})
  41. get_filename_component(_current_dir \${_current_dir} DIRECTORY)
  42. endwhile()
  43. if(_parent_dirs)
  44. list(REVERSE _parent_dirs)
  45. endif()
  46. # Create any missing folders with the useful permissions. Note: this uses
  47. # a hidden option of CMake, but it's been shown to work with 2.8.11 thru
  48. # 3.0.2.
  49. foreach(_current_dir \${_parent_dirs})
  50. if(NOT IS_DIRECTORY \${_current_dir})
  51. # file(INSTALL ...) implicitly respects DESTDIR, so there's no need to
  52. # prepend it here.
  53. file(INSTALL DESTINATION \${_current_dir}
  54. TYPE DIRECTORY
  55. DIR_PERMISSIONS \${_dir_permissions}
  56. FILES \"\")
  57. endif()
  58. endforeach()
  59. ")
  60. endfunction()
  61. # This is to prevent the user's umask from corrupting the expected permissions
  62. # for the parent directories. We want to behave like the install tool here:
  63. # preserve what's there already, but create new things with useful permissions.
  64. function(install_helper)
  65. cmake_parse_arguments(_install_helper
  66. ""
  67. "DESTINATION;DIRECTORY;RENAME"
  68. "FILES;PROGRAMS;TARGETS;DIRECTORY_PERMISSIONS;FILE_PERMISSIONS"
  69. ${ARGN}
  70. )
  71. if(NOT _install_helper_DESTINATION AND NOT _install_helper_TARGETS)
  72. message(FATAL_ERROR "Must specify the DESTINATION path")
  73. endif()
  74. if(NOT _install_helper_FILES AND NOT _install_helper_DIRECTORY AND
  75. NOT _install_helper_PROGRAMS AND NOT _install_helper_TARGETS)
  76. message(FATAL_ERROR "Must specify FILES, PROGRAMS, TARGETS, or a DIRECTORY to install")
  77. endif()
  78. if(NOT _install_helper_DIRECTORY_PERMISSIONS)
  79. set(_install_helper_DIRECTORY_PERMISSIONS
  80. OWNER_READ OWNER_WRITE OWNER_EXECUTE
  81. GROUP_READ GROUP_EXECUTE
  82. WORLD_READ WORLD_EXECUTE)
  83. endif()
  84. if(NOT _install_helper_FILE_PERMISSIONS)
  85. set(_install_helper_FILE_PERMISSIONS
  86. OWNER_READ OWNER_WRITE
  87. GROUP_READ
  88. WORLD_READ)
  89. endif()
  90. if(NOT _install_helper_PROGRAM_PERMISSIONS)
  91. set(_install_helper_PROGRAM_PERMISSIONS
  92. OWNER_READ OWNER_WRITE OWNER_EXECUTE
  93. GROUP_READ GROUP_EXECUTE
  94. WORLD_READ WORLD_EXECUTE)
  95. endif()
  96. if(_install_helper_RENAME)
  97. set(RENAME RENAME ${_install_helper_RENAME})
  98. endif()
  99. if(_install_helper_TARGETS)
  100. set(_install_helper_DESTINATION "")
  101. endif()
  102. if(_install_helper_TARGETS)
  103. # Ensure the bin area exists with the correct permissions.
  104. create_install_dir_with_perms(DESTINATION ${CMAKE_INSTALL_BINDIR})
  105. install(
  106. TARGETS ${_install_helper_TARGETS}
  107. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  108. else()
  109. create_install_dir_with_perms(
  110. DESTINATION ${_install_helper_DESTINATION}
  111. DIRECTORY_PERMISSIONS ${_install_helper_DIRECTORY_PERMISSIONS})
  112. endif()
  113. if(_install_helper_DIRECTORY)
  114. install(
  115. DIRECTORY ${_install_helper_DIRECTORY}
  116. DESTINATION ${_install_helper_DESTINATION}
  117. DIRECTORY_PERMISSIONS ${_install_helper_DIRECTORY_PERMISSIONS}
  118. FILE_PERMISSIONS ${_install_helper_FILE_PERMISSIONS})
  119. endif()
  120. if(_install_helper_FILES)
  121. install(
  122. FILES ${_install_helper_FILES}
  123. DESTINATION ${_install_helper_DESTINATION}
  124. PERMISSIONS ${_install_helper_FILE_PERMISSIONS}
  125. ${RENAME})
  126. endif()
  127. if(_install_helper_PROGRAMS)
  128. install(
  129. PROGRAMS ${_install_helper_PROGRAMS}
  130. DESTINATION ${_install_helper_DESTINATION}
  131. PERMISSIONS ${_install_helper_PROGRAM_PERMISSIONS}
  132. ${RENAME})
  133. endif()
  134. endfunction()
  135. # Without CONFIGURE_DEPENDS globbing reuses cached file tree on rebuild.
  136. # For example it will ignore new files.
  137. # CONFIGURE_DEPENDS was introduced in 3.12
  138. function(glob_wrapper outvar)
  139. if(${CMAKE_VERSION} VERSION_LESS 3.12)
  140. file(GLOB ${outvar} ${ARGN})
  141. else()
  142. file(GLOB ${outvar} CONFIGURE_DEPENDS ${ARGN})
  143. endif()
  144. set(${outvar} ${${outvar}} PARENT_SCOPE)
  145. endfunction()