From 69e153c7e2829941fb81157df5d09e7abd99044a Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Mon, 23 Mar 2026 10:19:28 -0700 Subject: [PATCH] Fix build system: MSVC static, UserInterface, clangInterpreter linking Three build fixes for embedding cling as a static library: 1. Suppress dllexport on MSVC static builds: AddClang.cmake sets CLANG_BUILD_STATIC for clang targets but not cling targets, causing __declspec(dllexport) symbols to leak into consumer DLLs. Add the same definition for cling targets when building static on MSVC. 2. Build UserInterface unconditionally: clingUserInterface is a runtime dependency of cling.exe, libcling, and embedders, but was gated behind CLING_INCLUDE_TESTS. Building with -DLLVM_INCLUDE_TESTS=OFF silently skipped it, causing link failures. 3. Link clangInterpreter into libcling.so: needed for ReplCodeCompleter autocomplete support in shared library builds. --- CMakeLists.txt | 11 +++++++++++ lib/CMakeLists.txt | 4 +--- tools/libcling/CMakeLists.txt | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a09c3bf495..795070fa63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -345,6 +345,17 @@ macro(add_cling_library name) # Set DISABLE_LLVM_LINK_LLVM_DYLIB to disable linking against shared LLVM llvm_add_library(${name} ${ARG_ENABLE_SHARED} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARG_UNPARSED_ARGUMENTS} ${srcs}) + # Suppress dllexport from Clang headers when building cling as static libs on MSVC. + # AddClang.cmake sets CLANG_BUILD_STATIC for clang targets, but cling targets + # (which include clang headers like Attrs.inc) miss it, causing thousands of + # __declspec(dllexport) symbols to leak into any DLL that links cling libs. + if(MSVC AND NOT CLANG_LINK_CLANG_DYLIB) + target_compile_definitions(${name} PUBLIC CLANG_BUILD_STATIC) + if(TARGET "obj.${name}") + target_compile_definitions("obj.${name}" PUBLIC CLANG_BUILD_STATIC) + endif() + endif() + if (MSVC AND cling_ex_file_match) # /EHs because cling_runtime_internal_throwIfInvalidPointer is extern ā€œCā€ if (cling_ex_file_match) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index eb29252742..63d4f1e52b 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -8,7 +8,5 @@ add_subdirectory(Interpreter) add_subdirectory(MetaProcessor) -if(CLING_INCLUDE_TESTS) - add_subdirectory(UserInterface) -endif() +add_subdirectory(UserInterface) add_subdirectory(Utils) diff --git a/tools/libcling/CMakeLists.txt b/tools/libcling/CMakeLists.txt index 3d119b9061..7d3b9d904f 100644 --- a/tools/libcling/CMakeLists.txt +++ b/tools/libcling/CMakeLists.txt @@ -22,6 +22,7 @@ set(LIBS clangCodeGen clangBasic clangEdit + clangInterpreter clingUtils )