Linking Clang driver as a library?

I have a compiler (TinyGo) where I’d like to link to the Clang driver itself, so that we can ship a single binary that contains everything. Basically, this compiler will invoke itself again with the clang subcommand and then call into the Clang driver.

So far I’ve solved this by writing a small driver-like function and have copied ExecuteAssembler and modified it to be non-static. This works but is a bit of a hack and needs to be updated on every LLVM release because ExecuteAssembler tends to change in various small ways.

I would like to link to the Clang driver directly, but I’m not sure how. Possible ways this could work:

  • Let CMake build an actual .a archive for the Clang driver. Right now it will just link Clang with a bunch of .o files.
  • Build a library outside of the LLVM build system that contains tools/clang/tools/driver/CMakeFiles/clang.dir/*.cpp.o. This works, but doesn’t seem very clean to me.
  • Move ExecuteAssembler into a separate .a library so that it can be linked to from external code. Basically what I do now but upstream.

Basically, it would be perfect if it were possible to call clang_main but I don’t know the proper way to do that. Any suggestions?