2018-07-06 00:13:07 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-02-21 21:47:25 +00:00
|
|
|
import { FileData, Dependency, SourceLine, SourceFile, CodeListing, CodeListingMap, WorkerError, Segment, WorkerResult } from "./workertypes";
|
2019-05-12 19:39:09 +00:00
|
|
|
import { getFilenamePrefix, getFolderForPath, isProbablyBinary, getBasePlatform } from "./util";
|
2019-05-06 01:49:08 +00:00
|
|
|
import { Platform } from "./baseplatform";
|
2018-07-06 00:13:07 +00:00
|
|
|
|
2018-07-26 13:43:49 +00:00
|
|
|
type BuildResultCallback = (result:WorkerResult) => void;
|
2018-07-06 00:13:07 +00:00
|
|
|
type BuildStatusCallback = (busy:boolean) => void;
|
|
|
|
type IterateFilesCallback = (path:string, data:FileData) => void;
|
2019-03-02 23:15:03 +00:00
|
|
|
type GetRemoteCallback = (path:string, callback:(data:FileData) => void, datatype:'text'|'arraybuffer') => any;
|
2018-07-06 00:13:07 +00:00
|
|
|
|
|
|
|
export class CodeProject {
|
|
|
|
filedata : {[path:string]:FileData} = {};
|
|
|
|
listings : CodeListingMap;
|
2019-02-21 21:47:25 +00:00
|
|
|
segments : Segment[];
|
2018-07-06 00:13:07 +00:00
|
|
|
mainpath : string;
|
|
|
|
pendingWorkerMessages = 0;
|
|
|
|
tools_preloaded = {};
|
|
|
|
callbackBuildResult : BuildResultCallback;
|
|
|
|
callbackBuildStatus : BuildStatusCallback;
|
2018-07-08 04:58:11 +00:00
|
|
|
worker : Worker;
|
2018-07-06 00:13:07 +00:00
|
|
|
platform_id : string;
|
2019-05-06 01:49:08 +00:00
|
|
|
platform : Platform;
|
|
|
|
store : any;
|
2018-07-06 00:13:07 +00:00
|
|
|
callbackGetRemote : GetRemoteCallback;
|
2018-09-11 00:44:53 +00:00
|
|
|
mainPath : string;
|
|
|
|
isCompiling : boolean = false;
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
constructor(worker, platform_id:string, platform, store) {
|
|
|
|
this.worker = worker;
|
|
|
|
this.platform_id = platform_id;
|
|
|
|
this.platform = platform;
|
|
|
|
this.store = store;
|
|
|
|
|
|
|
|
worker.onmessage = (e) => {
|
2018-12-01 11:48:33 +00:00
|
|
|
this.receiveWorkerMessage(e.data);
|
2018-07-06 00:13:07 +00:00
|
|
|
};
|
|
|
|
}
|
2019-03-02 23:15:03 +00:00
|
|
|
|
2018-12-01 11:48:33 +00:00
|
|
|
receiveWorkerMessage(data : WorkerResult) {
|
|
|
|
var notfinal = this.pendingWorkerMessages > 1;
|
|
|
|
if (notfinal) {
|
|
|
|
this.sendBuild();
|
|
|
|
this.pendingWorkerMessages = 1;
|
|
|
|
} else {
|
|
|
|
if (this.callbackBuildStatus) this.callbackBuildStatus(false);
|
|
|
|
if (!this.isCompiling) { console.log(this.pendingWorkerMessages); console.trace(); } // debug compile problems
|
|
|
|
this.isCompiling = false;
|
|
|
|
this.pendingWorkerMessages = 0;
|
|
|
|
}
|
|
|
|
if (data && !data.unchanged) {
|
|
|
|
this.processBuildResult(data);
|
|
|
|
if (this.callbackBuildResult) this.callbackBuildResult(data); // call with data when changed
|
|
|
|
}
|
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
preloadWorker(path:string) {
|
|
|
|
var tool = this.platform.getToolForFilename(path);
|
|
|
|
if (tool && !this.tools_preloaded[tool]) {
|
|
|
|
this.worker.postMessage({preload:tool, platform:this.platform_id});
|
|
|
|
this.tools_preloaded[tool] = true;
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 23:15:03 +00:00
|
|
|
|
2018-11-27 15:14:24 +00:00
|
|
|
pushAllFiles(files:string[], fn:string) {
|
|
|
|
// look for local and preset files
|
|
|
|
files.push(fn);
|
|
|
|
// look for files in current (main file) folder
|
|
|
|
var dir = getFolderForPath(this.mainpath);
|
2018-11-27 20:14:22 +00:00
|
|
|
if (dir.length > 0 && dir != 'local') // TODO
|
2018-11-27 15:14:24 +00:00
|
|
|
files.push(dir + '/' + fn);
|
|
|
|
}
|
2019-03-02 23:15:03 +00:00
|
|
|
|
2018-08-03 18:06:08 +00:00
|
|
|
parseIncludeDependencies(text:string):string[] {
|
2019-04-24 17:46:19 +00:00
|
|
|
let files = [];
|
|
|
|
let m;
|
2018-11-18 17:30:41 +00:00
|
|
|
if (this.platform_id.startsWith('verilog')) {
|
2018-12-03 15:51:47 +00:00
|
|
|
// include verilog includes
|
2019-04-24 17:46:19 +00:00
|
|
|
let re1 = /^\s*(`include|[.]include)\s+"(.+?)"/gmi;
|
2018-11-28 16:10:24 +00:00
|
|
|
while (m = re1.exec(text)) {
|
2018-11-27 15:14:24 +00:00
|
|
|
this.pushAllFiles(files, m[2]);
|
2018-07-06 00:13:07 +00:00
|
|
|
}
|
2018-11-28 16:10:24 +00:00
|
|
|
// include .arch (json) statements
|
2019-04-24 17:46:19 +00:00
|
|
|
let re2 = /^\s*([.]arch)\s+(\w+)/gmi;
|
2018-11-28 16:10:24 +00:00
|
|
|
while (m = re2.exec(text)) {
|
|
|
|
this.pushAllFiles(files, m[2]+".json");
|
|
|
|
}
|
2018-12-03 15:51:47 +00:00
|
|
|
// include $readmem[bh] (TODO)
|
2019-05-02 03:33:49 +00:00
|
|
|
let re3 = /\$readmem[bh]\("(.+?)"/gmi;
|
2018-12-03 15:51:47 +00:00
|
|
|
while (m = re3.exec(text)) {
|
2019-05-02 03:33:49 +00:00
|
|
|
this.pushAllFiles(files, m[1]);
|
2018-12-03 15:51:47 +00:00
|
|
|
}
|
2018-07-06 00:13:07 +00:00
|
|
|
} else {
|
2018-08-03 18:06:08 +00:00
|
|
|
// for .asm -- [.]include "file"
|
2018-08-12 23:59:08 +00:00
|
|
|
// for .c -- #include "file"
|
2019-04-24 17:46:19 +00:00
|
|
|
let re2 = /^\s*[.#]?(include|incbin)\s+"(.+?)"/gmi;
|
2018-08-03 18:06:08 +00:00
|
|
|
while (m = re2.exec(text)) {
|
2018-11-27 15:14:24 +00:00
|
|
|
this.pushAllFiles(files, m[2]);
|
2018-08-03 18:06:08 +00:00
|
|
|
}
|
2019-05-22 01:39:37 +00:00
|
|
|
// for .c -- //#resource "file" (or ;resource or #resource)
|
|
|
|
let re3 = /^\s*([;]|[/][/])#resource\s+"(.+?)"/gm;
|
2019-04-24 17:46:19 +00:00
|
|
|
while (m = re3.exec(text)) {
|
|
|
|
this.pushAllFiles(files, m[2]);
|
|
|
|
}
|
2018-08-03 18:06:08 +00:00
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
parseLinkDependencies(text:string):string[] {
|
2019-04-24 17:46:19 +00:00
|
|
|
let files = [];
|
|
|
|
let m;
|
2018-11-18 17:30:41 +00:00
|
|
|
if (this.platform_id.startsWith('verilog')) {
|
2018-08-03 18:06:08 +00:00
|
|
|
//
|
|
|
|
} else {
|
|
|
|
// for .c -- //#link "file" (or ;link or #link)
|
2019-05-22 01:39:37 +00:00
|
|
|
let re = /^\s*([;]|[/][/])#link\s+"(.+?)"/gm;
|
2018-07-06 00:13:07 +00:00
|
|
|
while (m = re.exec(text)) {
|
2018-11-27 15:14:24 +00:00
|
|
|
this.pushAllFiles(files, m[2]);
|
2018-07-06 00:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
2019-05-31 19:05:33 +00:00
|
|
|
|
2019-05-23 12:32:53 +00:00
|
|
|
loadFileDependencies(text:string) : Promise<Dependency[]> {
|
2019-04-24 17:46:19 +00:00
|
|
|
let includes = this.parseIncludeDependencies(text);
|
|
|
|
let linkfiles = this.parseLinkDependencies(text);
|
|
|
|
let allfiles = includes.concat(linkfiles);
|
2019-05-23 12:32:53 +00:00
|
|
|
return this.loadFiles(allfiles).then((result) => {
|
2018-08-14 20:28:29 +00:00
|
|
|
// set 'link' property on files that are link dependencies (must match filename)
|
2019-04-24 17:46:19 +00:00
|
|
|
if (result) {
|
|
|
|
for (let dep of result) {
|
2018-11-27 15:14:24 +00:00
|
|
|
dep.link = linkfiles.indexOf(dep.path) >= 0;
|
2019-04-24 17:46:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-23 12:32:53 +00:00
|
|
|
return result;
|
2018-08-03 18:06:08 +00:00
|
|
|
});
|
2018-07-06 00:13:07 +00:00
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
okToSend():boolean {
|
|
|
|
return this.pendingWorkerMessages++ == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFileInStore(path:string, text:FileData) {
|
|
|
|
// protect against accidential whole-file deletion
|
|
|
|
if ((<string>text).trim && (<string>text).trim().length) {
|
|
|
|
this.store.setItem(path, text);
|
|
|
|
}
|
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
// TODO: test duplicate files, local paths mixed with presets
|
|
|
|
buildWorkerMessage(depends:Dependency[]) {
|
|
|
|
this.preloadWorker(this.mainpath);
|
|
|
|
var msg = {updates:[], buildsteps:[]};
|
|
|
|
// TODO: add preproc directive for __MAINFILE__
|
2019-04-26 19:38:34 +00:00
|
|
|
var mainfilename = this.stripLocalPath(this.mainpath);
|
2018-07-06 00:13:07 +00:00
|
|
|
var maintext = this.getFile(this.mainpath);
|
2018-08-12 23:59:08 +00:00
|
|
|
var depfiles = [];
|
2018-07-06 00:13:07 +00:00
|
|
|
msg.updates.push({path:mainfilename, data:maintext});
|
2018-08-03 18:06:08 +00:00
|
|
|
for (var dep of depends) {
|
|
|
|
if (!dep.link) {
|
|
|
|
msg.updates.push({path:dep.filename, data:dep.data});
|
2018-08-12 23:59:08 +00:00
|
|
|
depfiles.push(dep.filename);
|
2018-08-03 18:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 23:59:08 +00:00
|
|
|
msg.buildsteps.push({path:mainfilename, files:[mainfilename].concat(depfiles), platform:this.platform_id, tool:this.platform.getToolForFilename(this.mainpath), mainfile:true});
|
2018-08-03 18:06:08 +00:00
|
|
|
for (var dep of depends) {
|
|
|
|
if (dep.data && dep.link) {
|
2018-07-06 00:13:07 +00:00
|
|
|
this.preloadWorker(dep.filename);
|
|
|
|
msg.updates.push({path:dep.filename, data:dep.data});
|
2018-08-12 23:59:08 +00:00
|
|
|
msg.buildsteps.push({path:dep.filename, files:[dep.filename].concat(depfiles), platform:this.platform_id, tool:this.platform.getToolForFilename(dep.path)});
|
2018-07-06 00:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
|
|
|
// TODO: get local file as well as presets?
|
2019-05-23 12:32:53 +00:00
|
|
|
loadFiles(paths:string[]) : Promise<Dependency[]> {
|
|
|
|
return new Promise( (yes,no) => {
|
2018-07-06 23:12:58 +00:00
|
|
|
var result : Dependency[] = [];
|
2019-04-26 19:38:34 +00:00
|
|
|
var addResult = (path, data) => {
|
2018-07-06 23:12:58 +00:00
|
|
|
result.push({
|
|
|
|
path:path,
|
2019-04-26 19:38:34 +00:00
|
|
|
filename:this.stripLocalPath(path),
|
2018-08-03 18:06:08 +00:00
|
|
|
link:true,
|
2018-07-06 23:12:58 +00:00
|
|
|
data:data
|
|
|
|
});
|
|
|
|
}
|
2018-07-06 00:13:07 +00:00
|
|
|
var loadNext = () => {
|
|
|
|
var path = paths.shift();
|
|
|
|
if (!path) {
|
2018-07-06 23:12:58 +00:00
|
|
|
// finished loading all files; return result
|
2019-05-23 12:32:53 +00:00
|
|
|
yes(result);
|
2018-07-06 00:13:07 +00:00
|
|
|
} else {
|
2018-07-24 15:38:56 +00:00
|
|
|
// look in cache
|
|
|
|
if (path in this.filedata) { // found in cache?
|
|
|
|
var data = this.filedata[path];
|
|
|
|
if (data)
|
|
|
|
addResult(path, data);
|
|
|
|
loadNext();
|
|
|
|
} else {
|
|
|
|
// look in store
|
|
|
|
this.store.getItem(path, (err, value) => {
|
|
|
|
if (err) { // err fetching from store
|
2019-05-23 12:32:53 +00:00
|
|
|
no(err);
|
2018-07-24 15:38:56 +00:00
|
|
|
} else if (value) { // found in store?
|
2018-08-21 14:16:47 +00:00
|
|
|
this.filedata[path] = value; // do not update store, just cache
|
2018-07-24 15:38:56 +00:00
|
|
|
addResult(path, value);
|
2018-07-06 00:13:07 +00:00
|
|
|
loadNext();
|
2019-05-12 19:39:09 +00:00
|
|
|
} else {
|
2018-07-24 15:38:56 +00:00
|
|
|
// found on remote fetch?
|
2018-08-06 17:47:55 +00:00
|
|
|
var preset_id = this.platform_id;
|
2019-05-12 19:39:09 +00:00
|
|
|
preset_id = getBasePlatform(preset_id); // remove .suffix from preset name
|
2018-08-06 17:47:55 +00:00
|
|
|
var webpath = "presets/" + preset_id + "/" + path;
|
2019-02-09 18:42:35 +00:00
|
|
|
// try to GET file, use file ext to determine text/binary
|
2019-03-02 23:15:03 +00:00
|
|
|
this.callbackGetRemote( webpath, (data:FileData) => {
|
2019-03-03 16:32:25 +00:00
|
|
|
if (data == null) {
|
|
|
|
console.log("Could not load preset file", path);
|
2018-08-21 14:16:47 +00:00
|
|
|
this.filedata[path] = null; // mark cache entry as invalid
|
2019-03-03 16:32:25 +00:00
|
|
|
} else {
|
|
|
|
if (data instanceof ArrayBuffer)
|
|
|
|
data = new Uint8Array(data); // convert to typed array
|
|
|
|
console.log("GET",webpath,data.length,'bytes');
|
|
|
|
this.filedata[path] = data; // do not update store, just cache
|
|
|
|
addResult(path, data);
|
|
|
|
}
|
2018-07-24 15:38:56 +00:00
|
|
|
loadNext();
|
2019-03-03 16:32:25 +00:00
|
|
|
}, isProbablyBinary(path) ? 'arraybuffer' : 'text');
|
2018-07-24 15:38:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-07-06 00:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
loadNext(); // load first file
|
2019-05-23 12:32:53 +00:00
|
|
|
});
|
2018-07-06 00:13:07 +00:00
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
getFile(path:string):FileData {
|
|
|
|
return this.filedata[path];
|
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-08-03 18:06:08 +00:00
|
|
|
// TODO: purge files not included in latest build?
|
2018-07-06 00:13:07 +00:00
|
|
|
iterateFiles(callback:IterateFilesCallback) {
|
|
|
|
for (var path in this.filedata) {
|
|
|
|
callback(path, this.getFile(path));
|
|
|
|
}
|
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
sendBuild() {
|
2018-08-04 14:21:50 +00:00
|
|
|
if (!this.mainpath) throw "need to call setMainFile first";
|
2018-07-06 00:13:07 +00:00
|
|
|
var maindata = this.getFile(this.mainpath);
|
2018-12-01 11:48:33 +00:00
|
|
|
// if binary blob, just return it as ROM
|
|
|
|
if (maindata instanceof Uint8Array) {
|
|
|
|
this.isCompiling = true;
|
|
|
|
this.receiveWorkerMessage({
|
|
|
|
output:maindata,
|
|
|
|
errors:[],
|
|
|
|
listings:null,
|
|
|
|
symbolmap:null,
|
|
|
|
params:{}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// otherwise, make it a string
|
2018-07-06 00:13:07 +00:00
|
|
|
var text = typeof maindata === "string" ? maindata : '';
|
2018-08-12 23:59:08 +00:00
|
|
|
// TODO: load dependencies of non-main files
|
2019-05-23 12:32:53 +00:00
|
|
|
return this.loadFileDependencies(text).then( (depends) => {
|
2018-07-06 06:42:01 +00:00
|
|
|
if (!depends) depends = [];
|
2018-12-15 18:14:40 +00:00
|
|
|
var workermsg = this.buildWorkerMessage(depends);
|
|
|
|
this.worker.postMessage(workermsg);
|
2018-09-11 00:44:53 +00:00
|
|
|
this.isCompiling = true;
|
2018-07-06 00:13:07 +00:00
|
|
|
});
|
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
updateFile(path:string, text:FileData) {
|
|
|
|
this.updateFileInStore(path, text); // TODO: isBinary
|
|
|
|
this.filedata[path] = text;
|
2018-08-04 14:21:50 +00:00
|
|
|
if (this.okToSend() && this.mainpath) {
|
2018-07-06 00:13:07 +00:00
|
|
|
if (this.callbackBuildStatus) this.callbackBuildStatus(true);
|
|
|
|
this.sendBuild();
|
|
|
|
}
|
|
|
|
};
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-08-04 14:21:50 +00:00
|
|
|
setMainFile(path:string) {
|
|
|
|
this.mainpath = path;
|
|
|
|
if (this.callbackBuildStatus) this.callbackBuildStatus(true);
|
|
|
|
this.sendBuild();
|
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
processBuildResult(data:WorkerResult) {
|
|
|
|
// TODO: link listings with source files
|
2018-09-05 02:28:12 +00:00
|
|
|
if (data.listings) {
|
|
|
|
this.listings = data.listings;
|
2018-07-06 00:13:07 +00:00
|
|
|
for (var lstname in this.listings) {
|
|
|
|
var lst = this.listings[lstname];
|
2019-02-22 16:43:07 +00:00
|
|
|
if (lst.lines)
|
2019-02-21 00:38:30 +00:00
|
|
|
lst.sourcefile = new SourceFile(lst.lines, lst.text);
|
2019-02-22 16:43:07 +00:00
|
|
|
if (lst.asmlines)
|
2018-07-06 00:13:07 +00:00
|
|
|
lst.assemblyfile = new SourceFile(lst.asmlines, lst.text);
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 21:47:25 +00:00
|
|
|
// save and sort segment list
|
|
|
|
this.segments = data.segments;
|
|
|
|
if (this.segments) {
|
|
|
|
this.segments.sort((a,b) => {return a.start-b.start});
|
|
|
|
}
|
2018-07-06 00:13:07 +00:00
|
|
|
}
|
2018-11-21 12:21:07 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
getListings() : CodeListingMap {
|
|
|
|
return this.listings;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns first listing in format [prefix].lst (TODO: could be better)
|
|
|
|
getListingForFile(path) : CodeListing {
|
2019-04-26 19:38:34 +00:00
|
|
|
var fnprefix = getFilenamePrefix(this.stripLocalPath(path));
|
2018-07-06 00:13:07 +00:00
|
|
|
var listings = this.getListings();
|
|
|
|
for (var lstfn in listings) {
|
|
|
|
if (getFilenamePrefix(lstfn) == fnprefix) {
|
|
|
|
return listings[lstfn];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 19:38:34 +00:00
|
|
|
|
|
|
|
stripLocalPath(path : string) : string {
|
2019-05-07 19:37:37 +00:00
|
|
|
if (this.mainPath) {
|
|
|
|
var folder = getFolderForPath(this.mainPath);
|
|
|
|
if (folder != '' && path.startsWith(folder)) {
|
|
|
|
path = path.substring(folder.length+1);
|
|
|
|
}
|
2019-04-26 19:38:34 +00:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
2019-05-08 13:39:57 +00:00
|
|
|
|
2018-07-06 00:13:07 +00:00
|
|
|
}
|