1set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm)
2set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}")
3
4# First for users who use an installed LLVM, create the LLVMExports.cmake file.
5set(LLVM_EXPORTS_FILE ${llvm_cmake_builddir}/LLVMExports.cmake)
6get_property(LLVM_EXPORTS GLOBAL PROPERTY LLVM_EXPORTS)
7export(TARGETS ${LLVM_EXPORTS} FILE ${LLVM_EXPORTS_FILE})
8
9# Then for users who want to link against the LLVM build tree, provide the
10# normal targets and the build tree only targets.
11set(LLVM_BUILDTREEONLY_EXPORTS_FILE ${llvm_cmake_builddir}/LLVMBuildTreeOnlyTargets.cmake)
12get_property(LLVM_EXPORTS_BUILDTREE_ONLY GLOBAL PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY)
13export(TARGETS ${LLVM_EXPORTS_BUILDTREE_ONLY} FILE ${LLVM_BUILDTREEONLY_EXPORTS_FILE})
14
15get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
16
17foreach(lib ${LLVM_AVAILABLE_LIBS})
18  get_property(llvm_lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
19  set(all_llvm_lib_deps
20    "${all_llvm_lib_deps}\nset_property(GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib} ${llvm_lib_deps})")
21endforeach(lib)
22
23# CMake requires that all targets expressed as dependencies exist, so we can't
24# have intrinsics_gen in LLVM_COMMON_DEPENDS when it is written out, otherwise
25# projects building out of tree will have CMake errors. This only gets hit when
26# LLVM_ENABLE_MODULES=On. Eventually we should come up with a better solution to
27# this, but there is no easy solution.
28if(intrinsics_gen IN_LIST LLVM_COMMON_DEPENDS)
29  list(REMOVE_ITEM LLVM_COMMON_DEPENDS intrinsics_gen)
30endif()
31
32# Generate LLVMConfig.cmake for the build tree.
33set(LLVM_CONFIG_CODE "
34# LLVM_BUILD_* values available only from LLVM build tree.
35set(LLVM_BUILD_BINARY_DIR \"${LLVM_BINARY_DIR}\")
36set(LLVM_BUILD_LIBRARY_DIR \"${LLVM_LIBRARY_DIR}\")
37set(LLVM_BUILD_MAIN_INCLUDE_DIR \"${LLVM_MAIN_INCLUDE_DIR}\")
38set(LLVM_BUILD_MAIN_SRC_DIR \"${LLVM_MAIN_SRC_DIR}\")
39")
40set(LLVM_CONFIG_INCLUDE_DIRS
41  "${LLVM_MAIN_INCLUDE_DIR}"
42  "${LLVM_INCLUDE_DIR}"
43  )
44set(LLVM_CONFIG_LIBRARY_DIRS
45  "${LLVM_LIBRARY_DIR}"
46  )
47set(LLVM_CONFIG_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
48set(LLVM_CONFIG_BINARY_DIR "${LLVM_BINARY_DIR}")
49set(LLVM_CONFIG_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}")
50
51if (LLVM_LINK_LLVM_DYLIB)
52  set(LLVM_CONFIG_LINK_LLVM_DYLIB
53      "set(LLVM_LINK_LLVM_DYLIB ${LLVM_LINK_LLVM_DYLIB})")
54endif()
55
56# We need to use the full path to the LLVM Exports file to make sure we get the
57# one from the build tree. This is due to our cmake files being split between
58# this source dir and the binary dir in the build tree configuration and the
59# LLVM_CONFIG_CMAKE_DIR being the source directory. In contrast in the install
60# tree, both the generated LLVMExports.cmake file and the rest of the cmake
61# source files are put in the same cmake directory.
62set(LLVM_CONFIG_EXPORTS_FILE "${LLVM_EXPORTS_FILE}")
63set(LLVM_CONFIG_EXPORTS "${LLVM_EXPORTS};${LLVM_EXPORTS_BUILDTREE_ONLY}")
64set(llvm_config_include_buildtree_only_exports
65"include(\"${LLVM_BUILDTREEONLY_EXPORTS_FILE}\")")
66configure_file(
67  LLVMConfig.cmake.in
68  ${llvm_cmake_builddir}/LLVMConfig.cmake
69  @ONLY)
70set(llvm_config_include_buildtree_only_exports)
71
72# For compatibility with projects that include(LLVMConfig)
73# via CMAKE_MODULE_PATH, place API modules next to it.
74# This should be removed in the future.
75file(COPY .
76  DESTINATION ${llvm_cmake_builddir}
77  FILES_MATCHING PATTERN *.cmake
78  PATTERN .svn EXCLUDE
79  PATTERN CMakeFiles EXCLUDE
80  )
81
82# Generate LLVMConfig.cmake for the install tree.
83set(LLVM_CONFIG_CODE "
84# Compute the installation prefix from this LLVMConfig.cmake file location.
85get_filename_component(LLVM_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)")
86# Construct the proper number of get_filename_component(... PATH)
87# calls to compute the installation prefix.
88string(REGEX REPLACE "/" ";" _count "${LLVM_INSTALL_PACKAGE_DIR}")
89foreach(p ${_count})
90  set(LLVM_CONFIG_CODE "${LLVM_CONFIG_CODE}
91get_filename_component(LLVM_INSTALL_PREFIX \"\${LLVM_INSTALL_PREFIX}\" PATH)")
92endforeach(p)
93set(LLVM_CONFIG_INCLUDE_DIRS "\${LLVM_INSTALL_PREFIX}/include")
94set(LLVM_CONFIG_LIBRARY_DIRS "\${LLVM_INSTALL_PREFIX}/lib\${LLVM_LIBDIR_SUFFIX}")
95set(LLVM_CONFIG_CMAKE_DIR "\${LLVM_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}")
96set(LLVM_CONFIG_BINARY_DIR "\${LLVM_INSTALL_PREFIX}")
97set(LLVM_CONFIG_TOOLS_BINARY_DIR "\${LLVM_INSTALL_PREFIX}/bin")
98set(LLVM_CONFIG_EXPORTS_FILE "\${LLVM_CMAKE_DIR}/LLVMExports.cmake")
99set(LLVM_CONFIG_EXPORTS "${LLVM_EXPORTS}")
100configure_file(
101  LLVMConfig.cmake.in
102  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/LLVMConfig.cmake
103  @ONLY)
104
105# Generate LLVMConfigVersion.cmake for build and install tree.
106configure_file(
107  LLVMConfigVersion.cmake.in
108  ${llvm_cmake_builddir}/LLVMConfigVersion.cmake
109  @ONLY)
110
111if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
112  get_property(llvm_has_exports GLOBAL PROPERTY LLVM_HAS_EXPORTS)
113  if(llvm_has_exports)
114    install(EXPORT LLVMExports DESTINATION ${LLVM_INSTALL_PACKAGE_DIR}
115            COMPONENT cmake-exports)
116  endif()
117
118  install(FILES
119    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/LLVMConfig.cmake
120    ${llvm_cmake_builddir}/LLVMConfigVersion.cmake
121    LLVM-Config.cmake
122    DESTINATION ${LLVM_INSTALL_PACKAGE_DIR}
123    COMPONENT cmake-exports)
124
125  install(DIRECTORY .
126    DESTINATION ${LLVM_INSTALL_PACKAGE_DIR}
127    COMPONENT cmake-exports
128    FILES_MATCHING PATTERN *.cmake
129    PATTERN .svn EXCLUDE
130    PATTERN LLVMConfig.cmake EXCLUDE
131    PATTERN LLVMConfigVersion.cmake EXCLUDE
132    PATTERN LLVM-Config.cmake EXCLUDE
133    PATTERN GetHostTriple.cmake EXCLUDE)
134
135  if (NOT CMAKE_CONFIGURATION_TYPES)
136    # Add a dummy target so this can be used with LLVM_DISTRIBUTION_COMPONENTS
137    add_custom_target(cmake-exports)
138    add_llvm_install_targets(install-cmake-exports
139                             COMPONENT cmake-exports)
140  endif()
141endif()
142