Skip to content

Commit

Permalink
Add step to validate build files before compiling with GBDK, currentl…
Browse files Browse the repository at this point in the history
…y just confirms that MAX_GLOBAL_VARS is not larger than VM_HEAP_SIZE
  • Loading branch information
chrismaltby committed Mar 18, 2024
1 parent a56a336 commit 03af18f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/bin/gb-studio-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import makeBuild from "lib/compiler/makeBuild";
import initElectronL10N from "lib/lang/initElectronL10N";
import { loadEngineFields } from "lib/project/engineFields";
import loadAllScriptEventHandlers from "lib/project/loadScriptEventHandlers";
import { validateEjectedBuild } from "lib/compiler/validate/validateEjectedBuild";

const rmdir = promisify(rimraf);

Expand Down Expand Up @@ -74,6 +75,12 @@ const main = async (
warnings,
});

await validateEjectedBuild({
buildRoot: tmpBuildDir,
progress,
warnings,
});

if (command === "export") {
if (program.onlyData) {
// Export src/data and include/data to destination
Expand Down
6 changes: 6 additions & 0 deletions src/lib/compiler/buildProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import copy from "lib/helpers/fsCopy";
import type { ProjectData } from "store/features/project/projectActions";
import type { EngineFieldSchema } from "store/features/engine/engineState";
import { ScriptEventHandlers } from "lib/project/loadScriptEventHandlers";
import { validateEjectedBuild } from "lib/compiler/validate/validateEjectedBuild";

type BuildOptions = {
buildType: "rom" | "web" | "pocket";
Expand Down Expand Up @@ -53,6 +54,11 @@ const buildProject = async (
progress,
warnings,
});
await validateEjectedBuild({
buildRoot: outputRoot,
progress,
warnings,
});
await makeBuild({
buildRoot: outputRoot,
tmpPath,
Expand Down
39 changes: 39 additions & 0 deletions src/lib/compiler/validate/validateEjectedBuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { readFile } from "fs-extra";
import Path from "path";

type ValidateOptions = {
buildRoot: string;
progress: (msg: string) => void;
warnings: (msg: string) => void;
};

export const validateEjectedBuild = async ({
buildRoot,
progress = (_msg) => {},
warnings = (_msg) => {},
}: ValidateOptions) => {
const vmIncludePath = Path.join(buildRoot, "include/vm.h");
const gameGlobalsPath = Path.join(buildRoot, "include/data/game_globals.i");
const vmInclude = await readFile(vmIncludePath, "utf8");
const gameGlobals = await readFile(gameGlobalsPath, "utf8");

const vmHeapSizeStr = vmInclude.match(/#define VM_HEAP_SIZE (\d+)/m)?.[1];
const maxGlobalVarsStr = gameGlobals.match(/MAX_GLOBAL_VARS = (\d+)/m)?.[1];

const vmHeapSize = parseInt(vmHeapSizeStr ?? "", 10);
const maxGlobalVars = parseInt(maxGlobalVarsStr ?? "", 10);

progress(`Validating build files...`);

if (isNaN(vmHeapSize) || isNaN(maxGlobalVars)) {
warnings(
"Unable to read VM_HEAP_SIZE and MAX_GLOBAL_VARS to determine if project contains too many unique variables"
);
}

if (maxGlobalVars > vmHeapSize) {
warnings(
`Your project contains too many unique variables and will not work as expected. VM_HEAP_SIZE defines the maximum amount of variables allowed ${vmHeapSize} but your project contained ${maxGlobalVars} unique variables.`
);
}
};

0 comments on commit 03af18f

Please sign in to comment.