r221411 - Make it easier to build against a pre-built Clang package with CMake

Reid Kleckner reid at kleckner.net
Wed Nov 5 15:14:59 PST 2014


Author: rnk
Date: Wed Nov  5 17:14:59 2014
New Revision: 221411

URL: http://llvm.org/viewvc/llvm-project?rev=221411&view=rev
Log:
Make it easier to build against a pre-built Clang package with CMake

Installing <prefix>/share/clang/cmake/ClangConfig.cmake makes CMake's
builtin find_package() utility work with Clang. This also allows
downstream consumers of Clang to statically link against libraries like
clangAST and have that pull in dependencies like clangBasic and
LLVMSupport.

See the CMake docs on packages:
http://www.cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html

Added:
    cfe/trunk/cmake/
    cfe/trunk/cmake/modules/
    cfe/trunk/cmake/modules/ClangConfig.cmake
Modified:
    cfe/trunk/CMakeLists.txt

Modified: cfe/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CMakeLists.txt?rev=221411&r1=221410&r2=221411&view=diff
==============================================================================
--- cfe/trunk/CMakeLists.txt (original)
+++ cfe/trunk/CMakeLists.txt Wed Nov  5 17:14:59 2014
@@ -337,6 +337,7 @@ macro(add_clang_library name)
         ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
         RUNTIME DESTINATION bin)
     endif()
+    set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
   else()
     # Add empty "phony" target
     add_custom_target(${name})
@@ -478,3 +479,25 @@ endif()
 
 set(CLANG_ORDER_FILE "" CACHE FILEPATH
   "Order file to use when compiling clang in order to improve startup time.")
+
+# Generate a list of CMake library targets so that other CMake projects can
+# link against them. LLVM calls its version of this file LLVMExports.cmake, but
+# the usual CMake convention seems to be ${Project}Targets.cmake.
+set(CLANG_INSTALL_PACKAGE_DIR share/clang/cmake)
+set(clang_cmake_builddir "${CMAKE_BINARY_DIR}/${CLANG_INSTALL_PACKAGE_DIR}")
+get_property(CLANG_EXPORTS GLOBAL PROPERTY CLANG_EXPORTS)
+export(TARGETS ${CLANG_EXPORTS} FILE ${clang_cmake_builddir}/ClangTargets.cmake)
+
+# Install a <prefix>/share/clang/cmake/ClangConfig.cmake file so that
+# find_package(Clang) works. Install the target list with it.
+install(FILES
+  ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
+  ${CLANG_BINARY_DIR}/share/clang/cmake/ClangTargets.cmake
+  DESTINATION share/clang/cmake)
+
+# Also copy ClangConfig.cmake to the build directory so that dependent projects
+# can build against a build directory of Clang more easily.
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake
+  ${CLANG_BINARY_DIR}/share/clang/cmake/ClangConfig.cmake
+  COPYONLY)

Added: cfe/trunk/cmake/modules/ClangConfig.cmake
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/cmake/modules/ClangConfig.cmake?rev=221411&view=auto
==============================================================================
--- cfe/trunk/cmake/modules/ClangConfig.cmake (added)
+++ cfe/trunk/cmake/modules/ClangConfig.cmake Wed Nov  5 17:14:59 2014
@@ -0,0 +1,8 @@
+# This file allows users to call find_package(Clang) and pick up our targets.
+
+# Clang doesn't have any CMake configuration settings yet because it mostly
+# uses LLVM's. When it does, we should move this file to ClangConfig.cmake.in
+# and call configure_file() on it.
+
+# Provide all our library targets to users.
+include("${CMAKE_CURRENT_LIST_DIR}/ClangTargets.cmake")





More information about the cfe-commits mailing list