Clang Plugin for Msys2's mingw x86_64-w64-windows

Hello there,

I am trying to create a simple Clang plugin and test it on my Windows host with Msys2’s mingw x86_64-w64-windows toolchain. The issue is that clang is failing to “find” the plugin after loading windows plugin dll. Can you please help me to figure out what is wrong here? I am very new to clang and the plugin framework. Thank you for any suggestion, Mark

  1. creating plugin dll:

$ make -f makefile.mk
clang++ -c -std=c++14 -fno-exceptions -fno-rtti -D_FILE_OFFSET_BITS=64 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I. -Ic:/msys64/mingw64/include example_plugin_clang.cpp -o example_plugin_clang.o
clang++ -shared example_plugin_clang.o -static -o example-plugin-clang.dll -Lc:/msys64/mingw64/lib c:/msys64/mingw64/lib/libclang-cpp.dll.a c:/msys64/mingw64/lib/libLLVM.dll.a

  1. test run:

$ make -f makefile.mk test

clang++ -Xclang -load -Xclang /example-plugin-clang.dll -Xclang -add-plugin -Xclang example-plugin-clang -c -std=c++11 example_cpp_class.cc -o example_cpp_class.o
error: unable to find plugin ‘example-plugin-clang’
1 error generated.
mingw32-make: *** [makefile.mk:86: example_cpp_class.o] Error 1


$ nm example-plugin-clang.dll | grep example
00000000701c1cb0 t _GLOBAL__sub_I_example_plugin_clang.cpp
00000000701e9020 b _ZL26_example_plugin_clang_node

per example_plugin_clang.cpp:

static FrontendPluginRegistry::Add
_example_plugin_clang_node(/plugin Name=/ “example-plugin-clang”,
/plugin Description=/ “Print out header dependencies”);

$ ntldd example-plugin-clang.dll
libclang-cpp.dll => C:\msys64\mingw64\bin\libclang-cpp.dll (0x0000000000e10000)
KERNEL32.dll => C:\WINDOWS\SYSTEM32\KERNEL32.dll (0x0000000005690000)
msvcrt.dll => C:\WINDOWS\SYSTEM32\msvcrt.dll (0x00000000006d0000)

clang/LLVM does not support dynamically loaded plugins on Windows. The
reasons are that
1. importing symbols require specifying which binary they are loaded
from. That is, clang.exe, clang++.exe, libLLVM.dll or clang-cpp.dll
2. There is a limit of ~2^16 exported symbols per dll. LLVM alone
already has more symbols.

Also see the thread
http://lists.llvm.org/pipermail/cfe-dev/2020-March/064919.html

Michael