diff options
author | Aleix Pol <aleixpol@kde.org> | 2014-09-10 10:03:25 +0200 |
---|---|---|
committer | Aleix Pol <aleixpol@kde.org> | 2014-09-10 10:03:25 +0200 |
commit | f5f4d572ba0904a5c5c52a0de710112b953767ce (patch) | |
tree | 4a8aeaa9be5be80107db3cb8cfa33eee043d2468 | |
parent | a9e748d1b56470769450483baa5c16079ed99871 (diff) | |
download | extra-cmake-modules-f5f4d572ba0904a5c5c52a0de710112b953767ce.tar.gz extra-cmake-modules-f5f4d572ba0904a5c5c52a0de710112b953767ce.tar.bz2 |
Introduce ECMGeneratePkgConfigFile
A new module has been introduced to generate pkgconfig files from
cmake projects.
REVIEW: 119798
-rw-r--r-- | modules/ECMGeneratePkgConfigFile.cmake | 156 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/ECMGeneratePkgConfigFile/CMakeLists.txt | 6 | ||||
-rw-r--r-- | tests/ECMGeneratePkgConfigFile/KF5CoreAddons.pc | 6 | ||||
-rw-r--r-- | tests/ECMGeneratePkgConfigFile/run_test.cmake.config | 54 |
5 files changed, 223 insertions, 0 deletions
diff --git a/modules/ECMGeneratePkgConfigFile.cmake b/modules/ECMGeneratePkgConfigFile.cmake new file mode 100644 index 00000000..eb0e385d --- /dev/null +++ b/modules/ECMGeneratePkgConfigFile.cmake @@ -0,0 +1,156 @@ +#.rst: +# ECMGeneratePkgConfigFile +# ------------------ +# +# Generate a ``.pc`` file for the benefit of autotools-based projects. +# +# :: +# +# ecm_generate_pkgconfig_file(BASE_NAME <baseName> +# [LIB_NAME <libName>] +# [DEPS "<dep> [<dep> [...]]"] +# [FILENAME_VAR <filename_variable>] +# [INCLUDE_INSTALL_DIR <dir>] +# [LIB_INSTALL_DIR <dir>] +# [DEFINES -D<variable=value>...] +# [INSTALL]) +# +# BASE_NAME is the name of the module. It's the name projects will use to find +# the module. +# +# LIB_NAME is the name of the library that is being exported. If undefined, it +# will default to the BASE_NAME. That means the LIB_NAME will be set as the name +# field as well as the library to link to. +# +# FILENAME_VAR is specified with a variable name. This variable will receive the +# location of the generated file will be set, within the build directory. This +# way it can be used in case some processing is required. See also INSTALL. +# +# INCLUDE_INSTALL_DIR specifies where the includes will be installed. If it's not +# specified, it will default to INSTALL_INCLUDEDIR, CMAKE_INSTALL_INCLUDEDIR or just +# "include/" in case they are specified, with the BASE_NAME postfixed. +# +# LIB_INSTALL_DIR specifies where the library is being installed. If it's not +# specified, it will default to LIB_INSTALL_DIR, CMAKE_INSTALL_LIBDIR or just +# "lib/" in case they are specified. +# +# DEFINES is a list of preprocessor defines that it is recommended users of the +# library pass to the compiler when using it. +# +# INSTALL will cause the module to be installed to the ``pkgconfig`` subdirectory +# of LIB_INSTALL_DIR, unless the ECM_PKGCONFIG_INSTALL_DIR cache variable is set +# to something different. Note that the first call to ecm_generate_pkgconfig_file +# with the INSTALL argument will cause ECM_PKGCONFIG_INSTALL_DIR to be set to the +# cache, and will be used in any subsequent calls. +# +# To properly use this macro a version needs to be set. To retrieve it ``ECM_PKGCONFIG_INSTALL_DIR`` +# uses PROJECT_VERSION. To set it, use the project() command (only available since CMake 3.0) or the +# ecm_setup_version() macro. +# +# +# Example usage: +# +# .. code-block:: cmake +# +# ecm_generate_pkgconfig_file( +# BASE_NAME KF5Archive +# DEPS Qt5Core +# FILENAME_VAR pkgconfig_filename +# INSTALL +# ) +# + +#============================================================================= +# Copyright 2014 Aleix Pol Gonzalez <aleixpol@kde.org> +# Copyright 2014 David Faure <faure@kde.org> +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file COPYING-CMAKE-SCRIPTS for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of extra-cmake-modules, substitute the full +# License text for the above reference.) + +function(ECM_GENERATE_PKGCONFIG_FILE) + set(options INSTALL) + set(oneValueArgs BASE_NAME LIB_NAME FILENAME_VAR INCLUDE_INSTALL_DIR LIB_INSTALL_DIR) + set(multiValueArgs DEPS DEFINES) + + cmake_parse_arguments(EGPF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(EGPF_UNPARSED_ARGUMENTS) + message(FATAL_ERROR "Unknown keywords given to ECM_GENERATE_PKGCONFIG_FILE(): \"${EGPF_UNPARSED_ARGUMENTS}\"") + endif() + + if(NOT EGPF_BASE_NAME) + message(FATAL_ERROR "Required argument BASE_NAME missing in ECM_GENERATE_PKGCONFIG_FILE() call") + endif() + if(NOT PROJECT_VERSION) + message(FATAL_ERROR "Required variable PROJECT_VERSION not set before ECM_GENERATE_PKGCONFIG_FILE() call. Did you call ecm_setup_version or project with the VERSION argument?") + endif() + if(NOT EGPF_LIB_NAME) + set(EGPF_LIB_NAME ${EGPF_BASE_NAME}) + endif() + if(NOT EGPF_INCLUDE_INSTALL_DIR) + if(INCLUDE_INSTALL_DIR) + set(EGPF_INCLUDE_INSTALL_DIR "${INCLUDE_INSTALL_DIR}/${EGPF_BASE_NAME}") + elseif(CMAKE_INSTALL_INCLUDEDIR) + set(EGPF_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}/${EGPF_BASE_NAME}") + else() + set(EGPF_INCLUDE_INSTALL_DIR "include/${EGPF_BASE_NAME}") + endif() + endif() + if(NOT EGPF_LIB_INSTALL_DIR) + if(LIB_INSTALL_DIR) + set(EGPF_LIB_INSTALL_DIR "${LIB_INSTALL_DIR}") + elseif(CMAKE_INSTALL_LIBDIR) + set(EGPF_LIB_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}") + else() + set(EGPF_LIB_INSTALL_DIR "lib") + endif() + endif() + + set(PKGCONFIG_TARGET_BASENAME ${EGPF_BASE_NAME}) + set(PKGCONFIG_TARGET_LIBNAME ${EGPF_LIB_NAME}) + if (DEFINED EGPF_DEPS) + string(REPLACE ";" " " PKGCONFIG_TARGET_DEPS "${EGPF_DEPS}") + endif () + if(IS_ABSOLUTE "${EGPF_INCLUDE_INSTALL_DIR}") + set(PKGCONFIG_TARGET_INCLUDES "-I${EGPF_INCLUDE_INSTALL_DIR}") + else() + set(PKGCONFIG_TARGET_INCLUDES "-I${CMAKE_INSTALL_PREFIX}/${EGPF_INCLUDE_INSTALL_DIR}") + endif() + if(IS_ABSOLUTE "${EGPF_LIB_INSTALL_DIR}") + set(PKGCONFIG_TARGET_LIBS "${EGPF_LIB_INSTALL_DIR}") + else() + set(PKGCONFIG_TARGET_LIBS "${CMAKE_INSTALL_PREFIX}/${EGPF_LIB_INSTALL_DIR}") + endif() + set(PKGCONFIG_TARGET_DEFINES "") + if(EGPF_DEFINES) + set(PKGCONFIG_TARGET_DEFINES "${EGPF_DEFINE}") + endif() + + set(PKGCONFIG_FILENAME ${CMAKE_CURRENT_BINARY_DIR}/${PKGCONFIG_TARGET_BASENAME}.pc) + if (EGPF_FILENAME_VAR) + set(${EGPF_FILENAME_VAR} ${PKGCONFIG_FILENAME} PARENT_SCOPE) + endif() + + file(WRITE ${PKGCONFIG_FILENAME} +" +Name: @PKGCONFIG_TARGET_LIBNAME@ +Version: @PROJECT_VERSION@ +Libs: -L@CMAKE_INSTALL_PREFIX@/@EGPF_LIB_INSTALL_DIR@ -l@PKGCONFIG_TARGET_LIBNAME@ +Cflags: @PKGCONFIG_TARGET_INCLUDES@ @PKGCONFIG_TARGET_DEFINES@ +Requires: @PKGCONFIG_TARGET_DEPS@ +" + ) + + if(EGPF_INSTALL) + set(ECM_PKGCONFIG_INSTALL_DIR "${EGPF_LIB_INSTALL_DIR}/pkgconfig" CACHE PATH "The directory where pkgconfig will be installed to.") + install(FILES ${PKGCONFIG_FILENAME} DESTINATION ${ECM_PKGCONFIG_INSTALL_DIR}) + endif() +endfunction() + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 65de038e..69da6e6e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,6 +6,7 @@ project(ECMTests C) add_subdirectory(ECMGenerateHeadersTest) add_subdirectory(ECMSetupVersionTest) +add_subdirectory(ECMGeneratePkgConfigFile) # a macro for tests that have a simple format where the name matches the # directory and project diff --git a/tests/ECMGeneratePkgConfigFile/CMakeLists.txt b/tests/ECMGeneratePkgConfigFile/CMakeLists.txt new file mode 100644 index 00000000..9f407cb0 --- /dev/null +++ b/tests/ECMGeneratePkgConfigFile/CMakeLists.txt @@ -0,0 +1,6 @@ +set(MODULES_DIR "${extra-cmake-modules_SOURCE_DIR}/modules") +configure_file(run_test.cmake.config "${CMAKE_CURRENT_BINARY_DIR}/run_test.cmake" @ONLY) + +add_test( + NAME ECMGenerateHeaders + COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/run_test.cmake") diff --git a/tests/ECMGeneratePkgConfigFile/KF5CoreAddons.pc b/tests/ECMGeneratePkgConfigFile/KF5CoreAddons.pc new file mode 100644 index 00000000..68e0004d --- /dev/null +++ b/tests/ECMGeneratePkgConfigFile/KF5CoreAddons.pc @@ -0,0 +1,6 @@ + +Name: KF5CoreAddons +Version: 5.43 +Libs: -L/usr/lib -lKF5CoreAddons +Cflags: -I/usr/KCoreAddons +Requires: Qt5Core diff --git a/tests/ECMGeneratePkgConfigFile/run_test.cmake.config b/tests/ECMGeneratePkgConfigFile/run_test.cmake.config new file mode 100644 index 00000000..dad4369b --- /dev/null +++ b/tests/ECMGeneratePkgConfigFile/run_test.cmake.config @@ -0,0 +1,54 @@ +set(CMAKE_MODULE_PATH "@MODULES_DIR@") +set(CMAKE_CURRENT_SOURCE_DIR "@CMAKE_CURRENT_SOURCE_DIR@") +set(CMAKE_CURRENT_BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@") + +set(CMAKE_INSTALL_PREFIX "/usr") + +include(ECMGeneratePkgConfigFile) +include(ECMSetupVersion) +include(CMakeParseArguments) + + +function (compare_files) + set(options) + set(oneValueArgs GENERATED ORIGINALS) + set(multiValueArgs) + cmake_parse_arguments(CF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + list(LENGTH CF_GENERATED count) + foreach(i RANGE ${count}) + list(GET CF_GENERATED 0 generated_file) + if (NOT EXISTS "${generated_file}") + message(FATAL_ERROR "${generated_file} was not generated") + endif() + file(READ "${generated_file}" file_contents) + string(STRIP "${file_contents}" file_contents) + + list(GET CF_ORIGINALS 0 original_file) + if (NOT EXISTS "${original_file}") + message(FATAL_ERROR "Original ${original_file} was not found") + endif() + file(READ "${original_file}" original_contents) + string(STRIP "${original_contents}" original_contents) + + if(NOT original_contents STREQUAL file_contents) + message(FATAL_ERROR "Different files: ${original_file} ${generated_file}") + endif() + endforeach() +endfunction() + +########################################################### + + +ecm_setup_version(5.43 VARIABLE_PREFIX KCOREADDONS + VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kcoreaddons_version.h" + PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KF5CoreAddonsConfigVersion.cmake" + SOVERSION 5) + +message(STATUS "Test 1: no optional arguments") +set(origfiles "${CMAKE_CURRENT_SOURCE_DIR}/KF5CoreAddons.pc") + +ecm_generate_pkgconfig_file(BASE_NAME KF5CoreAddons DEPS Qt5Core INCLUDE_INSTALL_DIR /usr/KCoreAddons FILENAME_VAR OutputFile) + +compare_files(GENERATED ${OutputFile} + ORIGINALS ${origfiles}) +file(REMOVE ${OutputFile}) |