1 // 2 // Copyright (c) 2010-2021 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using System.IO; 10 using System.CodeDom.Compiler; 11 using System.Collections.Generic; 12 using System.Linq; 13 using Antmicro.Renode.Exceptions; 14 15 namespace Antmicro.Renode.Utilities 16 { 17 public class AdHocCompiler 18 { Compile(string sourcePath)19 public string Compile(string sourcePath) 20 { 21 var outputFileName = TemporaryFilesManager.Instance.GetTemporaryFile(); 22 var parameters = new List<string>(); 23 24 if(AssemblyHelper.BundledAssembliesCount > 0) 25 { 26 // portable already has all the libs included 27 parameters.Add("/nostdlib+"); 28 } 29 30 if(Environment.OSVersion.Platform == PlatformID.Unix) 31 { 32 parameters.Add("/langversion:experimental"); 33 } 34 35 var locations = AssemblyHelper.GetAssembliesLocations(); 36 foreach(var location in locations) 37 { 38 parameters.Add($"/r:{location}"); 39 } 40 41 parameters.Add("/target:library"); 42 parameters.Add("/debug-"); 43 parameters.Add("/optimize+"); 44 parameters.Add($"/out:{outputFileName}"); 45 parameters.Add("/noconfig"); 46 47 parameters.Add("--"); 48 parameters.Add(sourcePath); 49 50 var result = false; 51 var errorOutput = new StringWriter(); 52 try 53 { 54 result = Mono.CSharp.CompilerCallableEntryPoint.InvokeCompiler(parameters.ToArray(), errorOutput); 55 } 56 catch(Exception e) 57 { 58 throw new RecoverableException($"Could not compile assembly: {e.Message}"); 59 } 60 61 if(!result) 62 { 63 throw new RecoverableException($"There were compilation errors:\n{(errorOutput.ToString())}"); 64 } 65 66 return outputFileName; 67 } 68 } 69 } 70 71