InstallHelpers.cmake 4.8 KB

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