Skip to content

Commit b987a75

Browse files
fsfodvgvassilev
authored andcommitted
[Clang] Add explicit visibility symbol macros and update CMake to control them
We need a different set of visibility macros to LLVM's since on windows you need a different attribute to import and export a symbol unlike other platforms and many translation units will be importing LLVM symbols and export some of Clangs. Updated Clang CMake to define a macro to enable the symbol visibility macros.
1 parent 09ba83b commit b987a75

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

clang/cmake/modules/AddClang.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ macro(add_clang_library name)
108108
endif()
109109
llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
110110

111+
if(NOT ARG_SHARED AND NOT ARG_STATIC)
112+
target_compile_definitions("obj.${name}" PRIVATE CLANG_EXPORTS)
113+
endif()
114+
111115
set(libs ${name})
112116
if(ARG_SHARED AND ARG_STATIC)
113117
list(APPEND libs ${name}_static)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===-- clang/Support/Compiler.h - Compiler abstraction support -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines explicit visibility macros used to export symbols from
10+
// clang-cpp
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef CLANG_SUPPORT_COMPILER_H
15+
#define CLANG_SUPPORT_COMPILER_H
16+
17+
#include "llvm/Support/Compiler.h"
18+
19+
/// CLANG_ABI is the main export/visibility macro to mark something as
20+
/// explicitly exported when clang is built as a shared library with everything
21+
/// else that is unannotated will have internal visibility.
22+
///
23+
/// CLANG_EXPORT_TEMPLATE is used on explicit template instantiations in source
24+
/// files that were declared extern in a header. This macro is only set as a
25+
/// compiler export attribute on windows, on other platforms it does nothing.
26+
///
27+
/// CLANG_TEMPLATE_ABI is for annotating extern template declarations in headers
28+
/// for both functions and classes. On windows its turned in to dllimport for
29+
/// library consumers, for other platforms its a default visibility attribute.
30+
#ifndef CLANG_ABI_GENERATING_ANNOTATIONS
31+
// Marker to add to classes or functions in public headers that should not have
32+
// export macros added to them by the clang tool
33+
#define CLANG_ABI_NOT_EXPORTED
34+
#if defined(LLVM_BUILD_LLVM_DYLIB) || defined(LLVM_BUILD_SHARED_LIBS)
35+
// Some libraries like those for tablegen are linked in to tools that used
36+
// in the build so can't depend on the llvm shared library. If export macros
37+
// were left enabled when building these we would get duplicate or
38+
// missing symbol linker errors on windows.
39+
#if defined(CLANG_BUILD_STATIC)
40+
#define CLANG_ABI
41+
#define CLANG_TEMPLATE_ABI
42+
#define CLANG_EXPORT_TEMPLATE
43+
#elif defined(_WIN32) && !defined(__MINGW32__)
44+
#if defined(CLANG_EXPORTS)
45+
#define CLANG_ABI __declspec(dllexport)
46+
#define CLANG_TEMPLATE_ABI
47+
#define CLANG_EXPORT_TEMPLATE __declspec(dllexport)
48+
#else
49+
#define CLANG_ABI __declspec(dllimport)
50+
#define CLANG_TEMPLATE_ABI __declspec(dllimport)
51+
#define CLANG_EXPORT_TEMPLATE
52+
#endif
53+
#elif defined(__ELF__) || defined(__MINGW32__) || defined(_AIX)
54+
#define CLANG_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
55+
#define CLANG_TEMPLATE_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
56+
#define CLANG_EXPORT_TEMPLATE
57+
#elif defined(__MACH__) || defined(__WASM__)
58+
#define CLANG_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
59+
#define CLANG_TEMPLATE_ABI
60+
#define CLANG_EXPORT_TEMPLATE
61+
#endif
62+
#else
63+
#define CLANG_ABI
64+
#define CLANG_TEMPLATE_ABI
65+
#define CLANG_EXPORT_TEMPLATE
66+
#endif
67+
#endif
68+
69+
#endif

clang/tools/libclang/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ if(ENABLE_SHARED)
166166
set_target_properties(libclang
167167
PROPERTIES
168168
VERSION ${LIBCLANG_LIBRARY_VERSION}
169-
DEFINE_SYMBOL _CINDEX_LIB_)
169+
DEFINE_SYMBOL _CINDEX_LIB_ DEFINE_SYMBOL CLANG_EXPORTS)
170170
elseif(APPLE)
171171
set(LIBCLANG_LINK_FLAGS " -Wl,-compatibility_version -Wl,1")
172172
set(LIBCLANG_LINK_FLAGS "${LIBCLANG_LINK_FLAGS} -Wl,-current_version -Wl,${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")

0 commit comments

Comments
 (0)