Skip to content

Commit def175f

Browse files
Use standard CMake constructs to export the library's targets. (PCRE2Project#260)
* Use standard CMake constructs to export the library's targets. * Fix typo. * Document that PCRE2_USE_STATIC_LIBS is not required if only one edition of the library has been built. * Replace some missed stuff from previous review. * Don't explicitly set `PCRE2_FOUND` to true. * Avoid defining the target aliases if they are already defined. * Set `PCRE2_LIBRARIES` and `PCRE2_***_LIBRARY` for compatibility. * Change `PCRE2_USE_STATIC_LIBS` to prohibit loading the shared library if set.
1 parent 6bc8ccf commit def175f

File tree

3 files changed

+77
-127
lines changed

3 files changed

+77
-127
lines changed

CMakeLists.txt

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
# 2021-08-28 PH added test for realpath()
102102
# 2022-12-10 PH added support for pcre2posix_test
103103
# 2023-01-15 Carlo added C99 as the minimum required
104+
# 2023-06-03 Theodore used standard CMake constructs to export the library's targets.
104105
# 2023-08-06 PH added support for setting variable length lookbehind maximum
105106

106107
# Increased minimum to 3.3 to support visibility.
@@ -142,6 +143,7 @@ INCLUDE(CheckFunctionExists)
142143
INCLUDE(CheckSymbolExists)
143144
INCLUDE(CheckIncludeFile)
144145
INCLUDE(CheckTypeSize)
146+
INCLUDE(CMakePackageConfigHelpers)
145147
INCLUDE(GNUInstallDirs) # for CMAKE_INSTALL_LIBDIR
146148

147149
CHECK_INCLUDE_FILE(dirent.h HAVE_DIRENT_H)
@@ -734,7 +736,9 @@ IF(PCRE2_BUILD_PCRE2_8)
734736
VERSION ${LIBPCRE2_8_VERSION}
735737
SOVERSION ${LIBPCRE2_8_SOVERSION})
736738
TARGET_COMPILE_DEFINITIONS(pcre2-8-static PUBLIC PCRE2_STATIC)
737-
TARGET_INCLUDE_DIRECTORIES(pcre2-8-static PUBLIC ${PROJECT_BINARY_DIR})
739+
TARGET_INCLUDE_DIRECTORIES(pcre2-8-static PUBLIC
740+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
741+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
738742
IF(REQUIRE_PTHREAD)
739743
TARGET_LINK_LIBRARIES(pcre2-8-static Threads::Threads)
740744
ENDIF(REQUIRE_PTHREAD)
@@ -747,7 +751,9 @@ IF(PCRE2_BUILD_PCRE2_8)
747751
VERSION ${LIBPCRE2_POSIX_VERSION}
748752
SOVERSION ${LIBPCRE2_POSIX_SOVERSION})
749753
TARGET_LINK_LIBRARIES(pcre2-posix-static pcre2-8-static)
750-
TARGET_INCLUDE_DIRECTORIES(pcre2-posix-static PUBLIC ${PROJECT_BINARY_DIR})
754+
TARGET_INCLUDE_DIRECTORIES(pcre2-posix-static PUBLIC
755+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
756+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
751757
set(targets ${targets} pcre2-posix-static)
752758

753759
IF(MSVC)
@@ -764,7 +770,9 @@ IF(PCRE2_BUILD_PCRE2_8)
764770

765771
IF(BUILD_SHARED_LIBS)
766772
ADD_LIBRARY(pcre2-8-shared SHARED ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
767-
TARGET_INCLUDE_DIRECTORIES(pcre2-8-shared PUBLIC ${PROJECT_BINARY_DIR})
773+
TARGET_INCLUDE_DIRECTORIES(pcre2-8-shared PUBLIC
774+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
775+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
768776
SET_TARGET_PROPERTIES(pcre2-8-shared PROPERTIES
769777
COMPILE_DEFINITIONS PCRE2_CODE_UNIT_WIDTH=8
770778
MACHO_COMPATIBILITY_VERSION "${LIBPCRE2_8_MACHO_COMPATIBILITY_VERSION}"
@@ -778,7 +786,9 @@ IF(PCRE2_BUILD_PCRE2_8)
778786
set(targets ${targets} pcre2-8-shared)
779787

780788
ADD_LIBRARY(pcre2-posix-shared SHARED ${PCRE2POSIX_HEADERS} ${PCRE2POSIX_SOURCES})
781-
TARGET_INCLUDE_DIRECTORIES(pcre2-posix-shared PUBLIC ${PROJECT_BINARY_DIR})
789+
TARGET_INCLUDE_DIRECTORIES(pcre2-posix-shared PUBLIC
790+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
791+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
782792
SET_TARGET_PROPERTIES(pcre2-posix-shared PROPERTIES
783793
COMPILE_DEFINITIONS PCRE2_CODE_UNIT_WIDTH=8
784794
MACHO_COMPATIBILITY_VERSION "${LIBPCRE2_POSIX_MACHO_COMPATIBILITY_VERSION}"
@@ -816,7 +826,9 @@ ENDIF(PCRE2_BUILD_PCRE2_8)
816826
IF(PCRE2_BUILD_PCRE2_16)
817827
IF(BUILD_STATIC_LIBS)
818828
ADD_LIBRARY(pcre2-16-static STATIC ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
819-
TARGET_INCLUDE_DIRECTORIES(pcre2-16-static PUBLIC ${PROJECT_BINARY_DIR})
829+
TARGET_INCLUDE_DIRECTORIES(pcre2-16-static PUBLIC
830+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
831+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
820832
SET_TARGET_PROPERTIES(pcre2-16-static PROPERTIES UNITY_BUILD OFF
821833
COMPILE_DEFINITIONS PCRE2_CODE_UNIT_WIDTH=16
822834
MACHO_COMPATIBILITY_VERSION "${LIBPCRE2_32_MACHO_COMPATIBILITY_VERSION}"
@@ -841,7 +853,9 @@ IF(PCRE2_BUILD_PCRE2_16)
841853

842854
IF(BUILD_SHARED_LIBS)
843855
ADD_LIBRARY(pcre2-16-shared SHARED ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
844-
TARGET_INCLUDE_DIRECTORIES(pcre2-16-shared PUBLIC ${PROJECT_BINARY_DIR})
856+
TARGET_INCLUDE_DIRECTORIES(pcre2-16-shared PUBLIC
857+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
858+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
845859
SET_TARGET_PROPERTIES(pcre2-16-shared PROPERTIES UNITY_BUILD OFF
846860
COMPILE_DEFINITIONS PCRE2_CODE_UNIT_WIDTH=16
847861
MACHO_COMPATIBILITY_VERSION "${LIBPCRE2_32_MACHO_COMPATIBILITY_VERSION}"
@@ -878,7 +892,9 @@ ENDIF(PCRE2_BUILD_PCRE2_16)
878892
IF(PCRE2_BUILD_PCRE2_32)
879893
IF(BUILD_STATIC_LIBS)
880894
ADD_LIBRARY(pcre2-32-static STATIC ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
881-
TARGET_INCLUDE_DIRECTORIES(pcre2-32-static PUBLIC ${PROJECT_BINARY_DIR})
895+
TARGET_INCLUDE_DIRECTORIES(pcre2-32-static PUBLIC
896+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
897+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
882898
SET_TARGET_PROPERTIES(pcre2-32-static PROPERTIES UNITY_BUILD OFF
883899
COMPILE_DEFINITIONS PCRE2_CODE_UNIT_WIDTH=32
884900
MACHO_COMPATIBILITY_VERSION "${LIBPCRE2_32_MACHO_COMPATIBILITY_VERSION}"
@@ -903,7 +919,9 @@ IF(PCRE2_BUILD_PCRE2_32)
903919

904920
IF(BUILD_SHARED_LIBS)
905921
ADD_LIBRARY(pcre2-32-shared SHARED ${PCRE2_HEADERS} ${PCRE2_SOURCES} ${PROJECT_BINARY_DIR}/config.h)
906-
TARGET_INCLUDE_DIRECTORIES(pcre2-32-shared PUBLIC ${PROJECT_BINARY_DIR})
922+
TARGET_INCLUDE_DIRECTORIES(pcre2-32-shared PUBLIC
923+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
924+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
907925
SET_TARGET_PROPERTIES(pcre2-32-shared PROPERTIES UNITY_BUILD OFF
908926
COMPILE_DEFINITIONS PCRE2_CODE_UNIT_WIDTH=32
909927
MACHO_COMPATIBILITY_VERSION "${LIBPCRE2_32_MACHO_COMPATIBILITY_VERSION}"
@@ -1105,9 +1123,13 @@ ENDIF(PCRE2_BUILD_TESTS)
11051123
SET(CMAKE_INSTALL_ALWAYS 1)
11061124

11071125
INSTALL(TARGETS ${targets}
1108-
RUNTIME DESTINATION bin
1126+
EXPORT pcre2-targets
1127+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
11091128
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
11101129
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
1130+
INSTALL(EXPORT pcre2-targets
1131+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pcre2
1132+
NAMESPACE pcre2::)
11111133
INSTALL(FILES ${pkg_config_files} DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
11121134
INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/pcre2-config"
11131135
DESTINATION bin
@@ -1119,11 +1141,12 @@ INSTALL(FILES ${PCRE2_HEADERS} ${PCRE2POSIX_HEADERS} DESTINATION include)
11191141
# CMake config files.
11201142
set(PCRE2_CONFIG_IN ${CMAKE_CURRENT_SOURCE_DIR}/cmake/pcre2-config.cmake.in)
11211143
set(PCRE2_CONFIG_OUT ${CMAKE_CURRENT_BINARY_DIR}/cmake/pcre2-config.cmake)
1122-
configure_file(${PCRE2_CONFIG_IN} ${PCRE2_CONFIG_OUT} @ONLY)
1123-
set(PCRE2_CONFIG_VERSION_IN ${CMAKE_CURRENT_SOURCE_DIR}/cmake/pcre2-config-version.cmake.in)
1144+
configure_package_config_file(${PCRE2_CONFIG_IN} ${PCRE2_CONFIG_OUT} INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pcre2)
11241145
set(PCRE2_CONFIG_VERSION_OUT ${CMAKE_CURRENT_BINARY_DIR}/cmake/pcre2-config-version.cmake)
1125-
configure_file(${PCRE2_CONFIG_VERSION_IN} ${PCRE2_CONFIG_VERSION_OUT} @ONLY)
1126-
install(FILES ${PCRE2_CONFIG_OUT} ${PCRE2_CONFIG_VERSION_OUT} DESTINATION cmake)
1146+
write_basic_package_version_file(${PCRE2_CONFIG_VERSION_OUT}
1147+
VERSION ${PCRE2_MAJOR}.${PCRE2_MINOR}.0
1148+
COMPATIBILITY SameMajorVersion)
1149+
install(FILES ${PCRE2_CONFIG_OUT} ${PCRE2_CONFIG_VERSION_OUT} DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pcre2)
11271150

11281151
FILE(GLOB html ${PROJECT_SOURCE_DIR}/doc/html/*.html)
11291152
FILE(GLOB man1 ${PROJECT_SOURCE_DIR}/doc/*.1)

cmake/pcre2-config-version.cmake.in

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

cmake/pcre2-config.cmake.in

Lines changed: 41 additions & 99 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,70 +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 ()
32+
@PACKAGE_INIT@
4033

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 ()
34+
include(CMakeFindDependencyMacro)
35+
if("@REQUIRE_PTHREAD@") # REQUIRE_PTHREAD
36+
find_dependency(Threads)
37+
endif()
4838

49-
set(PCRE2_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
50-
if (MINGW AND PCRE2_NON_STANDARD_LIB_SUFFIX)
51-
set(PCRE2_SUFFIX "-0.dll")
52-
endif ()
53-
endif ()
54-
find_library(PCRE2_8BIT_LIBRARY NAMES ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} DOC "8 bit PCRE2 library")
55-
find_library(PCRE2_16BIT_LIBRARY NAMES ${PCRE2_PREFIX}${PCRE2_16BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} DOC "16 bit PCRE2 library")
56-
find_library(PCRE2_32BIT_LIBRARY NAMES ${PCRE2_PREFIX}${PCRE2_32BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} DOC "32 bit PCRE2 library")
57-
find_library(PCRE2_POSIX_LIBRARY NAMES ${PCRE2_PREFIX}${PCRE2_POSIX_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} DOC "8 bit POSIX PCRE2 library")
58-
unset(PCRE2_NON_STANDARD_LIB_PREFIX)
59-
unset(PCRE2_NON_STANDARD_LIB_SUFFIX)
60-
unset(PCRE2_8BIT_NAME)
61-
unset(PCRE2_16BIT_NAME)
62-
unset(PCRE2_32BIT_NAME)
63-
unset(PCRE2_POSIX_NAME)
39+
include("${CMAKE_CURRENT_LIST_DIR}/pcre2-targets.cmake")
6440

6541
# Set version
66-
if (PCRE2_INCLUDE_DIR)
67-
set(PCRE2_VERSION "@PCRE2_MAJOR@.@[email protected]")
68-
endif ()
42+
set(PCRE2_VERSION "@PCRE2_MAJOR@.@[email protected]")
6943

70-
# Which components have been found.
71-
if (PCRE2_8BIT_LIBRARY)
72-
set(PCRE2_8BIT_FOUND TRUE)
73-
endif ()
74-
if (PCRE2_16BIT_LIBRARY)
75-
set(PCRE2_16BIT_FOUND TRUE)
76-
endif ()
77-
if (PCRE2_32BIT_LIBRARY)
78-
set(PCRE2_32BIT_FOUND TRUE)
79-
endif ()
80-
if (PCRE2_POSIX_LIBRARY)
81-
set(PCRE2_POSIX_FOUND TRUE)
82-
endif ()
83-
84-
# Check if at least one component has been specified.
85-
list(LENGTH PCRE2_FIND_COMPONENTS PCRE2_NCOMPONENTS)
86-
if (PCRE2_NCOMPONENTS LESS 1)
87-
message(FATAL_ERROR "No components have been specified. This is not allowed. Please, specify at least one component.")
88-
endif ()
89-
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)
9068

9169
# When POSIX component has been specified make sure that also 8BIT component is specified.
9270
set(PCRE2_8BIT_COMPONENT FALSE)
@@ -105,41 +83,5 @@ endif()
10583
unset(PCRE2_8BIT_COMPONENT)
10684
unset(PCRE2_POSIX_COMPONENT)
10785

108-
include(FindPackageHandleStandardArgs)
109-
set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG "${CMAKE_CURRENT_LIST_FILE}")
110-
find_package_handle_standard_args(PCRE2
111-
FOUND_VAR PCRE2_FOUND
112-
REQUIRED_VARS PCRE2_INCLUDE_DIR
113-
HANDLE_COMPONENTS
114-
VERSION_VAR PCRE2_VERSION
115-
CONFIG_MODE
116-
)
117-
118-
set(PCRE2_LIBRARIES)
119-
if (PCRE2_FOUND)
120-
foreach(component ${PCRE2_FIND_COMPONENTS})
121-
if (PCRE2_USE_STATIC_LIBS)
122-
add_library(PCRE2::${component} STATIC IMPORTED)
123-
target_compile_definitions(PCRE2::${component} INTERFACE PCRE2_STATIC)
124-
else ()
125-
add_library(PCRE2::${component} SHARED IMPORTED)
126-
endif ()
127-
set_target_properties(PCRE2::${component} PROPERTIES
128-
IMPORTED_LOCATION "${PCRE2_${component}_LIBRARY}"
129-
INTERFACE_INCLUDE_DIRECTORIES "${PCRE2_INCLUDE_DIR}"
130-
)
131-
if (component STREQUAL "POSIX")
132-
set_target_properties(PCRE2::${component} PROPERTIES
133-
INTERFACE_LINK_LIBRARIES "PCRE2::8BIT"
134-
LINK_LIBRARIES "PCRE2::8BIT"
135-
)
136-
endif ()
137-
138-
set(PCRE2_LIBRARIES ${PCRE2_LIBRARIES} ${PCRE2_${component}_LIBRARY})
139-
mark_as_advanced(PCRE2_${component}_LIBRARY)
140-
endforeach()
141-
endif ()
142-
143-
mark_as_advanced(
144-
PCRE2_INCLUDE_DIR
145-
)
86+
# Check for required components.
87+
check_required_components("PCRE2")

0 commit comments

Comments
 (0)