From 75d224d735657df098239df8cd51920e82e8d94d Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Mon, 12 Aug 2024 15:30:14 +0800 Subject: [PATCH] update --- .gitignore | 1 + src/Builder/CppBuilder.ts | 40 ++++++++++++++++++++++------- src/Compiler/GppCompiler.ts | 4 +-- src/Project/CppProject.ts | 3 ++- src/Tools/GetExecutableExtension.ts | 13 ++++++++++ src/Tools/NmakeDir.ts | 2 +- src/Tools/SourceFilesToString.ts | 7 ++--- src/index.ts | 8 +++--- tests/test2/nmake.js | 6 ----- tests/test2/nmake.ts | 2 +- tests/test2/tsconfig.json | 4 +-- 11 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 src/Tools/GetExecutableExtension.ts delete mode 100644 tests/test2/nmake.js diff --git a/.gitignore b/.gitignore index 1a738a9..e1d55d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /build +/test* # ---> Node # Logs logs diff --git a/src/Builder/CppBuilder.ts b/src/Builder/CppBuilder.ts index 03f8458..69b5df4 100644 --- a/src/Builder/CppBuilder.ts +++ b/src/Builder/CppBuilder.ts @@ -7,29 +7,51 @@ import fs from "fs" import printDebug from "../Tools/DebugPrint.js"; import sourceFilesToString from "../Tools/SourceFilesToString.js"; import execAsync from "../Tools/ExecAsync.js"; +import protectPath from "../Tools/ProtectPath.js"; +import printErrorOrDebug from "../Tools/PrintErrorOrDebug.js"; +import getExecutableExtension from "../Tools/GetExecutableExtension.js"; export default class CppBuilder implements Builder{ compile = async ()=>{ printDebug("compiling") const cppProjects = getProjectsByTypeName("cpp") as CppProject[] - const tasks = cppProjects.map((e)=>{ - printDebug(`making dir ${path.join(getBuildDir(),e.name)}`) - fs.mkdirSync(path.join(getBuildDir(),e.name),{recursive:true}) - const command = `${e.compiler.compilerPath} -o ${path.join(getBuildDir(),e.name,`${e.name}.o`)} ${e.optimizeToBuildString()} ${sourceFilesToString(e)}` - printDebug(command) - // tasks.push() - return execAsync(command) + const tasks:Promise<{ + stdout: string; + stderr: string; + }>[] = [] + cppProjects.forEach((e)=>{ + e.sourceFilesPath.forEach((v)=>{ + const objDir = path.join(getBuildDir(),e.name,".objs") + printDebug(`making dir ${objDir}`) + if(!fs.existsSync(objDir))fs.mkdirSync(objDir,{recursive:true}); + const objPath = path.join(objDir,path.parse(v).name+".o") + e.objs.push(objPath) + const command = `${protectPath(e.compiler.compilerPath)} -c ${protectPath(v)} -o ${protectPath(objPath)} ${e.optimizeToBuildString()}` + tasks.push(execAsync(command)) + printDebug(command) + }) }); (await Promise.all(tasks)).forEach(e=>{ printDebug(`stdout:${e.stdout}`) - printDebug(`stderr:${e.stderr}`) + if(e.stderr){printErrorOrDebug(`stderr:${e.stderr}`)} }) } link = async()=>{ - + printDebug("linking") + const cppProjects = getProjectsByTypeName("cpp") as CppProject[] + const tasks:Promise<{ + stdout: string; + stderr: string; + }>[] =[] + cppProjects.forEach(i=>{ + const command = `${protectPath(i.compiler.compilerPath)} ${sourceFilesToString(i.objs)} -o ${path.join(getBuildDir(),i.name,`${i.name}${getExecutableExtension()}`)}` + printDebug(command) + tasks.push(execAsync(command)) + }) } build = async () =>{ printDebug("building") await this.compile() + await this.link() } } \ No newline at end of file diff --git a/src/Compiler/GppCompiler.ts b/src/Compiler/GppCompiler.ts index db00bce..4e6e18d 100644 --- a/src/Compiler/GppCompiler.ts +++ b/src/Compiler/GppCompiler.ts @@ -12,11 +12,11 @@ export default class GppCompiler implements Compiler{ constructor(path:string) constructor(path?:string,version?:string){ if(path){ - this.compilerPath = protectPath(path) + this.compilerPath = path }else{ const compilerPaths = getExecutablePathsFromEnv(this.compilerName) if(compilerPaths.length==0)throw Error(`cannot find ${this.compilerName} compiler`) - this.compilerPath = protectPath(compilerPaths[0]) + this.compilerPath = compilerPaths[0] } if(version){ this.compilerVersion = version diff --git a/src/Project/CppProject.ts b/src/Project/CppProject.ts index 8eff0b3..3db5f69 100644 --- a/src/Project/CppProject.ts +++ b/src/Project/CppProject.ts @@ -17,6 +17,7 @@ export default class CppProject implements Project , SourceFiles,HaveCompiler,Op compiler: GppCompiler; sourceFilesPath: string[]=[]; optimize: "fast" | "fastest" | "normal" = "fast" + objs:string[]=[] optimizeToBuildString = ():string=>{ let ret = "" switch (this.optimize) { @@ -40,7 +41,7 @@ export default class CppProject implements Project , SourceFiles,HaveCompiler,Op printDebug("adding source files "+files) files.forEach(v=>{ this.sourceFilesPath.push( - protectPath(path.join(getNmakeDir(),v)) + path.join(getNmakeDir(),v) ) }); printDebug("all files in "+this.name+" :"+this.sourceFilesPath) diff --git a/src/Tools/GetExecutableExtension.ts b/src/Tools/GetExecutableExtension.ts new file mode 100644 index 0000000..a37e0dd --- /dev/null +++ b/src/Tools/GetExecutableExtension.ts @@ -0,0 +1,13 @@ +import os from "os" + +function getExecutableExtension() { + const platform = os.platform(); + + if (platform === 'win32') { + return '.exe'; + } else { + return ''; // Unix-like systems usually don't have a suffix for executables + } +} + +export default getExecutableExtension diff --git a/src/Tools/NmakeDir.ts b/src/Tools/NmakeDir.ts index 73e6aa2..4f81c05 100644 --- a/src/Tools/NmakeDir.ts +++ b/src/Tools/NmakeDir.ts @@ -3,7 +3,7 @@ import { getNmakePath } from "./NmakePath.js" import printDebug from "./DebugPrint.js" export const getNmakeDir =():string=>{ - let nmakeDir = path.join(getNmakePath(),"../") + let nmakeDir = path.join(process.cwd()) printDebug("getting nmake dir "+nmakeDir) return nmakeDir } \ No newline at end of file diff --git a/src/Tools/SourceFilesToString.ts b/src/Tools/SourceFilesToString.ts index 5b733ec..8c8b9f0 100644 --- a/src/Tools/SourceFilesToString.ts +++ b/src/Tools/SourceFilesToString.ts @@ -1,10 +1,11 @@ import SourceFiles from "../Project/interface/SourceFiles.js"; import printDebug from "./DebugPrint.js"; +import protectPath from "./ProtectPath.js"; -const sourceFilesToString = (project:SourceFiles):string=>{ +const sourceFilesToString = (project:string[]):string=>{ let ret = "" - project.sourceFilesPath.forEach(v=>{ - ret = `${ret} ${v}` + project.forEach(v=>{ + ret = `${ret} ${protectPath(v)}` }) printDebug(`tostring: ${ret}`) return ret diff --git a/src/index.ts b/src/index.ts index 8a82931..d24c30c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import CppBuilder from "./Builder/CppBuilder.js"; import minimist from "minimist"; const argv = minimist(process.argv.slice(2)) -let nmakeFileName = "nmake.ts" +let nmakeFileName = "nmake.js" if(argv["v"])(global as any).isDebug=true if (argv["f"]) {nmakeFileName = argv["f"];printDebug(`setting nmake file name to ${argv["f"]}`)} @@ -24,9 +24,9 @@ printDebug("found nmake file") setNmakePath(nmakeFilePath) printDebug("adding ts-node") -import ts_node from "ts-node" +// import ts_node from "ts-node" import { pathToFileURL } from "url"; -ts_node.register() +// ts_node.register() printDebug("adding global values") @@ -34,7 +34,7 @@ console.log("running nmake file "+nmakeFilePath) printDebug(`running file ${nmakeFilePath}`) // require(nmakeFilePath) -import(pathToFileURL(nmakeFilePath).toString()) +await import(pathToFileURL(nmakeFilePath).toString()) printDebug(`run completion!`,nmakeFilePath) if(argv["b"]){ diff --git a/tests/test2/nmake.js b/tests/test2/nmake.js deleted file mode 100644 index d5f3f71..0000000 --- a/tests/test2/nmake.js +++ /dev/null @@ -1,6 +0,0 @@ -import CppProject from "../../src/Project/CppProject.js"; -import { addProject } from "../../src/Tools/AddProject.js"; -// const compiler = new GppCompiler() -addProject(function () { - return new CppProject("hello"); -}); diff --git a/tests/test2/nmake.ts b/tests/test2/nmake.ts index 34b6c08..6ca9f88 100644 --- a/tests/test2/nmake.ts +++ b/tests/test2/nmake.ts @@ -4,5 +4,5 @@ import { addProject } from "../../src/Tools/AddProject.js"; // const compiler = new GppCompiler() addProject(()=>{ - return new CppProject("hello") + return new CppProject("hello").addFiles("main.cpp") }) \ No newline at end of file diff --git a/tests/test2/tsconfig.json b/tests/test2/tsconfig.json index 68bfde1..a1de8a1 100644 --- a/tests/test2/tsconfig.json +++ b/tests/test2/tsconfig.json @@ -11,7 +11,7 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "ES2023", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ @@ -55,7 +55,7 @@ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ - // "outDir": "./", /* Specify an output folder for all emitted files. */ + "outDir": "./build", /* Specify an output folder for all emitted files. */ // "removeComments": true, /* Disable emitting comments. */ // "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */