Hello everyone!
Over the past couple weeks, I’ve been working on porting some useful parts of LLVM to the Zig build system for my personal compiler project, Conch, which is written in C++. About a week ago I posted about my progress towards this goal, and I’m now happy to announce that, as of today, LLVM, Clang, and LLD are being built from source purely through Zig’s build system! About 10k lines of build rules and source file lists later, I’m finally in somewhat of a good spot here! With this being achieved, every major component in Conch’s development environment can be (and is!) built completely locally:
- LLVM 21.1.8, which requires libxml2, zstd, and zlib. This provides:
- LLVM for Conch’s upcoming backend
- Clang, including clang-format for code formatting
- LLD for linking
- libarchive for in-house release packaging, producing zip and zst archives using zlib and zstd
- cppcheck for static analysis
- Other miscellaneous C++ libraries like catch2, fmt and magic enum
While the core libraries are indeed building correctly (I’ve tested the LLVM libraries against the the provided kaleidoscope examples), I have not yet ported over the respective library’s tests.This isn’t a big issue now since I am not yet at a stage where I can start actually using LLVM in Conch’s compiler pipeline, but it is definitely something that’d be nice to have, especially if the build system is hoisted out into its own repo in the future. If anyone is interested in helping out with porting the tests over to the existing build system, that’s be a great help, but again, this is far from pressing. I’ve got an issue open for this on the repo if you’d like to take a look.
There’s also a few libraries here and there that are technically optional (they don’t link against anything by default), so I skipped them for my own sake. I’ve categorized these (mentally) as ‘as needed’, since everything technically compiles now without them. If you catch something that looks completely wrong because a core component isn’t being built, please don’t hesitate to open an issue!
Thanks for all the support everyone! It’s always great interacting with the Zig community!
Cheers!
19 Likes
This looks pretty nice. I’ll give it a try this weekend.
I know this isn’t important for your use cases, I understand if its not up there on the priority list.
Does this build the runtime? Like compiler-rt and libstdc++, etc? If so in what order? When building it for LFS I don’t have the c++ standard library so I need to build them first against my sysroot and then I could use that to build llvm/clang.
Otherwise this is pretty impressive I wonder if the zig team themselves will adopt it.
1 Like
Thanks! Just a side note: Since I don’t actively need LLVM, Clang, and LLD to be built alongside Conch at the moment, the libraries are opt-in. On the main branch, the only way to build related LLVM artifacts is by building a kaleidoscope example by running zig build kaleidoscope -DChapter=ChapterX where X is the chapter of interest. If you’d like to just flat our install certain artifacts, I made a branch called build that has steps for building and installing LLVM, LLD, and Clang. This is likely to never be merged into main since it was made purely as a tool for people interested in the resulting artifacts.
To answer your question about the runtime, it is not being built at the moment. Since Zig ships with its own C++ stdlib and lets you bundle compiler_rt through its shipped components, I did not have the need to build these libraries for my current use case. I’m fairly certain building these would be somewhat straightforward, but I just don’t have the bandwidth to do this myself. To determine the order of building libraries, I always scan through the CMake files in LLVM’s directories. The process is always identifying what includes need to be generated (config headers, tablegen, etc.), what needs to be built (source files), what needs to be ‘internally’ linked (from this subproject, i.e. lldCommon), and what needs to be ‘externally’ linked (from another subproject, i.e. LLVMSupport). If you’d like to contribute to Conch by adding this, feel free! Again, it’s very methodical, I just need a break from scanning so many CMake files lol.
Should be noted that it’s likely I end up needing (or just wanting) to build compiler_rt and libstd++ at some point, it just wasn’t a part of my short-term goals for this endeavor.
Thank you for your continued support!
1 Like