BoostScanDeps.cmake 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <rleigh@codelibre.net>
  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. file(GLOB_RECURSE _boost_mpi_python_headers
  38. RELATIVE "${includedir}"
  39. "${includedir}/boost/mpi/python/*")
  40. list(INSERT _boost_mpi_python_headers 0 "boost/mpi/python.hpp")
  41. file(GLOB_RECURSE _boost_python_numpy_headers
  42. RELATIVE "${includedir}"
  43. "${includedir}/boost/python/numpy/*")
  44. list(INSERT _boost_python_numpy_headers 0 "boost/python/numpy.hpp")
  45. # Special-case since it is part of mpi; look only in boost/mpi/python*
  46. if(component STREQUAL "mpi_python")
  47. set(_boost_DEPS "python")
  48. set(library_component TRUE)
  49. set(_boost_unprocessed_headers ${_boost_mpi_python_headers})
  50. # Special-case since it is part of python; look only in boost/python/numpy*
  51. elseif(component STREQUAL "numpy")
  52. set(_boost_DEPS "python")
  53. set(library_component TRUE)
  54. set(_boost_unprocessed_headers ${_boost_python_numpy_headers})
  55. # Special-case since it is a serialization variant; look in boost/serialization
  56. elseif(component STREQUAL "wserialization")
  57. set(library_component TRUE)
  58. file(GLOB_RECURSE _boost_unprocessed_headers
  59. RELATIVE "${includedir}"
  60. "${includedir}/boost/serialization/*")
  61. list(INSERT _boost_unprocessed_headers 0 "boost/serialization.hpp")
  62. # Not really a library in its own right, but treat it as one
  63. elseif(component STREQUAL "math")
  64. set(library_component TRUE)
  65. file(GLOB_RECURSE _boost_unprocessed_headers
  66. RELATIVE "${includedir}"
  67. "${includedir}/boost/math/*")
  68. list(INSERT _boost_unprocessed_headers 0 "boost/math.hpp")
  69. # Single test header
  70. elseif(component STREQUAL "unit_test_framework")
  71. set(library_component TRUE)
  72. set(_boost_unprocessed_headers "${BOOST_DIR}/test/unit_test.hpp")
  73. # Single test header
  74. elseif(component STREQUAL "prg_exec_monitor")
  75. set(library_component TRUE)
  76. set(_boost_unprocessed_headers "${BOOST_DIR}/test/prg_exec_monitor.hpp")
  77. # Single test header
  78. elseif(component STREQUAL "test_exec_monitor")
  79. set(library_component TRUE)
  80. set(_boost_unprocessed_headers "${BOOST_DIR}/test/test_exec_monitor.hpp")
  81. else()
  82. # Default behaviour where header directory is the same as the library name.
  83. file(GLOB_RECURSE _boost_unprocessed_headers
  84. RELATIVE "${includedir}"
  85. "${includedir}/boost/${component}/*")
  86. list(INSERT _boost_unprocessed_headers 0 "boost/${component}.hpp")
  87. list(REMOVE_ITEM _boost_unprocessed_headers ${_boost_mpi_python_headers} ${_boost_python_numpy_headers})
  88. endif()
  89. while(_boost_unprocessed_headers)
  90. list(APPEND _boost_processed_headers ${_boost_unprocessed_headers})
  91. foreach(header ${_boost_unprocessed_headers})
  92. if(EXISTS "${includedir}/${header}")
  93. file(STRINGS "${includedir}/${header}" _boost_header_includes REGEX "^#[ \t]*include[ \t]*<boost/[^>][^>]*>")
  94. # The optional whitespace before "#" is intentional
  95. # (boost/serialization/config.hpp bug).
  96. file(STRINGS "${includedir}/${header}" _boost_header_deps REGEX "^[ \t]*#[ \t]*define[ \t][ \t]*BOOST_LIB_NAME[ \t][ \t]*boost_")
  97. foreach(line ${_boost_header_includes})
  98. string(REGEX REPLACE "^#[ \t]*include[ \t]*<(boost/[^>][^>]*)>.*" "\\1" _boost_header_match "${line}")
  99. list(FIND _boost_processed_headers "${_boost_header_match}" _boost_header_processed)
  100. list(FIND _boost_new_headers "${_boost_header_match}" _boost_header_new)
  101. if (_boost_header_processed EQUAL -1 AND _boost_header_new EQUAL -1)
  102. list(APPEND _boost_new_headers ${_boost_header_match})
  103. endif()
  104. endforeach()
  105. foreach(line ${_boost_header_deps})
  106. string(REGEX REPLACE "^[ \t]*#[ \t]*define[ \t][ \t]*BOOST_LIB_NAME[ \t][ \t]*boost_([^ \t][^ \t]*).*" "\\1" _boost_component_match "${line}")
  107. string(REPLACE "python3" "python" _boost_component_match "${_boost_component_match}")
  108. string(REPLACE "numpy3" "numpy" _boost_component_match "${_boost_component_match}")
  109. list(FIND _boost_DEPS "${_boost_component_match}" _boost_dep_found)
  110. if(_boost_component_match STREQUAL "bzip2" OR
  111. _boost_component_match STREQUAL "zlib")
  112. # These components may or may not be required; not
  113. # possible to tell without knowing where and when
  114. # BOOST_BZIP2_BINARY and BOOST_ZLIB_BINARY are defined.
  115. # If building against an external zlib or bzip2, this is
  116. # undesirable.
  117. continue()
  118. endif()
  119. if(component STREQUAL "mpi" AND
  120. (_boost_component_match STREQUAL "mpi_python" OR
  121. _boost_component_match STREQUAL "python"))
  122. # Optional python dependency; skip to avoid making it a
  123. # hard dependency (handle as special-case for mpi_python).
  124. continue()
  125. endif()
  126. if(component STREQUAL "python" AND
  127. boost_component_match STREQUAL "numpy")
  128. # Optional python dependency; skip to avoid making it a
  129. # hard dependency (handle as special-case for numpy).
  130. continue()
  131. endif()
  132. if (_boost_dep_found EQUAL -1 AND
  133. NOT "${_boost_component_match}" STREQUAL "${component}")
  134. list(APPEND _boost_DEPS "${_boost_component_match}")
  135. endif()
  136. if("${_boost_component_match}" STREQUAL "${component}")
  137. set(library_component TRUE)
  138. endif()
  139. endforeach()
  140. endif()
  141. endforeach()
  142. set(_boost_unprocessed_headers ${_boost_new_headers})
  143. unset(_boost_new_headers)
  144. endwhile()
  145. # message(STATUS "Unfiltered dependencies for Boost::${component}: ${_boost_DEPS}")
  146. if(NOT library_component)
  147. unset(_boost_DEPS)
  148. endif()
  149. set(${_ret_libs} ${_boost_DEPS} PARENT_SCOPE)
  150. #string(REGEX REPLACE ";" " " _boost_DEPS_STRING "${_boost_DEPS}")
  151. #if (NOT _boost_DEPS_STRING)
  152. # set(_boost_DEPS_STRING "(none)")
  153. #endif()
  154. #message(STATUS "Dependencies for Boost::${component}: ${_boost_DEPS_STRING}")
  155. endfunction()
  156. message(STATUS "Scanning ${BOOST_DIR}")
  157. # List of all directories and files
  158. file(GLOB boost_contents RELATIVE "${BOOST_DIR}/boost" "${BOOST_DIR}/boost/*")
  159. # Components as directories
  160. foreach(component ${boost_contents})
  161. if(IS_DIRECTORY "${BOOST_DIR}/boost/${component}")
  162. list(APPEND boost_components "${component}")
  163. endif()
  164. endforeach()
  165. # The following components are not top-level directories, so require
  166. # special-casing:
  167. # Special-case mpi_python, since it's a part of mpi
  168. if(IS_DIRECTORY "${BOOST_DIR}/boost/mpi" AND
  169. IS_DIRECTORY "${BOOST_DIR}/boost/python")
  170. list(APPEND boost_components "mpi_python")
  171. endif()
  172. # Special-case numpy, since it's a part of python
  173. if(IS_DIRECTORY "${BOOST_DIR}/boost/python" AND
  174. IS_DIRECTORY "${BOOST_DIR}/boost/python/numpy")
  175. list(APPEND boost_components "numpy")
  176. endif()
  177. # Special-case wserialization, which is a variant of serialization
  178. if(IS_DIRECTORY "${BOOST_DIR}/boost/serialization")
  179. list(APPEND boost_components "wserialization")
  180. endif()
  181. # Special-case math* since there are six libraries, but no actual math
  182. # library component. Handle specially when scanning above.
  183. #
  184. # Special-case separate test libraries, which are all part of test
  185. if(EXISTS "${BOOST_DIR}/test/unit_test.hpp")
  186. list(APPEND boost_components "unit_test_framework")
  187. endif()
  188. if(EXISTS "${BOOST_DIR}/test/prg_exec_monitor.hpp")
  189. list(APPEND boost_components "prg_exec_monitor")
  190. endif()
  191. if(EXISTS "${BOOST_DIR}/test/test_exec_monitor.hpp")
  192. list(APPEND boost_components "test_exec_monitor")
  193. endif()
  194. if(boost_components)
  195. list(SORT boost_components)
  196. endif()
  197. # Process each component defined above
  198. foreach(component ${boost_components})
  199. string(TOUPPER ${component} UPPERCOMPONENT)
  200. _Boost_FIND_COMPONENT_DEPENDENCIES("${component}" "${BOOST_DIR}"
  201. _Boost_${UPPERCOMPONENT}_LIBRARY_DEPENDENCIES)
  202. endforeach()
  203. # Output results
  204. foreach(component ${boost_components})
  205. string(TOUPPER ${component} UPPERCOMPONENT)
  206. if(_Boost_${UPPERCOMPONENT}_LIBRARY_DEPENDENCIES)
  207. string(REGEX REPLACE ";" " " _boost_DEPS_STRING "${_Boost_${UPPERCOMPONENT}_LIBRARY_DEPENDENCIES}")
  208. message(STATUS "set(_Boost_${UPPERCOMPONENT}_DEPENDENCIES ${_boost_DEPS_STRING})")
  209. endif()
  210. endforeach()