@@ -13,16 +13,13 @@ The following function is exposed:
1313```cmake
1414php_configure_file(
1515 <template-file>
16- <file-output>
17- [INSTALL_DESTINATION <path>]
16+ <output-file>
1817 [VARIABLES [<variable> <value>] ...]
1918)
2019```
2120
22- * `INSTALL_DESTINATION`
23- Path to the directory where the generated file `<file-output>` will be
24- installed to. If not provided, `<file-output>` will not be installed.
2521* `VARIABLES`
22+
2623 Pairs of variable names and values.
2724
2825 The `$<INSTALL_PREFIX>` generator expression can be used in variable values,
@@ -37,31 +34,68 @@ php_configure_file(
3734
3835include_guard (GLOBAL )
3936
40- # Parse given variables and create a list of options or variables for passing to
37+ # Parse given variables and create variables and values lists for passing to the
4138# configure_file().
42- function (_php_configure_file_parse_variables variables )
39+ # _php_configure_file_parse_variables(
40+ # variables
41+ # VARIABLES <variable-name>
42+ # VALUES <values-variable-name>
43+ # VALUES_IN_CODE <code-values-variable-name>
44+ # )
45+ function (_php_configure_file_parse_variables)
46+ cmake_parse_arguments (
47+ PARSE_ARGV
48+ 1
49+ parsed # prefix
50+ "" # options
51+ "" # one-value keywords
52+ "VARIABLES;VALUES;VALUES_IN_CODE" # multi-value keywords
53+ )
54+
55+ if (parsed_UNPARSED_ARGUMENTS)
56+ message (FATAL_ERROR "Bad arguments: ${parsed_UNPARSED_ARGUMENTS} " )
57+ endif ()
58+
59+ if (NOT ARGV0)
60+ message (
61+ FATAL_ERROR
62+ "${CMAKE_CURRENT_FUNCTION} expects 1st argument"
63+ )
64+ endif ()
65+
66+ foreach (item VARIABLES VALUES VALUES_IN_CODE)
67+ if (NOT parsed_${item} )
68+ message (FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} : missing keyword ${item} " )
69+ endif ()
70+ endforeach ()
71+
4372 # Check for even number of keyword values.
73+ set (variables "${ARGV0} " )
4474 list (LENGTH variables length )
4575 math (EXPR modulus "${length} % 2" )
4676 if (NOT modulus EQUAL 0)
4777 message (
4878 FATAL_ERROR
49- "The keyword VARIABLES must be a list of pairs - variable-name and "
50- "value (it must contain an even number of items)."
79+ "${CMAKE_CURRENT_FUNCTION} : The keyword VARIABLES must be a list of "
80+ "pairs - variable-name and value (it must contain an even number of "
81+ "items)."
5182 )
5283 endif ()
5384
54- set (is_value FALSE )
55- set (result_variables "" )
56- set (result_values "" )
85+ set (isValue FALSE )
86+ set (resultVariables "" )
87+ set (resultValues "" )
88+ set (resultValuesInCode "" )
5789 foreach (variable IN LISTS variables )
58- if (is_value )
59- set (is_value FALSE )
90+ if (isValue )
91+ set (isValue FALSE )
6092 continue ()
6193 endif ()
94+ set (isValue TRUE )
95+
6296 list (POP_FRONT variables var value )
6397
64- list (APPEND result_variables ${var} )
98+ list (APPEND resultVariables ${var} )
6599
66100 if (value MATCHES [[^\$<PHP_EXPAND:(.*)>.*]])
67101 if (IS_ABSOLUTE "${CMAKE_MATCH_1} " )
@@ -73,9 +107,23 @@ function(_php_configure_file_parse_variables variables)
73107 endif ()
74108 endif ()
75109
76- # The result_values are for the install(CODE) and generator expression
77- # $<INSTALL_PREFIX> works since CMake 3.27, for earlier versions the escaped
78- # variable CMAKE_INSTALL_PREFIX can be used.
110+ # The resultValues are for the first configure_file().
111+ if (value MATCHES [[.*\$<INSTALL_PREFIX>.*]])
112+ string (
113+ REPLACE
114+ "$<INSTALL_PREFIX>"
115+ "${CMAKE_INSTALL_PREFIX} "
116+ replaced
117+ "${value} "
118+ )
119+ list (APPEND resultValues ${replaced} )
120+ else ()
121+ list (APPEND resultValues ${value} )
122+ endif ()
123+
124+ # The resultValuesInCode are for the configure_file inside the install(CODE)
125+ # and generator expression $<INSTALL_PREFIX> works since CMake 3.27. For
126+ # earlier versions the escaped variable CMAKE_INSTALL_PREFIX can be used.
79127 if (
80128 CMAKE_VERSION VERSION_LESS 3.27
81129 AND value MATCHES [[.*\$<INSTALL_PREFIX>.*]]
@@ -84,89 +132,93 @@ function(_php_configure_file_parse_variables variables)
84132 REPLACE
85133 "$<INSTALL_PREFIX>"
86134 "\$ {CMAKE_INSTALL_PREFIX}"
87- replaced_value
135+ replaced
88136 "${value} "
89137 )
90- list (APPEND result_values "${replaced_value } " )
138+ list (APPEND resultValuesInCode "${replaced } " )
91139 else ()
92- list (APPEND result_values "${value} " )
140+ list (APPEND resultValuesInCode "${value} " )
93141 endif ()
94-
95- set (is_value TRUE )
96142 endforeach ()
97143
98- set (result_variables "${result_variables} " PARENT_SCOPE)
99- set (result_values "${result_values} " PARENT_SCOPE)
144+ set (${parsed_VARIABLES} "${resultVariables} " PARENT_SCOPE)
145+ set (${parsed_VALUES} "${resultValues} " PARENT_SCOPE)
146+ set (${parsed_VALUES_IN_CODE} "${resultValuesInCode} " PARENT_SCOPE)
100147endfunction ()
101148
102149function (php_configure_file)
103150 cmake_parse_arguments (
104151 PARSE_ARGV
105152 2
106- parsed # prefix
107- "" # options
108- "INSTALL_DESTINATION" # one-value keywords
109- "VARIABLES" # multi-value keywords
153+ parsed # prefix
154+ "" # options
155+ "" # one-value keywords
156+ "VARIABLES" # multi-value keywords
110157 )
111158
112159 if (parsed_UNPARSED_ARGUMENTS)
113160 message (FATAL_ERROR "Bad arguments: ${parsed_UNPARSED_ARGUMENTS} " )
114161 endif ()
115162
116163 if (NOT ARGV0)
117- message (FATAL_ERROR "php_configure_file expects a template file name " )
164+ message (FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} expects a template filename " )
118165 endif ()
119166
120167 if (NOT ARGV1)
121- message (FATAL_ERROR "php_configure_file expects an output file name " )
168+ message (FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} expects an output filename " )
122169 endif ()
123170
124- set (template "${ARGV0} " )
125- if (NOT IS_ABSOLUTE "${template} " )
126- set (template "${CMAKE_CURRENT_SOURCE_DIR} /${template} " )
171+ set (___phpConfigureFileTemplate "${ARGV0} " )
172+ if (NOT IS_ABSOLUTE "${___phpConfigureFileTemplate} " )
173+ set (
174+ ___phpConfigureFileTemplate
175+ "${CMAKE_CURRENT_SOURCE_DIR} /${___phpConfigureFileTemplate} "
176+ )
127177 endif ()
128178
129- set (output "${ARGV1} " )
130- if (NOT IS_ABSOLUTE "${output} " )
131- set (output "${CMAKE_CURRENT_BINARY_DIR} /${output} " )
179+ set (___phpConfigureFileOutput "${ARGV1} " )
180+ if (NOT IS_ABSOLUTE "${___phpConfigureFileOutput} " )
181+ set (
182+ ___phpConfigureFileOutput
183+ "${CMAKE_CURRENT_BINARY_DIR} /${___phpConfigureFileOutput} "
184+ )
132185 endif ()
133186
134187 if (parsed_VARIABLES)
135- _php_configure_file_parse_variables("${parsed_VARIABLES} " )
188+ _php_configure_file_parse_variables(
189+ "${parsed_VARIABLES} "
190+ VARIABLES variables
191+ VALUES values
192+ VALUES_IN_CODE valuesInCode
193+ )
136194 endif ()
137195
138- cmake_path(GET template FILENAME filename)
139-
140- configure_file (
141- ${template}
142- ${output}
143- @ONLY
144- )
196+ block()
197+ foreach (var value IN ZIP_LISTS variables values)
198+ set (${var} "${value} " )
199+ endforeach ()
145200
146- cmake_path(GET output FILENAME output_file)
201+ configure_file (
202+ ${___phpConfigureFileTemplate}
203+ ${___phpConfigureFileOutput}
204+ @ONLY
205+ )
206+ endblock()
147207
148208 install (CODE "
149209 block()
150- set(result_variables ${result_variables } )
151- set(result_values \" ${result_values } \" )
210+ set(variables ${variables } )
211+ set(valuesInCode \" ${valuesInCode } \" )
152212
153- foreach(var value IN ZIP_LISTS result_variables result_values )
213+ foreach(var value IN ZIP_LISTS variables valuesInCode )
154214 set(\$ {var} \"\$ {value}\" )
155215 endforeach()
156216
157217 configure_file(
158- ${template }
159- ${CMAKE_CURRENT_BINARY_DIR} /CMakeFiles/ ${output_file }
218+ ${___phpConfigureFileTemplate }
219+ ${___phpConfigureFileOutput }
160220 @ONLY
161221 )
162222 endblock()
163223 " )
164-
165- if (parsed_INSTALL_DESTINATION)
166- install (
167- FILES
168- ${CMAKE_CURRENT_BINARY_DIR} /CMakeFiles/${output_file}
169- DESTINATION ${parsed_INSTALL_DESTINATION}
170- )
171- endif ()
172224endfunction ()
0 commit comments