Debian Codegraph @

ext/re2/extconf.rb

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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
137 # Pass -x c++ to force gcc to compile the test program
138 # as C++ (as it will end in .c by default).
139 compile_options = +"-x c++"
140
141 have_library("stdc++")
142
143 minimal_program = <<~SRC
144 #include <re2/re2.h>
145 int main() { return 0; }
146 SRC
147
148 re2_requires_version_flag = checking_for("re2 that requires explicit C++ version flag") do
149 !try_compile(minimal_program, compile_options)
150 end
151
152 if re2_requires_version_flag
153 # Recent versions of RE2 depend directly on Abseil, which requires a
154 # compiler with C++17 support.
155 abort "Cannot compile re2 with your compiler: recent versions require C++17 support." unless %w[c++20 c++17 c++11].any? do |std|
156 checking_for("re2 that compiles with #{std} standard") do
157 if try_compile(minimal_program, compile_options + " -std=#{std}")
158 compile_options << " -std=#{std}"
159 $CPPFLAGS << " -std=#{std}"
160
161 true
162 end
163 end
164 end
165 end
166
167 # Determine which version of re2 the user has installed.
168 # Revision d9f8806c004d added an `endpos` argument to the
169 # generic Match() function.
170 #
171 # To test for this, try to compile a simple program that uses
172 # the newer form of Match() and set a flag if it is successful.
173 checking_for("RE2::Match() with endpos argument") do
174 test_re2_match_signature = <<~SRC
175 #include <re2/re2.h>
176
177 int main() {
178 RE2 pattern("test");
179 re2::StringPiece *match;
180 pattern.Match("test", 0, 0, RE2::UNANCHORED, match, 0);
181
182 return 0;
183 }
184 SRC
185
186 if try_compile(test_re2_match_signature, compile_options)
187 $defs.push("-DHAVE_ENDPOS_ARGUMENT")
188 end
189 end
190
191 checking_for("RE2::Set::Match() with error information") do
192 test_re2_set_match_signature = <<~SRC
193 #include <vector>
194 #include <re2/re2.h>
195 #include <re2/set.h>
196
197 int main() {
198 RE2::Set s(RE2::DefaultOptions, RE2::UNANCHORED);
199 s.Add("foo", NULL);
200 s.Compile();
201
202 std::vector<int> v;
203 RE2::Set::ErrorInfo ei;
204 s.Match("foo", &v, &ei);
205
206 return 0;
207 }
208 SRC
209
210 if try_compile(test_re2_set_match_signature, compile_options)
211 $defs.push("-DHAVE_ERROR_INFO_ARGUMENT")
212 end
213 end
214
215 checking_for("RE2::Set::Size()") do
216 test_re2_set_size = <<~SRC
217 #include <re2/re2.h>
218 #include <re2/set.h>
219
220 int main() {
221 RE2::Set s(RE2::DefaultOptions, RE2::UNANCHORED);
222 s.Size();
223
224 return 0;
225 }
226 SRC
227
228 if try_compile(test_re2_set_size, compile_options)
229 $defs.push("-DHAVE_SET_SIZE")
230 end
231 end
232 end
233
234 def static_pkg_config(pc_file, pkg_config_paths)
235 ENV["PKG_CONFIG_PATH"] = [*pkg_config_paths, ENV["PKG_CONFIG_PATH"]].compact.join(File::PATH_SEPARATOR)
236
237 static_library_paths = minimal_pkg_config(pc_file, '--libs-only-L', '--static')
238 .shellsplit
239 .map { |flag| flag.delete_prefix('-L') }
240
241 # Replace all -l flags that can be found in one of the static library
242 # paths with the absolute path instead.
243 minimal_pkg_config(pc_file, '--libs-only-l', '--static')
244 .shellsplit
245 .each do |flag|
246 lib = "lib#{flag.delete_prefix('-l')}.#{$LIBEXT}"
247
248 if (static_lib_path = static_library_paths.find { |path| File.exist?(File.join(path, lib)) })
249 $libs << ' ' << File.join(static_lib_path, lib).shellescape
250 else
251 $libs << ' ' << flag.shellescape
252 end
253 end
254
255 append_ldflags(minimal_pkg_config(pc_file, '--libs-only-other', '--static'))
256
257 incflags = minimal_pkg_config(pc_file, '--cflags-only-I')
258 $INCFLAGS = [incflags, $INCFLAGS].join(" ").strip
259
260 cflags = minimal_pkg_config(pc_file, '--cflags-only-other')
261 $CFLAGS = [$CFLAGS, cflags].join(" ").strip
262 $CXXFLAGS = [$CXXFLAGS, cflags].join(" ").strip
263 end
264
265 def process_recipe(recipe)
266 cross_build_p = config_cross_build?
267 message "Cross build is #{cross_build_p ? "enabled" : "disabled"}.\n"
268
269 recipe.host = target_host
270 # Ensure x64-mingw-ucrt uses different library paths since the host is
271 # the same (x86_64-w64-mingw32).
272 recipe.target = File.join(recipe.target, target_arch) if cross_build_p
273
274 yield recipe
275
276 checkpoint = "#{recipe.target}/#{recipe.name}-#{recipe.version}-#{recipe.host}.installed"
277 name = recipe.name
278 version = recipe.version
279
280 if File.exist?(checkpoint)
281 message("Building re2 with a packaged version of #{name}-#{version}.\n")
282 else
283 message(<<~EOM)
284 ---------- IMPORTANT NOTICE ----------
285 Building re2 with a packaged version of #{name}-#{version}.
286 Configuration options: #{recipe.configure_options.shelljoin}
287 EOM
288
289 # Use a temporary base directory to reduce filename lengths since
290 # Windows can hit a limit of 250 characters (CMAKE_OBJECT_PATH_MAX).
291 Dir.mktmpdir { |dir| Dir.chdir(dir) { recipe.cook } }
292
293 FileUtils.touch(checkpoint)
294 end
295 end
296
297 # See MiniPortile2's minimal_pkg_config:
298 # https://github.com/flavorjones/mini_portile/blob/52fb0bc41c89a10f1ac7b5abcf0157e059194374/lib/mini_portile2/mini_portile.rb#L760-L783
299 # and Ruby's pkg_config:
300 # https://github.com/ruby/ruby/blob/c505bb0ca0fd61c7ae931d26451f11122a2644e9/lib/mkmf.rb#L1916-L2004
301 def minimal_pkg_config(pc_file, *options)
302 if ($PKGCONFIG ||=
303 (pkgconfig = MakeMakefile.with_config("pkg-config") {MakeMakefile.config_string("PKG_CONFIG") || "pkg-config"}) &&
304 MakeMakefile.find_executable0(pkgconfig) && pkgconfig)
305 pkgconfig = $PKGCONFIG
306 else
307 raise RuntimeError, "pkg-config is not found"
308 end
309
310 response = xpopen([pkgconfig, *options, pc_file], err: %i[child out], &:read)
311 raise RuntimeError, response unless $?.success?
312
313 response.strip
314 end
315
316 def config_system_libraries?
317 enable_config("system-libraries", ENV.key?('RE2_USE_SYSTEM_LIBRARIES'))
318 end
319
320 def config_cross_build?
321 enable_config("cross-build")
322 end
323
324 # We use 'host' to set compiler prefix for cross-compiling. Prefer host_alias over host. And
325 # prefer i686 (what external dev tools use) to i386 (what ruby's configure.ac emits).
326 def target_host
327 host = RbConfig::CONFIG["host_alias"].empty? ? RbConfig::CONFIG["host"] : RbConfig::CONFIG["host_alias"]
328 host.gsub(/i386/, "i686")
329 end
330
331 def target_arch
332 RbConfig::CONFIG['arch']
333 end
334 end
335 end
336
337 extconf = RE2::Extconf.new
338
339 if arg_config('--help')
340 extconf.print_help
341 exit!(true)
342 end
343
344 extconf.configure