1 function(install_pkgconfig package target)
2 set(one_value_keywords "NAME;DESTINATION;HOMEPAGE_URL;LICENSE;VERSION")
3 set(multi_value_keywords "REQUIRES;PRIVATE_REQUIRES;CONFLICTS")
4 cmake_parse_arguments(PC "" "${one_value_keywords}" "${multi_value_keywords}" ${ARGN})
6 if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
7 set(CMAKE_INSTALL_LIBDIR lib)
10 if(NOT DEFINED CMAKE_INSTALL_INCLUDEDIR)
11 set(CMAKE_INSTALL_INCLUDEDIR include)
13 set(PKGCONFIGDIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
14 set(CONTENT "prefix=${CMAKE_INSTALL_PREFIX}
15 exec_prefix=\${prefix}
16 libdir=\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}
17 includedir=\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}
21 set(CONTENT "${CONTENT}\nName: ${PC_NAME}\n")
23 set(CONTENT "${CONTENT}\nName: ${PROJECT_NAME}\n")
27 set(CONTENT "${CONTENT}Description: ${PC_DESCRIPTION}\n")
28 elseif(PROJECT_DESCRIPTION)
29 set(CONTENT "${CONTENT}Description: ${PROJECT_DESCRIPTION}\n")
33 set(CONTENT "${CONTENT}URL: ${PC_HOMEPAGE_URL}\n")
34 elseif(PROJECT_HOMEPAGE_URL)
35 set(CONTENT "${CONTENT}URL: ${PROJECT_HOMEPAGE_URL}\n")
39 set(CONTENT "${CONTENT}License: ${PC_LICENSE}\n")
40 elseif(PROJECT_LICENSE)
41 set(CONTENT "${CONTENT}License: ${PROJECT_LICENSE}\n")
45 set(CONTENT "${CONTENT}Version: ${PC_VERSION}\n")
46 elseif(PROJECT_VERSION)
47 set(CONTENT "${CONTENT}Version: ${PROJECT_VERSION}\n")
51 set(CONTENT "${CONTENT}Requires: $<JOIN:${PC_REQUIRES}, >\n")
54 if(PC_PRIVATE_REQUIRES)
55 set(CONTENT "${CONTENT}Requires.private: $<JOIN:${PC_PRIVATE_REQUIRES}, >\n")
59 set(CONTENT "${CONTENT}Conflicts: $<JOIN:${PC_CONFLICTS},$<COMMA> >\n")
62 get_property(prop TARGET ${target} PROPERTY INTERFACE_COMPILE_DEFINITIONS)
63 string(REGEX REPLACE "\\$<BUILD_INTERFACE:[^>]*>" "" prop "${prop}")
64 string(REPLACE "INSTALL_INTERFACE" "BUILD_INTERFACE" prop "${prop}")
65 string(REPLACE "-D$<JOIN:, -D>" "" PC_CFLAGS "-D$<JOIN:${prop}, -D>")
67 set(CONTENT "${CONTENT}Cflags: -I\${includedir} ${PC_CFLAGS}\n")
69 set(CONTENT "${CONTENT}Cflags: -I\${includedir}\n")
72 get_property(name TARGET ${target} PROPERTY OUTPUT_NAME)
76 set(CONTENT "${CONTENT}Libs: -L\${libdir} -l${name}\n")
78 get_property(prop TARGET ${target} PROPERTY INTERFACE_LINK_LIBRARIES)
79 string(REGEX REPLACE "\\$<BUILD_INTERFACE:[^>]*>" "" prop "${prop}")
80 string(REPLACE "INSTALL_INTERFACE" "BUILD_INTERFACE" prop "${prop}")
81 string(REPLACE "-l$<JOIN:, -l>" "" PC_LDFLAGS "-l$<JOIN:${prop}, -l>")
83 set(CONTENT "${CONTENT}Libs.private: ${PC_LDFLAGS}\n")
86 file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${package}.pc CONTENT "${CONTENT}")
87 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${package}.pc DESTINATION ${PKGCONFIGDIR})