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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | 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 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 4x 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 1x 1x | import fs from "fs-extra";
import os from "os";
import Path from "path";
import {
buildLinkFile,
buildLinkFlags,
getBuildCommands,
} from "./buildMakeScript";
import { cacheObjData, fetchCachedObjData } from "./objCache";
import ensureBuildTools from "./ensureBuildTools";
import {
getDevKitProPaths,
isUsableGcc,
validateDevKitPro,
} from "lib/helpers/devkitpro";
import spawn, { ChildProcess } from "lib/helpers/cli/spawn";
import l10n from "shared/lib/lang/l10n";
import { ProjectResources } from "shared/lib/resources/types";
import psTree from "ps-tree";
import { promisify } from "util";
import { envWith } from "lib/helpers/cli/env";
const psTreeAsync = promisify(psTree);
type MakeOptions = {
buildRoot: string;
romFilename: string;
tmpPath: string;
data: ProjectResources;
buildType: "rom" | "web" | "pocket" | "gba";
debug: boolean;
progress: (msg: string) => void;
warnings: (msg: string) => void;
};
const cpuCount = os.cpus().length;
const childSet = new Set<ChildProcess>();
let cancelling = false;
const makeBuild = async ({
buildRoot = "/tmp",
tmpPath = "/tmp",
romFilename,
data,
debug = false,
buildType = "rom",
progress = (_msg) => {},
warnings = (_msg) => {},
}: MakeOptions) => {
cancelling = false;
const env = { ...process.env };
const { settings } = data;
const colorEnabled = settings.colorMode !== "mono";
const sgbEnabled = settings.sgbEnabled && settings.colorMode !== "color";
const colorOnly = settings.colorMode === "color";
const targetPlatform =
buildType === "gba" ? "gba" : buildType === "pocket" ? "pocket" : "gb";
const batterylessEnabled = settings.batterylessEnabled && buildType !== "web";
const buildToolsPath = await ensureBuildTools(tmpPath);
const buildToolsVersion = await fs.readFile(
`${buildToolsPath}/tools_version`,
"utf8",
);
const isGBA = targetPlatform === "gba";
if (isGBA) {
// GBA build setup - prefer system devkitPro, but fall back to bundled
// buildTools/devkitARM if devkitPro is not installed on the host.
const devkitPaths = getDevKitProPaths();
if (devkitPaths.isValid) {
env.PATH = envWith([Path.join(devkitPaths.devkitArm, "bin")]);
env.DEVKITPRO = devkitPaths.devkitPro;
env.DEVKITARM = devkitPaths.devkitArm;
// Ensure process.env also contains these for helpers that read it
process.env.DEVKITPRO = devkitPaths.devkitPro;
process.env.DEVKITARM = devkitPaths.devkitArm;
} else E{
// Fallback to bundled devkit in tmp/_gbstools
const bundledDevkitArm = Path.join(buildToolsPath, "devkitARM");
const bundledDevkitArmAlt = Path.join(buildToolsPath, "devkitarm");
const devkitArmPath = (await fs.pathExists(bundledDevkitArm))
? bundledDevkitArm
: (await fs.pathExists(bundledDevkitArmAlt))
? bundledDevkitArmAlt
: "";
const bundledGcc = devkitArmPath
? Path.join(
devkitArmPath,
"bin",
process.platform === "win32"
? "arm-none-eabi-gcc.exe"
: "arm-none-eabi-gcc",
)
: "";
if (devkitArmPath && isUsableGcc(bundledGcc)) {
env.PATH = envWith([Path.join(devkitArmPath, "bin")]);
env.DEVKITPRO = buildToolsPath;
env.DEVKITARM = devkitArmPath;
// Also set process.env so getBuildCommands can detect the fallback toolchain
process.env.DEVKITPRO = buildToolsPath;
process.env.DEVKITARM = devkitArmPath;
// Do not throw here; proceed using bundled toolchain
} else {
// No devkit available
validateDevKitPro();
}
}
env.GBA_TOOLS_VERSION = buildToolsVersion;
env.TARGET_PLATFORM = "gba";
} else E{
// Original GB build setup
env.PATH = envWith([Path.join(buildToolsPath, "gbdk", "bin")]);
env.GBDKDIR = `${buildToolsPath}/gbdk/`;
env.TARGET_PLATFORM = targetPlatform;
}
env.GBS_TOOLS_VERSION = buildToolsVersion;
env.TARGET_PLATFORM = targetPlatform;
env.CART_TYPE = settings.cartType || "mbc5";
env.TMP = tmpPath;
env.TEMP = tmpPath;
if (colorEnabled) {
env.COLOR = "true";
}
Iif (sgbEnabled) {
env.SGB = "true";
}
Iif (batterylessEnabled) {
env.BATTERYLESS = "true";
}
env.COLOR_MODE = settings.colorMode;
env.MUSIC_DRIVER = settings.musicDriver;
Iif (debug) {
env.DEBUG = "true";
}
if (settings.musicDriver === "huge") {
env.MUSIC_DRIVER = "HUGE_TRACKER";
} else E{
env.MUSIC_DRIVER = "GBT_PLAYER";
}
Iif (settings.cartType === "mbc3") {
env.RUMBLE_ENABLE = "0x20";
} else {
env.RUMBLE_ENABLE = "0x08";
}
env.GBDK_COMPILER_PRESET = String(settings.compilerPreset);
// Populate /obj with cached data
await fetchCachedObjData(buildRoot, tmpPath, env);
// Compile Source Files
const makeCommands = await getBuildCommands(buildRoot, {
colorEnabled,
sgb: sgbEnabled,
musicDriver: settings.musicDriver,
batteryless: batterylessEnabled,
debug,
platform: process.platform,
targetPlatform,
cartType: settings.cartType,
compilerPreset: settings.compilerPreset,
});
const options = {
cwd: buildRoot,
env,
// GBA tools are native executables. Running them through cmd.exe causes
// arguments such as a user-defined ROM filename containing spaces to be
// split before gcc/objcopy/gbafix receive them. In particular, gbafix then
// reports "Error opening input file!" and exits with -1 (4294967295 on
// Windows) even though linking itself succeeded.
shell: !isGBA,
};
// Build source files in parallel
const concurrency = cpuCount;
await Promise.all(
Array(concurrency)
.fill(makeCommands.entries())
.map(async (cursor) => {
for (const [_, makeCommand] of cursor) {
Iif (cancelling) {
throw new Error("BUILD_CANCELLED");
}
try {
progress(makeCommand.label);
} catch (e) {
throw e;
}
const { child, completed } = spawn(
makeCommand.command,
makeCommand.args,
options,
{
onLog: (msg) => warnings(msg), // LCC writes errors to stdout
onError: (msg) => warnings(msg),
},
);
childSet.add(child);
await completed;
childSet.delete(child);
}
}),
);
// GBSPack ---
Iif (cancelling) {
throw new Error("BUILD_CANCELLED");
}
// Link ROM ---
Iif (cancelling) {
throw new Error("BUILD_CANCELLED");
}
progress(`${l10n("COMPILER_LINKING")}...`);
const linkFile = await buildLinkFile(buildRoot, targetPlatform);
const linkFilePath = `${buildRoot}/obj/linkfile.lk`;
await fs.writeFile(linkFilePath, linkFile);
let linkCommand: string;
if (isGBA) {
const devkitPaths = getDevKitProPaths();
linkCommand = devkitPaths.gccPath;
} else E{
linkCommand =
process.platform === "win32"
? `..\\_gbstools\\gbdk\\bin\\lcc.exe`
: `../_gbstools/gbdk/bin/lcc`;
}
const linkArgs = buildLinkFlags(
linkFilePath,
romFilename,
data.metadata.name || "GBStudio",
settings.cartType,
colorEnabled,
sgbEnabled,
colorOnly,
settings.musicDriver,
batterylessEnabled,
debug,
targetPlatform,
);
const { completed: linkCompleted, child } = spawn(
linkCommand,
linkArgs,
options,
{
onLog: (msg) => progress(msg),
onError: (msg) => {
Iif (msg.indexOf("Converted build") > -1) {
return;
}
warnings(msg);
},
},
);
childSet.add(child);
await linkCompleted;
childSet.delete(child);
if (isGBA) {
const devkitPaths = getDevKitProPaths();
const objcopyCommand = Path.join(
devkitPaths.devkitArm,
"bin",
process.platform === "win32"
? "arm-none-eabi-objcopy.exe"
: "arm-none-eabi-objcopy",
);
const elfFilename = romFilename.replace(/\.gba$/i, ".elf");
const { completed: objcopyCompleted, child: objcopyChild } = spawn(
objcopyCommand,
["-O", "binary", `build/rom/${elfFilename}`, `build/rom/${romFilename}`],
options,
{
onLog: (msg) => progress(msg),
onError: (msg) => warnings(msg),
},
);
childSet.add(objcopyChild);
await objcopyCompleted;
childSet.delete(objcopyChild);
const gbafixCommand = Path.join(
devkitPaths.devkitPro,
"tools",
"bin",
process.platform === "win32" ? "gbafix.exe" : "gbafix",
);
const { completed: gbafixCompleted, child: gbafixChild } = spawn(
gbafixCommand,
[`build/rom/${romFilename}`],
options,
{
onLog: (msg) => progress(msg),
onError: (msg) => warnings(msg),
},
);
childSet.add(gbafixChild);
await gbafixCompleted;
childSet.delete(gbafixChild);
}
// Export game globals to ROM directory
const gameGlobalsPath = `${buildRoot}/include/data/game_globals.i`;
const gameGlobalsExportPath = `${buildRoot}/build/rom/globals.i`;
await fs.copyFile(gameGlobalsPath, gameGlobalsExportPath);
// Store /obj in cache
await cacheObjData(buildRoot, tmpPath, env);
};
export const cancelBuildCommandsInProgress = async () => {
cancelling = true;
// Kill all spawned commands and any commands that were spawned by those
// e.g lcc spawns sdcc, etc.
for (const child of childSet) {
Iif (child.pid === undefined) {
continue;
}
const spawnedChildren = await psTreeAsync(child.pid);
for (const childChild of spawnedChildren) {
try {
process.kill(Number(childChild.PID));
} catch (e) {}
}
try {
child.kill();
} catch (e) {}
}
};
export default makeBuild;
|