]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] clang-plugin: fix build with modern LLVM/Clang
authorVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 5 Feb 2026 13:07:03 +0000 (13:07 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 5 Feb 2026 13:07:03 +0000 (13:07 +0000)
clang-plugin/CMakeLists.txt
src/CMakeLists.txt

index 0d61c79d8e3b013741c378be87ccbacd122a2bbe..8502268a5740bdb9aaf59a488e7b61efeded4c12 100644 (file)
@@ -14,29 +14,47 @@ IF (ENABLE_CLANG_PLUGIN MATCHES "ON")
 
     find_package(Clang REQUIRED)
 
-    set(clang_libs clang)
-
     message(STATUS "Found LibClang in: ${CLANG_INSTALL_PREFIX}")
     include_directories(${CLANG_INCLUDE_DIRS})
 
     SET(CLANGPLUGINSRC plugin.cc printf_check.cc)
 
     ADD_LIBRARY(rspamd-clang SHARED ${CLANGPLUGINSRC})
+
+    # Clang plugins must not be built with sanitizers - they conflict when loaded
     IF (SANITIZE)
-        set (CMAKE_C_FLAGS "")
-        set (CMAKE_CXX_FLAGS "")
-        set (CMAKE_EXE_LINKER_FLAGS "")
+        # Remove sanitizer flags from compile options
+        set_target_properties(rspamd-clang PROPERTIES
+            COMPILE_OPTIONS ""
+            LINK_OPTIONS ""
+        )
+        target_compile_options(rspamd-clang PRIVATE
+            -fno-sanitize=all
+            -g -O0
+        )
+        target_link_options(rspamd-clang PRIVATE
+            -fno-sanitize=all
+        )
     ENDIF()
 
-
+    # Link against LLVM shared library
     find_library(found_LLVM LLVM HINTS ${LLVM_LIBRARY_DIRS})
     if(found_LLVM)
-        target_link_libraries(rspamd-clang PRIVATE ${LLVM})
+        target_link_libraries(rspamd-clang PRIVATE ${found_LLVM})
     else()
-        # XXX not tested yet
         llvm_map_components_to_libnames(llvm_libs support core)
         target_link_libraries(rspamd-clang PRIVATE ${llvm_libs})
     endif()
 
-    target_link_libraries(rspamd-clang PRIVATE ${clang_libs})
+    # Link against clang-cpp shared library (contains all clang components)
+    find_library(found_clang_cpp clang-cpp HINTS ${LLVM_LIBRARY_DIRS})
+    if(found_clang_cpp)
+        target_link_libraries(rspamd-clang PRIVATE ${found_clang_cpp})
+    else()
+        # Fallback to libclang
+        find_library(found_clang clang HINTS ${LLVM_LIBRARY_DIRS})
+        if(found_clang)
+            target_link_libraries(rspamd-clang PRIVATE ${found_clang})
+        endif()
+    endif()
 ENDIF()
index 6cc49e4e408b2b49f5d67cac6bfe60a26025563f..8e2777526219275e5b4b7f51746b405d67142f2a 100644 (file)
@@ -57,21 +57,24 @@ endfunction()
 
 # Configure Clang Plugin if enabled
 if (ENABLE_CLANG_PLUGIN)
-    set(CLANG_PLUGIN_FLAGS "-Xclang -load -Xclang ${CMAKE_CURRENT_BINARY_DIR}/../clang-plugin/librspamd-clang${CMAKE_SHARED_LIBRARY_SUFFIX} -Xclang -add-plugin -Xclang rspamd-ast")
-
-    # Apply to both C and C++ compiler flags
-    add_compile_options(${CLANG_PLUGIN_FLAGS})
+    # Apply clang plugin flags - use SHELL: to prevent CMake from deduplicating -Xclang
+    add_compile_options(
+        "SHELL:-Xclang -load"
+        "SHELL:-Xclang ${CMAKE_CURRENT_BINARY_DIR}/../clang-plugin/librspamd-clang${CMAKE_SHARED_LIBRARY_SUFFIX}"
+        "SHELL:-Xclang -add-plugin"
+        "SHELL:-Xclang rspamd-ast"
+    )
 
     # Add any extra clang plugins
     if (CLANG_EXTRA_PLUGINS_LIBS)
         foreach (lib ${CLANG_EXTRA_PLUGINS_LIBS})
-            add_compile_options("-Xclang" "-load" "-Xclang" "${lib}")
+            add_compile_options("SHELL:-Xclang -load" "SHELL:-Xclang ${lib}")
         endforeach ()
     endif ()
 
     if (CLANG_EXTRA_PLUGINS)
         foreach (plug ${CLANG_EXTRA_PLUGINS})
-            add_compile_options("-Xclang" "-add-plugin" "-Xclang" "${plug}")
+            add_compile_options("SHELL:-Xclang -add-plugin" "SHELL:-Xclang ${plug}")
         endforeach ()
     endif ()
 endif ()