1
1
using System ;
2
- using System . CodeDom . Compiler ;
3
2
using System . Collections . Generic ;
4
3
using System . IO ;
5
4
using System . Linq ;
6
- using System . Text ;
7
5
using CppSharp . AST ;
8
6
using CppSharp . Generators ;
7
+ using CppSharp . Generators . C ;
9
8
using CppSharp . Generators . CLI ;
9
+ using CppSharp . Generators . Cpp ;
10
10
using CppSharp . Generators . CSharp ;
11
11
using CppSharp . Parser ;
12
12
using CppSharp . Passes ;
13
13
using CppSharp . Utils ;
14
- using Microsoft . CSharp ;
15
14
using CppSharp . Types ;
16
- using CppSharp . Generators . Cpp ;
17
- using CppSharp . Generators . C ;
18
15
19
16
namespace CppSharp
20
17
{
21
18
public class Driver : IDisposable
22
19
{
23
- public DriverOptions Options { get ; private set ; }
20
+ public DriverOptions Options { get ; }
24
21
public ParserOptions ParserOptions { get ; set ; }
25
22
public BindingContext Context { get ; private set ; }
26
23
public Generator Generator { get ; private set ; }
@@ -351,68 +348,29 @@ private void WriteGeneratedCodeToFile(string file, string generatedCode)
351
348
File . WriteAllText ( file , generatedCode ) ;
352
349
}
353
350
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 )
357
352
{
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 />" ) ;
361
354
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 ( ) ) ;
366
360
367
- var compilerParameters = new CompilerParameters
361
+ string output = ProcessHelper . Run ( "dotnet" , $ "build { csproj } ",
362
+ out int error , out string errorMessage ) ;
363
+ if ( error == 0 )
368
364
{
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 ;
402
369
}
403
370
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 ;
416
374
}
417
375
418
376
public void AddTranslationUnitPass ( TranslationUnitPass pass )
@@ -433,6 +391,7 @@ public void Dispose()
433
391
}
434
392
435
393
private bool hasParsingErrors ;
394
+ private static readonly Dictionary < Module , string > libraryMappings = new Dictionary < Module , string > ( ) ;
436
395
}
437
396
438
397
public static class ConsoleDriver
@@ -498,12 +457,7 @@ public static void Run(ILibrary library)
498
457
499
458
driver . SaveCode ( outputs ) ;
500
459
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 ) ) ;
507
461
}
508
462
}
509
463
}
0 commit comments