-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for C++ libs into sourcelink #605
Open
nickdalt
wants to merge
7
commits into
dotnet:main
Choose a base branch
from
nickdalt:support-for-cpp-static-libs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
95a108d
Add support for C++ libs into sourcelink
nickdalt 093d114
Changed so that rather than merging all the sourcelink files into one…
nickdalt 1742dca
Add support for C++ libs into sourcelink
nickdalt 02c66c7
Changed so that rather than merging all the sourcelink files into one…
nickdalt b071384
Merge branch 'support-for-cpp-static-libs' of https://github.com/nick…
nickdalt 11db360
Review updates,
nickdalt 06ba507
Merge branch 'dotnet:main' into support-for-cpp-static-libs
nickdalt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/SourceLink.Common.UnitTests/FindAdditionalSourceLinkFilesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Licensed to the.NET Foundation under one or more agreements. | ||
// The.NET Foundation licenses this file to you under the MIT license. | ||
// See the License.txt file in the project root for more information. | ||
|
||
using System.IO; | ||
using TestUtilities; | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.SourceLink.Common.UnitTests | ||
{ | ||
public class FindAdditionalSourceLinkFilesTests | ||
{ | ||
[Fact] | ||
public void NoSourceLinkFilesExpected() | ||
{ | ||
var task = new FindAdditionalSourceLinkFiles() | ||
{ | ||
SourceLinkFile = "merged.sourcelink.json", | ||
ImportLibraries = new string[] { }, | ||
AdditionalDependencies = new string[] { } | ||
|
||
}; | ||
|
||
bool result = task.Execute(); | ||
|
||
Assert.NotNull(task.AllSourceLinkFiles); | ||
Assert.Single(task.AllSourceLinkFiles); | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void FoundSourceLinkForImportLib() | ||
{ | ||
string testLib = "test.lib"; | ||
string testSourceLink = "test.sourcelink.json"; | ||
|
||
using var temp = new TempRoot(); | ||
var root = temp.CreateDirectory(); | ||
var testDir = root.CreateDirectory("FoundSourceLinkForImportLib"); | ||
var testLibFile = root.CreateFile(Path.Combine(testDir.Path, testLib)); | ||
var testSourceLinkFile = root.CreateFile(Path.Combine(testDir.Path, testSourceLink)); | ||
var task = new FindAdditionalSourceLinkFiles() | ||
{ | ||
SourceLinkFile = "merged.sourcelink.json", | ||
ImportLibraries = new string[] { testLibFile.Path }, | ||
AdditionalDependencies = new string[] { } | ||
|
||
}; | ||
|
||
bool result = task.Execute(); | ||
Assert.NotNull(task.AllSourceLinkFiles); | ||
Assert.NotEmpty(task.AllSourceLinkFiles); | ||
#pragma warning disable CS8602 // Dereference of a possibly null reference - previously checked | ||
Assert.Equal(testSourceLinkFile.Path, task.AllSourceLinkFiles[1]); | ||
#pragma warning restore CS8602 // Dereference of a possibly null reference. | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void FoundSourceLinkForNonRootedAdditionalDependency() | ||
{ | ||
string testLib = "test.lib"; | ||
string testSourceLink = "test.sourcelink.json"; | ||
|
||
using var temp = new TempRoot(); | ||
var root = temp.CreateDirectory(); | ||
var testDir = root.CreateDirectory("FoundSourceLinkForNonRootedAdditionalDependency"); | ||
var testLibFile = root.CreateFile(Path.Combine(testDir.Path, testLib)); | ||
var testSourceLinkFile = root.CreateFile(Path.Combine(testDir.Path, testSourceLink)); | ||
var task = new FindAdditionalSourceLinkFiles() | ||
{ | ||
SourceLinkFile = "merged.sourcelink.json", | ||
ImportLibraries = new string[] { }, | ||
AdditionalDependencies = new string[] { testLib }, | ||
AdditionalLibraryDirectories = new string[] { testDir.Path } | ||
}; | ||
|
||
bool result = task.Execute(); | ||
Assert.NotNull(task.AllSourceLinkFiles); | ||
Assert.NotEmpty(task.AllSourceLinkFiles); | ||
#pragma warning disable CS8602 // Dereference of a possibly null reference - previously checked | ||
Assert.Equal(testSourceLinkFile.Path, task.AllSourceLinkFiles[1]); | ||
#pragma warning restore CS8602 // Dereference of a possibly null reference. | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void FoundSourceLinkForRootedAdditionalDependency() | ||
{ | ||
string testLib = "test.lib"; | ||
string testSourceLink = "test.sourcelink.json"; | ||
|
||
using var temp = new TempRoot(); | ||
var root = temp.CreateDirectory(); | ||
var testDir = root.CreateDirectory("FoundSourceLinkForRootedAdditionalDependency"); | ||
var testLibFile = root.CreateFile(Path.Combine(testDir.Path, testLib)); | ||
var testSourceLinkFile = root.CreateFile(Path.Combine(testDir.Path, testSourceLink)); | ||
var task = new FindAdditionalSourceLinkFiles() | ||
{ | ||
SourceLinkFile = "merged.sourcelink.json", | ||
ImportLibraries = new string[] { }, | ||
AdditionalDependencies = new string[] { testLibFile.Path }, | ||
AdditionalLibraryDirectories = new string[] { } | ||
}; | ||
|
||
bool result = task.Execute(); | ||
|
||
Assert.NotNull(task.AllSourceLinkFiles); | ||
Assert.NotEmpty(task.AllSourceLinkFiles); | ||
#pragma warning disable CS8602 // Dereference of a possibly null reference - previously checked | ||
Assert.Equal(testSourceLinkFile.Path, task.AllSourceLinkFiles[1]); | ||
#pragma warning restore CS8602 // Dereference of a possibly null reference. | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void SourceLinkError() | ||
{ | ||
var task = new FindAdditionalSourceLinkFiles() | ||
{ | ||
SourceLinkFile = "merged.sourcelink.json", | ||
ImportLibraries = new string[] { }, | ||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type - deliberate to cause error | ||
AdditionalDependencies = new string[] { null }, | ||
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. | ||
AdditionalLibraryDirectories = new string[] { @"C:\Does\Not\Exist" } | ||
}; | ||
|
||
bool result = task.Execute(); | ||
Assert.NotNull(task.AllSourceLinkFiles); | ||
Assert.Empty(task.AllSourceLinkFiles); | ||
Assert.False(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Build.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
|
||
namespace Microsoft.SourceLink.Common | ||
{ | ||
public sealed class FindAdditionalSourceLinkFiles : Build.Utilities.Task | ||
{ | ||
/// <summary> | ||
/// The name/path of the sourcelink file that we will merge into. | ||
/// </summary> | ||
[Required, NotNull] | ||
public string? SourceLinkFile { get; set; } | ||
|
||
/// <summary> | ||
/// Collection of all the library directories that will be searched for lib files. | ||
/// </summary> | ||
[Required, NotNull] | ||
public string[]? AdditionalLibraryDirectories { get; set; } | ||
|
||
/// <summary> | ||
/// Collection of all the libs that we will link to. | ||
/// </summary> | ||
[Required, NotNull] | ||
public string[]? AdditionalDependencies { get; set; } | ||
|
||
/// <summary> | ||
/// Collection of solution referenced import libraries. | ||
/// </summary> | ||
[Required, NotNull] | ||
public string[]? ImportLibraries { get; set; } | ||
|
||
[Output] | ||
public string[]? AllSourceLinkFiles { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
List<string> allSourceLinkFiles = new List<string>(); | ||
allSourceLinkFiles.Add(SourceLinkFile); | ||
|
||
try | ||
{ | ||
//// Throughout we expect that the sourcelink file for a lib is alongside | ||
//// the lib with the extension sourcelink.json instead of lib. | ||
|
||
// For import libraries we always have the full path to the lib. This shouldn't be needed since | ||
// the path will be common to the dll/exe project. We have this in case there are out of tree | ||
// references to library projects. | ||
foreach (var importLib in ImportLibraries) | ||
{ | ||
string sourceLinkName = Path.ChangeExtension(importLib, "sourcelink.json"); | ||
if (File.Exists(sourceLinkName)) | ||
{ | ||
if (BuildEngine != null) | ||
{ | ||
Log.LogMessage("Found additional sourcelink file '{0}'", sourceLinkName); | ||
} | ||
|
||
allSourceLinkFiles.Add(sourceLinkName); | ||
} | ||
} | ||
|
||
// Try and find sourcelink files for each lib | ||
foreach (var dependency in AdditionalDependencies) | ||
{ | ||
string sourceLinkName = Path.ChangeExtension(dependency, "sourcelink.json"); | ||
if (Path.IsPathRooted(dependency)) | ||
{ | ||
// If the lib path is rooted just look for the sourcelink file with the appropriate extension | ||
// on that path. | ||
if (File.Exists(sourceLinkName)) | ||
{ | ||
if (BuildEngine != null) | ||
{ | ||
Log.LogMessage("Found additional sourcelink file '{0}'", sourceLinkName); | ||
} | ||
|
||
allSourceLinkFiles.Add(sourceLinkName); | ||
} | ||
} | ||
else | ||
{ | ||
// Not-rooted, perform link like scanning of the lib directories to find the full lib path | ||
// and then look for the sourcelink file alongside the lib with the appropriate extension. | ||
foreach (var libDir in AdditionalLibraryDirectories) | ||
{ | ||
string potentialPath = Path.Combine(libDir, sourceLinkName); | ||
if (File.Exists(potentialPath)) | ||
{ | ||
if (BuildEngine != null) | ||
{ | ||
Log.LogMessage("Found additional sourcelink file '{0}'", potentialPath); | ||
} | ||
|
||
allSourceLinkFiles.Add(potentialPath); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
AllSourceLinkFiles = allSourceLinkFiles.ToArray(); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
AllSourceLinkFiles = new string[] { }; | ||
if (BuildEngine != null) | ||
{ | ||
Log.LogError("Failed to find sourcelink files for libs with dll/exe sourcelink file - {0}", ex.Message); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The values need to be escaped before they can be used as command line arguments. Perhaps, for simplicity of msbuild, FindAdditionalSourceLinkFiles could instead produce the command line string instead of individual items?