Skip to content

Commit 8afcf7f

Browse files
committed
Improve support for the optional ICU dependency.
For GNU make, conditionally populate the `Requires:` field in `re2.pc`. Use `pkg-config` to obtain the necessary compiler and linker flags when building `static-testinstall` and `shared-testinstall`. For CMake, the dependency has to be expressed to CMake itself, not just to `pkg-config` as above. Fixes google#407. Change-Id: Ie5e0ea88e0662f54be011b3bc0c57f2cfa852f88 Reviewed-on: https://code-review.googlesource.com/c/re2/+/60931 Reviewed-by: Paul Wankadia <[email protected]> Reviewed-by: Perry Lorier <[email protected]>
1 parent ba54156 commit 8afcf7f

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

CMakeLists.txt

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ include(CTest)
1212
include(GNUInstallDirs)
1313

1414
option(BUILD_SHARED_LIBS "build shared libraries" OFF)
15-
option(USEPCRE "use PCRE in tests and benchmarks" OFF)
15+
option(RE2_USE_ICU "build against ICU for full Unicode properties support" OFF)
16+
17+
# For historical reasons, this is just "USEPCRE", not "RE2_USE_PCRE".
18+
option(USEPCRE "build against PCRE for testing and benchmarking" OFF)
1619

1720
# CMake seems to have no way to enable/disable testing per subproject,
1821
# so we provide an option similar to BUILD_TESTING, but just for RE2.
1922
option(RE2_BUILD_TESTING "enable testing for RE2" ON)
2023

24+
# The pkg-config Requires: field.
25+
set(REQUIRES)
26+
2127
# ABI version
2228
# http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
2329
set(SONAME 10)
@@ -43,11 +49,19 @@ endif()
4349
if(WIN32)
4450
add_definitions(-DUNICODE -D_UNICODE -DSTRICT -DNOMINMAX)
4551
add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)
46-
elseif(UNIX)
52+
endif()
53+
54+
if(UNIX)
4755
set(THREADS_PREFER_PTHREAD_FLAG ON)
4856
find_package(Threads REQUIRED)
4957
endif()
5058

59+
if(RE2_USE_ICU)
60+
find_package(ICU REQUIRED COMPONENTS uc)
61+
add_definitions(-DRE2_USE_ICU)
62+
list(APPEND REQUIRES icu-uc)
63+
endif()
64+
5165
if(USEPCRE)
5266
add_definitions(-DUSEPCRE)
5367
list(APPEND EXTRA_TARGET_LINK_LIBRARIES pcre)
@@ -89,6 +103,10 @@ if(UNIX)
89103
target_link_libraries(re2 PUBLIC Threads::Threads)
90104
endif()
91105

106+
if(RE2_USE_ICU)
107+
target_link_libraries(re2 PUBLIC ICU::uc)
108+
endif()
109+
92110
if(RE2_BUILD_TESTING)
93111
set(TESTING_SOURCES
94112
re2/testing/backtrack.cc

Makefile

+16-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# CCICU=$(shell pkg-config icu-uc --cflags) -DRE2_USE_ICU
88
# LDICU=$(shell pkg-config icu-uc --libs)
99

10-
# To build against PCRE for testing or benchmarking,
10+
# To build against PCRE for testing and benchmarking,
1111
# uncomment the next two lines:
1212
# CCPCRE=-I/usr/local/include -DUSEPCRE
1313
# LDPCRE=-L/usr/local/lib -lpcre
@@ -42,6 +42,12 @@ else
4242
SED_INPLACE=sed -i
4343
endif
4444

45+
# The pkg-config Requires: field.
46+
REQUIRES=
47+
ifdef LDICU
48+
REQUIRES+=icu-uc
49+
endif
50+
4551
# ABI version
4652
# http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
4753
SONAME=10
@@ -320,6 +326,7 @@ common-install:
320326
$(INSTALL_DATA) re2.pc.in $(DESTDIR)$(libdir)/pkgconfig/re2.pc
321327
$(SED_INPLACE) -e "s#@CMAKE_INSTALL_FULL_INCLUDEDIR@#$(includedir)#" $(DESTDIR)$(libdir)/pkgconfig/re2.pc
322328
$(SED_INPLACE) -e "s#@CMAKE_INSTALL_FULL_LIBDIR@#$(libdir)#" $(DESTDIR)$(libdir)/pkgconfig/re2.pc
329+
$(SED_INPLACE) -e "s#@REQUIRES@#$(REQUIRES)#" $(DESTDIR)$(libdir)/pkgconfig/re2.pc
323330
$(SED_INPLACE) -e "s#@SONAME@#$(SONAME)#" $(DESTDIR)$(libdir)/pkgconfig/re2.pc
324331

325332
.PHONY: testinstall
@@ -329,27 +336,27 @@ testinstall: static-testinstall shared-testinstall
329336
@echo
330337

331338
.PHONY: static-testinstall
332-
static-testinstall: CXXFLAGS:=-pthread -I$(DESTDIR)$(includedir) $(CXXFLAGS)
333-
static-testinstall: LDFLAGS:=-pthread -L$(DESTDIR)$(libdir) -l:libre2.a $(LDICU) $(LDFLAGS)
334339
static-testinstall:
335-
@mkdir -p obj
336-
@cp testinstall.cc obj/static-testinstall.cc
337340
ifeq ($(shell uname),Darwin)
338341
@echo Skipping test for libre2.a on Darwin.
339342
else ifeq ($(shell uname),SunOS)
340343
@echo Skipping test for libre2.a on SunOS.
341344
else
342-
(cd obj && $(CXX) static-testinstall.cc -o static-testinstall $(CXXFLAGS) $(LDFLAGS))
345+
@mkdir -p obj
346+
@cp testinstall.cc obj/static-testinstall.cc
347+
(cd obj && export PKG_CONFIG_PATH=$(DESTDIR)$(libdir)/pkgconfig; \
348+
$(CXX) static-testinstall.cc -o static-testinstall $(CXXFLAGS) $(LDFLAGS) \
349+
$$(pkg-config re2 --cflags --libs | sed -e "s#-lre2#-l:libre2.a#"))
343350
obj/static-testinstall
344351
endif
345352

346353
.PHONY: shared-testinstall
347-
shared-testinstall: CXXFLAGS:=-pthread -I$(DESTDIR)$(includedir) $(CXXFLAGS)
348-
shared-testinstall: LDFLAGS:=-pthread -L$(DESTDIR)$(libdir) -lre2 $(LDICU) $(LDFLAGS)
349354
shared-testinstall:
350355
@mkdir -p obj
351356
@cp testinstall.cc obj/shared-testinstall.cc
352-
(cd obj && $(CXX) shared-testinstall.cc -o shared-testinstall $(CXXFLAGS) $(LDFLAGS))
357+
(cd obj && export PKG_CONFIG_PATH=$(DESTDIR)$(libdir)/pkgconfig; \
358+
$(CXX) shared-testinstall.cc -o shared-testinstall $(CXXFLAGS) $(LDFLAGS) \
359+
$$(pkg-config re2 --cflags --libs))
353360
ifeq ($(shell uname),Darwin)
354361
DYLD_LIBRARY_PATH="$(DESTDIR)$(libdir):$(DYLD_LIBRARY_PATH)" obj/shared-testinstall
355362
else

re2.pc.in

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ libdir=@CMAKE_INSTALL_FULL_LIBDIR@
33

44
Name: re2
55
Description: RE2 is a fast, safe, thread-friendly regular expression engine.
6+
Requires: @REQUIRES@
67
78
Cflags: -pthread -I${includedir}
89
Libs: -pthread -L${libdir} -lre2

re2Config.cmake.in

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ if(UNIX)
1313
find_dependency(Threads REQUIRED)
1414
endif()
1515

16+
if(@RE2_USE_ICU@)
17+
find_dependency(ICU REQUIRED COMPONENTS uc)
18+
endif()
19+
1620
check_required_components(re2)
1721

1822
if(TARGET re2::re2)

0 commit comments

Comments
 (0)