Skip to content

Commit

Permalink
Exported all additional symbols on macOS.
Browse files Browse the repository at this point in the history
Many functions in libc++ are marked with _LIBCPP_INLINE_VISIBILITY. This means they are only exported when actually used. This is why exporting just the templates themselves failed to export their functions but listing the functions themselves worked. We need to define _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS to have the functions always exported.

We use _LIBCPP_VERSION to detect if we use libc++ i.e. Clang's standard C++ library.

Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Jan 4, 2019
1 parent e5aaa2e commit 91e219c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include <ciso646>

#ifdef _LIBCPP_VERSION
#define _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
#endif

#include <string>


template __attribute__((visibility("default"))) std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string();
template __attribute__((visibility("default"))) std::basic_string<char, std::char_traits<char>, std::allocator<char>>::~basic_string() noexcept;
template __attribute__((visibility("default"))) std::basic_string<char, std::char_traits<char>, std::allocator<char>>& std::basic_string<char, std::char_traits<char>, std::allocator<char>>::assign(const std::basic_string<char, std::char_traits<char>, std::allocator<char>>::value_type*);
template __attribute__((visibility("default"))) const std::basic_string<char, std::char_traits<char>, std::allocator<char>>::value_type* std::basic_string<char, std::char_traits<char>, std::allocator<char>>::c_str() const noexcept;
template class std::char_traits<char>;
template class std::allocator<char>;
template class std::basic_string<char, std::char_traits<char>, std::allocator<char>>;
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#include <ciso646>

#ifdef _LIBCPP_VERSION
#define _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
#endif

#include <string>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include <ciso646>

#ifdef _LIBCPP_VERSION
#define _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
#endif

#include <string>


template __attribute__((visibility("default"))) std::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string();
template __attribute__((visibility("default"))) std::basic_string<char, std::char_traits<char>, std::allocator<char>>::~basic_string() noexcept;
template __attribute__((visibility("default"))) std::basic_string<char, std::char_traits<char>, std::allocator<char>>& std::basic_string<char, std::char_traits<char>, std::allocator<char>>::assign(const std::basic_string<char, std::char_traits<char>, std::allocator<char>>::value_type*);
template __attribute__((visibility("default"))) const std::basic_string<char, std::char_traits<char>, std::allocator<char>>::value_type* std::basic_string<char, std::char_traits<char>, std::allocator<char>>::c_str() const noexcept;
template class std::char_traits<char>;
template class std::allocator<char>;
template class std::basic_string<char, std::char_traits<char>, std::allocator<char>>;
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#include <ciso646>

#ifdef _LIBCPP_VERSION
#define _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
#endif

#include <string>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#include <ciso646>

#ifdef _LIBCPP_VERSION
#define _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
#endif

#include <string>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#include <ciso646>

#ifdef _LIBCPP_VERSION
#define _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
#endif

#include <string>


Expand Down
19 changes: 12 additions & 7 deletions src/Generator/Passes/SymbolsCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators;
using CppSharp.Parser;

namespace CppSharp.Passes
{
Expand All @@ -20,6 +19,16 @@ public SymbolsCodeGenerator(BindingContext context, IEnumerable<TranslationUnit>

public override void Process()
{
WriteLine("#include <ciso646>");
NewLine();

// check if we use the Clang standard C++ lib and if so,
// make sure we export all symbols marked with _LIBCPP_INLINE_VISIBILITY
WriteLine("#ifdef _LIBCPP_VERSION");
WriteLine(" #define _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS");
WriteLine("#endif");
NewLine();

if (TranslationUnit.Module == Options.SystemModule)
WriteLine("#include <string>");
else
Expand Down Expand Up @@ -62,12 +71,8 @@ public override bool VisitFunctionDecl(Function function)

private string GetExporting()
{
var exporting = string.Empty;
if (Context.ParserOptions.IsMicrosoftAbi)
exporting = "__declspec(dllexport) ";
else if (TargetTriple.IsMacOS(Context.ParserOptions.TargetTriple))
exporting = "__attribute__((visibility(\"default\"))) ";
return exporting;
return Context.ParserOptions.IsMicrosoftAbi ?
"__declspec(dllexport) " : string.Empty;
}

private string GetWrapper(Module module)
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Types/Std/Stdlib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
ctx.Before.WriteLine($@"{qualifiedBasicString}Extensions.{
assign.Name}({varBasicString}, {ctx.Parameter.Name});");
ctx.Return.Write($"{varBasicString}.{Helpers.InstanceIdentifier}");
if (!type.IsAddress())
if (!type.IsPointer())
ctx.Cleanup.WriteLine($"{varBasicString}.Dispose(false);");
}
}
Expand Down

0 comments on commit 91e219c

Please sign in to comment.