1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#!/usr/bin/env python3
"""
Regenerate the jar list files in jars/ (the input to gen_class.py).
These files used to be written by hand; this script scans the local Gradle
module cache and rewrites them deterministically, so a Kotlin/AGP upgrade is:
1. bump the version in test_project (gradle/libs.versions.toml) and
Makefile (KOTLIN_VERSION / AGP_VERSION),
2. run the test_project once so Gradle populates the cache with the new
jars (even a red build resolves everything),
3. run this script (or just `make` - it regenerates the lists when the
pinned versions drift from jars/.stamp).
Files written (one jar path per line, '#' comments allowed downstream):
defs.txt every org.jetbrains.kotlin jar at the pinned Kotlin version,
sorted by jar name. gen_class.py takes the FIRST jar that
contains a class (first-wins), and the KGP jar bundles its
own copies of some compiler classes (e.g. K2JVMCompiler)
that DIFFER from the ones in kotlin-compiler-embeddable;
alphabetical order keeps kotlin-compiler-* ahead of
kotlin-gradle-plugin, which is the ordering the daemon
actually sees.
scan.txt the AGP core jars at the pinned AGP version (the classes
"AGP uses" - seed source for the full-closure mode).
Also writes <out-dir>/.stamp as "<kotlin-version> <agp-version>" so the
Makefile can detect version drift without parsing the lists.
"""
import argparse
import os
import re
import sys
# AGP core jars on the buildscript classpath (seed sources for
# gen_class.py's full-closure mode). A fixed allowlist rather than "every
# com.android.tools.build jar" so aaptcompiler/lint/... don't bloat S0.
SCAN_ARTIFACTS = [
"gradle",
"gradle-api",
"gradle-common-api",
"gradle-settings-api",
"builder",
"builder-model",
]
KOTLIN_GROUP = "org.jetbrains.kotlin"
AGP_GROUP = "com.android.tools.build"
def ver_key(v):
"""Sortable key for a version string: numeric parts first, then text
(so 2.4.10 > 2.4.9 and 2.4.21-rc1 sorts near 2.4.21)."""
m = re.match(r"(\d+(?:\.\d+)*)", v)
nums = [int(p) for p in m.group(1).split(".")] if m else []
return (nums, v)
def find_jars(cache, group, artifact, version):
"""All .jar files for group:artifact:version in the cache layout
<cache>/<group>/<artifact>/<version>/<sha1>/<name>.jar."""
base = os.path.join(cache, group, artifact, version)
if not os.path.isdir(base):
return []
out = []
for sha in os.listdir(base):
d = os.path.join(base, sha)
if not os.path.isdir(d):
continue
for f in os.listdir(d):
# binary jars only - skip -sources.jar / -javadoc.jar
if (f.endswith(".jar") and os.path.isfile(os.path.join(d, f))
and "-sources" not in f and "-javadoc" not in f):
out.append(os.path.join(d, f))
return out
def all_versions(cache, group, artifact):
base = os.path.join(cache, group, artifact)
if not os.path.isdir(base):
return []
return sorted(
(v for v in os.listdir(base)
if os.path.isdir(os.path.join(base, v)) and v[0].isdigit()),
key=ver_key,
)
def write_if_changed(path, lines):
content = "\n".join(lines) + "\n"
old = None
if os.path.exists(path):
with open(path) as fh:
old = fh.read()
if old == content:
print(f"unchanged {os.path.basename(path)} ({len(lines)} lines)")
return False
with open(path, "w") as fh:
fh.write(content)
print(f"wrote {os.path.basename(path)} ({len(lines)} lines)")
return True
def main():
ap = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--kotlin-version", required=True,
help="pinned Kotlin/KGP version, e.g. 2.4.21")
ap.add_argument("--agp-version", required=True,
help="pinned AGP version, e.g. 9.4.1")
ap.add_argument("--gradle-cache",
default=os.path.expanduser(
"~/.gradle/caches/modules-2/files-2.1"),
help="Gradle module cache root "
"(default: %(default)s)")
ap.add_argument("--out-dir", default="jars",
help="directory for defs.txt/scan.txt "
"(default: %(default)s)")
args = ap.parse_args()
cache = args.gradle_cache
if not os.path.isdir(cache):
sys.exit(f"error: Gradle cache not found: {cache}")
# ---- defs.txt: all org.jetbrains.kotlin jars at the pinned version ----
defs = []
missing = []
for artifact in sorted(os.listdir(os.path.join(cache, KOTLIN_GROUP))
if os.path.isdir(os.path.join(cache, KOTLIN_GROUP))
else []):
jars = find_jars(cache, KOTLIN_GROUP, artifact, args.kotlin_version)
if not jars:
continue
defs.extend(jars)
# Sanity-check the core artifacts (we can't know the full expected
# artifact set; these three must be there or the lists are useless).
for core in ("kotlin-compiler-embeddable", "kotlin-gradle-plugin",
"kotlin-stdlib"):
if not find_jars(cache, KOTLIN_GROUP, core, args.kotlin_version):
missing.append(f"{KOTLIN_GROUP}:{core}:{args.kotlin_version}")
defs.sort(key=os.path.basename)
if missing:
sys.exit(
"error: cache is missing "
+ ", ".join(missing)
+ f"\n run the test_project once with Kotlin {args.kotlin_version} "
"so Gradle downloads the jars, then re-run this script.")
defs_lines = [
f"# auto-generated by tools/gen_jar_lists.py - all {KOTLIN_GROUP} "
f"{args.kotlin_version} jars, sorted by jar name.",
"# First jar wins for duplicate class names; the sort keeps "
"kotlin-compiler-* ahead of kotlin-gradle-plugin (which bundles "
"its own copies of some compiler classes - the compiler jar must "
"win, it is what the daemon loads).",
*defs,
]
# ---- scan.txt: AGP core jars at the pinned AGP version ----
scan = []
missing_scan = []
for artifact in SCAN_ARTIFACTS:
jars = find_jars(cache, AGP_GROUP, artifact, args.agp_version)
if jars:
scan.extend(jars)
else:
missing_scan.append(f"{AGP_GROUP}:{artifact}:{args.agp_version}")
scan.sort(key=os.path.basename)
if missing_scan:
sys.exit(
"error: cache is missing "
+ ", ".join(missing_scan)
+ f"\n run the test_project once with AGP {args.agp_version} "
"so Gradle downloads the jars, then re-run this script.")
scan_lines = [
f"# auto-generated by tools/gen_jar_lists.py - AGP "
f"{args.agp_version} core jars on the buildscript classpath "
"(the classes AGP uses; seed source for full-closure mode).",
*scan,
]
# ---- write ----
os.makedirs(args.out_dir, exist_ok=True)
write_if_changed(os.path.join(args.out_dir, "defs.txt"), defs_lines)
write_if_changed(os.path.join(args.out_dir, "scan.txt"), scan_lines)
stamp = os.path.join(args.out_dir, ".stamp")
stamp_content = f"{args.kotlin_version} {args.agp_version}\n"
old = None
if os.path.exists(stamp):
with open(stamp) as fh:
old = fh.read()
if old != stamp_content:
with open(stamp, "w") as fh:
fh.write(stamp_content)
print(f"wrote .stamp ({stamp_content.strip()})")
else:
print("unchanged .stamp")
if __name__ == "__main__":
main()
|