All files / src/lib/compiler buildMakeScript.ts

70.37% Statements 38/54
45.45% Branches 25/55
66.66% Functions 4/6
66.66% Lines 32/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 2032x   2x 2x                       2x                       3x     3x       3x   3x 3x 3x   3x 3x                 3x   3x       3x       3x   3x       3x 3x   3x                     3x       3x             3x                   3x     2x 2x   2x                         1x       1x 1x                                                                                           2x                       2x                                      
import { pathExists, readFile, writeFile } from "fs-extra";
import type { BuildManifest } from "lib/compiler/buildManifest";
import Path from "path";
import l10n from "shared/lib/lang/l10n";
 
type BuildOptions = {
  colorEnabled: boolean;
  sgb: boolean;
  debug: boolean;
  platform: string;
  batteryless: boolean;
  targetPlatform: "gb" | "pocket";
  compilerPreset: number;
};
 
export const getBuildCommands = async (
  manifest: BuildManifest,
  {
    colorEnabled,
    sgb,
    debug,
    platform,
    batteryless,
    targetPlatform,
    compilerPreset,
  }: BuildOptions,
) => {
  const output = [];
 
  const CC =
    platform === "win32"
      ? `..\\_gbstools\\gbdk\\bin\\lcc`
      : `../_gbstools/gbdk/bin/lcc`;
 
  const { sources, buildRoot, cartType } = manifest;
 
  for (const source of sources) {
    const file = Path.join(buildRoot, source.sourcePath);
    const objFile = source.objectPath;
 
    Eif (!(await pathExists(objFile))) {
      const buildArgs = [
        `-Iinclude`,
        `-Wa-Iinclude`,
        `-Wa-I../_gbstools/gbdk/lib/small/asxxxx`,
        `-Wl-a`,
        `-Wf-MMD`,
        `-c`,
      ];
 
      buildArgs.push(`-Wf"--max-allocs-per-node ${compilerPreset ?? 3000}"`);
 
      Iif (colorEnabled) {
        buildArgs.push("-DCGB");
      }
 
      Iif (sgb) {
        buildArgs.push("-DSGB");
      }
 
      buildArgs.push("-DHUGE_TRACKER");
 
      Iif (batteryless) {
        buildArgs.push("-DBATTERYLESS");
      }
 
      const rumbleBit = cartType === "mbc3" ? "0x20u" : "0x08u";
      buildArgs.push(`-DRUMBLE_ENABLE=${rumbleBit}`);
 
      Iif (debug) {
        buildArgs.push("-Wf--fverbose-asm");
        buildArgs.push("-Wf--debug");
        buildArgs.push("-Wl-m");
        buildArgs.push("-Wl-w");
        buildArgs.push("-Wl-y");
        buildArgs.push("-DVM_DEBUG_OUTPUT");
        buildArgs.push("-Wf--nolospre");
        buildArgs.push("-Wf--nogcse");
      }
 
      Iif (targetPlatform === "pocket") {
        buildArgs.push("-msm83:ap");
      }
 
      buildArgs.push(
        "-c",
        "-o",
        Path.relative(buildRoot, objFile),
        Path.relative(buildRoot, file),
      );
 
      output.push({
        label: `${l10n("COMPILER_COMPILING")}: ${Path.relative(
          buildRoot,
          file,
        )}`,
        command: CC,
        args: buildArgs,
      });
    }
  }
  return output;
};
 
export const buildLinkFile = (manifest: BuildManifest) =>
  manifest.sources.map((source) => source.objectPath).join("\n");
 
export const buildLinkFlags = (
  linkFile: string,
  romFilename: string,
  name = "GBSTUDIO",
  cartType: string,
  color = false,
  sgb = false,
  colorOnly = false,
  batteryless = false,
  debug = false,
  targetPlatform = "gb",
) => {
  const validName =
    name
      .toUpperCase()
      .replace(/[^A-Z]*/g, "")
      .substring(0, 15) || "GBSTUDIO";
  const cart = cartType === "mbc3" ? "0x10" : "0x1E";
  return ([] as Array<string>).concat(
    // General
    [
      // Cart
      `-Wm-yt${cart}`,
      // Banks
      "-autobank",
      "-Wb-ext=.rel",
      "-Wm-yoA",
      "-Wm-ya4",
      // Symbols
      "-Wl-j",
      "-Wl-m",
      "-Wl-w",
      "-Wm-yS",
      "-Wl-klib",
      "-Wl-g.STACK=0xDF00",
      "-Wi-e",
      `-Wm-yn"${validName}"`,
    ],
    // Color
    colorOnly ? ["-Wm-yC"] : color ? ["-Wm-yc"] : [],
    // SGB
    sgb ? ["-Wm-ys"] : [],
    // Pocket
    targetPlatform === "pocket" ? ["-msm83:ap"] : [],
    // Debug emulicious
    debug ? ["-Wf--debug", "-Wl-m", "-Wl-w", "-Wl-y"] : [],
    // Music Driver hugetracker
    ["-Wl-lhUGEDriver.lib"],
    // Batteryless cart
    batteryless
      ? [
          "-Wb-reserve=15:4000",
          "-Wb-reserve=14:4000",
          "-Wb-reserve=13:4000",
          "-Wb-reserve=12:4000",
          "-Wl-g__start_save=12",
        ]
      : ["-Wl-g__start_save=0"],
    // Output
    ["-o", `"build/rom/${romFilename}"`],
    [`-Wl-f${linkFile}`],
  );
};
 
export const makefileInjectToolsPath = async (
  filename: string,
  buildToolsPath: string,
) => {
  const makefile = await readFile(filename, "utf8");
  const updatedMakefile = makefile.replace(
    /GBSTOOLS_DIR =.*/,
    `GBSTOOLS_DIR = ${Path.normalize(buildToolsPath)}`,
  );
  await writeFile(filename, updatedMakefile);
};
 
export const buildMakeDotBuildFile = ({
  cartType = "mbc5",
  color = false,
  sgb = false,
  batteryless = false,
}) => {
  return (
    `settings: ` +
    ([] as Array<string>)
      .concat(
        color ? ["CGB"] : ["DMG"],
        sgb ? ["SGB"] : [],
        ["hUGE"],
        cartType === "mbc3" ? ["MBC3"] : ["MBC5"],
        batteryless ? ["batteryless"] : [],
      )
      .join(" ")
  );
};