Skip to content

Instantly share code, notes, and snippets.

@kastiglione
Last active February 4, 2020 11:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kastiglione/1b81f2547deaa0bb2620ad2ff605e7c0 to your computer and use it in GitHub Desktop.
Save kastiglione/1b81f2547deaa0bb2620ad2ff605e7c0 to your computer and use it in GitHub Desktop.
Using Swift and LLVM headers in SwiftPM

Recently, we wanted call swift-demangle from code, but calling out to the process frequently was a bit too slow, and meant parsing the output of -tree-only, since we wanted to access module and class names, for example.

Fortunately there's libswiftDemangle.dylib for this, but it's a C++ API. Compiling against it requires #includeing headers from both Swift and LLVM.

Using CMake, both Swift and LLVM can easily be added as dependencies, but we build this project with SwiftPM. To build with SwiftPM, we imported the necessary headers. Roughly, here are the steps to determine which specific Swift and LLVM headers were required, so that they could be imported to the repo.

  1. Created a small CMake project with Swift and LLVM as dependencies
  2. Ran ninja -v to see the clang commands
  3. Found the command that compiles CSwiftDemangle.cpp
  4. Manually ran the clang compile with -H flag added, to show all #included headers
  5. Grepped this output for the paths matching my Swift checkout

The matching paths from step 5 were the files I imported into our repo.

@shepting
Copy link

Wow, this is awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment