Skip to content

Commit ccf8d8b

Browse files
committed
Use file(GET_RUNTIME_DEPENDENCIES) for *.pc files
This is just an intermediate step in the evolution towards a better pkg-config pc files generation. Determining the required libraries to link in some automated and abstract way is abnormally difficult in build systems.
1 parent 6d8fa4e commit ccf8d8b

File tree

4 files changed

+52
-80
lines changed

4 files changed

+52
-80
lines changed

bin/check-cmake.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function getProjectModules(): array
211211
'PHP/CheckCompilerFlag' => ['php_check_compiler_flag'],
212212
'PHP/ConfigureFile' => ['php_configure_file'],
213213
'PHP/Install' => ['php_install'],
214-
'PHP/PkgConfigGenerator' => ['pkgconfig_generate_pc'],
214+
'PHP/PkgConfig' => ['php_pkgconfig_generate_pc'],
215215
'PHP/Re2c' => ['php_re2c', '/find_package\([\n ]*RE2C/'],
216216
'PHP/SearchLibraries' => ['php_search_libraries'],
217217
'PHP/Set' => ['php_set'],

cmake/cmake/modules/PHP/PkgConfigGenerator.cmake renamed to cmake/cmake/modules/PHP/PkgConfig.cmake

Lines changed: 47 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
#[=============================================================================[
2-
# PHP/PkgConfigGenerator
2+
# PHP/PkgConfig
33
44
Generate pkg-config .pc file.
55
6-
CMake at the time of writing doesn't provide a solution to generate pkg-config
7-
pc files with getting clean linked libraries retrieved from the targets:
6+
CMake at the time of writing doesn't provide an out-of-the-box solution to
7+
generate pkg-config pc files with required libraries to link retrieved from the
8+
targets:
89
https://gitlab.kitware.com/cmake/cmake/-/issues/22621
910
11+
Once pkg-config integration is added in CMake natively, this module will be
12+
replaced.
13+
1014
Also there is a common issue with installation prefix not being applied when
1115
using `--prefix` command-line option at the installation phase:
1216
1317
```sh
1418
cmake --install <build-dir> --prefix <prefix>
1519
```
1620
17-
The following function is exposed:
21+
This module provides the following function:
1822
1923
```cmake
20-
pkgconfig_generate_pc(
24+
php_pkgconfig_generate_pc(
2125
<pc-template-file>
2226
<pc-file-output>
2327
TARGET <target>
@@ -35,7 +39,7 @@ template.
3539
expressions. For example:
3640
3741
```cmake
38-
pkgconfig_generate_pc(
42+
php_pkgconfig_generate_pc(
3943
...
4044
VARIABLES
4145
debug "$<IF:$<CONFIG:Debug>,yes,no>"
@@ -53,7 +57,7 @@ include_guard(GLOBAL)
5357

5458
# Parse given variables and create a list of options or variables for passing to
5559
# add_custom_command and configure_file().
56-
function(_pkgconfig_parse_variables variables)
60+
function(_php_pkgconfig_parse_variables variables)
5761
# Check for even number of keyword values.
5862
list(LENGTH variables length)
5963
math(EXPR modulus "${length} % 2")
@@ -120,7 +124,7 @@ function(_pkgconfig_parse_variables variables)
120124
set(resultValues "${resultValues}" PARENT_SCOPE)
121125
endfunction()
122126

123-
function(pkgconfig_generate_pc)
127+
function(php_pkgconfig_generate_pc)
124128
cmake_parse_arguments(
125129
PARSE_ARGV
126130
2
@@ -134,7 +138,11 @@ function(pkgconfig_generate_pc)
134138
message(FATAL_ERROR "Unrecognized arguments: ${parsed_UNPARSED_ARGUMENTS}")
135139
endif()
136140

137-
if(parsed_TARGET AND NOT TARGET ${parsed_TARGET})
141+
if(NOT DEFINED parsed_TARGET)
142+
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} expects a TARGET.")
143+
endif()
144+
145+
if(NOT TARGET ${parsed_TARGET})
138146
message(FATAL_ERROR "${parsed_TARGET} is not a target.")
139147
endif()
140148

@@ -168,59 +176,8 @@ function(pkgconfig_generate_pc)
168176
NORMALIZE
169177
)
170178

171-
file(
172-
GENERATE
173-
OUTPUT CMakeFiles/PkgConfigGeneratePc.cmake
174-
CONTENT [=[
175-
cmake_minimum_required(VERSION 3.25...3.31)
176-
177-
# TODO: Recheck this type of implementation.
178-
if(LINK_TXT)
179-
file(STRINGS ${LINK_TXT} content LIMIT_COUNT 1)
180-
string(REGEX REPLACE "^.*-o php " "" content "${content}")
181-
string(REPLACE " " ";" content "${content}")
182-
set(libs "")
183-
foreach(item IN LISTS content)
184-
if(IS_ABSOLUTE "${item}")
185-
list(APPEND libs "${item}")
186-
elseif(item MATCHES "^-l")
187-
list(APPEND libs "${item}")
188-
endif()
189-
endforeach()
190-
list(REMOVE_DUPLICATES libs)
191-
endif()
192-
193-
if(CMAKE_OBJDUMP)
194-
execute_process(
195-
COMMAND ${CMAKE_OBJDUMP} -p ${TARGET_FILE}
196-
OUTPUT_VARIABLE result
197-
OUTPUT_STRIP_TRAILING_WHITESPACE
198-
ERROR_QUIET
199-
)
200-
string(REGEX MATCHALL [[NEEDED[ ]+[A-Za-z0-9.]+]] matches "${result}")
201-
set(libraries "")
202-
foreach(library IN LISTS matches)
203-
if(library MATCHES [[NEEDED[ ]+(.+)]])
204-
string(STRIP "${CMAKE_MATCH_1}" library)
205-
string(REGEX REPLACE "^lib(.*).so.*" [[\1]] library "${library}")
206-
if(NOT library MATCHES "c|root")
207-
list(APPEND libraries "-l${library}")
208-
endif()
209-
endif()
210-
endforeach()
211-
endif()
212-
213-
list(JOIN libraries " " PHP_LIBS_PRIVATE)
214-
configure_file(${TEMPLATE} ${OUTPUT} @ONLY)
215-
]=]
216-
)
217-
218-
if(parsed_TARGET)
219-
set(targetOption -D TARGET_FILE="$<TARGET_FILE:${parsed_TARGET}>")
220-
endif()
221-
222179
if(parsed_VARIABLES)
223-
_pkgconfig_parse_variables("${parsed_VARIABLES}")
180+
_php_pkgconfig_parse_variables("${parsed_VARIABLES}")
224181
endif()
225182

226183
cmake_path(
@@ -230,23 +187,38 @@ function(pkgconfig_generate_pc)
230187
OUTPUT_VARIABLE outputRelativePath
231188
)
232189

233-
string(MAKE_C_IDENTIFIER "${outputRelativePath}" targetName)
234-
235-
add_custom_target(
236-
pkgconfig_${targetName}
237-
ALL
238-
COMMAND ${CMAKE_COMMAND}
239-
-D CMAKE_OBJDUMP=${CMAKE_OBJDUMP}
240-
-D TEMPLATE=${template}
241-
-D OUTPUT=${output}
242-
${targetOption}
243-
${variablesOptions}
244-
-P CMakeFiles/PkgConfigGeneratePc.cmake
245-
COMMENT "[PkgConfig] Generating ${outputRelativePath}"
246-
)
190+
get_target_property(type ${parsed_TARGET} TYPE)
191+
set(fileOption "")
192+
if(type STREQUAL "EXECUTABLE")
193+
set(fileOption "" EXECUTABLES "$<TARGET_FILE:${parsed_TARGET}>")
194+
elseif(type MATCHES "^(MODULE|SHARED)_LIBRARY$")
195+
set(fileOption LIBRARIES "$<TARGET_FILE:${parsed_TARGET}>")
196+
elseif(type MATCHES "STATIC_LIBRARY")
197+
set(fileOption "")
198+
endif()
247199

248200
string(CONFIGURE [[
249201
block()
202+
file(
203+
GET_RUNTIME_DEPENDENCIES
204+
RESOLVED_DEPENDENCIES_VAR dependencies
205+
PRE_EXCLUDE_REGEXES
206+
libc\\.
207+
libroot\\.
208+
ld-linux
209+
libgcc
210+
libstdc\\+\\+
211+
POST_INCLUDE_REGEXES lib[^/]+$
212+
@fileOption@
213+
)
214+
set(libraries "")
215+
foreach(dependency IN LISTS dependencies)
216+
cmake_path(GET dependency STEM library)
217+
list(APPEND libraries "${library}")
218+
endforeach()
219+
list(TRANSFORM libraries REPLACE "^lib" "-l")
220+
list(JOIN libraries " " PHP_LIBS_PRIVATE)
221+
250222
set(resultVariables @resultVariables@)
251223
set(resultValues "@resultValues@")
252224

cmake/sapi/embed/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ set_target_properties(
6161
)
6262

6363
# Configure pkg-config php-embed.pc metadata file.
64-
include(PHP/PkgConfigGenerator)
65-
pkgconfig_generate_pc(
64+
include(PHP/PkgConfig)
65+
php_pkgconfig_generate_pc(
6666
php-embed.pc.in
6767
php-embed.pc
6868
TARGET PHP::sapi::embed

cmake/scripts/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ block()
127127
endblock()
128128

129129
# Configure pkg-config php.pc metadata file.
130-
include(PHP/PkgConfigGenerator)
131-
pkgconfig_generate_pc(
130+
include(PHP/PkgConfig)
131+
php_pkgconfig_generate_pc(
132132
php.pc.in
133133
php.pc
134134
TARGET PHP::sapi::cli

0 commit comments

Comments
 (0)