From d4c5b006a3f6dc2a8775f7871303b682636980d5 Mon Sep 17 00:00:00 2001 From: Curle Date: Fri, 12 Mar 2021 23:40:56 +0000 Subject: [PATCH] Move to using the BS toolchain --- build.json | 82 ++++++++++++++++++++++++++++++ plugins/c.js | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 build.json create mode 100644 plugins/c.js diff --git a/build.json b/build.json new file mode 100644 index 0000000..4820b78 --- /dev/null +++ b/build.json @@ -0,0 +1,82 @@ +{ + "name": "Chroma", + "target": "x86_64-elf-gcc", + + "author": "Curle", + + "source": { + "inc": [ + "$root/chroma/inc/" + ], + + "font": [ + "$root/font.o" + ], + + "linkerscript": [ + "$root/linker.ld" + ], + + "preamble": [ + "$root/global/crt0.o", + "$root/global/crti.o", + "$root/global/crtbegin.o" + ], + + "main": [ + "$root/chroma/kernel.c", + "$root/chroma/video/draw.c", + "$root/chroma/video/print.c", + "$root/chroma/system/cpu.c", + "$root/chroma/system/rw.c", + "$root/chroma/system/serial.c", + "$root/chroma/system/pci.c", + "$root/chroma/system/memory/stack.c", + "$root/chroma/system/memory/paging.c", + "$root/chroma/system/memory/abstract_allocator.c", + "$root/chroma/system/memory/physmem.c", + "$root/chroma/system/drivers/keyboard.c", + "$root/chroma/system/drivers/elf.c" + ], + + "no-sse": [ + "$root/chroma/system/interrupts.c" + ], + + "lib": [ + "$root/chroma/lainlib/list/basic_list.c", + "$root/chroma/lainlib/mutex/ticketlock.c", + "$root/chroma/lainlib/compression/lzgmini.c" + ], + + "epilogue": [ + "$root/global/crtend.o", + "$root/global/crtn.o" + ] + }, + + "build": { + "compile-no-sse": [ + "-I$inc -ffreestanding -O0 -Wall -Wextra -Wall -Werror -pedantic -fPIC -fno-exceptions -fno-omit-frame-pointer -mno-red-zone -fno-stack-protector -ggdb3 -mgeneral-regs-only", + "$no-sse" + ], + + "compile-main": [ + "-I$inc -ffreestanding -O0 -Wall -Wextra -Wall -Werror -pedantic -fPIC -fno-exceptions -fno-omit-frame-pointer -mno-red-zone -fno-stack-protector -ggdb3", + "$preamble", + "$main", + "$lib", + "$font", + "$epilogue" + ], + + "link": [ + "-T $linkerscript -ffreestanding -O2 -nostdlib -nostartfiles -lgcc", + "%.o" + ], + + "output": [ + "Chroma.elf" + ] + } +} \ No newline at end of file diff --git a/plugins/c.js b/plugins/c.js new file mode 100644 index 0000000..774e369 --- /dev/null +++ b/plugins/c.js @@ -0,0 +1,137 @@ +const child = require("child_process"); +const fs = require("fs"); + +let defaultCompiler = "gcc"; +let tempDir; +let defaultLinkFlags = "%.o" +let defaultOutput = "$name" + +let linkCache = ""; + + +module.exports = { + + /** + * Preprocess the buildscript. + * If it contains compile, assemble and link steps, it does nothing + * If it contains compile but no build, it implicitly adds it with the default arguments. + * @param {object} buildscript the JSON buildscript object + */ + extensionC: function(buildscript) { + console.log("c.js processing buildscript"); + if(!("link" in buildscript.build)) { + console.log("Inserting link step for gcc"); + buildscript.build["link"] = [defaultLinkFlags]; + } + + if(!("output" in buildscript.build)) { + console.log("Inserting output step"); + buildscript.build["output"] = [defaultOutput]; + } + + tempDir = process.cwd() + "/bsTemp/"; + + if(!fs.existsSync(tempDir)) { + console.log("Creating temporary dir for object files"); + fs.mkdirSync(tempDir); + } + + + }, + + extensionH: function() {}, + extensionO: function() {}, + extensionLD: function() {}, + + /** + * Called when it is time to execute the compile step. + * It is passed the array of strings associated with the step in the buildscript. + * @param {...string} params the list of strings + */ + stepCompile: function(...params) { + let cwd = process.cwd().replace(/\\/g, "/"); + let compileFlags = ""; + // Check whether we're doubly listed + if(Array.isArray(params[0])) { + params = params[0]; + } + + for(var param of params) { + console.log("stepCompile: param", param); + if(param.startsWith("-")) { + compileFlags = param; + } else { + let binPath = param.substr(param.lastIndexOf(cwd) + cwd.length + 1); + binPath = binPath.substr(0, binPath.lastIndexOf(".")); + binPath = cwd + "/bsTemp/" + binPath; + + // generate the file structure for it to go into + fs.mkdirSync(binPath.substr(0, binPath.lastIndexOf("/")), {recursive: true}); + if(param.substr(param.lastIndexOf(".") + 1) != "c" + && param.substr(param.lastIndexOf(".") + 1) != "s") { + + fs.copyFileSync(param, binPath + ".o"); + } else { + let compilerCommand = + defaultCompiler + + " -c " + param + + " -o " + binPath + ".o " + + compileFlags; + + child.execSync(compilerCommand); + } + + } + } + }, + + /** + * Take in the compiled binary object files, + * prepare the link command, and save it for the link execution. + * @param {...string} params the object files to link. + */ + stepLink: function(...params) { + let linkerCommand = ""; + // Check whether we're doubly listed + if(Array.isArray(params[0])) { + params = params[0]; + } + + for(var param of params) { + console.log("stepLink: param", param); + linkerCommand += param + " "; + } + + linkCache = linkerCommand; + }, + + /** + * Take in the name of a file, execute the link command to save the output. + * @param {string} output the name of the wanted file + */ + stepOutput: function(output) { + // Check whether we're doubly listed + if(Array.isArray(output[0])) { + output = output[0]; + } + + let linkerCommand = defaultCompiler + " -o " + process.cwd() + "/" + output + " " + linkCache; + console.log("stepOutput: param", output, "command", linkerCommand); + child.execSync(linkerCommand); + + }, + + /** + * Set the name of the compiler to be used. + * @param {string} comp + */ + setCompiler: function(comp) { + // Check whether we're doubly listed + if(Array.isArray(comp[0])) { + comp = comp[0]; + } + defaultCompiler = comp; + console.log("Set compiler to", comp); + } + +} \ No newline at end of file