remove `#!/usr/bin/env sh`
[liba.git] / cmake / InstallPkgConfig.cmake
blobc010983dda36c22b524b22c88d1ae8368e6fd421
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)
8   endif()
10   if(NOT DEFINED CMAKE_INSTALL_INCLUDEDIR)
11     set(CMAKE_INSTALL_INCLUDEDIR include)
12   endif()
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}
20   if(PC_NAME)
21     set(CONTENT "${CONTENT}\nName: ${PC_NAME}\n")
22   elseif(PROJECT_NAME)
23     set(CONTENT "${CONTENT}\nName: ${PROJECT_NAME}\n")
24   endif()
26   if(PC_DESCRIPTION)
27     set(CONTENT "${CONTENT}Description: ${PC_DESCRIPTION}\n")
28   elseif(PROJECT_DESCRIPTION)
29     set(CONTENT "${CONTENT}Description: ${PROJECT_DESCRIPTION}\n")
30   endif()
32   if(PC_HOMEPAGE_URL)
33     set(CONTENT "${CONTENT}URL: ${PC_HOMEPAGE_URL}\n")
34   elseif(PROJECT_HOMEPAGE_URL)
35     set(CONTENT "${CONTENT}URL: ${PROJECT_HOMEPAGE_URL}\n")
36   endif()
38   if(PC_LICENSE)
39     set(CONTENT "${CONTENT}License: ${PC_LICENSE}\n")
40   elseif(PROJECT_LICENSE)
41     set(CONTENT "${CONTENT}License: ${PROJECT_LICENSE}\n")
42   endif()
44   if(PC_VERSION)
45     set(CONTENT "${CONTENT}Version: ${PC_VERSION}\n")
46   elseif(PROJECT_VERSION)
47     set(CONTENT "${CONTENT}Version: ${PROJECT_VERSION}\n")
48   endif()
50   if(PC_REQUIRES)
51     set(CONTENT "${CONTENT}Requires: $<JOIN:${PC_REQUIRES}, >\n")
52   endif()
54   if(PC_PRIVATE_REQUIRES)
55     set(CONTENT "${CONTENT}Requires.private: $<JOIN:${PC_PRIVATE_REQUIRES}, >\n")
56   endif()
58   if(PC_CONFLICTS)
59     set(CONTENT "${CONTENT}Conflicts: $<JOIN:${PC_CONFLICTS},$<COMMA> >\n")
60   endif()
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>")
66   if(PC_LDFLAGS)
67     set(CONTENT "${CONTENT}Cflags: -I\${includedir} ${PC_CFLAGS}\n")
68   else()
69     set(CONTENT "${CONTENT}Cflags: -I\${includedir}\n")
70   endif()
72   get_property(name TARGET ${target} PROPERTY OUTPUT_NAME)
73   if(NOT DEFINED name)
74     set(name ${target})
75   endif()
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>")
82   if(PC_LDFLAGS)
83     set(CONTENT "${CONTENT}Libs.private: ${PC_LDFLAGS}\n")
84   endif()
86   file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${package}.pc CONTENT "${CONTENT}")
87   install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${package}.pc DESTINATION ${PKGCONFIGDIR})
88 endfunction()