Skip to content

Commit 09fa2f0

Browse files
fsfodAaronBallman
andauthored
[Clang] Add explicit visibility symbol macros (#108276)
This is part of the effort to support for enabling plugins on windows by adding better support for building llvm and clang as a DLL. These macros are similar to the ones i added in #96630, but are for clang. Added explicit symbol visibility macros definitions that are defined in a new header and will be used to for shared library builds of clang to export symbols. Updated clang cmake to define a macro to enable the symbol visibility macros and explicitly disable them for clang tools that always use static linking. --------- Co-authored-by: Aaron Ballman <[email protected]>
1 parent eb83e4a commit 09fa2f0

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

clang/cmake/modules/AddClang.cmake

+11
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ macro(add_clang_library name)
108108
endif()
109109
llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
110110

111+
if(MSVC AND NOT CLANG_LINK_CLANG_DYLIB)
112+
# Make sure all consumers also turn off visibility macros so there not trying to dllimport symbols.
113+
target_compile_definitions(${name} PUBLIC CLANG_BUILD_STATIC)
114+
if(TARGET "obj.${name}")
115+
target_compile_definitions("obj.${name}" PUBLIC CLANG_BUILD_STATIC)
116+
endif()
117+
elseif(NOT ARG_SHARED AND NOT ARG_STATIC)
118+
# Clang component libraries linked in to clang-cpp are declared without SHARED or STATIC
119+
target_compile_definitions("obj.${name}" PUBLIC CLANG_EXPORTS)
120+
endif()
121+
111122
set(libs ${name})
112123
if(ARG_SHARED AND ARG_STATIC)
113124
list(APPEND libs ${name}_static)
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 having hidden 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+
// Some libraries like those for tablegen are linked in to tools that used
35+
// in the build so can't depend on the llvm shared library. If export macros
36+
// were left enabled when building these we would get duplicate or
37+
// missing symbol linker errors on windows.
38+
#if defined(CLANG_BUILD_STATIC)
39+
#define CLANG_ABI
40+
#define CLANG_TEMPLATE_ABI
41+
#define CLANG_EXPORT_TEMPLATE
42+
#elif defined(_WIN32) && !defined(__MINGW32__)
43+
#if defined(CLANG_EXPORTS)
44+
#define CLANG_ABI __declspec(dllexport)
45+
#define CLANG_TEMPLATE_ABI
46+
#define CLANG_EXPORT_TEMPLATE __declspec(dllexport)
47+
#else
48+
#define CLANG_ABI __declspec(dllimport)
49+
#define CLANG_TEMPLATE_ABI __declspec(dllimport)
50+
#define CLANG_EXPORT_TEMPLATE
51+
#endif
52+
#elif defined(__ELF__) || defined(__MINGW32__) || defined(_AIX)
53+
#define CLANG_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
54+
#define CLANG_TEMPLATE_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
55+
#define CLANG_EXPORT_TEMPLATE
56+
#elif defined(__MACH__) || defined(__WASM__)
57+
#define CLANG_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
58+
#define CLANG_TEMPLATE_ABI
59+
#define CLANG_EXPORT_TEMPLATE
60+
#endif
61+
#endif
62+
63+
#endif

clang/tools/libclang/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ if(ENABLE_SHARED)
167167
PROPERTIES
168168
VERSION ${LIBCLANG_LIBRARY_VERSION}
169169
DEFINE_SYMBOL _CINDEX_LIB_)
170+
# Avoid declaring clang c++ symbols that are statically linked into libclang as dllimport'ed.
171+
# If llvm/libclang-cpp dll is also being built for windows clang c++ symbols will still be
172+
# implicitly be exported from libclang.
173+
target_compile_definitions(libclang PRIVATE CLANG_BUILD_STATIC)
170174
elseif(APPLE)
171175
set(LIBCLANG_LINK_FLAGS " -Wl,-compatibility_version -Wl,1")
172176
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)