|
| 1 | +use std::fs::{self, File}; |
| 2 | +use std::io::Write; |
| 3 | +use std::os::unix::fs::{symlink, PermissionsExt}; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | +use std::process::{Command, ExitCode}; |
| 6 | +use tempfile::TempDir; |
| 7 | + |
| 8 | +const EXE: &str = "opencode"; |
| 9 | +const PATCHELF: &str = "patchelf"; |
| 10 | +#[cfg(target_arch = "x86_64")] |
| 11 | +const LIBC_MUSL: &str = "libc.musl-x86_64.so.1"; |
| 12 | +#[cfg(target_arch = "aarch64")] |
| 13 | +const LIBC_MUSL: &str = "libc.musl-aarch64.so.1"; |
| 14 | +#[cfg(target_arch = "x86_64")] |
| 15 | +const LD_MUSL: &str = "ld-musl-x86_64.so.1"; |
| 16 | +#[cfg(target_arch = "aarch64")] |
| 17 | +const LD_MUSL: &str = "ld-musl-aarch64.so.1"; |
| 18 | +const LIBSTDCPP: &str = "libstdc++.so.6"; |
| 19 | +const LIBGCC: &str = "libgcc_s.so.1"; |
| 20 | + |
| 21 | +// Embed the files at compile time |
| 22 | +static EMBEDDED_EXE: &[u8] = include_bytes!(concat!("../embedded/", "opencode")); |
| 23 | +static EMBEDDED_PATCHELF: &[u8] = include_bytes!(concat!("../embedded/", "patchelf")); |
| 24 | +#[cfg(target_arch = "x86_64")] |
| 25 | +static EMBEDDED_LIBC_MUSL: &[u8] = include_bytes!(concat!("../embedded/", "libc.musl-x86_64.so.1")); |
| 26 | +#[cfg(target_arch = "aarch64")] |
| 27 | +static EMBEDDED_LIBC_MUSL: &[u8] = include_bytes!(concat!("../embedded/", "libc.musl-aarch64.so.1")); |
| 28 | +static EMBEDDED_LIBSTDCPP: &[u8] = include_bytes!(concat!("../embedded/", "libstdc++.so.6")); |
| 29 | +static EMBEDDED_LIBGCC: &[u8] = include_bytes!(concat!("../embedded/", "libgcc_s.so.1")); |
| 30 | + |
| 31 | +fn main() -> std::process::ExitCode { |
| 32 | + // Create temporary directory with automatic cleanup |
| 33 | + let tmp_dir = match TempDir::with_prefix("opencode-tmp-") { |
| 34 | + Ok(dir) => dir, |
| 35 | + Err(e) => { |
| 36 | + eprintln!("Error: {}", e); |
| 37 | + return ExitCode::FAILURE; |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + let result = (|| -> Result<ExitCode, Box<dyn std::error::Error>> { |
| 42 | + // Extract embedded files |
| 43 | + let exe_path = extract_file(tmp_dir.path(), EXE, EMBEDDED_EXE)?; |
| 44 | + let patchelf_path = extract_file(tmp_dir.path(), PATCHELF, EMBEDDED_PATCHELF)?; |
| 45 | + extract_file(tmp_dir.path(), LIBC_MUSL, EMBEDDED_LIBC_MUSL)?; |
| 46 | + extract_file(tmp_dir.path(), LIBSTDCPP, EMBEDDED_LIBSTDCPP)?; |
| 47 | + extract_file(tmp_dir.path(), LIBGCC, EMBEDDED_LIBGCC)?; |
| 48 | + |
| 49 | + // Create symlink for ld-musl (same as libc) |
| 50 | + let ld_path = tmp_dir.path().join(LD_MUSL); |
| 51 | + symlink(LIBC_MUSL, &ld_path)?; |
| 52 | + |
| 53 | + // Make executables executable (libraries don't need +x) |
| 54 | + set_executable(&exe_path)?; |
| 55 | + set_executable(&ld_path)?; |
| 56 | + set_executable(&patchelf_path)?; |
| 57 | + |
| 58 | + // Patch the ELF binary to use our embedded interpreter |
| 59 | + patch_elf( |
| 60 | + patchelf_path.to_str().ok_or("Invalid patchelf path")?, |
| 61 | + exe_path.to_str().ok_or("Invalid exe path")?, |
| 62 | + ld_path.to_str().ok_or("Invalid ld path")?, |
| 63 | + )?; |
| 64 | + |
| 65 | + // Collect command-line arguments (skip argv[0] which is this launcher) |
| 66 | + let args: Vec<String> = std::env::args().skip(1).collect(); |
| 67 | + |
| 68 | + // Launch the embedded exe |
| 69 | + let mut child = Command::new(&exe_path).args(&args).spawn()?; |
| 70 | + |
| 71 | + // Wait for the child process to complete |
| 72 | + let status = child.wait()?; |
| 73 | + |
| 74 | + Ok(ExitCode::from(status.code().unwrap_or(1) as u8)) |
| 75 | + })(); |
| 76 | + |
| 77 | + match result { |
| 78 | + Ok(code) => code, |
| 79 | + Err(e) => { |
| 80 | + eprintln!("Error: {}", e); |
| 81 | + ExitCode::FAILURE |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn extract_file( |
| 87 | + dir: &Path, |
| 88 | + name: &str, |
| 89 | + data: &[u8], |
| 90 | +) -> Result<PathBuf, Box<dyn std::error::Error>> { |
| 91 | + let path = dir.join(name); |
| 92 | + let mut file = File::create(&path)?; |
| 93 | + file.write_all(data)?; |
| 94 | + Ok(path) |
| 95 | +} |
| 96 | + |
| 97 | +fn set_executable(path: &Path) -> Result<(), Box<dyn std::error::Error>> { |
| 98 | + let mut perms = fs::metadata(path)?.permissions(); |
| 99 | + perms.set_mode(0o755); |
| 100 | + fs::set_permissions(path, perms)?; |
| 101 | + Ok(()) |
| 102 | +} |
| 103 | + |
| 104 | +fn patch_elf( |
| 105 | + patchelf_path: &str, |
| 106 | + elf_path: &str, |
| 107 | + ld_path: &str, |
| 108 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 109 | + let status = Command::new(ld_path) |
| 110 | + .arg(patchelf_path) |
| 111 | + .arg("--set-interpreter") |
| 112 | + .arg(ld_path) |
| 113 | + .arg(elf_path) |
| 114 | + .status()?; |
| 115 | + |
| 116 | + if status.success() { |
| 117 | + return Ok(()); |
| 118 | + } |
| 119 | + |
| 120 | + Err("patchelf failed".into()) |
| 121 | +} |
0 commit comments