From ae0fee1095747ae53d754a8f9e747d3ee29be1f7 Mon Sep 17 00:00:00 2001
From: Patrick Quist <partouf@gmail.com>
Date: Fri, 2 Dec 2022 13:42:47 +0100
Subject: [PATCH] first bits of compiler interface (#4378)

---
 lib/base-compiler.ts                          |  6 ++---
 .../{fake-for-test.js => fake-for-test.ts}    | 23 ++++++++++++++-----
 lib/handlers/compile.ts                       |  9 ++++----
 types/compiler.interfaces.ts                  | 12 +++++++++-
 4 files changed, 36 insertions(+), 14 deletions(-)
 rename lib/compilers/{fake-for-test.js => fake-for-test.ts} (81%)

diff --git a/lib/base-compiler.ts b/lib/base-compiler.ts
index f551b96b4..3aeb34b3f 100644
--- a/lib/base-compiler.ts
+++ b/lib/base-compiler.ts
@@ -42,7 +42,7 @@ import {
     LLVMOptPipelineBackendOptions,
     LLVMOptPipelineOutput,
 } from '../types/compilation/llvm-opt-pipeline-output.interfaces';
-import {CompilerInfo} from '../types/compiler.interfaces';
+import {CompilerInfo, ICompiler} from '../types/compiler.interfaces';
 import {
     BasicExecutionResult,
     ExecutableExecutionOptions,
@@ -79,8 +79,8 @@ import {getToolchainPath} from './toolchain-utils';
 import {Tool, ToolResult, ToolTypeKey} from './tooling/base-tool.interface';
 import * as utils from './utils';
 
-export class BaseCompiler {
-    public compiler: CompilerInfo & Record<string, any>; // TODO: Some missing types still present in Compiler type
+export class BaseCompiler implements ICompiler {
+    protected compiler: CompilerInfo & Record<string, any>; // TODO: Some missing types still present in Compiler type
     public lang: Language;
     protected compileFilename: string;
     protected env: any;
diff --git a/lib/compilers/fake-for-test.js b/lib/compilers/fake-for-test.ts
similarity index 81%
rename from lib/compilers/fake-for-test.js
rename to lib/compilers/fake-for-test.ts
index bef34d7e3..4eed76ebd 100644
--- a/lib/compilers/fake-for-test.js
+++ b/lib/compilers/fake-for-test.ts
@@ -24,7 +24,16 @@
 
 import _ from 'underscore';
 
-export class FakeCompiler {
+import {ICompiler} from '../../types/compiler.interfaces';
+import {Language} from '../../types/languages.interfaces';
+import {CompilerArguments} from '../compiler-arguments';
+
+export class FakeCompiler implements ICompiler {
+    public possibleArguments: CompilerArguments;
+    public lang: any;
+    private compiler: any;
+    private info: any;
+
     static get key() {
         return 'fake-for-test';
     }
@@ -40,10 +49,15 @@ export class FakeCompiler {
         );
         this.lang = {id: this.compiler.lang, name: `Language ${this.compiler.lang}`};
         this.info = info;
+        this.possibleArguments = new CompilerArguments();
+    }
+
+    initialise(mtime: Date, clientOptions: any, isPrediscovered: boolean) {
+        throw new Error('Method not implemented.');
     }
 
     getInfo() {
-        return null;
+        return this.compiler;
     }
 
     getDefaultFilters() {
@@ -61,6 +75,7 @@ export class FakeCompiler {
                 options: options,
                 backendOptions: backendOptions,
                 filters: filters,
+                files: undefined,
             },
         };
 
@@ -79,8 +94,4 @@ export class FakeCompiler {
             }),
         );
     }
-
-    initalize() {
-        return null;
-    }
 }
diff --git a/lib/handlers/compile.ts b/lib/handlers/compile.ts
index 4361f21dd..9577f6af1 100644
--- a/lib/handlers/compile.ts
+++ b/lib/handlers/compile.ts
@@ -33,6 +33,7 @@ import temp from 'temp';
 import _ from 'underscore';
 import which from 'which';
 
+import {ICompiler} from '../../types/compiler.interfaces';
 import {BaseCompiler} from '../base-compiler';
 import {CompilationEnvironment} from '../compilation-env';
 import {getCompilerTypeByKey} from '../compilers';
@@ -156,7 +157,7 @@ export class CompileHandler {
         });
     }
 
-    async create(compiler): Promise<BaseCompiler | null> {
+    async create(compiler): Promise<ICompiler | null> {
         const isPrediscovered = !!compiler.version;
 
         const type = compiler.compilerType || 'default';
@@ -208,7 +209,7 @@ export class CompileHandler {
         }
     }
 
-    async setCompilers(compilers: BaseCompiler[], clientOptions: Record<string, any>) {
+    async setCompilers(compilers: ICompiler[], clientOptions: Record<string, any>) {
         // Be careful not to update this.compilersById until we can replace it entirely.
         const compilersById = {};
         try {
@@ -217,9 +218,9 @@ export class CompileHandler {
             let compilersCreated = 0;
             const createdCompilers = _.compact(await Promise.all(_.map(compilers, this.create, this)));
             for (const compiler of createdCompilers) {
-                const langId = compiler.compiler.lang;
+                const langId = compiler.getInfo().lang;
                 if (!compilersById[langId]) compilersById[langId] = {};
-                compilersById[langId][compiler.compiler.id] = compiler;
+                compilersById[langId][compiler.getInfo().id] = compiler;
                 compilersCreated++;
             }
             logger.info('Compilers created: ' + compilersCreated);
diff --git a/types/compiler.interfaces.ts b/types/compiler.interfaces.ts
index 41cd1f40e..a896a46ab 100644
--- a/types/compiler.interfaces.ts
+++ b/types/compiler.interfaces.ts
@@ -22,9 +22,10 @@
 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 // POSSIBILITY OF SUCH DAMAGE.
 
-// Minimal Compiler properties until a better one can be sync'ed with the backend
+import {CompilerArguments} from '../lib/compiler-arguments';
 import {Tool, ToolInfo} from '../lib/tooling/base-tool.interface';
 
+import {Language} from './languages.interfaces';
 import {Library} from './libraries/libraries.interfaces';
 
 export type CompilerInfo = {
@@ -85,3 +86,12 @@ export type CompilerInfo = {
         preamble?: string;
     };
 };
+
+export interface ICompiler {
+    possibleArguments: CompilerArguments;
+    lang: Language;
+    compile(source, options, backendOptions, filters, bypassCache, tools, executionParameters, libraries, files);
+    cmake(files, key);
+    initialise(mtime: Date, clientOptions, isPrediscovered: boolean);
+    getInfo();
+}
-- 
GitLab