Skip to content

Commit

Permalink
Support trunk and execution for .NET (#4351)
Browse files Browse the repository at this point in the history
* Support trunk and execution for .NET
* Adjust compilers order
* Support stdin
* Update samples
  • Loading branch information
hez2010 committed Nov 27, 2022
1 parent 01e67e6 commit 75b9d30
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 12 deletions.
11 changes: 9 additions & 2 deletions etc/config/csharp.amazon.properties
@@ -1,10 +1,10 @@
compilers=&csharp
supportsBinary=false
supportsBinary=true
needsMulti=false
compilerType=csharp
defaultCompiler=dotnet700csharp

group.csharp.compilers=dotnet6011csharp:dotnet700csharp
group.csharp.compilers=dotnettrunkcsharp:dotnet700csharp:dotnet6011csharp

compiler.dotnet6011csharp.exe=/opt/compiler-explorer/dotnet-v6.0.11/.dotnet/dotnet
compiler.dotnet6011csharp.name=.NET 6.0.403
Expand All @@ -19,3 +19,10 @@ compiler.dotnet700csharp.clrDir=/opt/compiler-explorer/dotnet-v7.0.0
compiler.dotnet700csharp.targetFramework=net7.0
compiler.dotnet700csharp.buildConfig=Release
compiler.dotnet700csharp.langVersion=latest

compiler.dotnettrunkcsharp.exe=/opt/compiler-explorer/dotnet-trunk/.dotnet/dotnet
compiler.dotnettrunkcsharp.name=.NET (main)
compiler.dotnettrunkcsharp.clrDir=/opt/compiler-explorer/dotnet-trunk
compiler.dotnettrunkcsharp.targetFramework=net8.0
compiler.dotnettrunkcsharp.buildConfig=Release
compiler.dotnettrunkcsharp.langVersion=preview
2 changes: 1 addition & 1 deletion etc/config/csharp.defaults.properties
@@ -1,5 +1,5 @@
compilers=dotnet6csharp:dotnet7csharp
supportsBinary=false
supportsBinary=true
needsMulti=false
compilerType=csharp
defaultCompiler=dotnet7csharp
Expand Down
11 changes: 9 additions & 2 deletions etc/config/fsharp.amazon.properties
@@ -1,10 +1,10 @@
compilers=&fsharp
supportsBinary=false
supportsBinary=true
needsMulti=false
compilerType=fsharp
defaultCompiler=dotnet700fsharp

group.fsharp.compilers=dotnet6011fsharp:dotnet700fsharp
group.fsharp.compilers=dotnettrunkfsharp:dotnet700fsharp:dotnet6011fsharp

compiler.dotnet6011fsharp.exe=/opt/compiler-explorer/dotnet-v6.0.11/.dotnet/dotnet
compiler.dotnet6011fsharp.name=.NET 6.0.403
Expand All @@ -19,3 +19,10 @@ compiler.dotnet700fsharp.clrDir=/opt/compiler-explorer/dotnet-v7.0.0
compiler.dotnet700fsharp.targetFramework=net7.0
compiler.dotnet700fsharp.buildConfig=Release
compiler.dotnet700fsharp.langVersion=latest

compiler.dotnettrunkfsharp.exe=/opt/compiler-explorer/dotnet-trunk/.dotnet/dotnet
compiler.dotnettrunkfsharp.name=.NET (main)
compiler.dotnettrunkfsharp.clrDir=/opt/compiler-explorer/dotnet-trunk
compiler.dotnettrunkfsharp.targetFramework=net8.0
compiler.dotnettrunkfsharp.buildConfig=Release
compiler.dotnettrunkfsharp.langVersion=preview
2 changes: 1 addition & 1 deletion etc/config/fsharp.defaults.properties
@@ -1,5 +1,5 @@
compilers=dotnet6fsharp:dotnet7fsharp
supportsBinary=false
supportsBinary=true
needsMulti=false
compilerType=fsharp
defaultCompiler=dotnet7fsharp
Expand Down
11 changes: 9 additions & 2 deletions etc/config/vb.amazon.properties
@@ -1,10 +1,10 @@
compilers=&vb
supportsBinary=false
supportsBinary=true
needsMulti=false
compilerType=vb
defaultCompiler=dotnet700vb

group.vb.compilers=dotnet6011vb:dotnet700vb
group.vb.compilers=dotnettrunkvb:dotnet700vb:dotnet6011vb

compiler.dotnet6011vb.exe=/opt/compiler-explorer/dotnet-v6.0.11/.dotnet/dotnet
compiler.dotnet6011vb.name=.NET 6.0.403
Expand All @@ -19,3 +19,10 @@ compiler.dotnet700vb.clrDir=/opt/compiler-explorer/dotnet-v7.0.0
compiler.dotnet700vb.targetFramework=net7.0
compiler.dotnet700vb.buildConfig=Release
compiler.dotnet700vb.langVersion=latest

compiler.dotnettrunkvb.exe=/opt/compiler-explorer/dotnet-trunk/.dotnet/dotnet
compiler.dotnettrunkvb.name=.NET (main)
compiler.dotnettrunkvb.clrDir=/opt/compiler-explorer/dotnet-trunk
compiler.dotnettrunkvb.targetFramework=net8.0
compiler.dotnettrunkvb.buildConfig=Release
compiler.dotnettrunkvb.langVersion=latest
2 changes: 1 addition & 1 deletion etc/config/vb.defaults.properties
@@ -1,5 +1,5 @@
compilers=dotnet6vb:dotnet7vb
supportsBinary=false
supportsBinary=true
needsMulti=false
compilerType=vb
defaultCompiler=dotnet7vb
Expand Down
3 changes: 3 additions & 0 deletions examples/csharp/default.cs
@@ -1,4 +1,7 @@
using System;

class Program
{
static int Square(int num) => num * num;
static void Main() => Console.WriteLine(Sqaure(42));
}
5 changes: 5 additions & 0 deletions examples/fsharp/default.fs
@@ -1,3 +1,8 @@
module Program

let square num = num * num

[<EntryPoint>]
let main _ =
printfn "%d" (square 42)
0
6 changes: 6 additions & 0 deletions examples/vb/default.vb
@@ -1,5 +1,11 @@
Imports System

Module Program
Function Square(num As Integer) As Integer
Return num * num
End Function

Sub Main()
Console.WriteLine(Square(42))
End Sub
End Module
2 changes: 1 addition & 1 deletion lib/base-compiler.ts
Expand Up @@ -381,7 +381,7 @@ export class BaseCompiler {
}

supportsObjdump() {
return this.objdumperClass !== '';
return !!this.objdumperClass;
}

getObjdumpOutputFilename(defaultOutputFilename) {
Expand Down
58 changes: 56 additions & 2 deletions lib/compilers/dotnet.ts
Expand Up @@ -22,19 +22,29 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import fs from 'fs-extra';
import path from 'path';
import _ from 'underscore';

import fs from 'fs-extra';
import * as exec from '../exec';
import * as utils from '../utils';

import {
BasicExecutionResult,
ExecutableExecutionOptions,
UnprocessedExecResult,
} from '../../types/execution/execution.interfaces';
import {CompilationResult, ExecutionOptions} from '../../types/compilation/compilation.interfaces';
import {BaseCompiler} from '../base-compiler';
import {DotNetAsmParser} from '../parsers/asm-parser-dotnet';
import {ParseFilters} from '../../types/features/filters.interfaces';

class DotNetCompiler extends BaseCompiler {
private targetFramework: string;
private buildConfig: string;
private clrBuildDir: string;
private langVersion: string;
private compileToBinary = false;

constructor(compilerInfo, env) {
super(compilerInfo, env);
Expand Down Expand Up @@ -104,6 +114,7 @@ class DotNetCompiler extends BaseCompiler {
<LangVersion>${this.langVersion}</LangVersion>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Nullable>enable</Nullable>
<OutputType>${this.compileToBinary ? 'Exe' : 'Library'}</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="${sourceFile}" />
Expand Down Expand Up @@ -193,10 +204,53 @@ class DotNetCompiler extends BaseCompiler {
return compilerResult;
}

override optionsForFilter() {
override optionsForFilter(filters: ParseFilters) {
if (filters.binary) {
this.compileToBinary = true;
}

return this.compilerOptions;
}

override async execBinary(
executable: string,
maxSize: number | undefined,
executeParameters: ExecutableExecutionOptions,
homeDir: string | undefined,
): Promise<BasicExecutionResult> {
const programDir = path.dirname(executable);
const programOutputPath = path.join(programDir, 'bin', this.buildConfig, this.targetFramework);
const programDllPath = path.join(programOutputPath, 'CompilerExplorer.dll');
const execOptions = this.getDefaultExecOptions();
execOptions.maxOutput = maxSize;
execOptions.timeoutMs = this.env.ceProps('binaryExecTimeoutMs', 2000);
execOptions.ldPath = _.union(this.compiler.ldPath, executeParameters.ldPath);
execOptions.customCwd = homeDir;
execOptions.appHome = homeDir;
execOptions.env = executeParameters.env;
execOptions.env.DOTNET_EnableWriteXorExecute = '0';
execOptions.env.DOTNET_CLI_HOME = programDir;
execOptions.env.CORE_ROOT = this.clrBuildDir;
execOptions.input = executeParameters.stdin;
const execArgs = ['-p', 'System.Runtime.TieredCompilation=false', programDllPath, ...executeParameters.args];
const corerun = path.join(this.clrBuildDir, 'corerun');
try {
const execResult: UnprocessedExecResult = await exec.sandbox(corerun, execArgs, execOptions);
return this.processExecutionResult(execResult);
} catch (err: UnprocessedExecResult | any) {
if (err.code && err.stderr) {
return this.processExecutionResult(err);
} else {
return {
...this.getEmptyExecutionResult(),
stdout: err.stdout ? utils.parseOutput(err.stdout) : [],
stderr: err.stderr ? utils.parseOutput(err.stderr) : [],
code: err.code !== undefined ? err.code : -1,
};
}
}
}

async runCrossgen2(compiler, execOptions, crossgen2Path, bclPath, dllPath, options, outputPath) {
const crossgen2Options = [
crossgen2Path,
Expand Down

0 comments on commit 75b9d30

Please sign in to comment.