Skip to content

Commit 841b744

Browse files
committed
[GR-59341] Upgrade XZ for Java to 1.10.
PullRequest: graal/19142
2 parents 6dcfa43 + 60c6777 commit 841b744

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

sdk/mx.sdk/mx_sdk_shaded.py

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,22 @@ def _collect_files(self):
200200

201201
# collect project files first, then extend with shaded (re)sources
202202
super()._collect_files()
203-
javafiles = self._javafiles
204-
non_javafiles = self._non_javafiles
205-
206203
proj = self.subject
207204
binDir = proj.output_dir()
205+
srcGenDir = proj.source_gen_dir()
206+
excludedPaths = proj.excluded_paths()
207+
includedPaths = proj.included_paths()
208+
209+
# remove possibly outdated src_gen files; they will be added below
210+
javafiles = self._javafiles = {src: out for src, out in self._javafiles.items() if not src.startswith(srcGenDir)}
211+
non_javafiles = self._non_javafiles = {src: out for src, out in self._non_javafiles.items() if not src.startswith(srcGenDir)}
212+
208213
for dep in proj.shaded_deps():
209214
srcFilePath = dep.get_source_path(False)
210215
if srcFilePath is None:
211216
continue
212217

213-
for zipFilePath, outDir in [(srcFilePath, proj.source_gen_dir())]:
214-
excludedPaths = proj.excluded_paths()
215-
includedPaths = proj.included_paths()
218+
for zipFilePath, outDir in [(srcFilePath, srcGenDir)]:
216219
try:
217220
with zipfile.ZipFile(zipFilePath, 'r') as zf:
218221
for zi in zf.infolist():
@@ -228,12 +231,20 @@ def _collect_files(self):
228231
if filepath.suffix not in ['.java', '.class'] and not any((i, path_mappings := m) for (i, m) in includedPaths.items() if glob_match(filepath, i)):
229232
continue
230233

234+
# strip any META-INF/versions/*/ prefix, will be flattened during build()
235+
if len(filepath.parts) > 3 and filepath.parts[0:2] == ('META-INF', 'versions') and filepath.parts[2].isdigit():
236+
java_version = int(filepath.parts[2])
237+
if java_version > proj.javaCompliance.value:
238+
# ignore versioned files whose version is higher than the project's javaCompliance
239+
continue
240+
old_filename = '/'.join(filepath.parts[3:])
241+
231242
new_filename = proj.substitute_path(old_filename, mappings=path_mappings)
232243
src_gen_path = os.path.join(outDir, new_filename)
233244
if filepath.suffix == '.java':
234-
javafiles.setdefault(src_gen_path, os.path.join(binDir, new_filename[:-len('.java')] + '.class'))
245+
javafiles[src_gen_path] = os.path.join(binDir, new_filename[:-len('.java')] + '.class')
235246
else:
236-
non_javafiles.setdefault(src_gen_path, os.path.join(binDir, new_filename))
247+
non_javafiles[src_gen_path] = os.path.join(binDir, new_filename)
237248
except FileNotFoundError:
238249
continue
239250
return self
@@ -244,6 +255,11 @@ def build(self):
244255
excludedPaths = dist.excluded_paths()
245256
includedPaths = dist.included_paths()
246257
patch = dist.shade.get('patch', {})
258+
# pre-compile regex patterns
259+
patchSubs = {
260+
filepattern: [(re.compile(srch, flags=re.MULTILINE), repl) for (srch, repl) in subs.items()]
261+
for filepattern, subs in patch.items()
262+
}
247263

248264
binDir = dist.output_dir()
249265
srcDir = dist.source_gen_dir()
@@ -269,6 +285,10 @@ def build(self):
269285
mx.abort(f'Cannot shade {dep} without a source jar (missing sourceDigest?)')
270286

271287
for zipFilePath, outDir in [(jarFilePath, binDir), (srcFilePath, srcDir)]:
288+
versioned_resources = {} # {old_filename: version}
289+
java_version_none = 0
290+
java_version_base = 8 # (0 < v < 9)
291+
272292
with zipfile.ZipFile(zipFilePath, 'r') as zf:
273293
for zi in zf.infolist():
274294
if zi.is_dir():
@@ -285,19 +305,37 @@ def build(self):
285305
mx.warn(f'file {old_filename} is not included (if this is intended, please add the file to the exclude list)')
286306
continue
287307

288-
new_filename = dist.substitute_path(old_filename, mappings=path_mappings)
289308
applicableSubs = []
290-
291309
if filepath.suffix == '.java':
292310
applicableSubs += javaSubstitutions
293311
if filepath.suffix == '.class':
294312
continue
295313

296-
mx.logv(f'extracting file {old_filename} to {new_filename}')
297-
extraPatches = [sub for filepattern, subs in patch.items() if glob_match(filepath, filepattern) for sub in subs.items()]
298-
extraSubs = list((re.compile(s, flags=re.MULTILINE), r) for (s, r) in extraPatches)
314+
# strip any META-INF/versions/*/ prefix and extract the file with the highest compatible version
315+
if len(filepath.parts) > 3 and filepath.parts[0:2] == ('META-INF', 'versions') and filepath.parts[2].isdigit():
316+
java_version = int(filepath.parts[2])
317+
# ignore versioned files whose version is higher than the project's javaCompliance
318+
if versioned_resources.get(old_filename, java_version_base) < java_version <= dist.javaCompliance.value:
319+
mx.logv(f"using versioned {old_filename} ({java_version} > {versioned_resources.get(old_filename, java_version_none)})")
320+
old_filename = '/'.join(filepath.parts[3:])
321+
versioned_resources[old_filename] = java_version
322+
else:
323+
if java_version > dist.javaCompliance.value:
324+
mx.logv(f"ignoring file {old_filename} due to the project's javaCompliance ({java_version} > {dist.javaCompliance.value})")
325+
continue
326+
else:
327+
if versioned_resources.get(old_filename, java_version_none) < java_version_base:
328+
versioned_resources[old_filename] = java_version_base
329+
else:
330+
mx.logv(f"skipping file {old_filename} replaced by META-INF/versions/{versioned_resources.get(old_filename)}")
331+
continue
332+
333+
new_filename = dist.substitute_path(old_filename, mappings=path_mappings)
334+
extraSubs = [sub for filepattern, subs in patchSubs.items() if glob_match(filepath, filepattern) for sub in subs]
299335
applicableSubs += extraSubs
300-
if old_filename == new_filename and len(applicableSubs) == 0:
336+
337+
mx.logv(f'extracting file {zi.filename} to {new_filename}')
338+
if zi.filename == new_filename and len(applicableSubs) == 0:
301339
# same file name, no substitutions: just extract
302340
zf.extract(zi, outDir)
303341
else:

truffle/mx.truffle/suite.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@
164164
},
165165
},
166166

167-
"XZ-1.9" : {
168-
"digest" : "sha512:a4362db234d4e83683e90f5baf90c82107450cc4404acab96e3fab14b8a3d4588a19722171d32f27d18463682a6994cad9af0b1065c954e3a77ea7bdcf586bac",
169-
"sourceDigest" : "sha512:5625f7392d1958466507f0530585c2c40c5b301981ab64115f489dc4599e3364acd7f00bfdc69aa3124d4641a562923dc3ee038faee5a52dd3cbe29a1a35e5ab",
167+
"XZ-1.10" : {
168+
"digest" : "sha512:af234bb2a5d42b355ea020c5b687268f0336e393eae69a05251677151d1e85b1e34999d5a6be6451e0b047e3cf13341dc227a5483553766252b0ea66025a44f9",
169+
"sourceDigest" : "sha512:19439a7f83d34528a3b457baec1a352901eb311c38ffeeea6aed6f49d91417207cf9798572cdbd6eae1769944dab692629dd7668f7a3073b30ba5d242cf6a4b2",
170170
"maven" : {
171171
"groupId" : "org.tukaani",
172172
"artifactId" : "xz",
173-
"version" : "1.9",
173+
"version" : "1.10",
174174
},
175175
},
176176

@@ -1261,7 +1261,7 @@
12611261
"javaCompliance" : "17+",
12621262
"spotbugsIgnoresGenerated" : True,
12631263
"shadedDependencies" : [
1264-
"truffle:XZ-1.9",
1264+
"truffle:XZ-1.10",
12651265
],
12661266
"class" : "ShadedLibraryProject",
12671267
"shade" : {

0 commit comments

Comments
 (0)