Dear Chandler Carruth,

I'm sending you a patch today, which adds a CMake package configuration file for libclang. So that others CMake based project can easily find libclang and link to it, as we are used to with CMake :

find_package(libclang 3.2 REQUIRED)
link_directories(${LIBCLANG_LIBRARY_DIRS})
include_directories(${LIBCLANG_INCLUDE_DIRS})
add_definitions(${LIBCLANG_DEFINITIONS})

I'm sending you this patch, because I first proposed a FindLibClang to the CMake project, but as Brad King from Kitware told me that it would be better for libclang to provide a package configuration file; I decided to drop my FindLibClang (See the pull request : https://github.com/Kitware/CMake/pull/34).

The patch simply adds a configuration and install step for libclang-config.cmake and libclang-config-version.cmake.

The libclang-config.cmake provides the LIBCLANG_INCLUDE_DIRS, LIBCLANG_LIBRARY_DIRS, LIBCLANG_DEFINITIONS variables.

I decided to hardcode LIBCLANG_DEFINITIONS to "-D__STDC_LIMIT_MACROS" "-D__STDC_CONSTANT_MACROS", instead of using something like list(APPEND @LLVM_DEFINITIONS@ ...), because I think it's the more correct decision since clang is a separate project based on llvm, but isn't fully part of it (i.e. Neither the same git repository nor a git submodule of llvm).

That's why I made the supposition that it's not set in stone that both llvm and clang will always share the same #defines in the future. Perhaps I misinterpreted something here, so don't hesitate to ask me to change this to the non hardcoded variant.

The other file libclang-config-version.cmake simply checks that the requested version is totally exact (major and minor) to the one of the installed library. It's currently checking for exact equality, because I didn't find anything in the documentation telling that the API stays backward compatible accross minor versions. Is it the case ?

On our side we are already using this patch to integrate libclang in our build. We use libclang as a c++ parser for a tool generating interfaces and bindings between different programming languages. Until now we were using gcc plugins to extract the information from c++ code, but it was such a hell of macros and of #pragma poison, that we wanted to change to something cleaner, and libclang has a so nice API, that we moved.

Thank you alot for the nice work done on llvm and clang, these projects are drastically changing the day-to-day work of many developers I know. :)

Cheers,
--
Damien Buhl
alias daminetreg
>From d41e007b4c2f2c9929e7acd594f44758e8e0f0aa Mon Sep 17 00:00:00 2001
From: "Damien Buhl (alias daminetreg)" <[email protected]>
Date: Thu, 21 Feb 2013 23:37:23 +0100
Subject: [PATCH] Adds a CMake package configuration file for libclang, so
 that library users from CMake based project can configure
 their dependency to libclang easily.

Signed-off-by: Damien Buhl (alias daminetreg) <[email protected]>
---
 tools/libclang/cmake/modules/CMakeLists.txt        |   17 ++++++++++
 .../cmake/modules/libclang-config-version.cmake.in |    9 ++++++
 .../cmake/modules/libclang-config.cmake.in         |   33 ++++++++++++++++++++
 3 files changed, 59 insertions(+)
 create mode 100644 tools/libclang/cmake/modules/CMakeLists.txt
 create mode 100644 tools/libclang/cmake/modules/libclang-config-version.cmake.in
 create mode 100644 tools/libclang/cmake/modules/libclang-config.cmake.in

diff --git a/tools/libclang/cmake/modules/CMakeLists.txt b/tools/libclang/cmake/modules/CMakeLists.txt
new file mode 100644
index 0000000..c733284
--- /dev/null
+++ b/tools/libclang/cmake/modules/CMakeLists.txt
@@ -0,0 +1,17 @@
+set(libclang_cmake_builddir "${CMAKE_BINARY_DIR}/share/clang/cmake")
+set(LLVM_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
+
+configure_file (
+    libclang-config.cmake.in
+    ${libclang_cmake_builddir}/libclang-config.cmake
+    @ONLY)
+
+configure_file (
+    libclang-config-version.cmake.in
+    ${libclang_cmake_builddir}/libclang-config-version.cmake
+    @ONLY)
+
+install(FILES
+    ${libclang_cmake_builddir}/libclang-config.cmake
+    ${libclang_cmake_builddir}/libclang-config-version.cmake
+    DESTINATION share/clang/cmake)
diff --git a/tools/libclang/cmake/modules/libclang-config-version.cmake.in b/tools/libclang/cmake/modules/libclang-config-version.cmake.in
new file mode 100644
index 0000000..50dffd3
--- /dev/null
+++ b/tools/libclang/cmake/modules/libclang-config-version.cmake.in
@@ -0,0 +1,9 @@
+set(PACKAGE_VERSION @LIBCLANG_LIBRARY_VERSION@)
+if("${PACKAGE_FIND_VERSION_MAJOR}" EQUAL @CLANG_VERSION_MAJOR@)
+    set(PACKAGE_VERSION_COMPATIBLE 1)
+
+    if("${PACKAGE_FIND_VERSION_MINOR}" EQUAL @CLANG_VERSION_MINOR@)
+        set(PACKAGE_VERSION_EXACT 1)
+    endif("${PACKAGE_FIND_VERSION_MINOR}" EQUAL @CLANG_VERSION_MINOR@)
+
+endif("${PACKAGE_FIND_VERSION_MAJOR}" EQUAL @CLANG_VERSION_MAJOR@)
diff --git a/tools/libclang/cmake/modules/libclang-config.cmake.in b/tools/libclang/cmake/modules/libclang-config.cmake.in
new file mode 100644
index 0000000..a704cb3
--- /dev/null
+++ b/tools/libclang/cmake/modules/libclang-config.cmake.in
@@ -0,0 +1,33 @@
+# libclang @LIBCLANG_LIBRARY_VERSION@ - CMake Package Configuration File
+#
+#   To use libclang in your software, simply add the following to your 
+#   CMakeLists.txt :
+#
+#       find_package(libclang @LIBCLANG_LIBRARY_VERSION@ REQUIRED)
+#       link_directories(${LIBCLANG_LIBRARY_DIRS}) 
+#       include_directories(${LIBCLANG_INCLUDE_DIRS})
+#       add_definitions(${LIBCLANG_DEFINITIONS})
+#
+#   Once this done you can simply add *clang* to you target_link_libraries, e.g:
+#       target_link_libraries(youTarget clang)
+#       
+#   In case you have installed an LLVM version which was compiled without 
+#   defining REQUIRES_RTTI=1, then you are probably getting problems with
+#   missing  typeid symbols (i.e. of the mangled form : _ZTIN5[...]E) when the 
+#   dynamic linker resolve symbols in your binary. 
+#
+#   To solve the issue, find a version of llvm and clang compiled with rtti 
+#   enabled, because this should be the case for released clang versions.
+#   (c.f. llvm/docs/Packaging.rst)
+#
+
+set(LIBCLANG_VERSION_MAJOR @CLANG_VERSION_MAJOR@)
+set(LIBCLANG_VERSION_MINOR @CLANG_VERSION_MINOR@)
+set(LIBCLANG_PACKAGE_VERSION @LIBCLANG_LIBRARY_VERSION@)
+
+set(LIBCLANG_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@")
+set(LIBCLANG_INCLUDE_DIRS ${LIBCLANG_INSTALL_PREFIX}/include)
+set(LIBCLANG_LIBRARY_DIRS ${LIBCLANG_INSTALL_PREFIX}/lib)
+set(LIBCLANG_DEFINITIONS "-D__STDC_LIMIT_MACROS" "-D__STDC_CONSTANT_MACROS")
+
+message(STATUS "libclang version: ${LIBCLANG_PACKAGE_VERSION}")
-- 
1.7.9.5




_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to