From 1897ebbcb084a64c478acf1f274e8a51362fbecd Mon Sep 17 00:00:00 2001 From: iFlameing Date: Tue, 4 Sep 2018 21:21:08 +0530 Subject: [PATCH 1/2] Now We can use Header File. --- src/compilerServices/clangService.ts | 21 ++++++++++++++------- src/compilerServices/types.ts | 4 +++- src/service.ts | 12 ++++++------ templates/hello_world_c/build.ts | 2 +- templates/hello_world_c/src/header.h | 3 +++ templates/hello_world_c/src/main.c | 3 +++ 6 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 templates/hello_world_c/src/header.h diff --git a/src/compilerServices/clangService.ts b/src/compilerServices/clangService.ts index 43eea7596..09461a182 100644 --- a/src/compilerServices/clangService.ts +++ b/src/compilerServices/clangService.ts @@ -31,11 +31,12 @@ export class ClangService implements CompilerService { async compile(input: ServiceInput): Promise { const files = Object.values(input.files); - if (files.length !== 1) { - throw new Error(`Supporting compilation of a single file, but ${files.length} file(s) found`); - } + // if (files.length !== 1) { + // throw new Error(`Supporting compilation of a single file, but ${files.length} file(s) found`); + // } const [ fileRef ] = Object.keys(input.files); const src = files[0].content; + const header = files[1].content; const from = this.lang; const project = { output: "wasm", @@ -43,10 +44,16 @@ export class ClangService implements CompilerService { files: [ { type: from, - name: "file." + from, - options: input.options, - src - } + name: "main."+from, + options:input.options, + src, + }, + { + type:Language.h, + name:"header."+ Language.h, + options:input.options, + src:header, + }, ] }; const result = await sendRequestJSON(project, ServiceTypes.Clang); diff --git a/src/compilerServices/types.ts b/src/compilerServices/types.ts index 0679e772f..f2a4c861a 100644 --- a/src/compilerServices/types.ts +++ b/src/compilerServices/types.ts @@ -30,7 +30,9 @@ export enum Language { JavaScript = "javascript", TypeScript = "typescript", Toml = "toml", - Text = "text" + Text = "text", + h = "h", + hpp = "hpp" } export interface InputFile { diff --git a/src/service.ts b/src/service.ts index 58e8e26c4..414ce1539 100644 --- a/src/service.ts +++ b/src/service.ts @@ -61,7 +61,8 @@ export { Language } from "./compilerServices"; function getProjectFilePath(file: File): string { const project = file.getProject(); - return file.getPath(project); + const result = file.getPath(project); + return result; } export class ServiceWorker { @@ -197,18 +198,17 @@ export class Service { return annotations; } - static async compileFiles(files: File[], from: Language, to: Language, options = ""): Promise<{ [name: string]: (string|ArrayBuffer); }> { + static async compileFiles(files: Array, from: Language, to: Language, options = ""): Promise<{ [name: string]: (string|ArrayBuffer); }> { gaEvent("compile", "Service", `${from}->${to}`); const service = await createCompilerService(from, to); - - const fileNameMap: {[name: string]: File} = files.reduce((acc: any, f: File) => { + const fileNameMap: {[name: string]: File} = files[0].reduce((acc: any, f: any) => { acc[getProjectFilePath(f)] = f; return acc; }, {} as any); const input = { - files: files.reduce((acc: any, f: File) => { + files: files[0].reduce((acc: any, f: File) => { acc[getProjectFilePath(f)] = { content: f.getData(), }; @@ -218,7 +218,7 @@ export class Service { }; const result = await service.compile(input); - for (const file of files) { + for (const file of files[0]) { file.setProblems([]); } diff --git a/templates/hello_world_c/build.ts b/templates/hello_world_c/build.ts index 626bb3aad..f7249addf 100644 --- a/templates/hello_world_c/build.ts +++ b/templates/hello_world_c/build.ts @@ -2,7 +2,7 @@ import * as gulp from "gulp"; import { Service, project } from "@wasm/studio-utils"; gulp.task("build", async () => { - const data = await Service.compileFile(project.getFile("src/main.c"), "c", "wasm", "-g -O3"); + const data = await Service.compileFile([project.getFile("src/main.c"),project.getFile("src/header.h")], "c", "wasm", "-g -O3"); const outWasm = project.newFile("out/main.wasm", "wasm", true); outWasm.setData(data); }); diff --git a/templates/hello_world_c/src/header.h b/templates/hello_world_c/src/header.h new file mode 100644 index 000000000..63063556d --- /dev/null +++ b/templates/hello_world_c/src/header.h @@ -0,0 +1,3 @@ +int hello(int a,int b){ +return a+b; +} diff --git a/templates/hello_world_c/src/main.c b/templates/hello_world_c/src/main.c index 241126fc9..a02cb79e3 100644 --- a/templates/hello_world_c/src/main.c +++ b/templates/hello_world_c/src/main.c @@ -1,10 +1,13 @@ #include #include +#include "header.h" #define WASM_EXPORT __attribute__((visibility("default"))) WASM_EXPORT int main(void) { + int c= hello(3,2);//you can define any function in headerfile and use it here.It is just an example. + printf("%d",c); printf("Hello World\n"); } From 266e90eea0e5d7f3b8654b701e5b1128ab396218 Mon Sep 17 00:00:00 2001 From: iFlameing Date: Tue, 16 Oct 2018 19:31:41 +0530 Subject: [PATCH 2/2] Now it is supporting multiple headerfile --- src/compilerServices/clangService.ts | 34 ++++++++++++---------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/compilerServices/clangService.ts b/src/compilerServices/clangService.ts index 09461a182..e03eaa4dd 100644 --- a/src/compilerServices/clangService.ts +++ b/src/compilerServices/clangService.ts @@ -31,30 +31,26 @@ export class ClangService implements CompilerService { async compile(input: ServiceInput): Promise { const files = Object.values(input.files); - // if (files.length !== 1) { - // throw new Error(`Supporting compilation of a single file, but ${files.length} file(s) found`); - // } const [ fileRef ] = Object.keys(input.files); - const src = files[0].content; - const header = files[1].content; const from = this.lang; + const inputFile=Object.keys(input.files); + const File = function(inputFile:any,file:any){ + const compileFile =[]; + for(let i=0;i