Browse Source

Find libraries even when given with no extensions

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1398/head
Dimitar Dobrev 5 years ago
parent
commit
17b8e9800f
  1. 33
      src/CppParser/Parser.cpp
  2. 13
      src/Generator.Tests/ReadNativeDependenciesTest.cs
  3. 13
      src/Generator.Tests/ReadNativeSymbolsTest.cs
  4. 0
      tests/Native/linux/libexpat.so.0
  5. 0
      tests/Native/macos/libexpat.dylib
  6. 0
      tests/Native/windows/libexpat.dll

33
src/CppParser/Parser.cpp

@ -4447,24 +4447,34 @@ ParserResult* Parser::ParseLibrary(const LinkerOptions* Opts) @@ -4447,24 +4447,34 @@ ParserResult* Parser::ParseLibrary(const LinkerOptions* Opts)
{
auto res = new ParserResult();
for (const auto& File : Opts->Libraries)
for (const auto& Library : Opts->Libraries)
{
if (File.empty())
if (Library.empty())
{
res->kind = ParserResultKind::FileNotFound;
return res;
}
llvm::StringRef FileEntry("");
std::string FileEntry;
for (unsigned I = 0, E = Opts->LibraryDirs.size(); I != E; ++I)
for (const auto& LibDir : Opts->LibraryDirs)
{
auto& LibDir = Opts->LibraryDirs[I];
llvm::SmallString<256> Path(LibDir);
llvm::sys::path::append(Path, File);
using namespace llvm::sys;
if (!(FileEntry = Path.str()).empty() && llvm::sys::fs::exists(FileEntry))
break;
std::error_code ErrorCode;
fs::directory_iterator Dir(LibDir, ErrorCode);
for (const auto& File = Dir;
Dir != fs::directory_iterator() && !ErrorCode;
Dir = Dir.increment(ErrorCode))
{
if (path::filename(File->path()) == Library ||
path::stem(File->path()) == Library ||
path::stem(path::stem(File->path())) == Library)
{
FileEntry = File->path();
goto found;
}
}
}
if (FileEntry.empty())
@ -4473,6 +4483,7 @@ ParserResult* Parser::ParseLibrary(const LinkerOptions* Opts) @@ -4473,6 +4483,7 @@ ParserResult* Parser::ParseLibrary(const LinkerOptions* Opts)
return res;
}
found:
auto BinaryOrErr = llvm::object::createBinary(FileEntry);
if (!BinaryOrErr)
{
@ -4484,14 +4495,14 @@ ParserResult* Parser::ParseLibrary(const LinkerOptions* Opts) @@ -4484,14 +4495,14 @@ ParserResult* Parser::ParseLibrary(const LinkerOptions* Opts)
auto OwningBinary = std::move(BinaryOrErr.get());
auto Bin = OwningBinary.getBinary();
if (auto Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
res->kind = ParseArchive(File, Archive, res->Libraries);
res->kind = ParseArchive(Library, Archive, res->Libraries);
if (res->kind == ParserResultKind::Error)
return res;
}
if (auto ObjectFile = llvm::dyn_cast<llvm::object::ObjectFile>(Bin))
{
res->kind = ParseSharedLib(File, ObjectFile, res->Libraries);
res->kind = ParseSharedLib(Library, ObjectFile, res->Libraries);
if (res->kind == ParserResultKind::Error)
return res;
}

13
src/Generator.Tests/ReadNativeDependenciesTest.cs

@ -2,6 +2,7 @@ using System.Collections.Generic; @@ -2,6 +2,7 @@ using System.Collections.Generic;
using CppSharp.Utils;
using NUnit.Framework;
using CppSharp.AST;
using System.IO;
namespace CppSharp.Generator.Tests
{
@ -11,7 +12,7 @@ namespace CppSharp.Generator.Tests @@ -11,7 +12,7 @@ namespace CppSharp.Generator.Tests
[Test]
public void TestReadDependenciesWindows()
{
IList<string> dependencies = GetDependencies("libexpat-windows");
IList<string> dependencies = GetDependencies("windows", "libexpat");
Assert.AreEqual("KERNEL32.dll", dependencies[0]);
Assert.AreEqual("msvcrt.dll", dependencies[1]);
Assert.AreEqual("USER32.dll", dependencies[2]);
@ -20,23 +21,23 @@ namespace CppSharp.Generator.Tests @@ -20,23 +21,23 @@ namespace CppSharp.Generator.Tests
[Test]
public void TestReadDependenciesLinux()
{
IList<string> dependencies = GetDependencies("libexpat-linux");
IList<string> dependencies = GetDependencies("linux", "libexpat");
Assert.AreEqual("libc.so.6", dependencies[0]);
}
[Test]
public void TestReadDependenciesOSX()
public void TestReadDependenciesMacOS()
{
IList<string> dependencies = GetDependencies("libexpat-osx");
IList<string> dependencies = GetDependencies("macos", "libexpat");
Assert.AreEqual("libexpat.1.dylib", dependencies[0]);
Assert.AreEqual("libSystem.B.dylib", dependencies[1]);
}
private static IList<string> GetDependencies(string library)
private static IList<string> GetDependencies(string dir, string library)
{
var driverOptions = new DriverOptions();
Module module = driverOptions.AddModule("Test");
module.LibraryDirs.Add(GeneratorTest.GetTestsDirectory("Native"));
module.LibraryDirs.Add(Path.Combine(GeneratorTest.GetTestsDirectory("Native"), dir));
module.Libraries.Add(library);
using (var driver = new Driver(driverOptions))
{

13
src/Generator.Tests/ReadNativeSymbolsTest.cs

@ -2,6 +2,7 @@ using System.Collections.Generic; @@ -2,6 +2,7 @@ using System.Collections.Generic;
using CppSharp.Utils;
using NUnit.Framework;
using CppSharp.AST;
using System.IO;
namespace CppSharp.Generator.Tests
{
@ -11,7 +12,7 @@ namespace CppSharp.Generator.Tests @@ -11,7 +12,7 @@ namespace CppSharp.Generator.Tests
[Test]
public void TestReadSymbolsWindows()
{
var symbols = GetSymbols("libexpat-windows");
var symbols = GetSymbols("windows", "libexpat");
Assert.That(symbols, Is.EquivalentTo(
new[]
{
@ -103,7 +104,7 @@ namespace CppSharp.Generator.Tests @@ -103,7 +104,7 @@ namespace CppSharp.Generator.Tests
[Test]
public void TestReadSymbolsLinux()
{
var symbols = GetSymbols("libexpat-linux");
var symbols = GetSymbols("linux", "libexpat");
Assert.That(symbols, Is.EquivalentTo(
new []
{
@ -214,9 +215,9 @@ namespace CppSharp.Generator.Tests @@ -214,9 +215,9 @@ namespace CppSharp.Generator.Tests
}
[Test]
public void TestReadSymbolsOSX()
public void TestReadSymbolsMacOS()
{
var symbols = GetSymbols("libexpat-osx");
var symbols = GetSymbols("macos", "libexpat");
Assert.That(symbols, Is.EquivalentTo(
new[]
{
@ -304,11 +305,11 @@ namespace CppSharp.Generator.Tests @@ -304,11 +305,11 @@ namespace CppSharp.Generator.Tests
}));
}
private static IList<string> GetSymbols(string library)
private static IList<string> GetSymbols(string dir, string library)
{
var driverOptions = new DriverOptions();
Module module = driverOptions.AddModule("Test");
module.LibraryDirs.Add(GeneratorTest.GetTestsDirectory("Native"));
module.LibraryDirs.Add(Path.Combine(GeneratorTest.GetTestsDirectory("Native"), dir));
module.Libraries.Add(library);
using (var driver = new Driver(driverOptions))
{

0
tests/Native/libexpat-linux → tests/Native/linux/libexpat.so.0

0
tests/Native/libexpat-osx → tests/Native/macos/libexpat.dylib

0
tests/Native/libexpat-windows → tests/Native/windows/libexpat.dll

Loading…
Cancel
Save