11using System ;
2- using System . CodeDom . Compiler ;
32using System . Collections . Generic ;
43using System . IO ;
54using System . Linq ;
6- using System . Text ;
75using CppSharp . AST ;
86using CppSharp . Generators ;
7+ using CppSharp . Generators . C ;
98using CppSharp . Generators . CLI ;
9+ using CppSharp . Generators . Cpp ;
1010using CppSharp . Generators . CSharp ;
1111using CppSharp . Parser ;
1212using CppSharp . Passes ;
1313using CppSharp . Utils ;
14- using Microsoft . CSharp ;
1514using CppSharp . Types ;
16- using CppSharp . Generators . Cpp ;
17- using CppSharp . Generators . C ;
1815
1916namespace CppSharp
2017{
2118 public class Driver : IDisposable
2219 {
23- public DriverOptions Options { get ; private set ; }
20+ public DriverOptions Options { get ; }
2421 public ParserOptions ParserOptions { get ; set ; }
2522 public BindingContext Context { get ; private set ; }
2623 public Generator Generator { get ; private set ; }
@@ -351,68 +348,29 @@ private void WriteGeneratedCodeToFile(string file, string generatedCode)
351348 File . WriteAllText ( file , generatedCode ) ;
352349 }
353350
354- private static readonly Dictionary < Module , string > libraryMappings = new Dictionary < Module , string > ( ) ;
355-
356- public void CompileCode ( Module module )
351+ public bool CompileCode ( Module module )
357352 {
358- var assemblyFile = Path . Combine ( Options . OutputDir , module . LibraryName + ".dll" ) ;
359-
360- var docFile = Path . ChangeExtension ( assemblyFile , ".xml" ) ;
353+ File . WriteAllText ( Path . Combine ( Options . OutputDir , "Directory.Build.props" ) , "<Project />" ) ;
361354
362- var compilerOptions = new StringBuilder ( ) ;
363- compilerOptions . Append ( $ " /doc:\" { docFile } \" ") ;
364- compilerOptions . Append ( " /debug:pdbonly" ) ;
365- compilerOptions . Append ( " /unsafe" ) ;
355+ var msBuildGenerator = new MSBuildGenerator ( Context , module , libraryMappings ) ;
356+ msBuildGenerator . Process ( ) ;
357+ string csproj = Path . Combine ( Options . OutputDir ,
358+ $ "{ module . LibraryName } .{ msBuildGenerator . FileExtension } ") ;
359+ File . WriteAllText ( csproj , msBuildGenerator . Generate ( ) ) ;
366360
367- var compilerParameters = new CompilerParameters
361+ string output = ProcessHelper . Run ( "dotnet" , $ "build { csproj } ",
362+ out int error , out string errorMessage ) ;
363+ if ( error == 0 )
368364 {
369- GenerateExecutable = false ,
370- TreatWarningsAsErrors = false ,
371- OutputAssembly = assemblyFile ,
372- GenerateInMemory = false ,
373- CompilerOptions = compilerOptions . ToString ( )
374- } ;
375-
376- if ( module != Options . SystemModule )
377- compilerParameters . ReferencedAssemblies . Add (
378- Path . Combine ( Options . OutputDir , $ "{ Options . SystemModule . LibraryName } .dll") ) ;
379- // add a reference to System.Core
380- compilerParameters . ReferencedAssemblies . Add ( typeof ( Enumerable ) . Assembly . Location ) ;
381-
382- var location = System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ;
383- var outputDir = Path . GetDirectoryName ( location ) ;
384- var locationRuntime = Path . Combine ( outputDir , "CppSharp.Runtime.dll" ) ;
385- compilerParameters . ReferencedAssemblies . Add ( locationRuntime ) ;
386-
387- compilerParameters . ReferencedAssemblies . AddRange (
388- ( from dependency in module . Dependencies
389- where libraryMappings . ContainsKey ( dependency )
390- select libraryMappings [ dependency ] ) . ToArray ( ) ) ;
391-
392- compilerParameters . ReferencedAssemblies . AddRange ( module . ReferencedAssemblies . ToArray ( ) ) ;
393-
394- Diagnostics . Message ( $ "Compiling { module . LibraryName } ...") ;
395- CompilerResults compilerResults ;
396- using ( var codeProvider = new CSharpCodeProvider (
397- new Dictionary < string , string > {
398- { "CompilerDirectoryPath" , ManagedToolchain . FindCSharpCompilerDir ( ) } } ) )
399- {
400- compilerResults = codeProvider . CompileAssemblyFromFile (
401- compilerParameters , module . CodeFiles . ToArray ( ) ) ;
365+ Diagnostics . Message ( $@ "Compilation succeeded: {
366+ libraryMappings [ module ] = Path . Combine (
367+ Options . OutputDir , $ "{ module . LibraryName } .dll") } ." ) ;
368+ return true ;
402369 }
403370
404- var errors = compilerResults . Errors . Cast < CompilerError > ( ) . Where ( e => ! e . IsWarning &&
405- // HACK: auto-compiling on OS X produces "errors" which do not affect compilation so we ignore them
406- ( ! Platform . IsMacOS || ! e . ErrorText . EndsWith ( "(Location of the symbol related to previous warning)" , StringComparison . Ordinal ) ) ) . ToList ( ) ;
407- foreach ( var error in errors )
408- Diagnostics . Error ( error . ToString ( ) ) ;
409-
410- HasCompilationErrors = errors . Count > 0 ;
411- if ( ! HasCompilationErrors )
412- {
413- libraryMappings [ module ] = Path . Combine ( outputDir , assemblyFile ) ;
414- Diagnostics . Message ( "Compilation succeeded." ) ;
415- }
371+ Diagnostics . Error ( output ) ;
372+ Diagnostics . Error ( errorMessage ) ;
373+ return false ;
416374 }
417375
418376 public void AddTranslationUnitPass ( TranslationUnitPass pass )
@@ -433,6 +391,7 @@ public void Dispose()
433391 }
434392
435393 private bool hasParsingErrors ;
394+ private static readonly Dictionary < Module , string > libraryMappings = new Dictionary < Module , string > ( ) ;
436395 }
437396
438397 public static class ConsoleDriver
@@ -498,12 +457,7 @@ public static void Run(ILibrary library)
498457
499458 driver . SaveCode ( outputs ) ;
500459 if ( driver . Options . IsCSharpGenerator && driver . Options . CompileCode )
501- foreach ( var module in driver . Options . Modules )
502- {
503- driver . CompileCode ( module ) ;
504- if ( driver . HasCompilationErrors )
505- break ;
506- }
460+ driver . Options . Modules . Any ( m => ! driver . CompileCode ( m ) ) ;
507461 }
508462 }
509463 }
0 commit comments