BoostScanDeps.cmake 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # Scan the Boost headers and determine the library dependencies. Note
  4. # that this script only scans one Boost version at once; invoke once
  5. # for each Boost release. Note that this does require the headers for
  6. # a given component to match the library name, since this computes
  7. # inter-library dependencies. Library components for which this
  8. # assumption does not hold true and which have dependencies on other
  9. # Boost libraries will require special-casing. It also doesn't handle
  10. # private dependencies not described in the headers, for static
  11. # library dependencies--this is also a limitation of auto-linking, and
  12. # I'm unaware of any specific instances where this would be
  13. # problematic.
  14. #
  15. # Invoke in script mode, defining these variables:
  16. # BOOST_DIR - the root of the boost includes
  17. #
  18. # The script will process each directory under the root as a
  19. # "component". For each component, all the headers will be scanned to
  20. # determine the components it depends upon by following all the
  21. # possible includes from this component. This is to match the
  22. # behaviour of autolinking.
  23. # Written by Roger Leigh <[email protected]>
  24. # Determine header dependencies on libraries using the embedded dependency information.
  25. #
  26. # component - the component to check (uses all headers from boost/${component})
  27. # includedir - the path to the Boost headers
  28. # _ret_libs - list of library dependencies
  29. #
  30. function(_Boost_FIND_COMPONENT_DEPENDENCIES component includedir _ret_libs)
  31. # _boost_unprocessed_headers - list of headers requiring parsing
  32. # _boost_processed_headers - headers already parsed (or currently being parsed)
  33. # _boost_new_headers - new headers discovered for future processing
  34. set(library_component FALSE)
  35. # Start by finding all headers for the component; header
  36. # dependencies via #include will be solved by future passes
  37. # Special-case since it is part of mpi; look only in boost/mpi/python*
  38. if(component STREQUAL "mpi_python")
  39. set(_boost_DEPS "python")
  40. set(library_component TRUE)
  41. file(GLOB_RECURSE _boost_unprocessed_headers
  42. RELATIVE "${includedir}"
  43. "${includedir}/boost/mpi/python/*")
  44. list(INSERT _boost_unprocessed_headers 0 "${includedir}/boost/mpi/python.hpp")
  45. # Special-case since it is a serialization variant; look in boost/serialization
  46. elseif(component STREQUAL "wserialization")
  47. set(library_component TRUE)
  48. file(GLOB_RECURSE _boost_unprocessed_headers
  49. RELATIVE "${includedir}"
  50. "${includedir}/boost/serialization/*")
  51. list(INSERT _boost_unprocessed_headers 0 "${includedir}/boost/serialization.hpp")
  52. # Not really a library in its own right, but treat it as one
  53. elseif(component STREQUAL "math")
  54. set(library_component TRUE)
  55. file(GLOB_RECURSE _boost_unprocessed_headers
  56. RELATIVE "${includedir}"
  57. "${includedir}/boost/math/*")
  58. list(INSERT _boost_unprocessed_headers 0 "${includedir}/boost/math.hpp")
  59. # Single test header
  60. elseif(component STREQUAL "unit_test_framework")
  61. set(library_component TRUE)
  62. set(_boost_unprocessed_headers "${BOOST_DIR}/test/unit_test.hpp")
  63. # Single test header
  64. elseif(component STREQUAL "prg_exec_monitor")
  65. set(library_component TRUE)
  66. set(_boost_unprocessed_headers "${BOOST_DIR}/test/prg_exec_monitor.hpp")
  67. # Single test header
  68. elseif(component STREQUAL "test_exec_monitor")
  69. set(library_component TRUE)
  70. set(_boost_unprocessed_headers "${BOOST_DIR}/test/test_exec_monitor.hpp")
  71. else()
  72. # Default behaviour where header directory is the same as the library name.
  73. file(GLOB_RECURSE _boost_unprocessed_headers
  74. RELATIVE "${includedir}"
  75. "${includedir}/boost/${component}/*")
  76. list(INSERT _boost_unprocessed_headers 0 "${includedir}/boost/${component}.hpp")
  77. endif()
  78. while(_boost_unprocessed_headers)
  79. list(APPEND _boost_processed_headers ${_boost_unprocessed_headers})
  80. foreach(header ${_boost_unprocessed_headers})
  81. if(EXISTS "${includedir}/${header}")
  82. file(STRINGS "${includedir}/${header}" _boost_header_includes REGEX "^#[ \t]*include[ \t]*<boost/[^>][^>]*>")
  83. # The optional whitespace before "#" is intentional
  84. # (boost/serialization/config.hpp bug).
  85. file(STRINGS "${includedir}/${header}" _boost_header_deps REGEX "^[ \t]*#[ \t]*define[ \t][ \t]*BOOST_LIB_NAME[ \t][ \t]*boost_")
  86. foreach(line ${_boost_header_includes})
  87. string(REGEX REPLACE "^#[ \t]*include[ \t]*<(boost/[^>][^>]*)>.*" "\\1" _boost_header_match "${line}")
  88. list(FIND _boost_processed_headers "${_boost_header_match}" _boost_header_processed)
  89. list(FIND _boost_new_headers "${_boost_header_match}" _boost_header_new)
  90. if (_boost_header_processed EQUAL -1 AND _boost_header_new EQUAL -1)
  91. list(APPEND _boost_new_headers ${_boost_header_match})
  92. endif()
  93. endforeach()
  94. foreach(line ${_boost_header_deps})
  95. string(REGEX REPLACE "^[ \t]*#[ \t]*define[ \t][ \t]*BOOST_LIB_NAME[ \t][ \t]*boost_([^ \t][^ \t]*).*" "\\1" _boost_component_match "${line}")
  96. list(FIND _boost_DEPS "${_boost_component_match}" _boost_dep_found)
  97. if(_boost_component_match STREQUAL "bzip2" OR
  98. _boost_component_match STREQUAL "zlib")
  99. # These components may or may not be required; not
  100. # possible to tell without knowing where and when
  101. # BOOST_BZIP2_BINARY and BOOST_ZLIB_BINARY are defined.
  102. # If building against an external zlib or bzip2, this is
  103. # undesirable.
  104. continue()
  105. endif()
  106. if(component STREQUAL "mpi" AND
  107. (_boost_component_match STREQUAL "mpi_python" OR
  108. _boost_component_match STREQUAL "python"))
  109. # Optional python dependency; skip to avoid making it a
  110. # hard dependency (handle as special-case for mpi_python).
  111. continue()
  112. endif()
  113. if (_boost_dep_found EQUAL -1 AND
  114. NOT "${_boost_component_match}" STREQUAL "${component}")
  115. list(APPEND _boost_DEPS "${_boost_component_match}")
  116. endif()
  117. if("${_boost_component_match}" STREQUAL "${component}")
  118. set(library_component TRUE)
  119. endif()
  120. endforeach()
  121. endif()
  122. endforeach()
  123. set(_boost_unprocessed_headers ${_boost_new_headers})
  124. unset(_boost_new_headers)
  125. endwhile()
  126. # message(STATUS "Unfiltered dependencies for Boost::${component}: ${_boost_DEPS}")
  127. if(NOT library_component)
  128. unset(_boost_DEPS)
  129. endif()
  130. set(${_ret_libs} ${_boost_DEPS} PARENT_SCOPE)
  131. #string(REGEX REPLACE ";" " " _boost_DEPS_STRING "${_boost_DEPS}")
  132. #if (NOT _boost_DEPS_STRING)
  133. # set(_boost_DEPS_STRING "(none)")
  134. #endif()
  135. #message(STATUS "Dependencies for Boost::${component}: ${_boost_DEPS_STRING}")
  136. endfunction()
  137. message(STATUS "Scanning ${BOOST_DIR}")
  138. # List of all directories and files
  139. file(GLOB boost_contents RELATIVE "${BOOST_DIR}/boost" "${BOOST_DIR}/boost/*")
  140. # Components as directories
  141. foreach(component ${boost_contents})
  142. if(IS_DIRECTORY "${BOOST_DIR}/boost/${component}")
  143. list(APPEND boost_components "${component}")
  144. endif()
  145. endforeach()
  146. # The following components are not top-level directories, so require
  147. # special-casing:
  148. # Special-case mpi_python, since it's a part of mpi
  149. if(IS_DIRECTORY "${BOOST_DIR}/boost/mpi" AND
  150. IS_DIRECTORY "${BOOST_DIR}/boost/python")
  151. list(APPEND boost_components "mpi_python")
  152. endif()
  153. # Special-case wserialization, which is a variant of serialization
  154. if(IS_DIRECTORY "${BOOST_DIR}/boost/serialization")
  155. list(APPEND boost_components "wserialization")
  156. endif()
  157. # Special-case math* since there are six libraries, but no actual math
  158. # library component. Handle specially when scanning above.
  159. #
  160. # Special-case separate test libraries, which are all part of test
  161. if(EXISTS "${BOOST_DIR}/test/unit_test.hpp")
  162. list(APPEND boost_components "unit_test_framework")
  163. endif()
  164. if(EXISTS "${BOOST_DIR}/test/prg_exec_monitor.hpp")
  165. list(APPEND boost_components "prg_exec_monitor")
  166. endif()
  167. if(EXISTS "${BOOST_DIR}/test/test_exec_monitor.hpp")
  168. list(APPEND boost_components "test_exec_monitor")
  169. endif()
  170. if(boost_components)
  171. list(SORT boost_components)
  172. endif()
  173. # Process each component defined above
  174. foreach(component ${boost_components})
  175. string(TOUPPER ${component} UPPERCOMPONENT)
  176. _Boost_FIND_COMPONENT_DEPENDENCIES("${component}" "${BOOST_DIR}"
  177. _Boost_${UPPERCOMPONENT}_LIBRARY_DEPENDENCIES)
  178. endforeach()
  179. # Output results
  180. foreach(component ${boost_components})
  181. string(TOUPPER ${component} UPPERCOMPONENT)
  182. if(_Boost_${UPPERCOMPONENT}_LIBRARY_DEPENDENCIES)
  183. string(REGEX REPLACE ";" " " _boost_DEPS_STRING "${_Boost_${UPPERCOMPONENT}_LIBRARY_DEPENDENCIES}")
  184. message(STATUS "set(_Boost_${UPPERCOMPONENT}_DEPENDENCIES ${_boost_DEPS_STRING})")
  185. endif()
  186. endforeach()