All files / src/lib/compiler buildMakeScript.ts

68.25% Statements 43/63
21.05% Branches 12/57
50% Functions 3/6
64.91% Lines 37/57

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 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 2281x 1x 1x 1x                         1x 3x           1x                         1x       1x     1x       1x 1x       1x   1x 1x                 1x   1x       1x       1x   1x       1x 1x   1x                     1x       1x             1x                   1x     1x 1x 1x       1x 1x   1x   1x     1x                                                                                                                               1x                       1x                                      
import { glob } from "lib/helpers/glob";
import { pathExists, readFile, writeFile } from "fs-extra";
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";
  cartType: "mbc3" | "mbc5";
  compilerPreset: number;
};
 
export const objectPathForSource = (buildRoot: string, filename: string) =>
  Path.join(
    buildRoot,
    "obj",
    `${Path.basename(filename, Path.extname(filename))}.o`,
  );
 
export const getBuildCommands = async (
  buildRoot: string,
  {
    colorEnabled,
    sgb,
    debug,
    platform,
    batteryless,
    targetPlatform,
    cartType,
    compilerPreset,
  }: BuildOptions,
) => {
  const buildFiles = await glob("src/**/*.@(c|s)", {
    cwd: buildRoot,
    absolute: true,
  });
  const output = [];
 
  const CC =
    platform === "win32"
      ? `..\\_gbstools\\gbdk\\bin\\lcc`
      : `../_gbstools/gbdk/bin/lcc`;
 
  for (const file of buildFiles) {
    Iif (file.indexOf("GBT_PLAYER") !== -1) {
      continue;
    }
 
    const objFile = objectPathForSource(buildRoot, file);
 
    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 = async (buildRoot: string) => {
  const output = [];
  const buildFiles = await glob("src/**/*.@(c|s)", {
    cwd: buildRoot,
    absolute: true,
  });
  for (const file of buildFiles) {
    const objFile = objectPathForSource(buildRoot, file);
 
    output.push(objFile);
  }
  return output.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(" ")
  );
};