Skip to content

Commit 3a4310e

Browse files
feuersteNWilson
andauthored
Bazel: Stop leaking internal headers to other libraries (#729)
This is important for e.g. glib/gio, which also has an internal config.h, and fails to compile, as it often picks up the pcre2 config.h first. --------- Co-authored-by: Nicholas Wilson <nicholas@nicholaswilson.me.uk>
1 parent fa68936 commit 3a4310e

File tree

1 file changed

+62
-57
lines changed

1 file changed

+62
-57
lines changed

BUILD.bazel

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,47 @@ copy_file(
2020
out = "src/pcre2_chartables.c",
2121
)
2222

23+
LOCAL_DEFINES = [
24+
"HAVE_CONFIG_H",
25+
"HAVE_MEMMOVE",
26+
"HAVE_STRERROR",
27+
"PCRE2_CODE_UNIT_WIDTH=8",
28+
"PCRE2_STATIC",
29+
"SUPPORT_PCRE2_8",
30+
"SUPPORT_UNICODE",
31+
] + select({
32+
"@platforms//os:windows": [],
33+
"//conditions:default": ["HAVE_UNISTD_H"],
34+
})
35+
36+
# Workaround for a Bazel quirk. It is extremely strict about the #include path
37+
# used for internal headers. We have our headers inside src/, but we #include
38+
# them as '#include "pcre2_internal.h"', assuming that src/ is added to the
39+
# compiler's include path. Unfortunately, that violates the conventions used by
40+
# Bazel.
41+
#
42+
# This is a workaround. Note that we can't use the "include = [...]" property
43+
# to add the src/ directory, since that pollutes the include path for projects
44+
# depending on PCRE2 (we must not make our config.h file visible to consumers
45+
# of PCRE2).
46+
cc_library(
47+
name = "pcre2_internal_headers",
48+
hdrs = [
49+
"src/pcre2_compile.h",
50+
"src/pcre2_internal.h",
51+
"src/pcre2_intmodedep.h",
52+
"src/pcre2_jit_match_inc.h",
53+
"src/pcre2_jit_misc_inc.h",
54+
"src/pcre2_printint_inc.h",
55+
"src/pcre2_ucp.h",
56+
"src/pcre2_ucptables_inc.h",
57+
"src/pcre2_util.h",
58+
":config_h_generic",
59+
],
60+
strip_include_prefix = "src",
61+
visibility = ["//visibility:private"],
62+
)
63+
2364
cc_library(
2465
name = "pcre2",
2566
srcs = [
@@ -36,15 +77,12 @@ cc_library(
3677
"src/pcre2_extuni.c",
3778
"src/pcre2_find_bracket.c",
3879
"src/pcre2_jit_compile.c",
39-
"src/pcre2_jit_match_inc.h",
40-
"src/pcre2_jit_misc_inc.h",
4180
"src/pcre2_maketables.c",
4281
"src/pcre2_match.c",
4382
"src/pcre2_match_data.c",
4483
"src/pcre2_newline.c",
4584
"src/pcre2_ord2utf.c",
4685
"src/pcre2_pattern_info.c",
47-
"src/pcre2_printint_inc.h",
4886
"src/pcre2_script_run.c",
4987
"src/pcre2_serialize.c",
5088
"src/pcre2_string_utils.c",
@@ -56,48 +94,20 @@ cc_library(
5694
"src/pcre2_valid_utf.c",
5795
"src/pcre2_xclass.c",
5896
":pcre2_chartables_c",
59-
"src/pcre2_compile.h",
60-
"src/pcre2_internal.h",
61-
"src/pcre2_intmodedep.h",
62-
"src/pcre2_ucp.h",
63-
"src/pcre2_ucptables_inc.h",
64-
"src/pcre2_util.h",
65-
":config_h_generic",
66-
],
67-
textual_hdrs = [
6897
],
69-
hdrs = [
70-
":pcre2_h_generic",
71-
],
72-
local_defines = [
73-
"HAVE_CONFIG_H",
74-
"HAVE_MEMMOVE",
75-
"PCRE2_CODE_UNIT_WIDTH=8",
76-
"PCRE2_STATIC",
77-
"SUPPORT_UNICODE",
78-
],
79-
includes = ["src"],
98+
hdrs = [":pcre2_h_generic"],
99+
implementation_deps = [":pcre2_internal_headers"],
100+
local_defines = LOCAL_DEFINES,
80101
strip_include_prefix = "src",
81102
visibility = ["//visibility:public"],
82103
)
83104

84105
cc_library(
85106
name = "pcre2-posix",
86-
srcs = [
87-
"src/pcre2posix.c",
88-
":config_h_generic",
89-
],
90-
hdrs = [
91-
"src/pcre2posix.h",
92-
],
93-
local_defines = [
94-
"HAVE_CONFIG_H",
95-
"HAVE_MEMMOVE",
96-
"PCRE2_CODE_UNIT_WIDTH=8",
97-
"PCRE2_STATIC",
98-
"SUPPORT_UNICODE",
99-
],
100-
includes = ["src"],
107+
srcs = ["src/pcre2posix.c"],
108+
hdrs = ["src/pcre2posix.h"],
109+
implementation_deps = [":pcre2_internal_headers"],
110+
local_defines = LOCAL_DEFINES,
101111
strip_include_prefix = "src",
102112
visibility = ["//visibility:public"],
103113
deps = [":pcre2"],
@@ -127,27 +137,19 @@ cc_library(
127137

128138
cc_binary(
129139
name = "pcre2test",
130-
srcs = [
131-
"src/pcre2test.c",
132-
":config_h_generic",
133-
],
134-
local_defines = [
135-
"HAVE_CONFIG_H",
136-
"HAVE_MEMMOVE",
137-
"HAVE_STRERROR",
138-
"PCRE2_STATIC",
139-
"SUPPORT_UNICODE",
140-
"SUPPORT_PCRE2_8",
141-
] + select({
142-
"@platforms//os:windows": [],
143-
"//conditions:default": ["HAVE_UNISTD_H"],
144-
}),
140+
srcs = ["src/pcre2test.c"],
145141
linkopts = select({
146142
"@platforms//os:windows": ["-STACK:2500000"],
147143
"//conditions:default": [],
148144
}),
145+
local_defines = LOCAL_DEFINES,
149146
visibility = ["//visibility:public"],
150-
deps = [":pcre2test_dotc_headers", ":pcre2", ":pcre2-posix"],
147+
deps = [
148+
":pcre2",
149+
":pcre2-posix",
150+
":pcre2_internal_headers",
151+
":pcre2test_dotc_headers",
152+
],
151153
)
152154

153155
filegroup(
@@ -157,6 +159,7 @@ filegroup(
157159

158160
native_test(
159161
name = "pcre2_test",
162+
size = "small",
160163
src = select({
161164
"@platforms//os:windows": "RunTest.bat",
162165
"//conditions:default": "RunTest",
@@ -165,6 +168,8 @@ native_test(
165168
"@platforms//os:windows": "RunTest.bat",
166169
"//conditions:default": "RunTest",
167170
}),
168-
data = [":pcre2test", ":testdata"],
169-
size = "small",
170-
)
171+
data = [
172+
":pcre2test",
173+
":testdata",
174+
],
175+
)

0 commit comments

Comments
 (0)