Warning, /frameworks/extra-cmake-modules/modules/ECMGeneratePkgConfigFile.cmake is written in an unsupported language. File is not indexed.

0001 # SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@kde.org>
0002 # SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org>
0003 #
0004 # SPDX-License-Identifier: BSD-3-Clause
0005 
0006 #[=======================================================================[.rst:
0007 ECMGeneratePkgConfigFile
0008 ------------------------
0009 
0010 Generate a `pkg-config <https://www.freedesktop.org/wiki/Software/pkg-config/>`_
0011 file for the benefit of
0012 `autotools <https://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html>`_-based
0013 projects.
0014 
0015 ::
0016 
0017   ecm_generate_pkgconfig_file(BASE_NAME <baseName>
0018                         [LIB_NAME <libName>]
0019                         [DEPS [PRIVATE|PUBLIC] <dep> [[PRIVATE|PUBLIC] <dep> [...]]]
0020                         [FILENAME_VAR <filename_variable>]
0021                         [INCLUDE_INSTALL_DIR <dir>]
0022                         [LIB_INSTALL_DIR <dir>]
0023                         [DEFINES -D<variable=value>...]
0024                         [DESCRIPTION <library description>] # since 5.41.0
0025                         [URL <url>] # since 5.89.0
0026                         [INSTALL])
0027 
0028 ``BASE_NAME`` is the name of the module. It's the name projects will use to
0029 find the module.
0030 
0031 ``LIB_NAME`` is the name of the library that is being exported. If undefined,
0032 it will default to the ``BASE_NAME``. That means the ``LIB_NAME`` will be set
0033 as the name field as well as the library to link to.
0034 
0035 ``DEPS`` is the list of libraries required by this library. Libraries that are
0036 not exposed to applications should be marked with ``PRIVATE``. The default
0037 is ``PUBLIC``, but note that according to the
0038 `Guide to pkg-config <https://people.freedesktop.org/~dbn/pkg-config-guide.html>`_
0039 marking dependencies as private is usually preferred. The ``PUBLIC`` and
0040 ``PRIVATE`` keywords are supported since 5.89.0.
0041 
0042 ``FILENAME_VAR`` is specified with a variable name. This variable will
0043 receive the location of the generated file will be set, within the build
0044 directory. This way it can be used in case some processing is required. See
0045 also ``INSTALL``.
0046 
0047 ``INCLUDE_INSTALL_DIR`` specifies where the includes will be installed. If
0048 it's not specified, it will default to ``INSTALL_INCLUDEDIR``,
0049 ``CMAKE_INSTALL_INCLUDEDIR`` or just "include/" in case they are specified,
0050 with the ``BASE_NAME`` postfixed.
0051 
0052 ``LIB_INSTALL_DIR`` specifies where the library is being installed. If it's
0053 not specified, it will default to ``LIB_INSTALL_DIR``,
0054 ``CMAKE_INSTALL_LIBDIR`` or just "lib/" in case they are specified.
0055 
0056 ``DEFINES`` is a list of preprocessor defines that it is recommended users of
0057 the library pass to the compiler when using it.
0058 
0059 ``DESCRIPTION`` describes what this library is. If it's not specified, CMake
0060 will first try to get the description from the metainfo.yaml file or will
0061 create one based on ``LIB_NAME``. Since 5.41.0.
0062 
0063 ``URL`` An URL where people can get more information about and download the
0064 package. Defaults to "https://www.kde.org/". Since 5.89.0.
0065 
0066 ``INSTALL`` will cause the module to be installed to the ``pkgconfig``
0067 subdirectory of ``LIB_INSTALL_DIR``, unless the ``ECM_PKGCONFIG_INSTALL_DIR``
0068 cache variable is set to something different.
0069 
0070 .. note::
0071   The first call to ``ecm_generate_pkgconfig_file()`` with the ``INSTALL``
0072   argument will cause ``ECM_PKGCONFIG_INSTALL_DIR`` to be set to the cache,
0073   and will be used in any subsequent calls.
0074 
0075 To properly use this macro a version needs to be set. To retrieve it,
0076 ``ECM_PKGCONFIG_INSTALL_DIR`` uses ``PROJECT_VERSION``. To set it, use the
0077 ``project()`` command or the ``ecm_setup_version()`` macro
0078 
0079 Example usage:
0080 
0081 .. code-block:: cmake
0082 
0083   ecm_generate_pkgconfig_file(
0084       BASE_NAME KF5Archive
0085       DEPS Qt5Core
0086       FILENAME_VAR pkgconfig_filename
0087       INSTALL
0088   )
0089 
0090 Since 1.3.0.
0091 #]=======================================================================]
0092 
0093 function(ECM_GENERATE_PKGCONFIG_FILE)
0094   set(options INSTALL)
0095   set(oneValueArgs BASE_NAME LIB_NAME FILENAME_VAR INCLUDE_INSTALL_DIR LIB_INSTALL_DIR DESCRIPTION URL)
0096   set(multiValueArgs DEPS DEFINES)
0097 
0098   cmake_parse_arguments(EGPF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
0099 
0100   if(EGPF_UNPARSED_ARGUMENTS)
0101     message(FATAL_ERROR "Unknown keywords given to ECM_GENERATE_PKGCONFIG_FILE(): \"${EGPF_UNPARSED_ARGUMENTS}\"")
0102   endif()
0103 
0104   if(NOT EGPF_BASE_NAME)
0105     message(FATAL_ERROR "Required argument BASE_NAME missing in ECM_GENERATE_PKGCONFIG_FILE() call")
0106   endif()
0107   if(NOT PROJECT_VERSION)
0108     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?")
0109   endif()
0110   if(NOT EGPF_LIB_NAME)
0111     set(EGPF_LIB_NAME ${EGPF_BASE_NAME})
0112   endif()
0113   if(NOT EGPF_INCLUDE_INSTALL_DIR)
0114       if(INCLUDE_INSTALL_DIR)
0115           set(EGPF_INCLUDE_INSTALL_DIR "${INCLUDE_INSTALL_DIR}/${EGPF_BASE_NAME}")
0116       elseif(CMAKE_INSTALL_INCLUDEDIR)
0117           set(EGPF_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}/${EGPF_BASE_NAME}")
0118       else()
0119           set(EGPF_INCLUDE_INSTALL_DIR "include/${EGPF_BASE_NAME}")
0120       endif()
0121   endif()
0122   if(NOT EGPF_LIB_INSTALL_DIR)
0123       if(LIB_INSTALL_DIR)
0124           set(EGPF_LIB_INSTALL_DIR "${LIB_INSTALL_DIR}")
0125       elseif(CMAKE_INSTALL_LIBDIR)
0126           set(EGPF_LIB_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}")
0127       else()
0128           set(EGPF_LIB_INSTALL_DIR "lib")
0129       endif()
0130   endif()
0131   if(NOT EGPF_DESCRIPTION)
0132       if(EXISTS ${CMAKE_SOURCE_DIR}/metainfo.yaml)
0133           file(STRINGS "${CMAKE_SOURCE_DIR}/metainfo.yaml" _EGPF_METAINFO_DESCRIPTION_STRING REGEX "^description:.*$")
0134           if(_EGPF_METAINFO_DESCRIPTION_STRING)
0135               string(REGEX REPLACE "^description:[ ]*(.*)" "\\1" EGPF_DESCRIPTION ${_EGPF_METAINFO_DESCRIPTION_STRING})
0136           endif()
0137       endif()
0138       if("${EGPF_DESCRIPTION}" STREQUAL "")
0139           set(EGPF_DESCRIPTION "${EGPF_LIB_NAME} library.")
0140       endif()
0141   endif()
0142   if(NOT EGPF_URL)
0143       set(EGPF_URL "https://www.kde.org/")
0144   endif()
0145 
0146   set(PKGCONFIG_TARGET_BASENAME ${EGPF_BASE_NAME})
0147   set(PKGCONFIG_TARGET_LIBNAME ${EGPF_LIB_NAME})
0148   if (DEFINED EGPF_DEPS)
0149     # convert the dependencies to a list
0150     string(REPLACE " " ";" EGPF_DEPS "${EGPF_DEPS}")
0151     foreach(EGPF_DEP ${EGPF_DEPS})
0152         if("${EGPF_DEP}" STREQUAL "")
0153         elseif("${EGPF_DEP}" STREQUAL "PRIVATE")
0154             set(private_deps ON)
0155         elseif("${EGPF_DEP}" STREQUAL "PUBLIC")
0156             unset(private_deps)
0157         else()
0158             if(private_deps)
0159                 list(APPEND PKGCONFIG_TARGET_DEPS_PRIVATE "${EGPF_DEP}")
0160             else()
0161                 list(APPEND PKGCONFIG_TARGET_DEPS "${EGPF_DEP}")
0162             endif()
0163         endif()
0164     endforeach()
0165     list(JOIN PKGCONFIG_TARGET_DEPS " " PKGCONFIG_TARGET_DEPS)
0166     list(JOIN PKGCONFIG_TARGET_DEPS_PRIVATE " " PKGCONFIG_TARGET_DEPS_PRIVATE)
0167   endif ()
0168   if(IS_ABSOLUTE "${EGPF_INCLUDE_INSTALL_DIR}")
0169       set(PKGCONFIG_TARGET_INCLUDES "${EGPF_INCLUDE_INSTALL_DIR}")
0170   else()
0171       set(PKGCONFIG_TARGET_INCLUDES "\${prefix}/${EGPF_INCLUDE_INSTALL_DIR}")
0172   endif()
0173   if(IS_ABSOLUTE "${EGPF_LIB_INSTALL_DIR}")
0174       set(PKGCONFIG_TARGET_LIBS "${EGPF_LIB_INSTALL_DIR}")
0175   else()
0176       set(PKGCONFIG_TARGET_LIBS "\${prefix}/${EGPF_LIB_INSTALL_DIR}")
0177   endif()
0178   set(PKGCONFIG_TARGET_DESCRIPTION "${EGPF_DESCRIPTION}")
0179   set(PKGCONFIG_TARGET_URL "${EGPF_URL}")
0180   set(PKGCONFIG_TARGET_DEFINES "")
0181   if(EGPF_DEFINES)
0182     set(PKGCONFIG_TARGET_DEFINES "${EGPF_DEFINE}")
0183   endif()
0184 
0185   set(PKGCONFIG_FILENAME ${CMAKE_CURRENT_BINARY_DIR}/${PKGCONFIG_TARGET_BASENAME}.pc)
0186   if (EGPF_FILENAME_VAR)
0187      set(${EGPF_FILENAME_VAR} ${PKGCONFIG_FILENAME} PARENT_SCOPE)
0188   endif()
0189 
0190   set(PKGCONFIG_CONTENT
0191 "
0192 prefix=${CMAKE_INSTALL_PREFIX}
0193 exec_prefix=\${prefix}
0194 libdir=${PKGCONFIG_TARGET_LIBS}
0195 includedir=${PKGCONFIG_TARGET_INCLUDES}
0196 
0197 Name: ${PKGCONFIG_TARGET_LIBNAME}
0198 Description: ${PKGCONFIG_TARGET_DESCRIPTION}
0199 URL: ${PKGCONFIG_TARGET_URL}
0200 Version: ${PROJECT_VERSION}
0201 Libs: -L${PKGCONFIG_TARGET_LIBS} -l${PKGCONFIG_TARGET_LIBNAME}
0202 Cflags: -I${PKGCONFIG_TARGET_INCLUDES} ${PKGCONFIG_TARGET_DEFINES}
0203 Requires: ${PKGCONFIG_TARGET_DEPS}
0204 "
0205   )
0206   if(PKGCONFIG_TARGET_DEPS_PRIVATE)
0207     set(PKGCONFIG_CONTENT
0208 "${PKGCONFIG_CONTENT}Requires.private: ${PKGCONFIG_TARGET_DEPS_PRIVATE}
0209 "
0210     )
0211   endif()
0212   file(WRITE ${PKGCONFIG_FILENAME} "${PKGCONFIG_CONTENT}")
0213 
0214   if(EGPF_INSTALL)
0215     if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
0216       set(ECM_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/libdata/pkgconfig" CACHE PATH "The directory where pkgconfig will be installed to.")
0217     else()
0218       set(ECM_PKGCONFIG_INSTALL_DIR "${EGPF_LIB_INSTALL_DIR}/pkgconfig" CACHE PATH "The directory where pkgconfig will be installed to.")
0219     endif()
0220     install(FILES ${PKGCONFIG_FILENAME} DESTINATION ${ECM_PKGCONFIG_INSTALL_DIR})
0221   endif()
0222 endfunction()