-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathx.py
More file actions
311 lines (267 loc) · 11.3 KB
/
Copy pathx.py
File metadata and controls
311 lines (267 loc) · 11.3 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
import os
import platform
import shutil
import subprocess
import sys
import re
from pathlib import Path
import tarfile
import zipfile
import datetime
# ============================================================
# Color Definitions
# ============================================================
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
WHITE = "\033[97m"
RESET = "\033[0m"
def ctext(text, color):
return f"{color}{text}{RESET}"
# ============================================================
# Paths
# ============================================================
ROOT = Path(__file__).parent.resolve()
DIST = ROOT / "dist"
ARIAJDK_DIR = DIST / "AriaJDK"
BIN_DIR = ARIAJDK_DIR / "bin"
LIB_DIR = ARIAJDK_DIR / "lib"
INCLUDE_DIR = ARIAJDK_DIR / "include"
# ============================================================
# Version Readers
# ============================================================
def get_version():
version_file = ROOT / "VERSION"
if version_file.exists():
return version_file.read_text().strip()
print(ctext("VERSION file not found, defaulting to 0.0.0", YELLOW))
return "0.0.0"
def get_java_version():
version_file = ROOT / "VERSION_JAVA"
if version_file.exists():
return version_file.read_text().strip()
print(ctext("VERSION_JAVA file not found, defaulting to unknown", YELLOW))
return "unknown"
# ============================================================
# Utility
# ============================================================
def run(cmd, cwd=None):
print(ctext(f"\n[RUN] {' '.join(cmd)}", YELLOW))
result = subprocess.run(cmd, cwd=cwd)
if result.returncode != 0:
print(ctext(f"\n✗ Error running {' '.join(cmd)}", RED))
sys.exit(result.returncode)
def check_java():
print(ctext("\nChecking Java environment...", WHITE))
java_home = os.environ.get("JAVA_HOME")
java_bin = None
if java_home:
java_home_bin = Path(java_home) / "bin" / ("java.exe" if os.name == "nt" else "java")
if java_home_bin.exists():
java_bin = str(java_home_bin)
if not java_bin:
java_bin = shutil.which("java")
if not java_bin:
print(ctext("JAVA_HOME is not set and 'java' command not found in PATH.", RED))
print(ctext("Please install a JDK and set JAVA_HOME before continuing.", YELLOW))
sys.exit(1)
result = subprocess.run([java_bin, "-version"], capture_output=True, text=True)
output = (result.stderr or result.stdout or "").strip()
if result.returncode != 0:
print(ctext("Unable to execute 'java -version'.", RED))
if output:
print(ctext(output, YELLOW))
sys.exit(1)
match = re.search(r'version\s+"([^"]+)"', output)
if not match:
print(ctext("Could not parse Java version from 'java -version' output.", RED))
print(ctext(output, YELLOW))
sys.exit(1)
detected_version = match.group(1)
print(ctext(f"Java environment OK (detected Java {detected_version}).", GREEN))
# ============================================================
# Build Functions
# ============================================================
def build_rust(path):
print(ctext(f"\nBuilding Rust project: {path.name}", WHITE))
run(["cargo", "build", "--release"], cwd=path)
def build_kotlin(path):
print(ctext(f"\nBuilding Kotlin project: {path.name}", WHITE))
gradlew = path / ("gradlew.bat" if os.name == "nt" else "gradlew")
if not gradlew.exists():
run(["gradle", "wrapper"], cwd=path)
if path.name == "fs":
ensure_gradle_wrapper_jar(path)
if os.name == "nt":
run([str(gradlew), "build"], cwd=path)
else:
run(["./gradlew", "build"], cwd=path)
def ensure_gradle_wrapper_jar(path):
source = ROOT / "classlib" / "gradle" / "wrapper" / "gradle-wrapper.jar"
dest = path / "gradle" / "wrapper" / "gradle-wrapper.jar"
if not dest.exists():
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(source, dest)
print(ctext(f"Copied gradle-wrapper.jar to {dest}", GREEN))
# ============================================================
# Output & Packaging
# ============================================================
def copy_outputs():
print(ctext("\nCollecting build outputs...", WHITE))
BIN_DIR.mkdir(parents=True, exist_ok=True)
LIB_DIR.mkdir(parents=True, exist_ok=True)
INCLUDE_DIR.mkdir(parents=True, exist_ok=True)
# Rust outputs
shutil.copy(ROOT / "launcher" / "target" / "release" / "aria", BIN_DIR / "java")
shutil.copy(ROOT / "tools" / "jar" / "target" / "release" / "jar", BIN_DIR / "jar")
shutil.copy(ROOT / "core" / "target" / "release" / "libaria_core.a", LIB_DIR / "libaria_core.a")
# Kotlin outputs
shutil.copy(ROOT / "tools" / "compiler" / "target" / "release" / "aria-javac", BIN_DIR / "javac")
shutil.copy(ROOT / "classlib" / "build" / "libs" / "classlib.jar", LIB_DIR / "aria-rt.jar")
shutil.copy(ROOT / "tools" / "fs" / "build" / "libs" / "jrt-fs.jar", LIB_DIR / "jrt-fs.jar")
# Copy all compiled class files to modules/
copy_classlib_modules()
# JNI Header
jni_source = ROOT / "core" / "include" / "jni.h"
if jni_source.exists():
shutil.copy(jni_source, INCLUDE_DIR / "jni.h")
print(ctext(f"Copied JNI header from {jni_source}", GREEN))
else:
(INCLUDE_DIR / "jni.h").write_text("// JNI header placeholder\n")
print(ctext("No JNI header found; created placeholder.", YELLOW))
# Metadata
aria_version = get_version()
java_version = get_java_version()
build_date = datetime.datetime.now().strftime("%Y-%m-%d")
release_content = f'''IMPLEMENTOR="Wave Foundation"
IMPLEMENTOR_VERSION="{aria_version}"
JAVA_VERSION="{java_version}"
OS_ARCH="{platform.machine()}"
OS_NAME="{platform.system().lower()}"
SOURCE="https://github.com/wavefnd/aria"
BUILD_DATE="{build_date}"
'''
(ARIAJDK_DIR / "release").write_text(release_content)
print(ctext("Created release metadata", GREEN))
add_jdk_signatures()
verify_runtime()
print(ctext(f"AriaJDK directory structure ready at {ARIAJDK_DIR}", GREEN))
def add_jdk_signatures():
print(ctext("\nVerifying JDK module signatures (JDK 17+ layout)...", WHITE))
jrt_fs = LIB_DIR / "jrt-fs.jar"
if not jrt_fs.exists():
print(ctext("✗ ERROR: lib/jrt-fs.jar not found!", RED))
print(ctext("Run build_kotlin('tools/fs') before packaging.", YELLOW))
sys.exit(1)
else:
print(ctext("Verified lib/jrt-fs.jar exists (from tools/fs)", GREEN))
conf_dir = ARIAJDK_DIR / "conf"
conf_dir.mkdir(exist_ok=True)
conf_file = conf_dir / "jvm.conf"
if not conf_file.exists():
conf_file.write_text("# AriaJDK configuration placeholder\n")
print(ctext("Created conf/jvm.conf", GREEN))
else:
print(ctext("conf/jvm.conf already exists", GREEN))
print(ctext("Skipped tools.jar (removed since JDK 9, AriaJDK targets 17+)", YELLOW))
# ============================================================
# Enhancements
# ============================================================
def verify_runtime():
print(ctext("\nVerifying AriaJDK runtime output...", WHITE))
java_exec = BIN_DIR / "java"
if not java_exec.exists():
print(ctext("Runtime binary not found!", RED))
sys.exit(1)
result = subprocess.run([str(java_exec), "-version"], capture_output=True, text=True)
output = result.stdout.strip() or result.stderr.strip()
if result.returncode == 0 and "AriaJDK" in output:
print(ctext("Runtime responded:\n", GREEN) + ctext(output, WHITE))
else:
print(ctext("Runtime did not respond with an AriaJDK signature.", RED))
if output:
print(ctext(output, YELLOW))
sys.exit(1)
def copy_classlib_modules():
print(ctext("\nCopying compiled classlib modules...", WHITE))
build_classes = ROOT / "classlib" / "build" / "classes" / "kotlin" / "main"
if not build_classes.exists():
print(ctext("No compiled classlib classes found.", RED))
sys.exit(1)
for class_file in build_classes.rglob("*.class"):
relative_path = class_file.relative_to(build_classes)
dest_path = LIB_DIR / "modules" / "java.base" / relative_path
dest_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(class_file, dest_path)
print(ctext(f"Copied {relative_path}", GREEN))
# ============================================================
# Packaging
# ============================================================
def make_archive():
print(ctext("\nPackaging AriaJDK...", WHITE))
system = platform.system().lower()
aria_version = get_version()
archive_name = f"ariajdk-{aria_version}-{system}"
if system == "windows":
archive_path = DIST / f"{archive_name}.zip"
with zipfile.ZipFile(archive_path, "w", zipfile.ZIP_DEFLATED) as zf:
for root, _, files in os.walk(ARIAJDK_DIR):
for file in files:
full_path = Path(root) / file
zf.write(full_path, arcname=str(full_path.relative_to(ARIAJDK_DIR.parent)))
else:
archive_path = DIST / f"{archive_name}.tar.gz"
with tarfile.open(archive_path, "w:gz") as tar:
tar.add(ARIAJDK_DIR, arcname="AriaJDK")
print(ctext(f"Created archive: {archive_path}", GREEN))
return archive_path
# ============================================================
# PATH Integration
# ============================================================
def add_to_path(bin_dir):
system = platform.system().lower()
print(ctext(f"\nAdding {bin_dir} to PATH...", WHITE))
if system == "windows":
run(["setx", "PATH", f"%PATH%;{bin_dir}"])
print(ctext("PATH updated. Restart terminal to apply.", GREEN))
elif system in ("linux", "darwin"):
shell_rc = Path.home() / (".bashrc" if "bash" in os.environ.get("SHELL", "") else ".zshrc")
with open(shell_rc, "a") as f:
f.write(f"\n# Added by AriaJDK installer\nexport PATH=\"$PATH:{bin_dir}\"\n")
print(ctext(f"PATH added to {shell_rc}. Restart shell to apply.", GREEN))
else:
print(ctext("Unknown OS, please add manually.", YELLOW))
# ============================================================
# Main
# ============================================================
def main():
print(ctext("AriaJDK Universal Builder & Installer", GREEN))
print(ctext("======================================", WHITE))
check_java()
if DIST.exists():
shutil.rmtree(DIST)
DIST.mkdir(parents=True, exist_ok=True)
build_rust(ROOT / "core")
build_kotlin(ROOT / "classlib")
build_kotlin(ROOT / "tools" / "fs")
build_rust(ROOT / "tools" / "jar")
build_rust(ROOT / "launcher")
build_rust(ROOT / "tools/compiler")
copy_outputs()
archive_path = make_archive()
add_to_path(BIN_DIR)
print(ctext("\nBuild completed successfully!", GREEN))
print(ctext(f"AriaJDK package: {archive_path}", WHITE))
# ============================================================
# Entry Point
# ============================================================
if __name__ == "__main__":
try:
main()
except Exception as e:
print(ctext(f"\nBuild failed: {e}", RED))
if DIST.exists():
print(ctext("Cleaning dist directory...", YELLOW))
shutil.rmtree(DIST, ignore_errors=True)
sys.exit(1)