Skip to content

Commit

Permalink
Merge pull request #106 from SharpGenTools/msbuild-cleanup
Browse files Browse the repository at this point in the history
Add included files to the MSBuild Up-to-Date checks.
  • Loading branch information
jkoritzinsky committed Apr 15, 2019
2 parents a11088e + 85c7143 commit af9bc2e
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 25 deletions.
47 changes: 47 additions & 0 deletions SharpGen.UnitTests/Parsing/MacroManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using SharpGen.Config;
using SharpGen.Parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Xunit;

namespace SharpGen.UnitTests.Parsing
{
public class MacroManagerTests : ParsingTestBase
{
public MacroManagerTests(Xunit.Abstractions.ITestOutputHelper outputHelper)
: base(outputHelper)
{
}

[Fact]
public void MacroManagerCollectsIncludedHeaders()
{
CreateCppFile("included", "");

var includeRule = GetTestFileIncludeRule();

var config = new ConfigFile
{
Id = nameof(MacroManagerCollectsIncludedHeaders),
Assembly = nameof(MacroManagerCollectsIncludedHeaders),
Namespace = nameof(MacroManagerCollectsIncludedHeaders),
IncludeDirs = { includeRule },
Includes =
{
CreateCppFile("includer", "#include \"included.h\"")
}
};

var castXml = GetCastXml(ConfigFile.Load(config, Array.Empty<string>(), Logger));

var macroManager = new MacroManager(castXml);

macroManager.Parse(Path.Combine(includeRule.Path, "includer.h"), new CppModel.CppModule());

Assert.Contains(includeRule.Path + "/included.h",
macroManager.IncludedFiles);
}
}
}
23 changes: 15 additions & 8 deletions SharpGen.UnitTests/Parsing/ParsingTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ public IncludeRule CreateCppFile(string cppFileName, string cppFile, List<string
};
}

protected CastXml GetCastXml(ConfigFile config, string[] additionalArguments = null)
{
var resolver = new IncludeDirectoryResolver(Logger);
resolver.Configure(config);
return new CastXml(
Logger,
resolver,
CastXmlExecutablePath,
additionalArguments ?? Array.Empty<string>())
{
OutputPath = TestDirectory.FullName
};
}

protected CppModule ParseCpp(ConfigFile config, string[] additionalArguments = null)
{
var loaded = ConfigFile.Load(config, new string[0], Logger);
Expand All @@ -64,14 +78,7 @@ protected CppModule ParseCpp(ConfigFile config, string[] additionalArguments = n

var (updated, _) = cppHeaderGenerator.GenerateCppHeaders(loaded, configsWithIncludes, filesWithExtensionHeaders);

var resolver = new IncludeDirectoryResolver(Logger);

resolver.Configure(loaded);

var castXml = new CastXml(Logger, resolver, CastXmlExecutablePath, additionalArguments ?? Array.Empty<string>())
{
OutputPath = TestDirectory.FullName
};
var castXml = GetCastXml(loaded);

var extensionGenerator = new CppExtensionHeaderGenerator(new MacroManager(castXml));

Expand Down
7 changes: 6 additions & 1 deletion SharpGen/Parser/MacroManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class MacroManager
private readonly CastXml _gccxml;
private Dictionary<string, string> _currentMacros = null;
private readonly Dictionary<string, Dictionary<string, string>> _mapIncludeToMacros = new Dictionary<string, Dictionary<string, string>>();
private readonly List<string> _includedFiles = new List<string>();

/// <summary>
/// Initializes a new instance of the <see cref="MacroManager"/> class.
Expand All @@ -46,6 +47,8 @@ public MacroManager(CastXml gccxml)
_gccxml = gccxml;
}

public IEnumerable<string> IncludedFiles => _includedFiles;

/// <summary>
/// Parses the specified C++ header file and fills the <see cref="CppModule"/> with defined macros.
/// </summary>
Expand Down Expand Up @@ -88,7 +91,9 @@ private void ParseLine(object sendingProcess, DataReceivedEventArgs outLine)
_currentMacros = null;
else
{
_includedFiles.Add(result.Groups[1].Value.Replace(@"\\", @"\"));
var currentFile = Path.GetFileName(result.Groups[1].Value);

if (!_mapIncludeToMacros.TryGetValue(currentFile, out _currentMacros))
{
_currentMacros = new Dictionary<string,string>();
Expand All @@ -97,7 +102,7 @@ private void ParseLine(object sendingProcess, DataReceivedEventArgs outLine)
}
}
else if (_currentMacros != null)
{
{
result = MatchDefine.Match(line);
if (result.Success)
{
Expand Down
3 changes: 0 additions & 3 deletions SharpGenTools.Sdk/GenerateConsumerBindMappingFile.targets

This file was deleted.

6 changes: 5 additions & 1 deletion SharpGenTools.Sdk/GetSnPath.Core.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project>
<Target Name="GetSnPath" Condition="'$(SnPath)' == '' OR !Exists('$(SnPath)')">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>

<Target Name="GetSnPath" Condition="'$(SnPath)' == '' OR !Exists('$(SnPath)')">
<Exec Command="where sn &gt; sn-path.txt" ContinueOnError="true" Condition="'$(OS)' == 'Windows_NT'" WorkingDirectory="$(IntermediateOutputPath)" />
<Exec Command="which sn &gt; sn-path.txt" ContinueOnError="true" Condition="'$(OS)' == 'Unix'" WorkingDirectory="$(IntermediateOutputPath)" />
<ReadLinesFromFile File="$(IntermediateOutputPath)/sn-path.txt">
Expand Down
4 changes: 4 additions & 0 deletions SharpGenTools.Sdk/GetSnPath.Full.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<Project>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>

<!-- Pulled from https://stackoverflow.com/questions/12781870/how-should-i-reference-sn-exe-in-msbuild-script -->
<Target Name="GetSnPath" Condition="'$(SnPath)' == '' OR !Exists('$(SnPath)')">
<GetFrameworkSdkPath>
Expand Down
6 changes: 5 additions & 1 deletion SharpGenTools.Sdk/GetSnPath.Mono.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project>
<Target Name="GetSnPath" Condition="'$(SnPath)' == '' OR !Exists('$(SnPath)')">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>

<Target Name="GetSnPath" Condition="'$(SnPath)' == '' OR !Exists('$(SnPath)')">
<Exec Command="where sn &gt; sn-path.txt" ContinueOnError="true" Condition="'$(OS)' == 'Windows_NT'" WorkingDirectory="$(IntermediateOutputPath)" />
<Exec Command="which sn &gt; sn-path.txt" ContinueOnError="true" Condition="'$(OS)' == 'Unix'" WorkingDirectory="$(IntermediateOutputPath)" />
<ReadLinesFromFile File="$(IntermediateOutputPath)/sn-path.txt">
Expand Down
4 changes: 4 additions & 0 deletions SharpGenTools.Sdk/SharpGenTools.Sdk.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<Project>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>

<PropertyGroup>
<SharpGenGenerateConsumerBindMapping Condition="'$(SharpGenGenerateConsumerBindMapping)' == ''">true</SharpGenGenerateConsumerBindMapping>
<SharpGenGenerateDoc Condition="'$(SharpGenGenerateDoc)' == ''">false</SharpGenGenerateDoc>
Expand Down
44 changes: 35 additions & 9 deletions SharpGenTools.Sdk/SharpGenTools.Sdk.targets
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
<Project>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>

<PropertyGroup>
<SharpGenIntermediateDir>$(IntermediateOutputPath)SharpGen/</SharpGenIntermediateDir>
<SharpGenDocumentationOutputDir Condition="'$(SharpGenDocumentationOutputDir)' == ''">$(SharpGenIntermediateDir)</SharpGenDocumentationOutputDir>
<ShadowCopyDocumentationOutput Condition="'$(SharpGenDocumentationOutputDir)' != '$(SharpGenIntermediateDir)'">true</ShadowCopyDocumentationOutput>
<SharpGenGeneratedCodeFolder Condition="'$(SharpGenGeneratedCodeFolder)' == ''">$(SharpGenIntermediateDir)Generated</SharpGenGeneratedCodeFolder>
<SharpGenConsumerBindMappingConfigId Condition="'$(SharpGenConsumerBindMappingConfigId)' == ''">$(AssemblyName)</SharpGenConsumerBindMappingConfigId>
<SharpGenOutputDirectory Condition="'$(SharpGenOutputDirectory)' == ''">$(MSBuildProjectDirectory)</SharpGenOutputDirectory>
<GenerateConsumerBindMappingFilePlaceholderProperty>CustomBeforeMicrosoftCommonTargets=$(MSBuildThisFileDirectory)/GenerateConsumerBindMappingFile.targets</GenerateConsumerBindMappingFilePlaceholderProperty>
</PropertyGroup>

<ItemGroup Condition="'$(SharpGenSdkAssembly)' == ''">
<SharpGenSdkAssembly Condition="'$(MSBuildRuntimeType)' == 'Core'"
Include="$(MSBuildThisFileDirectory)/../tools/netstandard1.3/SharpGenTools.Sdk.dll" />
<SharpGenSdkAssembly Condition="'$(MSBuildRuntimeType)' != 'Core'"
Include="$(MSBuildThisFileDirectory)/../tools/net46/SharpGenTools.Sdk.dll" />
</ItemGroup>

<ItemGroup>
<SharpGenSdkAssembly Condition="'$(MSBuildRuntimeType)' == 'Core'" Include="$(MSBuildThisFileDirectory)/../tools/netstandard1.3/SharpGenTools.Sdk.dll" />
<SharpGenSdkAssembly Condition="'$(MSBuildRuntimeType)' != 'Core'" Include="$(MSBuildThisFileDirectory)/../tools/net46/SharpGenTools.Sdk.dll" />
<SharpGenIncludedHeadersCache Include="$(SharpGenIntermediateDir)HeadersCache.txt" />
<SharpGenSdkCheckFile Include="$(SharpGenIntermediateDir)Sdk.checkfile" />
<CppConsumerConfig Include="$(SharpGenIntermediateDir)CppConsumerConfig.xml" />
<PartialCppModule Include="$(SharpGenIntermediateDir)PartialCppModule.xml" />
Expand Down Expand Up @@ -44,7 +53,9 @@
Projects="@(_MSBuildProjectReferenceExistent)"
Targets="GenerateConsumerBindMappingFile"
BuildInParallel="$(BuildInParallel)"
Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration); %(_MSBuildProjectReferenceExistent.SetPlatform); %(_MSBuildProjectReferenceExistent.SetTargetFramework); $(GenerateConsumerBindMappingFilePlaceholderProperty)"
Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration);
%(_MSBuildProjectReferenceExistent.SetPlatform);"
SkipNonexistentTargets="true"
ContinueOnError="$(ContinueOnError)"
RemoveProperties="%(_MSBuildProjectReferenceExistent.GlobalPropertiesToRemove)">
<Output TaskParameter="TargetOutputs" ItemName="SharpGenConsumerMapping" />
Expand Down Expand Up @@ -109,7 +120,7 @@
Inputs="@(SharpGenSdkAssembly)"
Outputs="@(SharpGenSdkCheckFile)">
<CreateProperty Value="true">
<Output TaskParameter="ValueSetByTask" PropertyName="NewSdkAssembly" />
<Output TaskParameter="ValueSetByTask" PropertyName="NewSdkAssembly" />
</CreateProperty>
</Target>

Expand All @@ -127,6 +138,12 @@
<Output TaskParameter="Headers" ItemName="Headers" />
<Output TaskParameter="ExtensionHeaders" ItemName="ExtensionHeaders" />
</GetGeneratedHeaderNames>

<ReadLinesFromFile
Condition="Exists('@(SharpGenIncludedHeadersCache)')"
File="@(SharpGenIncludedHeadersCache)">
<Output TaskParameter="Lines" ItemName="IncludedHeaders" />
</ReadLinesFromFile>
</Target>

<Target
Expand All @@ -150,7 +167,7 @@
<Target
Name="GenerateExtensionHeaders"
DependsOnTargets="PrepareCppGeneration;GenerateHeaders"
Inputs="@(SharpGenSdkAssembly);@(SharpGenMapping);@(SharpGenConsumerMapping);@(Headers)"
Inputs="@(SharpGenSdkAssembly);@(SharpGenMapping);@(SharpGenConsumerMapping);@(Headers);@(IncludedHeaders)"
Outputs="@(ExtensionHeaders);@(PartialCppModule)"
Condition="'@(SharpGenMapping)' != ''">
<GenerateExtensionHeaders
Expand All @@ -161,14 +178,23 @@
OutputPath="$(MSBuildProjectDirectory)/$(SharpGenIntermediateDir)"
PartialCppModuleCache="@(PartialCppModule)"
UpdatedConfigs="@(UpdatedConfigs)"
CastXmlArguments="@(CastXmlArg)"
/>
CastXmlArguments="@(CastXmlArg)">
<Output TaskParameter="ReferencedHeaders" ItemName="IncludedHeaders" />
</GenerateExtensionHeaders>

<RemoveDuplicates Inputs="@(IncludedHeaders)">
<Output TaskParameter="Filtered" ItemName="FilteredHeaders" />
</RemoveDuplicates>

<WriteLinesToFile
File="@(SharpGenIncludedHeadersCache)"
Lines="@(FilteredHeaders)" />
</Target>

<Target
Name="ParseCPlusPlus"
DependsOnTargets="GenerateHeaders;GenerateExtensionHeaders"
Inputs="@(SharpGenSdkAssembly);@(SharpGenMapping);@(SharpGenConsumerMapping);@(PartialCppModule)"
Inputs="@(SharpGenSdkAssembly);@(SharpGenMapping);@(SharpGenConsumerMapping);@(PartialCppModule);@(IncludedHeaders)"
Outputs="@(ParsedCppModule)"
Condition="'@(SharpGenMapping)' != ''">
<ParseCPlusPlus
Expand Down
9 changes: 8 additions & 1 deletion SharpGenTools.Sdk/Tasks/GenerateExtensionHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class GenerateExtensionHeaders : SharpGenCppTaskBase
[Required]
public string[] CastXmlArguments { get; set; }

[Output]
public ITaskItem[] ReferencedHeaders { get; set; }

protected override bool Execute(ConfigFile config)
{
var configsWithExtensions = new HashSet<string>();
Expand All @@ -55,10 +58,14 @@ protected override bool Execute(ConfigFile config)
OutputPath = OutputPath
};

var cppExtensionGenerator = new CppExtensionHeaderGenerator(new MacroManager(castXml));
var macroManager = new MacroManager(castXml);

var cppExtensionGenerator = new CppExtensionHeaderGenerator(macroManager);

var module = cppExtensionGenerator.GenerateExtensionHeaders(config, OutputPath, configsWithExtensions, updatedConfigs);

ReferencedHeaders = macroManager.IncludedFiles.Select(file => new TaskItem(file)).ToArray();

if (SharpGenLogger.HasErrors)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion build/build-outerloop-native.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Push-Location SdkTests/Native

try {
cmake -A x64 | Write-Host
cmake . -A x64 | Write-Host

if ($LastExitCode -ne 0) {
Write-Error "Failed to generate native projects"
Expand Down

0 comments on commit af9bc2e

Please sign in to comment.