Home /Modules/operating-system/SLISOBuilder.sleela

SLISOBuilder.sleela

lib/operating-system/SLISOBuilder.sleela · 186 line(s) · sleela

<< lib/operating-system/SLISOBuilder.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLISOBuilder — the stage that turns a compiled SLOSModel
     into a bootable ISO image descriptor. It assembles the root filesystem from
     the model's directory layout and package seed (the debootstrap-style
     compositional assembly), lays down the EFI System Partition and installs
     the bootloader the model names, compiles the emitted C/C++ early-boot
     sources into the image, and emits the ISO build recipe (an mkisofs/xorriso
     invocation with the right volume label, boot catalog, and EFI image) as a
     script artifact. The result is an SLOSArtifact of KIND_ISO describing the
     final image plus the exact commands that produce it.
   
   It calls the established ISO contract rather than inventing a competing one:
   the recipe drives xorriso/mkisofs the same way the OS lineage's gen-iso.sh
   does, so the SLeeLa model becomes a real .iso through the existing toolchain.
   
   Family: operating-system. Consumes SLOSModel + SLOSCompiler; produces an SLOSArtifact. >>
#sleela 2.5
material;
class SLISOBuilder {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10898;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-10";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  < where the rootfs is assembled >
  String stagingRoot;
  int configured;

  void configure() { stagingRoot = "staging/rootfs"; configured = 1; }
  void stageAt(String dir) { stagingRoot = dir; }

  < Build the ISO descriptor + recipe for a model that was already compiled by >
  < the given SLOSCompiler (so the C/C++ artifacts exist to be built in). >
  SLOSArtifact build(SLOSModel m, SLOSCompiler compiler) {
    String vol = volumeLabel(m);
    String recipe = "#!/bin/sh\n";
    recipe = recipe + "# gen-iso recipe generated by SLeeLa lib/os for " + m.identity() + "\n";
    recipe = recipe + "set -eu\n";
    recipe = recipe + "STAGE=\"" + stagingRoot + "\"\n";
    recipe = recipe + "OUT=\"" + isoFileName(m) + "\"\n\n";

    recipe = recipe + "# 1. Assemble the root filesystem tree.\n";
    recipe = recipe + emitMkdirs(m);
    recipe = recipe + "\n# 2. Seed the base + layered packages into the rootfs.\n";
    recipe = recipe + emitPackageInstall(m);

    recipe = recipe + "\n# 3. Build the emitted early-boot C/C++ into the image.\n";
    recipe = recipe + "cc -c gen/os_boot.c -o \"$STAGE/boot/os_boot.o\"\n";
    recipe = recipe + "c++ -c gen/os_image.cpp -o \"$STAGE/boot/os_image.o\"\n";

    recipe = recipe + "\n# 4. Lay down the EFI System Partition and install the loader.\n";
    recipe = recipe + "mkdir -p \"$STAGE/boot/efi/EFI/BOOT\"\n";
    recipe = recipe + "install-loader --loader \"" + m.bootloader().loaderName()
                    + "\" --firmware \"" + m.bootloader().firmwareName()
                    + "\" --esp \"$STAGE/boot/efi\"\n";

    recipe = recipe + "\n# 5. Author the ISO with xorriso (El Torito + EFI).\n";
    recipe = recipe + "xorriso -as mkisofs \\\n";
    recipe = recipe + "  -V \"" + vol + "\" \\\n";
    recipe = recipe + "  -o \"$OUT\" \\\n";
    recipe = recipe + "  -e boot/efi/EFI/BOOT/BOOTX64.EFI -no-emul-boot \\\n";
    recipe = recipe + "  -J -R \"$STAGE\"\n";
    recipe = recipe + "echo \"wrote $OUT\"\n";

    SLOSArtifact iso = new SLOSArtifact();
    iso.configure(isoFileName(m), iso.KIND_ISO, recipe);
    return iso;
  }

  < ---- Recipe fragments --------------------------------------------------- >
  < One `mkdir -p` per directory in the model's filesystem manifest. >
  String emitMkdirs(SLOSModel m) {
    String tree = m.layout().manifest();
    String out = "";
    String line = "";
    int i = 0;
    int n = strLen(tree);
    while (i <= n) {
      String ch = charAt(tree, i);
      if (ch == "\n" || i == n) {
        if (strLen(line) > 0) {
          out = out + "mkdir -p \"$STAGE" + normalize(line) + "\"\n";
        }
        line = "";
      } else {
        line = line + ch;
      }
      i = i + 1;
    }
    return out;
  }

  < One package-install line per entry in the model's package manifest. >
  String emitPackageInstall(SLOSModel m) {
    String pkgs = m.packageSet().packages();
    String out = "";
    String line = "";
    int i = 0;
    int n = strLen(pkgs);
    while (i <= n) {
      String ch = charAt(pkgs, i);
      if (ch == "\n" || i == n) {
        if (strLen(line) > 0) {
          out = out + "seed-package --root \"$STAGE\" " + line + "\n";
        }
        line = "";
      } else {
        line = line + ch;
      }
      i = i + 1;
    }
    return out;
  }

  < Turn a Windows-style path into a POSIX staging subpath so the recipe is >
  < uniform (the ISO is authored on a POSIX builder regardless of shape). >
  String normalize(String path) {
    if (strLen(path) >= 2) {
      if (substr(path, 1, 2) == ":") {
        < "C:\Windows\System32" -> "/C/Windows/System32" >
        return "/" + substr(path, 0, 1) + replaceBackslash(substr(path, 2, strLen(path)));
      }
    }
    return path;
  }
  String replaceBackslash(String s) {
    String out = "";
    int i = 0;
    int n = strLen(s);
    while (i < n) {
      String ch = charAt(s, i);
      if (ch == "\\") { out = out + "/"; } else { out = out + ch; }
      i = i + 1;
    }
    return out;
  }

  String volumeLabel(SLOSModel m) { return m.name() + "-" + m.osVersion(); }
  String isoFileName(SLOSModel m) {
    return m.name() + "-" + m.osVersion() + "-"
         + m.architectures().primaryArch().arch() + ".iso";
  }

  < String primitives wrap the shared native text intrinsics. >
  int strLen(String s) { return native sleela_str_length(s); }
  String substr(String s, int a, int b) { return native sleela_str_substring(s, a, b); }
  String charAt(String s, int i) { return substr(s, i, i + 1); }
}