[PATCH 2/5] cmake: Use target export feature
Bernhard Sessler
bernhard.sessler at corscience.de
Mon Dec 16 02:48:31 PST 2013
This eliminates the need for a "Findcppunit.cmake" script in projects
needing the cppunit library. The project configuration files are
installed along with the library itself and provide information about
where to find the library and how to use it.
A prominent example of this method are the Qt5 libraries, which are
shipping CMake project configuration files by default.
Signed-off-by: Bernhard Sessler <bernhard.sessler at corscience.de>
---
CMakeLists.txt | 5 ++++-
cmake/ExportTargets.cmake | 40 +++++++++++++++++++++++++++++++++++++
cppunitConfig.cmake.in | 46 +++++++++++++++++++++++++++++++++++++++++++
cppunitConfigVersion.cmake.in | 29 +++++++++++++++++++++++++++
src/cppunit/CMakeLists.txt | 1 +
5 files changed, 120 insertions(+), 1 deletion(-)
create mode 100644 cmake/ExportTargets.cmake
create mode 100644 cppunitConfig.cmake.in
create mode 100644 cppunitConfigVersion.cmake.in
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c61228f..3b011b4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -32,7 +32,7 @@ include(Versioning)
# Build options
option(BUILD_SHARED_LIBS "Build cppunit as shared or static library" ON)
-if(BUILD_SHARED_LIBS)
+if(BUILD_SHARED_LIBS AND WIN32)
add_definitions(-DCPPUNIT_BUILD_DLL)
endif()
@@ -43,3 +43,6 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}/include")
# Subdirectories
add_subdirectory(include)
add_subdirectory(src)
+
+# Export targets
+include(ExportTargets)
diff --git a/cmake/ExportTargets.cmake b/cmake/ExportTargets.cmake
new file mode 100644
index 0000000..7282aab
--- /dev/null
+++ b/cmake/ExportTargets.cmake
@@ -0,0 +1,40 @@
+#=============================================================================
+# CppUnit - The C++ Unit Test Library
+# Copyright (c) 2013 - The Document Foundation
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#=============================================================================
+
+# Export targets
+if(WIN32 AND NOT CYGWIN)
+ set(INSTALL_CMAKE_DIR CMake)
+else()
+ set(INSTALL_CMAKE_DIR lib/cmake/cppunit)
+endif()
+
+file(RELATIVE_PATH CONF_REL_INCLUDE_DIR
+ "${CMAKE_INSTALL_PREFIX}/${INSTALL_CMAKE_DIR}"
+ "${CMAKE_INSTALL_PREFIX}/include")
+configure_file(cppunitConfig.cmake.in
+ "${PROJECT_BINARY_DIR}/cppunitConfig.cmake" @ONLY)
+configure_file(cppunitConfigVersion.cmake.in
+ "${PROJECT_BINARY_DIR}/cppunitConfigVersion.cmake" @ONLY)
+
+install(FILES
+ "${PROJECT_BINARY_DIR}/cppunitConfig.cmake"
+ "${PROJECT_BINARY_DIR}/cppunitConfigVersion.cmake"
+ DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev)
+
+install(EXPORT cppunitLibraryDepends DESTINATION "${INSTALL_CMAKE_DIR}")
diff --git a/cppunitConfig.cmake.in b/cppunitConfig.cmake.in
new file mode 100644
index 0000000..23e7cae
--- /dev/null
+++ b/cppunitConfig.cmake.in
@@ -0,0 +1,46 @@
+#=============================================================================
+# CppUnit - The C++ Unit Test Library
+# Copyright (c) 2013 - The Document Foundation
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#=============================================================================
+#
+# - CMake config file for cppunit
+# It defines the following variables
+# CPPUNIT_INCLUDE_DIRS - include directories for cppunit
+# CPPUNIT_LIBRARY - the cppunit base library
+# CPPUNIT_LIBRARIES - all of the cppunit libraries to link against
+# CPPUNIT_FOUND - the cppunit library has been found
+
+if(NOT TARGET cppunit AND NOT cppunit_BINARY_DIR)
+ # Compute paths
+ get_filename_component(CPPUNIT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
+ get_filename_component(CPPUNIT_INCLUDE_DIR
+ "${CPPUNIT_CMAKE_DIR}/@CONF_REL_INCLUDE_DIR@"
+ ABSOLUTE)
+
+ # Add the targets and dependencies
+ include("${CPPUNIT_CMAKE_DIR}/cppunitLibraryDepends.cmake")
+
+ get_target_property(_cppunit_lib_type cppunit TYPE)
+ if(${_cppunit_lib_type} EQUAL SHARED_LIBRARY AND WIN32)
+ add_definitions(-DCPPUNIT_DLL)
+ endif()
+
+ set(CPPUNIT_FOUND TRUE)
+ set(CPPUNIT_LIBRARY cppunit)
+ set(CPPUNIT_LIBRARIES ${CPPUNIT_LIBRARY})
+ message(STATUS "Found cppunit - version: @CPPUNIT_VERSION@")
+endif()
diff --git a/cppunitConfigVersion.cmake.in b/cppunitConfigVersion.cmake.in
new file mode 100644
index 0000000..b93119f
--- /dev/null
+++ b/cppunitConfigVersion.cmake.in
@@ -0,0 +1,29 @@
+#=============================================================================
+# CppUnit - The C++ Unit Test Library
+# Copyright (c) 2013 - The Document Foundation
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#=============================================================================
+
+set(PACKAGE_VERSION "@CPPUNIT_VERSION@")
+
+if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
+ set(PACKAGE_VERSION_COMPATIBLE FALSE)
+else()
+ set(PACKAGE_VERSION_COMPATIBLE TRUE)
+ if("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
+ set(PACKAGE_VERSION_EXACT TRUE)
+ endif()
+endif()
diff --git a/src/cppunit/CMakeLists.txt b/src/cppunit/CMakeLists.txt
index 1f0b058..0d6dd88 100644
--- a/src/cppunit/CMakeLists.txt
+++ b/src/cppunit/CMakeLists.txt
@@ -100,6 +100,7 @@ endif()
# Create install target
install(TARGETS cppunit
+ EXPORT cppunitLibraryDepends
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin)
--
1.8.3.2
More information about the LibreOffice
mailing list