All files / src/lib/compiler romUsage.ts

97.82% Statements 45/46
81.48% Branches 22/27
77.77% Functions 7/9
97.72% Lines 43/44

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 1443x 3x 3x 3x 3x 3x 3x                                                   6x   3x 36x 1x     35x   35x 4x     31x     3x 14x   14x           1x     13x 23x     23x         5x   18x             3x     3x           1x   1x 1x         1x 1x 1x   1x             1x       1x                     1x 1x 1x           1x     1x         1x   1x     3x  
import fs from "fs-extra";
import Path from "path";
import ensureBuildTools from "lib/compiler/ensureBuildTools";
import spawn from "lib/helpers/cli/spawn";
import l10n from "shared/lib/lang/l10n";
import { envWith } from "lib/helpers/cli/env";
import {
  ABSOLUTE_DATA_SIZE,
  SHADOW_OAM_ADDRESS,
  SHADOW_OAM_SIZE,
  STACK_RESERVE_BYTES,
  STACK_TOP_ADDRESS,
} from "shared/lib/compiler/memoryLayout";
import type { BuildManifest } from "lib/compiler/buildManifest";
 
type RomUsageOptions = {
  manifest: BuildManifest;
  tmpPath: string;
  progress: (msg: string) => void;
  warnings: (msg: string) => void;
};
 
export type RomUsageBank = {
  name: string;
  size: number;
  used: number;
};
 
export type RomUsageData = {
  banks: RomUsageBank[];
};
 
const hex = (value: number) => value.toString(16).toUpperCase();
 
const parseBankValue = (value: string) => {
  if (value.trim() === "") {
    throw new Error("Invalid bank value");
  }
 
  const number = Number(value);
 
  if (!Number.isSafeInteger(number) || number < 0) {
    throw new Error(`Invalid bank value: ${String(value)}`);
  }
 
  return number;
};
 
export const parseRomUsage = (output: string): RomUsageData => {
  const parsed: unknown = JSON.parse(output);
 
  if (
    !parsed ||
    typeof parsed !== "object" ||
    !("banks" in parsed) ||
    !Array.isArray(parsed.banks)
  ) {
    throw new Error("romusage output does not contain a bank list");
  }
 
  const banks = parsed.banks.map((bank, index) => {
    Iif (!bank || typeof bank !== "object" || !("name" in bank)) {
      throw new Error(`Invalid romusage bank at index ${index}`);
    }
    if (
      typeof bank.name !== "string" ||
      typeof bank.size !== "string" ||
      typeof bank.used !== "string"
    ) {
      throw new Error(`Invalid romusage bank data at index ${index}`);
    }
    return {
      name: bank.name,
      size: parseBankValue(bank.size),
      used: parseBankValue(bank.used),
    };
  });
 
  return { banks };
};
 
const romUsage = async ({
  manifest,
  tmpPath = "/tmp",
  warnings = (_msg) => {},
  progress = (_msg) => {},
}: RomUsageOptions): Promise<RomUsageData> => {
  const env = { ...process.env };
 
  const buildToolsPath = await ensureBuildTools(tmpPath);
  const buildToolsVersion = await fs.readFile(
    `${buildToolsPath}/tools_version`,
    "utf8",
  );
 
  env.PATH = envWith([Path.join(buildToolsPath, "gbdk", "bin")]);
  env.GBDKDIR = `${buildToolsPath}/gbdk/`;
  env.GBS_TOOLS_VERSION = buildToolsVersion;
 
  const options = {
    cwd: manifest.buildRoot,
    env,
    shell: true,
  };
 
  const romusageCommand =
    process.platform === "win32"
      ? `"${buildToolsPath}\\gbdk\\bin\\romusage.exe"`
      : "romusage";
 
  const romusageArgs = [
    `"${manifest.artifacts.mapPath}"`,
    `-sJ`,
    `-Q`,
    `-e:SHADOW_OAM:${hex(SHADOW_OAM_ADDRESS)}:${hex(SHADOW_OAM_SIZE)}`,
    `-e:STACK:${hex(STACK_TOP_ADDRESS - STACK_RESERVE_BYTES)}:${hex(
      STACK_RESERVE_BYTES,
    )}`,
    `-e:ABSOLUTE_DATA:${hex(STACK_TOP_ADDRESS)}:${hex(ABSOLUTE_DATA_SIZE)}`,
  ];
 
  let output = "";
  progress(`${l10n("COMPILER_ROMUSAGE")}...`);
  const { completed: romusageCompleted } = spawn(
    romusageCommand,
    romusageArgs,
    options,
    {
      onLog: (msg) => {
        output += msg;
      },
      onError: (msg) => {
        warnings(msg);
      },
    },
  );
 
  await romusageCompleted;
 
  return parseRomUsage(output);
};
 
export default romUsage;