FindAndImportCMakeProject.cmake 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # FIND_AND_IMPORT_CMAKE_PROJECT can be used by projects to find other
  2. # CMake projects.
  3. #
  4. # For example, the author of library FOO would include a file called
  5. # FOOConfig.cmake in the project's distribution. Usually such a file
  6. # would be configured and installed by FOO's CMake code into a
  7. # directory such as PREFIX/lib/foo.
  8. #
  9. # The author of application BAR that uses FOO would include the
  10. # following lines in BAR's CMake code:
  11. #
  12. # INCLUDE("${CMAKE_ROOT}/Modules/FindAndImportCMakeProject.cmake")
  13. # FIND_AND_IMPORT_CMAKE_PROJECT(FOO_DIR FOOConfig.cmake lib/foo)
  14. #
  15. # When CMake is run to build BAR, FOO_DIR will be the only
  16. # CMakeCache.txt entry that is needed for FOO. When FOO_DIR has been
  17. # set correctly, FOOConfig.cmake will automatically be included to
  18. # provide whatever settings are needed to use FOO.
  19. #
  20. MACRO(FIND_AND_IMPORT_CMAKE_PROJECT dir_var config_file rel_path)
  21. IF(NOT ${dir_var})
  22. # Get the system search path as a list.
  23. IF(UNIX)
  24. STRING(REGEX MATCHALL "[^:]+" ${dir_var}_SEARCH1 "$ENV{PATH}")
  25. ELSE(UNIX)
  26. STRING(REGEX REPLACE "\\\\" "/" ${dir_var}_SEARCH1 "$ENV{PATH}")
  27. ENDIF(UNIX)
  28. STRING(REGEX REPLACE "/;" ";" ${dir_var}_SEARCH2 "${${dir_var}_SEARCH1}")
  29. # Construct a set of paths relative to the system search path.
  30. SET(${dir_var}_SEARCH ${${dir_var}_SEARCH_PATH})
  31. FOREACH(dir ${${dir_var}_SEARCH2})
  32. SET(${dir_var}_SEARCH ${${dir_var}_SEARCH} "${dir}/../${rel_path}")
  33. ENDFOREACH(dir)
  34. # Look for the configuration file.
  35. FIND_PATH(${dir_var} ${config_file}
  36. # Search user paths and relative to system search path.
  37. ${${dir_var}_SEARCH}
  38. # Look in standard UNIX install locations.
  39. "/usr/local/${rel_path}"
  40. "/usr/${rel_path}"
  41. # Read from the CMakeSetup registry entries. It is likely that
  42. # the project will have been recently built.
  43. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild1]"
  44. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild2]"
  45. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild3]"
  46. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild4]"
  47. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild5]"
  48. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild6]"
  49. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild7]"
  50. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild8]"
  51. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild9]"
  52. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild10]"
  53. # Help the user find it if we cannot.
  54. DOC "The directory containing ${config_file}."
  55. )
  56. ENDIF(NOT ${dir_var})
  57. # If the configuration file was found, load it.
  58. IF(${dir_var})
  59. IF(EXISTS "${${dir_var}}/${config_file}")
  60. # We found the configuration file. Load the settings from it.
  61. INCLUDE("${${dir_var}}/${config_file}")
  62. ELSE(EXISTS "${${dir_var}}/${config_file}")
  63. IF(NOT ${dir_var}_NO_COMPLAIN)
  64. MESSAGE(SEND_ERROR "${dir_var} has been set to a directory "
  65. "that does not contain ${config_file}.")
  66. ENDIF(NOT ${dir_var}_NO_COMPLAIN)
  67. ENDIF(EXISTS "${${dir_var}}/${config_file}")
  68. ENDIF(${dir_var})
  69. ENDMACRO(FIND_AND_IMPORT_CMAKE_PROJECT)