|
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; }
|
@@ -353,66 +350,52 @@ private void WriteGeneratedCodeToFile(string file, string generatedCode)
|
353 | 350 |
|
354 | 351 | private static readonly Dictionary<Module, string> libraryMappings = new Dictionary<Module, string>();
|
355 | 352 |
|
356 |
| - public void CompileCode(Module module) |
| 353 | + public bool CompileCode(Module module) |
357 | 354 | {
|
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 | 355 | 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() } })) |
| 356 | + string csproj = Path.Combine(Options.OutputDir, $"{module.LibraryName}.csproj"); |
| 357 | + File.WriteAllText(Path.Combine(Options.OutputDir, "Directory.Build.props"), "<Project />"); |
| 358 | + File.WriteAllText(csproj, |
| 359 | + $@" |
| 360 | +<Project Sdk=""Microsoft.NET.Sdk""> |
| 361 | + <PropertyGroup> |
| 362 | + <TargetFramework>netcoreapp3.1</TargetFramework> |
| 363 | + <PlatformTarget>AnyCPU</PlatformTarget> |
| 364 | + <OutputPath>{Options.OutputDir}</OutputPath> |
| 365 | + <Configuration>Release</Configuration> |
| 366 | + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> |
| 367 | + <EnableDefaultNoneItems>false</EnableDefaultNoneItems> |
| 368 | + <EnableDefaultItems>false</EnableDefaultItems> |
| 369 | + <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> |
| 370 | + </PropertyGroup> |
| 371 | + <ItemGroup> |
| 372 | + {string.Join(Environment.NewLine, module.CodeFiles.Select(c => $"<Compile Include=\"{c}\" />"))} |
| 373 | + </ItemGroup> |
| 374 | + <ItemGroup> |
| 375 | + {string.Join(Environment.NewLine, |
| 376 | + new[] { Path.Combine(Path.GetDirectoryName(location), "CppSharp.Runtime.dll") } |
| 377 | + .Union(from dependency in module.Dependencies |
| 378 | + where libraryMappings.ContainsKey(dependency) |
| 379 | + select libraryMappings[dependency]) |
| 380 | + .Select(reference => |
| 381 | + $@"<Reference Include=""{Path.GetFileNameWithoutExtension(reference)}""> |
| 382 | + <HintPath>{reference}</HintPath> |
| 383 | + </Reference>"))} |
| 384 | + </ItemGroup> |
| 385 | +</Project>".Trim()); |
| 386 | + |
| 387 | + string output = ProcessHelper.Run("dotnet", $"msbuild -restore {csproj}", |
| 388 | + out int error, out string errorMessage); |
| 389 | + if (error == 0) |
399 | 390 | {
|
400 |
| - compilerResults = codeProvider.CompileAssemblyFromFile( |
401 |
| - compilerParameters, module.CodeFiles.ToArray()); |
| 391 | + Diagnostics.Message($@"Compilation succeeded: { |
| 392 | + libraryMappings[module] = Path.Combine(Options.OutputDir, $"{module.LibraryName}.dll")}."); |
| 393 | + return true; |
402 | 394 | }
|
403 | 395 |
|
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 |
| - } |
| 396 | + Diagnostics.Error(output); |
| 397 | + Diagnostics.Error(errorMessage); |
| 398 | + return false; |
416 | 399 | }
|
417 | 400 |
|
418 | 401 | public void AddTranslationUnitPass(TranslationUnitPass pass)
|
@@ -498,12 +481,7 @@ public static void Run(ILibrary library)
|
498 | 481 |
|
499 | 482 | driver.SaveCode(outputs);
|
500 | 483 | 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 |
| - } |
| 484 | + driver.Options.Modules.Any(m => !driver.CompileCode(m)); |
507 | 485 | }
|
508 | 486 | }
|
509 | 487 | }
|
|
0 commit comments