@@ -9,23 +9,26 @@ Also there is a common issue with installation prefix not being applied when
99using --prefix command line option at the installation phase:
1010 cmake --install <build-dir> --prefix <prefix>
1111
12- TODO: This module will be refactored and changed further in the future.
13-
1412The following function is exposed:
1513
1614pkgconfig_generate_pc(
1715 <pc-template-file>
1816 <pc-file-output>
1917 TARGET <target>
18+ [INSTALL_DESTINATION <path>]
2019 [VARIABLES [<variable> <value>] [<variable_2>:BOOL <value_2>...] ...]
21- SKIP_BOOL_NORMALIZATION
20+ [ SKIP_BOOL_NORMALIZATION]
2221)
2322
2423 Generate pkgconfig <pc-file-output> from the given pc <pc-template-file>
2524 template.
2625
2726 TARGET
2827 Name of the target for getting libraries.
28+ INSTALL_DESTINATION
29+ Path to the pkgconfig directory where generated .pc file will be installed
30+ to. Usually it is '${CMAKE_INSTALL_LIBDIR}/pkgconfig'. If not provided, .pc
31+ file will not be installed.
2932 VARIABLES
3033 Pairs of variable names and values. To pass booleans, add ':BOOL' to the
3134 variable name. For example:
@@ -35,6 +38,11 @@ pkgconfig_generate_pc(
3538 variable_name:BOOL "${variable_name}"
3639 )
3740
41+ The $<INSTALL_PREFIX> generator expression can be used in variable values,
42+ which is replaced with installation prefix either set via the
43+ CMAKE_INSTALL_PREFIX variable at the configuration phase, or the
44+ 'cmake --install --prefix' option at the installation phase.
45+
3846 SKIP_BOOL_NORMALIZATION
3947 CMake booleans have values yes, no, true, false, on, off, 1, 0, they can
4048 even be case insensitive and so on. By default, all booleans (var:BOOL see
@@ -52,14 +60,79 @@ find_program(
5260)
5361mark_as_advanced (PKGCONFIG_OBJDUMP_EXECUTABLE)
5462
63+ # Parse given variables and create a list of options or variables for passing to
64+ # add_custom_command and configure_file().
65+ function (_pkgconfig_parse_variables variables )
66+ # Check for even number of keyword values.
67+ list (LENGTH variables length )
68+ math (EXPR modulus "${length} % 2" )
69+ if (NOT modulus EQUAL 0)
70+ message (
71+ FATAL_ERROR
72+ "The keyword VARIABLES must be a list of pairs - variable-name and "
73+ "value (it must contain an even number of items)."
74+ )
75+ endif ()
76+
77+ set (is_value FALSE )
78+ set (variables_options "" )
79+ set (result_variables "" )
80+ set (result_values "" )
81+ foreach (variable IN LISTS variables )
82+ if (is_value)
83+ set (is_value FALSE )
84+ continue ()
85+ endif ()
86+ list (POP_FRONT variables var value )
87+
88+ # Normalize boolean values to either "yes" or "no".
89+ if (var MATCHES ".*:BOOL$" AND NOT parsed_SKIP_BOOL_NORMALIZATION)
90+ if (value )
91+ set (value "yes" )
92+ else ()
93+ set (value "no" )
94+ endif ()
95+ endif ()
96+
97+ # Remove possible :<TYPE> part from the variable name.
98+ if (var MATCHES "(.*):BOOL$" )
99+ set (var ${CMAKE_MATCH_1} )
100+ endif ()
101+
102+ list (APPEND result_variables ${var} )
103+ list (APPEND result_values "${value} " )
104+
105+ # Replace possible INSTALL_PREFIX in value for usage in add_custom_command,
106+ # in the result_values above the intact genex is left for enabling the
107+ # possible 'cmake --install --prefix ...' override.
108+ if (value MATCHES [[.*\$<INSTALL_PREFIX>.*]])
109+ string (
110+ REPLACE
111+ "$<INSTALL_PREFIX>"
112+ "${CMAKE_INSTALL_PREFIX} "
113+ value
114+ "${value} "
115+ )
116+ endif ()
117+
118+ list (APPEND variables_options -D ${var} ="${value} " )
119+
120+ set (is_value TRUE )
121+ endforeach ()
122+
123+ set (variables_options "${variables_options} " PARENT_SCOPE)
124+ set (result_variables "${result_variables} " PARENT_SCOPE)
125+ set (result_values "${result_values} " PARENT_SCOPE)
126+ endfunction ()
127+
55128function (pkgconfig_generate_pc)
56129 cmake_parse_arguments (
57130 PARSE_ARGV
58131 2
59- parsed # prefix
60- "SKIP_BOOL_NORMALIZATION" # options
61- "TARGET" # one-value keywords
62- "VARIABLES" # multi-value keywords
132+ parsed # prefix
133+ "SKIP_BOOL_NORMALIZATION" # options
134+ "TARGET;INSTALL_DESTINATION" # one-value keywords
135+ "VARIABLES" # multi-value keywords
63136 )
64137
65138 if (parsed_UNPARSED_ARGUMENTS)
@@ -106,7 +179,6 @@ function(pkgconfig_generate_pc)
106179 endif()
107180 endforeach()
108181 list(REMOVE_DUPLICATES libs)
109- message(STATUS "Libs from link.txt: ${libs}")
110182 endif()
111183
112184 if(PKGCONFIG_OBJDUMP_EXECUTABLE)
@@ -127,8 +199,6 @@ function(pkgconfig_generate_pc)
127199 endif()
128200 endif()
129201 endforeach()
130-
131- message(STATUS "Libraries from objdump: ${libraries}")
132202 endif()
133203
134204 list(JOIN libraries " " PHP_LIBS_PRIVATE)
@@ -141,45 +211,7 @@ function(pkgconfig_generate_pc)
141211 endif ()
142212
143213 if (parsed_VARIABLES)
144- set (variables "${parsed_VARIABLES} " )
145-
146- # Check for even number of keyword values.
147- list (LENGTH variables length )
148- math (EXPR modulus "${length} % 2" )
149- if (NOT modulus EQUAL 0)
150- message (
151- FATAL_ERROR
152- "The keyword VARIABLES must be a list of pairs - variable-name and "
153- "value (it must contain an even number of items)."
154- )
155- endif ()
156-
157- set (is_value FALSE )
158- set (variables_options "" )
159- foreach (variable IN LISTS variables )
160- if (is_value)
161- set (is_value FALSE )
162- continue ()
163- endif ()
164- list (POP_FRONT variables var value )
165-
166- # Normalize boolean values to either "yes" or "no".
167- if (var MATCHES ".*:BOOL$" AND NOT parsed_SKIP_BOOL_NORMALIZATION)
168- if (value )
169- set (value "yes" )
170- else ()
171- set (value "no" )
172- endif ()
173- endif ()
174-
175- # Remove possible :<TYPE> part from the variable name.
176- if (var MATCHES "(.*):BOOL$" )
177- set (var ${CMAKE_MATCH_1} )
178- endif ()
179-
180- list (APPEND variables_options -D ${var} ="${value} " )
181- set (is_value TRUE )
182- endforeach ()
214+ _pkgconfig_parse_variables("${parsed_VARIABLES} " )
183215 endif ()
184216
185217 cmake_path(GET template FILENAME filename)
@@ -198,4 +230,29 @@ function(pkgconfig_generate_pc)
198230 -P CMakeFiles/PkgConfigGeneratePc.cmake
199231 COMMENT "[PkgConfig] Generating pkg-config ${filename} file"
200232 )
233+
234+ if (parsed_INSTALL_DESTINATION)
235+ cmake_path(GET output FILENAME output_file)
236+
237+ install (CODE "
238+ set(result_variables ${result_variables} )
239+ set(result_values \" ${result_values} \" )
240+
241+ foreach(var value IN ZIP_LISTS result_variables result_values)
242+ set(\$ {var} \"\$ {value}\" )
243+ endforeach()
244+
245+ configure_file(
246+ ${template}
247+ ${CMAKE_CURRENT_BINARY_DIR} /CMakeFiles/${output_file}
248+ @ONLY
249+ )
250+ " )
251+
252+ install (
253+ FILES
254+ ${CMAKE_CURRENT_BINARY_DIR} /CMakeFiles/${output_file}
255+ DESTINATION ${parsed_INSTALL_DESTINATION}
256+ )
257+ endif ()
201258endfunction ()
0 commit comments