I have cloned the llvm-project repository.
Then I have generated a Visual Studio solution for the project using
cmake -G "Visual Studio 16" -DLLVM_ENABLE_PROJECTS="clang;lld" -DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS=ON -DLLVM_TARGETS_TO_BUILD=X86 -DCLANG_BUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=install ...\llvm-project\llvm
The important part is the DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS
, as suggested here.
The solution and all the projects are generated. Then I build Clang itself and an example clang plugin that is already provided in the llvm-project source. The PrintFunctionNames seem to be the hello world of Clang plugins, so I have built that, according to the llvm documentation.
The build successfully ran, and now I have llvm/clang in the install dir, with the PrintFunctionNames plugin.
There are two ways to tell clang to use a plugin:
clang -cc1 -load PrintFunctionNames.dll -plugin print-fns test.cpp
clang++ -c -Xclang -load -Xclang PrintFunctionNames.dll -Xclang -plugin -Xclang print-fns test.cpp
The first one works, however, the second one doesn't. Moreover, using clang++ with the first command line arguments won't work either. Both commands work with clang but neither with clang++, so it would seem that there's a problem with clang++. Also, omitting the -c command and actually building an executable with a plugin active yields a link error (1137).
It seems like clang++ doesn't work with dll plugins, which is quite weird as clang++ seems to be the same clang driver, except with different presets for linking with c++ libraries.
Another problem I have encountered was that when building out-of-tree, the cmake install command fails to copy over a library file, clang.lib to the install dir, without which out-of-tree plugins can't be built. Manually setting the path for this library (that resides in the original output directory, not where cmake install would move the other build outputs) seems to allow out-of-tree plugins to be correctly built.
But the problem persists: you can not use Xclang plugin loader or the clang++ driver with plugins on windows. Not with the provided example plugin, not with a test plugin a made out-of-tree.
The question is: am I building clang or the plugins wrong somehow? If so, how come -cc1 -load
can use the plugins with clang.exe? How does one build the provided example plugin and have it work with the clang++ driver? I have looked at this GitHub repo which implements a basic out-of-tree plugin, but it doesn't provide any information on building on windows.