Debian Codegraph @

ext/re2/extconf.rb

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 # frozen_string_literal: true
2
3 # re2 (https://github.com/mudge/re2)
4 # Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
5 # backtracking regular expression engines like those used in PCRE, Perl, and
6 # Python".
7 #
8 # Copyright (c) 2010, Paul Mucur (https://mudge.name)
9 # Released under the BSD Licence, please see LICENSE.txt
10
11 require 'mkmf'
12 require_relative 'recipes'
13
14 module RE2
15 class Extconf
16 def configure
17 configure_cross_compiler
18
19 if config_system_libraries?
20 build_with_system_libraries
21 else
22 build_with_vendored_libraries
23 end
24
25 build_extension
26
27 create_makefile("re2")
28 end
29
30 def print_help
31 print(<<~TEXT)
32 USAGE: ruby #{$0} [options]
33
34 Flags that are always valid:
35
36 --enable-system-libraries
37 Use system libraries instead of building and using the packaged libraries.
38
39 --disable-system-libraries
40 Use the packaged libraries, and ignore the system libraries. This is the default.
41
42
43 Flags only used when using system libraries:
44
45 Related to re2 library:
46
47 --with-re2-dir=DIRECTORY
48 Look for re2 headers and library in DIRECTORY.
49
50
51 Flags only used when building and using the packaged libraries:
52
53 --enable-cross-build
54 Enable cross-build mode. (You probably do not want to set this manually.)
55
56
57 Environment variables used:
58
59 CC
60 Use this path to invoke the compiler instead of `RbConfig::CONFIG['CC']`
61
62 CPPFLAGS
63 If this string is accepted by the C preprocessor, add it to the flags passed to the C preprocessor
64
65 CFLAGS
66 If this string is accepted by the compiler, add it to the flags passed to the compiler
67
68 LDFLAGS
69 If this string is accepted by the linker, add it to the flags passed to the linker
70
71 LIBS
72 Add this string to the flags passed to the linker
73 TEXT
74 end
75
76 private
77
78 def configure_cross_compiler
79 RbConfig::CONFIG["CC"] = RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"] if ENV["CC"]
80 RbConfig::CONFIG["CXX"] = RbConfig::MAKEFILE_CONFIG["CXX"] = ENV["CXX"] if ENV["CXX"]
81 end
82
83 def build_with_system_libraries
84 header_dirs = [
85 "/usr/local/include",
86 "/opt/homebrew/include",
87 "/usr/include"
88 ]
89
90 lib_dirs = [
91 "/usr/local/lib",
92 "/opt/homebrew/lib",
93 "/usr/lib"
94 ]
95
96 dir_config("re2", header_dirs, lib_dirs)
97
98 unless have_library("re2")
99 abort "You must have re2 installed and specified with --with-re2-dir, please see https://github.com/google/re2/wiki/Install"
100 end
101 end
102
103 def build_with_vendored_libraries
104 message "Building re2 using packaged libraries.\n"
105
106 ENV["MACOSX_DEPLOYMENT_TARGET"] = "10.14"
107
108 abseil_recipe, re2_recipe = load_recipes
109
110 process_recipe(abseil_recipe) do |recipe|
111 recipe.configure_options << '-DABSL_PROPAGATE_CXX_STD=ON'
112 # Workaround for https://github.com/abseil/abseil-cpp/issues/1510
113 recipe.configure_options << '-DCMAKE_CXX_FLAGS=-DABSL_FORCE_WAITER_MODE=4 -D_WIN32_WINNT=0x0601' if MiniPortile.windows?
114 end
115
116 process_recipe(re2_recipe) do |recipe|
117 recipe.configure_options += [
118 # Specify Abseil's path so RE2 will prefer that over any system Abseil
119 "-DCMAKE_PREFIX_PATH=#{abseil_recipe.path}",
120 '-DCMAKE_CXX_FLAGS=-DNDEBUG'
121 ]
122 end
123
124 pc_file = File.join(re2_recipe.lib_path, 'pkgconfig', 're2.pc')
125 pkg_config_paths = [
126 File.join(abseil_recipe.lib_path, 'pkgconfig'),
127 File.join(re2_recipe.lib_path, 'pkgconfig')
128 ]
129
130 static_pkg_config(pc_file, pkg_config_paths)
131 end
132
133 def build_extension
134 $CFLAGS << " -Wall -Wextra -funroll-loops"
135 $CXXFLAGS << " -Wall -Wextra -funroll-loops"
•136 if ENV["X_DEB_LINK_ATOMIC"]
137 $LDFLAGS << " -latomic"
138 end
139
140 # Pass -x c++ to force gcc to compile the test program
141 # as C++ (as it will end in .c by default).
142 compile_options = +"-x c++"
143
144 have_library("stdc++")
145
146 minimal_program = <<~SRC
147 #include <re2/re2.h>
148 int main() { return 0; }
149 SRC
150
151 re2_requires_version_flag = checking_for("re2 that requires explicit C++ version flag") do
152 !try_compile(minimal_program, compile_options)
153 end
154
155 if re2_requires_version_flag
156 # Recent versions of RE2 depend directly on Abseil, which requires a
157 # compiler with C++17 support.
158 abort "Cannot compile re2 with your compiler: recent versions require C++17 support." unless %w[c++20 c++17 c++11].any? do |std|
159 checking_for("re2 that compiles with #{std} standard") do
160 if try_compile(minimal_program, compile_options + " -std=#{std}")
161 compile_options << " -std=#{std}"
162 $CPPFLAGS << " -std=#{std}"
163
164 true
165 end
166 end
167 end
168 end
169
170 # Determine which version of re2 the user has installed.
171 # Revision d9f8806c004d added an `endpos` argument to the
172 # generic Match() function.
173 #
174 # To test for this, try to compile a simple program that uses
175 # the newer form of Match() and set a flag if it is successful.
176 checking_for("RE2::Match() with endpos argument") do
177 test_re2_match_signature = <<~SRC
178 #include <re2/re2.h>
179
180 int main() {
181 RE2 pattern("test");
182 re2::StringPiece *match;
183 pattern.Match("test", 0, 0, RE2::UNANCHORED, match, 0);
184
185 return 0;
186 }
187 SRC
188
189 if try_compile(test_re2_match_signature, compile_options)
190 $defs.push("-DHAVE_ENDPOS_ARGUMENT")
191 end
192 end
193
194 checking_for("RE2::Set::Match() with error information") do
195 test_re2_set_match_signature = <<~SRC
196 #include <vector>
197 #include <re2/re2.h>
198 #include <re2/set.h>
199
200 int main() {
201 RE2::Set s(RE2::DefaultOptions, RE2::UNANCHORED);
202 s.Add("foo", NULL);
203 s.Compile();
204
205 std::vector<int> v;
206 RE2::Set::ErrorInfo ei;
207 s.Match("foo", &v, &ei);
208
209 return 0;
210 }
211 SRC
212
213 if try_compile(test_re2_set_match_signature, compile_options)
214 $defs.push("-DHAVE_ERROR_INFO_ARGUMENT")
215 end
216 end
217
218 checking_for("RE2::Set::Size()") do
219 test_re2_set_size = <<~SRC
220 #include <re2/re2.h>
221 #include <re2/set.h>
222
223 int main() {
224 RE2::Set s(RE2::DefaultOptions, RE2::UNANCHORED);
225 s.Size();
226
227 return 0;
228 }
229 SRC
230
231 if try_compile(test_re2_set_size, compile_options)
232 $defs.push("-DHAVE_SET_SIZE")
233 end
234 end
235 end
236
237 def static_pkg_config(pc_file, pkg_config_paths)
238 ENV["PKG_CONFIG_PATH"] = [*pkg_config_paths, ENV["PKG_CONFIG_PATH"]].compact.join(File::PATH_SEPARATOR)
239
240 static_library_paths = minimal_pkg_config(pc_file, '--libs-only-L', '--static')
241 .shellsplit
242 .map { |flag| flag.delete_prefix('-L') }
243
244 # Replace all -l flags that can be found in one of the static library
245 # paths with the absolute path instead.
246 minimal_pkg_config(pc_file, '--libs-only-l', '--static')
247 .shellsplit
248 .each do |flag|
249 lib = "lib#{flag.delete_prefix('-l')}.#{$LIBEXT}"
250
251 if (static_lib_path = static_library_paths.find { |path| File.exist?(File.join(path, lib)) })
252 $libs << ' ' << File.join(static_lib_path, lib).shellescape
253 else
254 $libs << ' ' << flag.shellescape
255 end
256 end
257
258 append_ldflags(minimal_pkg_config(pc_file, '--libs-only-other', '--static'))
259
260 incflags = minimal_pkg_config(pc_file, '--cflags-only-I')
261 $INCFLAGS = [incflags, $INCFLAGS].join(" ").strip
262
263 cflags = minimal_pkg_config(pc_file, '--cflags-only-other')
264 $CFLAGS = [$CFLAGS, cflags].join(" ").strip
265 $CXXFLAGS = [$CXXFLAGS, cflags].join(" ").strip
266 end
267
268 def process_recipe(recipe)
269 cross_build_p = config_cross_build?
270 message "Cross build is #{cross_build_p ? "enabled" : "disabled"}.\n"
271
272 recipe.host = target_host
273 # Ensure x64-mingw-ucrt uses different library paths since the host is
274 # the same (x86_64-w64-mingw32).
275 recipe.target = File.join(recipe.target, target_arch) if cross_build_p
276
277 yield recipe
278
279 checkpoint = "#{recipe.target}/#{recipe.name}-#{recipe.version}-#{recipe.host}.installed"
280 name = recipe.name
281 version = recipe.version
282
283 if File.exist?(checkpoint)
284 message("Building re2 with a packaged version of #{name}-#{version}.\n")
285 else
286 message(<<~EOM)
287 ---------- IMPORTANT NOTICE ----------
288 Building re2 with a packaged version of #{name}-#{version}.
289 Configuration options: #{recipe.configure_options.shelljoin}
290 EOM
291
292 # Use a temporary base directory to reduce filename lengths since
293 # Windows can hit a limit of 250 characters (CMAKE_OBJECT_PATH_MAX).
294 Dir.mktmpdir { |dir| Dir.chdir(dir) { recipe.cook } }
295
296 FileUtils.touch(checkpoint)
297 end
298 end
299
300 # See MiniPortile2's minimal_pkg_config:
301 # https://github.com/flavorjones/mini_portile/blob/52fb0bc41c89a10f1ac7b5abcf0157e059194374/lib/mini_portile2/mini_portile.rb#L760-L783
302 # and Ruby's pkg_config:
303 # https://github.com/ruby/ruby/blob/c505bb0ca0fd61c7ae931d26451f11122a2644e9/lib/mkmf.rb#L1916-L2004
304 def minimal_pkg_config(pc_file, *options)
305 if ($PKGCONFIG ||=
306 (pkgconfig = MakeMakefile.with_config("pkg-config") {MakeMakefile.config_string("PKG_CONFIG") || "pkg-config"}) &&
307 MakeMakefile.find_executable0(pkgconfig) && pkgconfig)
308 pkgconfig = $PKGCONFIG
309 else
310 raise RuntimeError, "pkg-config is not found"
311 end
312
313 response = xpopen([pkgconfig, *options, pc_file], err: %i[child out], &:read)
314 raise RuntimeError, response unless $?.success?
315
316 response.strip
317 end
318
319 def config_system_libraries?
320 enable_config("system-libraries", ENV.key?('RE2_USE_SYSTEM_LIBRARIES'))
321 end
322
323 def config_cross_build?
324 enable_config("cross-build")
325 end
326
327 # We use 'host' to set compiler prefix for cross-compiling. Prefer host_alias over host. And
328 # prefer i686 (what external dev tools use) to i386 (what ruby's configure.ac emits).
329 def target_host
330 host = RbConfig::CONFIG["host_alias"].empty? ? RbConfig::CONFIG["host"] : RbConfig::CONFIG["host_alias"]
331 host.gsub(/i386/, "i686")
332 end
333
334 def target_arch
335 RbConfig::CONFIG['arch']
336 end
337 end
338 end
339
340 extconf = RE2::Extconf.new
341
342 if arg_config('--help')
343 extconf.print_help
344 exit!(true)
345 end
346
347 extconf.configure