Skip to content

Commit 466adc3

Browse files
committed
Generate makefile matching function from patch list.
1 parent 39a8d42 commit 466adc3

File tree

7 files changed

+101
-73
lines changed

7 files changed

+101
-73
lines changed

lib/cext/preprocess.rb

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,9 @@
66
# GNU General Public License version 2, or
77
# GNU Lesser General Public License version 2.1.
88

9-
require_relative 'patches/json_patches'
10-
require_relative 'patches/nokogiri_patches'
11-
require_relative 'patches/pg_patches'
9+
require_relative '../truffle/truffle/cext_preprocessor.rb'
1210

13-
class Preprocessor
14-
15-
PATCHED_FILES = {}
16-
17-
def self.add_gem_patches(patch_hash, gem_patches)
18-
gem = gem_patches[:gem]
19-
patch_list = gem_patches[:patches]
20-
patch_list.each do |path_parts, patch|
21-
processed_patch = {}
22-
if path_parts.kind_of?(String)
23-
key = path_parts
24-
else
25-
key = path_parts.last
26-
processed_patch[:ext_dir] = path_parts.first if path_parts.size > 1
27-
end
28-
processed_patch[:patches] = patch
29-
processed_patch[:gem] = gem
30-
raise "Duplicate patch file #{key}." if patch_hash.include?(key)
31-
patch_hash[key] = processed_patch
32-
end
33-
end
34-
35-
add_gem_patches(PATCHED_FILES, ::JsonPatches::PATCHES)
36-
add_gem_patches(PATCHED_FILES, ::NokogiriPatches::PATCHES)
37-
add_gem_patches(PATCHED_FILES, ::PgPatches::PATCHES)
38-
39-
def self.patch(file, contents, directory)
40-
if patched_file = PATCHED_FILES[File.basename(file)]
41-
matched = if patched_file[:ext_dir]
42-
directory.end_with?(File.join(patched_file[:gem], 'ext', patched_file[:ext_dir]))
43-
else
44-
regexp = /^#{Regexp.escape(patched_file[:gem])}\b/
45-
directory.split('/').last(3).any? { |part| part =~ regexp } || file.split('/').last(2).any? { |part| part =~ regexp }
46-
end
47-
if matched
48-
patched_file[:patches].each do |patch|
49-
contents = contents.gsub(patch[:match], patch[:replacement].rstrip)
50-
end
51-
end
52-
end
53-
contents
54-
end
11+
class Truffle::CExt::Preprocessor
5512

5613
if __FILE__ == $0
5714
require 'stringio'

lib/truffle/rbconfig-for-mkmf.rb

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# clang, opt and llvm-link versions.
1111

1212
require 'rbconfig'
13+
require_relative 'truffle/cext_preprocessor.rb'
1314

1415
search_paths = {}
1516
if Truffle::Platform.darwin?
@@ -131,33 +132,7 @@
131132
# compiler which disables this standard behaviour of the C preprocessor.
132133
begin
133134
with_conditional_preprocessing = proc do |command1, command2|
134-
<<-EOF
135-
$(if $(or\\
136-
$(and\\
137-
$(findstring nokogiri, $(realpath $(<))),\\
138-
$(or\\
139-
$(findstring xml_node_set.c, $(<)),\\
140-
$(findstring xslt_stylesheet.c, $(<)),\\
141-
$(findstring xml_document.c, $(<)),\\
142-
$(findstring xml_sax_parser.c, $(<)),\\
143-
$(findstring xml_xpath_context.c, $(<)))),\\
144-
$(and\\
145-
$(findstring pg, $(realpath $(<))),\\
146-
$(or\\
147-
$(findstring pg_binary_encoder.c, $(<)),\\
148-
$(findstring pg_result.c, $(<)),\\
149-
$(findstring pg_tuple.c, $(<)),\\
150-
$(findstring pg_text_decoder.c, $(<)),\\
151-
$(findstring pg_text_encoder.c, $(<)),\\
152-
$(findstring pg_type_map_by_class.c, $(<)))),\\
153-
$(and\\
154-
$(findstring json, $(realpath $(<))),\\
155-
$(or\\
156-
$(findstring parser.c, $(<))))\\
157-
),\\
158-
#{preprocess_ruby} #{cext_dir}/preprocess.rb $< | #{command1},\\
159-
#{command2})
160-
EOF
135+
Truffle::CExt::Preprocessor.makefile_matcher("#{preprocess_ruby} #{cext_dir}/preprocess.rb $< | #{command1}", command2)
161136
end
162137

163138
mkconfig['COMPILE_C'] = with_conditional_preprocessing.call(
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 1.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative 'patches/json_patches'
10+
require_relative 'patches/nokogiri_patches'
11+
require_relative 'patches/pg_patches'
12+
13+
class Truffle::CExt::Preprocessor
14+
15+
PATCHED_FILES = {}
16+
17+
def self.add_gem_patches(patch_hash, gem_patches)
18+
gem = gem_patches[:gem]
19+
patch_list = gem_patches[:patches]
20+
patch_list.each do |path_parts, patch|
21+
processed_patch = {}
22+
if path_parts.kind_of?(String)
23+
key = path_parts
24+
else
25+
key = path_parts.last
26+
processed_patch[:ext_dir] = path_parts.first if path_parts.size > 1
27+
end
28+
processed_patch[:patches] = patch
29+
processed_patch[:gem] = gem
30+
raise "Duplicate patch file #{key}." if patch_hash.include?(key)
31+
patch_hash[key] = processed_patch
32+
end
33+
end
34+
35+
add_gem_patches(PATCHED_FILES, ::JsonPatches::PATCHES)
36+
add_gem_patches(PATCHED_FILES, ::NokogiriPatches::PATCHES)
37+
add_gem_patches(PATCHED_FILES, ::PgPatches::PATCHES)
38+
39+
def self.makefile_matcher(command1, command2)
40+
file_list = {}
41+
PATCHED_FILES.each_pair do |file, patch|
42+
dir = if patch[:ext_dir]
43+
File.join(patch[:gem], 'ext', patch[:ext_dir])
44+
else
45+
"/#{patch[:gem]}"
46+
end
47+
(file_list[dir] ||= []) << file
48+
end
49+
50+
make_function = <<-EOF
51+
$(if\\
52+
$(or\\
53+
EOF
54+
file_list.each_pair do |dir, files|
55+
if !files.empty?
56+
make_function += <<-EOF
57+
$(and\\
58+
$(findstring #{dir}, $(realpath $(<))),\\
59+
$(or\\
60+
EOF
61+
files.each do |file|
62+
make_function += <<-EOF
63+
$(findstring #{file}, $(<)),\\
64+
EOF
65+
end
66+
make_function += <<-EOF
67+
)\\
68+
),\\
69+
EOF
70+
end
71+
end
72+
make_function += <<-EOF
73+
),\\
74+
#{command1},\\
75+
#{command2}\\
76+
)
77+
EOF
78+
79+
end
80+
81+
def self.patch(file, contents, directory)
82+
if patched_file = PATCHED_FILES[File.basename(file)]
83+
matched = if patched_file[:ext_dir]
84+
directory.end_with?(File.join(patched_file[:gem], 'ext', patched_file[:ext_dir]))
85+
else
86+
regexp = /^#{Regexp.escape(patched_file[:gem])}\b/
87+
directory.split('/').last(3).any? { |part| part =~ regexp } || file.split('/').last(2).any? { |part| part =~ regexp }
88+
end
89+
if matched
90+
patched_file[:patches].each do |patch|
91+
contents = contents.gsub(patch[:match], patch[:replacement].rstrip)
92+
end
93+
end
94+
end
95+
contents
96+
end
97+
end

mx.truffleruby/suite.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@
388388
"file:lib/truffle",
389389
],
390390
"lib/cext/": [
391-
"file:lib/cext/patches",
392391
"file:lib/cext/*.rb",
393392
"dependency:org.truffleruby.cext/src/main/c/truffleposix/<lib:truffleposix>",
394393
"dependency:org.truffleruby.cext/src/main/c/sulongmock/sulongmock.o",

0 commit comments

Comments
 (0)