Skip to content

Commit 6c0593d

Browse files
committed
Revert "Revert "Use standard CMake constructs to export the library's targets. (#260)" (#364)"
This reverts commit 92d56a1.
1 parent 2e03e32 commit 6c0593d

File tree

3 files changed

+92
-146
lines changed

3 files changed

+92
-146
lines changed

CMakeLists.txt

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
# 2022-12-10 PH added support for pcre2posix_test
103103
# 2023-01-15 Carlo added C99 as the minimum required
104104
# 2023-08-06 PH added support for setting variable length lookbehind maximum
105+
# 2025-03-27 Theodore used standard CMake constructs to export the library's
106+
# targets.
105107

106108
################################################################################
107109
# We have used `gersemi` for auto-formatting our CMake files.
@@ -160,6 +162,7 @@ include(CheckFunctionExists)
160162
include(CheckSymbolExists)
161163
include(CheckIncludeFile)
162164
include(CheckTypeSize)
165+
include(CMakePackageConfigHelpers)
163166
include(GNUInstallDirs) # for CMAKE_INSTALL_LIBDIR
164167
include(PCRE2CheckLinkerFlag)
165168

@@ -885,7 +888,10 @@ if(PCRE2_BUILD_PCRE2_8)
885888
SOVERSION ${LIBPCRE2_8_SOVERSION}
886889
)
887890
target_compile_definitions(pcre2-8-static PUBLIC PCRE2_STATIC)
888-
target_include_directories(pcre2-8-static PUBLIC ${PROJECT_BINARY_DIR})
891+
target_include_directories(
892+
pcre2-8-static
893+
PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
894+
)
889895
if(REQUIRE_PTHREAD)
890896
target_link_libraries(pcre2-8-static Threads::Threads)
891897
endif()
@@ -901,7 +907,12 @@ if(PCRE2_BUILD_PCRE2_8)
901907
SOVERSION ${LIBPCRE2_POSIX_SOVERSION}
902908
)
903909
target_link_libraries(pcre2-posix-static pcre2-8-static)
904-
target_include_directories(pcre2-posix-static PUBLIC ${PROJECT_SOURCE_DIR}/src)
910+
target_include_directories(
911+
pcre2-posix-static
912+
PUBLIC
913+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> # XXX WAS ${PROJECT_SOURCE_DIR}/src
914+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
915+
)
905916
set(TARGETS ${TARGETS} pcre2-posix-static)
906917

907918
if(MSVC)
@@ -918,7 +929,10 @@ if(PCRE2_BUILD_PCRE2_8)
918929

919930
if(BUILD_SHARED_LIBS)
920931
add_library(pcre2-8-shared SHARED ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
921-
target_include_directories(pcre2-8-shared PUBLIC ${PROJECT_BINARY_DIR})
932+
target_include_directories(
933+
pcre2-8-shared
934+
PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
935+
)
922936
set_target_properties(
923937
pcre2-8-shared
924938
PROPERTIES
@@ -941,7 +955,12 @@ if(PCRE2_BUILD_PCRE2_8)
941955
set(DLL_PDB_DEBUG_FILES $<TARGET_PDB_FILE_DIR:pcre2-8-shared>/pcre2-8d.pdb ${DLL_PDB_DEBUG_FILES})
942956

943957
add_library(pcre2-posix-shared SHARED ${PCRE2POSIX_HEADERS} ${PCRE2POSIX_SOURCES})
944-
target_include_directories(pcre2-posix-shared PUBLIC ${PROJECT_SOURCE_DIR}/src)
958+
target_include_directories(
959+
pcre2-posix-shared
960+
PUBLIC
961+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> # XXX WAS ${PROJECT_SOURCE_DIR}/src
962+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
963+
)
945964
set_target_properties(
946965
pcre2-posix-shared
947966
PROPERTIES
@@ -987,7 +1006,10 @@ endif()
9871006
if(PCRE2_BUILD_PCRE2_16)
9881007
if(BUILD_STATIC_LIBS)
9891008
add_library(pcre2-16-static STATIC ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
990-
target_include_directories(pcre2-16-static PUBLIC ${PROJECT_BINARY_DIR})
1009+
target_include_directories(
1010+
pcre2-16-static
1011+
PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
1012+
)
9911013
set_target_properties(
9921014
pcre2-16-static
9931015
PROPERTIES
@@ -1015,7 +1037,10 @@ if(PCRE2_BUILD_PCRE2_16)
10151037

10161038
if(BUILD_SHARED_LIBS)
10171039
add_library(pcre2-16-shared SHARED ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
1018-
target_include_directories(pcre2-16-shared PUBLIC ${PROJECT_BINARY_DIR})
1040+
target_include_directories(
1041+
pcre2-16-shared
1042+
PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
1043+
)
10191044
set_target_properties(
10201045
pcre2-16-shared
10211046
PROPERTIES
@@ -1059,7 +1084,10 @@ endif()
10591084
if(PCRE2_BUILD_PCRE2_32)
10601085
if(BUILD_STATIC_LIBS)
10611086
add_library(pcre2-32-static STATIC ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
1062-
target_include_directories(pcre2-32-static PUBLIC ${PROJECT_BINARY_DIR})
1087+
target_include_directories(
1088+
pcre2-32-static
1089+
PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
1090+
)
10631091
set_target_properties(
10641092
pcre2-32-static
10651093
PROPERTIES
@@ -1087,7 +1115,10 @@ if(PCRE2_BUILD_PCRE2_32)
10871115

10881116
if(BUILD_SHARED_LIBS)
10891117
add_library(pcre2-32-shared SHARED ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
1090-
target_include_directories(pcre2-32-shared PUBLIC ${PROJECT_BINARY_DIR})
1118+
target_include_directories(
1119+
pcre2-32-shared
1120+
PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
1121+
)
10911122
set_target_properties(
10921123
pcre2-32-shared
10931124
PROPERTIES
@@ -1347,12 +1378,16 @@ endif()
13471378

13481379
set(CMAKE_INSTALL_ALWAYS 1)
13491380

1381+
# XXX HERE
13501382
install(
13511383
TARGETS ${TARGETS}
1352-
RUNTIME DESTINATION bin
1384+
EXPORT pcre2-targets
1385+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
13531386
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
13541387
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
1388+
# XXX what about the header files - where should they go?
13551389
)
1390+
install(EXPORT pcre2-targets DESTINATION ${PCRE2_INSTALL_CMAKEDIR} NAMESPACE pcre2::) # is this the conventional casing for namespaces?
13561391
install(FILES ${pkg_config_files} DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
13571392
install(
13581393
FILES "${CMAKE_CURRENT_BINARY_DIR}/pcre2-config"
@@ -1366,11 +1401,14 @@ install(FILES ${PCRE2_HEADERS} ${PCRE2POSIX_HEADERS} DESTINATION include)
13661401
# CMake config files.
13671402
set(PCRE2_CONFIG_IN ${CMAKE_CURRENT_SOURCE_DIR}/cmake/pcre2-config.cmake.in)
13681403
set(PCRE2_CONFIG_OUT ${CMAKE_CURRENT_BINARY_DIR}/cmake/pcre2-config.cmake)
1369-
configure_file(${PCRE2_CONFIG_IN} ${PCRE2_CONFIG_OUT} @ONLY)
1370-
set(PCRE2_CONFIG_VERSION_IN ${CMAKE_CURRENT_SOURCE_DIR}/cmake/pcre2-config-version.cmake.in)
1404+
configure_package_config_file(${PCRE2_CONFIG_IN} ${PCRE2_CONFIG_OUT} INSTALL_DESTINATION ${PCRE2_INSTALL_CMAKEDIR})
13711405
set(PCRE2_CONFIG_VERSION_OUT ${CMAKE_CURRENT_BINARY_DIR}/cmake/pcre2-config-version.cmake)
1372-
configure_file(${PCRE2_CONFIG_VERSION_IN} ${PCRE2_CONFIG_VERSION_OUT} @ONLY)
1373-
install(FILES ${PCRE2_CONFIG_OUT} ${PCRE2_CONFIG_VERSION_OUT} DESTINATION "${PCRE2_INSTALL_CMAKEDIR}")
1406+
write_basic_package_version_file(
1407+
${PCRE2_CONFIG_VERSION_OUT}
1408+
VERSION ${PCRE2_MAJOR}.${PCRE2_MINOR}.0
1409+
COMPATIBILITY SameMajorVersion
1410+
)
1411+
install(FILES ${PCRE2_CONFIG_OUT} ${PCRE2_CONFIG_VERSION_OUT} DESTINATION ${PCRE2_INSTALL_CMAKEDIR})
13741412

13751413
file(GLOB html ${PROJECT_SOURCE_DIR}/doc/html/*.html ${PROJECT_SOURCE_DIR}/doc/html/*.txt)
13761414
file(

cmake/pcre2-config-version.cmake.in

Lines changed: 0 additions & 14 deletions
This file was deleted.

cmake/pcre2-config.cmake.in

Lines changed: 41 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
#
66
# Static vs. shared
77
# -----------------
8-
# To make use of the static library instead of the shared one, one needs
8+
# To force using the static library instead of the shared one, one needs
99
# to set the variable PCRE2_USE_STATIC_LIBS to ON before calling find_package.
10+
# If the variable is not set, the static library will be used if only that has
11+
# been built, otherwise the shared library will be used.
12+
#
13+
# The following components are supported: 8BIT, 16BIT, 32BIT and POSIX.
14+
# They used to be required but not anymore; all available targets will
15+
# be defined regardless of the requested components.
1016
# Example:
1117
# set(PCRE2_USE_STATIC_LIBS ON)
12-
# find_package(PCRE2 CONFIG COMPONENTS 8BIT)
18+
# find_package(PCRE2 CONFIG)
1319
#
1420
# This will define the following variables:
1521
#
@@ -23,88 +29,42 @@
2329
# PCRE2::32BIT - The 32 bit PCRE2 library.
2430
# PCRE2::POSIX - The POSIX PCRE2 library.
2531

26-
set(PCRE2_NON_STANDARD_LIB_PREFIX @NON_STANDARD_LIB_PREFIX@)
27-
set(PCRE2_NON_STANDARD_LIB_SUFFIX @NON_STANDARD_LIB_SUFFIX@)
28-
set(PCRE2_8BIT_NAME pcre2-8)
29-
set(PCRE2_16BIT_NAME pcre2-16)
30-
set(PCRE2_32BIT_NAME pcre2-32)
31-
set(PCRE2_POSIX_NAME pcre2-posix)
32-
find_path(PCRE2_INCLUDE_DIR NAMES pcre2.h DOC "PCRE2 include directory")
33-
if(PCRE2_USE_STATIC_LIBS)
34-
if(MSVC)
35-
set(PCRE2_8BIT_NAME pcre2-8-static)
36-
set(PCRE2_16BIT_NAME pcre2-16-static)
37-
set(PCRE2_32BIT_NAME pcre2-32-static)
38-
set(PCRE2_POSIX_NAME pcre2-posix-static)
39-
endif()
40-
41-
set(PCRE2_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
42-
set(PCRE2_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
43-
else()
44-
set(PCRE2_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
45-
if(MINGW AND PCRE2_NON_STANDARD_LIB_PREFIX)
46-
set(PCRE2_PREFIX "")
47-
endif()
32+
@PACKAGE_INIT@
4833

49-
set(PCRE2_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
50-
if(MINGW AND PCRE2_NON_STANDARD_LIB_SUFFIX)
51-
set(PCRE2_SUFFIX "-0.dll")
52-
elseif(MSVC)
53-
set(PCRE2_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
54-
endif()
34+
include(CMakeFindDependencyMacro)
35+
if("@REQUIRE_PTHREAD@") # REQUIRE_PTHREAD
36+
find_dependency(Threads)
5537
endif()
56-
find_library(
57-
PCRE2_8BIT_LIBRARY
58-
NAMES ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX}
59-
DOC "8 bit PCRE2 library"
60-
)
61-
find_library(
62-
PCRE2_16BIT_LIBRARY
63-
NAMES ${PCRE2_PREFIX}${PCRE2_16BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_16BIT_NAME}d${PCRE2_SUFFIX}
64-
DOC "16 bit PCRE2 library"
65-
)
66-
find_library(
67-
PCRE2_32BIT_LIBRARY
68-
NAMES ${PCRE2_PREFIX}${PCRE2_32BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_32BIT_NAME}d${PCRE2_SUFFIX}
69-
DOC "32 bit PCRE2 library"
70-
)
71-
find_library(
72-
PCRE2_POSIX_LIBRARY
73-
NAMES ${PCRE2_PREFIX}${PCRE2_POSIX_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_POSIX_NAME}d${PCRE2_SUFFIX}
74-
DOC "8 bit POSIX PCRE2 library"
75-
)
76-
unset(PCRE2_NON_STANDARD_LIB_PREFIX)
77-
unset(PCRE2_NON_STANDARD_LIB_SUFFIX)
78-
unset(PCRE2_8BIT_NAME)
79-
unset(PCRE2_16BIT_NAME)
80-
unset(PCRE2_32BIT_NAME)
81-
unset(PCRE2_POSIX_NAME)
8238

83-
# Set version
84-
if(PCRE2_INCLUDE_DIR)
85-
set(PCRE2_VERSION "@PCRE2_MAJOR@.@[email protected]")
86-
endif()
39+
include("${CMAKE_CURRENT_LIST_DIR}/pcre2-targets.cmake")
8740

88-
# Which components have been found.
89-
if(PCRE2_8BIT_LIBRARY)
90-
set(PCRE2_8BIT_FOUND TRUE)
91-
endif()
92-
if(PCRE2_16BIT_LIBRARY)
93-
set(PCRE2_16BIT_FOUND TRUE)
94-
endif()
95-
if(PCRE2_32BIT_LIBRARY)
96-
set(PCRE2_32BIT_FOUND TRUE)
97-
endif()
98-
if(PCRE2_POSIX_LIBRARY)
99-
set(PCRE2_POSIX_FOUND TRUE)
100-
endif()
41+
# Set version
42+
set(PCRE2_VERSION "@PCRE2_MAJOR@.@[email protected]")
10143

102-
# Check if at least one component has been specified.
103-
list(LENGTH PCRE2_FIND_COMPONENTS PCRE2_NCOMPONENTS)
104-
if(PCRE2_NCOMPONENTS LESS 1)
105-
message(FATAL_ERROR "No components have been specified. This is not allowed. Please, specify at least one component.")
106-
endif()
107-
unset(PCRE2_NCOMPONENTS)
44+
# Chooses the linkage of the library to expose in the
45+
# unsuffixed edition of the target.
46+
macro(_pcre2_add_component_target component target)
47+
# If the static library exists and either PCRE2_USE_STATIC_LIBS
48+
# is defined, or the dynamic library does not exist, use the static library.
49+
if(NOT TARGET PCRE2::${component})
50+
if(TARGET pcre2::pcre2-${target}-static AND (PCRE2_USE_STATIC_LIBS OR NOT TARGET pcre2::pcre2-${target}-shared))
51+
add_library(PCRE2::${component} ALIAS pcre2::pcre2-${target}-static)
52+
set(PCRE2_${component}_FOUND TRUE)
53+
# Otherwise use the dynamic library if it exists.
54+
elseif(TARGET pcre2::pcre2-${target}-shared AND NOT PCRE2_USE_STATIC_LIBS)
55+
add_library(PCRE2::${component} ALIAS pcre2::pcre2-${target}-shared)
56+
set(PCRE2_${component}_FOUND TRUE)
57+
endif()
58+
if(PCRE2_${component}_FOUND)
59+
get_target_property(PCRE2_${component}_LIBRARY PCRE2::${component} IMPORTED_LOCATION)
60+
set(PCRE2_LIBRARIES ${PCRE2_LIBRARIES} ${PCRE2_${component}_LIBRARY})
61+
endif()
62+
endif()
63+
endmacro()
64+
_pcre2_add_component_target(8BIT 8)
65+
_pcre2_add_component_target(16BIT 16)
66+
_pcre2_add_component_target(32BIT 32)
67+
_pcre2_add_component_target(POSIX posix)
10868

10969
# When POSIX component has been specified make sure that also 8BIT component is specified.
11070
set(PCRE2_8BIT_COMPONENT FALSE)
@@ -126,43 +86,5 @@ endif()
12686
unset(PCRE2_8BIT_COMPONENT)
12787
unset(PCRE2_POSIX_COMPONENT)
12888

129-
include(FindPackageHandleStandardArgs)
130-
set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG "${CMAKE_CURRENT_LIST_FILE}")
131-
find_package_handle_standard_args(
132-
PCRE2
133-
FOUND_VAR PCRE2_FOUND
134-
REQUIRED_VARS PCRE2_INCLUDE_DIR
135-
HANDLE_COMPONENTS
136-
VERSION_VAR PCRE2_VERSION
137-
CONFIG_MODE
138-
)
139-
140-
set(PCRE2_LIBRARIES)
141-
if(PCRE2_FOUND)
142-
foreach(component ${PCRE2_FIND_COMPONENTS})
143-
if(PCRE2_USE_STATIC_LIBS)
144-
add_library(PCRE2::${component} STATIC IMPORTED)
145-
target_compile_definitions(PCRE2::${component} INTERFACE PCRE2_STATIC)
146-
else()
147-
add_library(PCRE2::${component} SHARED IMPORTED)
148-
endif()
149-
set_target_properties(
150-
PCRE2::${component}
151-
PROPERTIES
152-
IMPORTED_LOCATION "${PCRE2_${component}_LIBRARY}"
153-
IMPORTED_IMPLIB "${PCRE2_${component}_LIBRARY}"
154-
INTERFACE_INCLUDE_DIRECTORIES "${PCRE2_INCLUDE_DIR}"
155-
)
156-
if(component STREQUAL "POSIX")
157-
set_target_properties(
158-
PCRE2::${component}
159-
PROPERTIES INTERFACE_LINK_LIBRARIES "PCRE2::8BIT" LINK_LIBRARIES "PCRE2::8BIT"
160-
)
161-
endif()
162-
163-
set(PCRE2_LIBRARIES ${PCRE2_LIBRARIES} ${PCRE2_${component}_LIBRARY})
164-
mark_as_advanced(PCRE2_${component}_LIBRARY)
165-
endforeach()
166-
endif()
167-
168-
mark_as_advanced(PCRE2_INCLUDE_DIR)
89+
# Check for required components.
90+
check_required_components("PCRE2")

0 commit comments

Comments
 (0)