directive #embed for sdcc and cmoc

This commit is contained in:
Steven Hugg 2023-12-13 23:38:38 -05:00
parent f8462de014
commit dff5c73d6a
3 changed files with 31 additions and 22 deletions

View File

@ -1,4 +1,4 @@
import { getBasePlatform } from "../common/util";
import { convertDataToUint8Array, getBasePlatform } from "../common/util";
import { WorkerBuildStep, WorkerError, WorkerErrorResult, WorkerMessage, WorkerResult, WorkingStore } from "../common/workertypes";
import { PLATFORM_PARAMS } from "./platforms";
import { TOOLS } from "./workertools";
@ -414,3 +414,20 @@ export function fixParamsWithDefines(path: string, params) {
}
}
}
export function processEmbedDirective(code: string) {
let re3 = /^\s*#embed\s+"(.+?)"/gm;
// find #embed "filename.bin" and replace with C array data
return code.replace(re3, (m, m1) => {
let filename = m1;
let filedata = store.getFileData(filename);
let bytes = convertDataToUint8Array(filedata);
if (!bytes) throw new Error('#embed: file not found: "' + filename + '"');
let out = '';
for (let i = 0; i < bytes.length; i++) {
out += bytes[i].toString() + ',';
}
return out.substring(0, out.length-1);
});
}

View File

@ -1,7 +1,7 @@
import { convertDataToUint8Array, getFilenamePrefix, getRootBasePlatform, safeident } from "../../common/util";
import { getRootBasePlatform } from "../../common/util";
import { CodeListingMap, WorkerError } from "../../common/workertypes";
import { BuildStep, BuildStepResult, gatherFiles, staleFiles, populateFiles, fixParamsWithDefines, putWorkFile, populateExtraFiles, store, populateEntry, anyTargetChanged } from "../builder";
import { BuildStep, BuildStepResult, gatherFiles, staleFiles, populateFiles, fixParamsWithDefines, putWorkFile, populateExtraFiles, store, populateEntry, anyTargetChanged, processEmbedDirective } from "../builder";
import { re_crlf, makeErrorMatcher } from "../listingutils";
import { loadNative, moduleInstFn, print_fn, setupFS, execMain, emglobal, EmscriptenModule } from "../wasmutils";
@ -270,22 +270,6 @@ export function linkLD65(step: BuildStep): BuildStepResult {
}
}
function processIncbin(code: string) {
let re3 = /^\s*#embed\s+"(.+?)"/gm;
// find #embed "filename.bin" and replace with C array data
return code.replace(re3, (m, m1) => {
let filename = m1;
let filedata = store.getFileData(filename);
let bytes = convertDataToUint8Array(filedata);
if (!bytes) throw new Error('#embed: file not found: "' + filename + '"');
let out = '';
for (let i = 0; i < bytes.length; i++) {
out += bytes[i].toString() + ',';
}
return out;
});
}
export function compileCC65(step: BuildStep): BuildStepResult {
loadNative("cc65");
var params = step.params;
@ -321,7 +305,7 @@ export function compileCC65(step: BuildStep): BuildStepResult {
mainFilePath: step.path,
processFn: (path, code) => {
if (typeof code === 'string') {
code = processIncbin(code);
code = processEmbedDirective(code);
}
return code;
}

View File

@ -1,5 +1,5 @@
import { getBasePlatform } from "../../common/util";
import { BuildStep, populateFiles, populateExtraFiles, errorResult } from "../builder";
import { BuildStep, populateFiles, populateExtraFiles, errorResult, processEmbedDirective } from "../builder";
import { makeErrorMatcher, extractErrors } from "../listingutils";
import { PLATFORM_PARAMS } from "../platforms";
import { load, print_fn, setupFS, execMain, emglobal, EmscriptenModule } from "../wasmutils";
@ -24,7 +24,15 @@ export function preprocessMCPP(step: BuildStep, filesys: string) {
});
var FS = MCPP.FS;
if (filesys) setupFS(FS, filesys);
populateFiles(step, FS);
populateFiles(step, FS, {
mainFilePath: step.path,
processFn: (path, code) => {
if (typeof code === 'string') {
code = processEmbedDirective(code);
}
return code;
}
});
populateExtraFiles(step, FS, params.extra_compile_files);
// TODO: make configurable by other compilers
var args = [