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,27 @@ 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" ) ;
361-
362- var compilerOptions = new StringBuilder ( ) ;
363- compilerOptions . Append ( $ " /doc:\" { docFile } \" ") ;
364- compilerOptions . Append ( " /debug:pdbonly" ) ;
365- compilerOptions . Append ( " /unsafe" ) ;
366-
367- var compilerParameters = new CompilerParameters
368- {
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 ( ) } } ) )
353+ var msBuildGenerator = new MSBuildGenerator ( Context , module , libraryMappings ) ;
354+ msBuildGenerator . Process ( ) ;
355+ string csproj = Path . Combine ( Options . OutputDir ,
356+ $ "{ module . LibraryName } .{ msBuildGenerator . FileExtension } ") ;
357+ File . WriteAllText ( csproj , msBuildGenerator . Generate ( ) ) ;
358+
359+ string output = ProcessHelper . Run ( "dotnet" , $ "build { csproj } ",
360+ out int error , out string errorMessage ) ;
361+ if ( error == 0 )
399362 {
400- compilerResults = codeProvider . CompileAssemblyFromFile (
401- compilerParameters , module . CodeFiles . ToArray ( ) ) ;
363+ Diagnostics . Message ( $@ "Compilation succeeded: {
364+ libraryMappings [ module ] = Path . Combine (
365+ Options . OutputDir , $ "{ module . LibraryName } .dll") } ." ) ;
366+ return true ;
402367 }
403368
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- }
369+ Diagnostics . Error ( output ) ;
370+ Diagnostics . Error ( errorMessage ) ;
371+ return false ;
416372 }
417373
418374 public void AddTranslationUnitPass ( TranslationUnitPass pass )
@@ -433,6 +389,7 @@ public void Dispose()
433389 }
434390
435391 private bool hasParsingErrors ;
392+ private static readonly Dictionary < Module , string > libraryMappings = new Dictionary < Module , string > ( ) ;
436393 }
437394
438395 public static class ConsoleDriver
@@ -498,12 +455,7 @@ public static void Run(ILibrary library)
498455
499456 driver . SaveCode ( outputs ) ;
500457 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- }
458+ driver . Options . Modules . Any ( m => ! driver . CompileCode ( m ) ) ;
507459 }
508460 }
509461 }
0 commit comments