diff --git a/src/worker/builder.ts b/src/worker/builder.ts index 4b427541..51c7ec88 100644 --- a/src/worker/builder.ts +++ b/src/worker/builder.ts @@ -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); + }); +} + diff --git a/src/worker/tools/cc65.ts b/src/worker/tools/cc65.ts index 656512a5..a4d99f2f 100644 --- a/src/worker/tools/cc65.ts +++ b/src/worker/tools/cc65.ts @@ -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; } diff --git a/src/worker/tools/mcpp.ts b/src/worker/tools/mcpp.ts index ccc57614..e707a76f 100644 --- a/src/worker/tools/mcpp.ts +++ b/src/worker/tools/mcpp.ts @@ -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 = [