Chibi C Compiler (chibicc) on Alpine Linux
Rui Ueyama is the developer of the mold
linker, but also the creator of the
chibicc C compiler project. There are lots
and lots of "toy" C compilers on github these days, and hacking on them is a
great way to get some insight into (simple) compiler design. The chibicc
compiler ("chibi" means "small" in Japanese) is nice because it's small but the
code is readable, but it's also fairly complete and you can build real projects
with it.
At the time I'm writing this, chibicc
doesn't support Alpine Linux out of the
box. I like Alpine because it's compact and easy to use in Docker, so I wanted
to see if I could get chibi running.
It took relatively small changes to get it to build.
- Add
crtbegin.o
path for Alpine inmain.c
.
static char *find_gcc_libpath(void)
// add this v ----------------------------
"/usr/lib/gcc/x86_64-alpine-linux-musl/*/crtbegin.o" // For Alpine
- Add the Alpine library path to
find_libpath
inmain.c
:
static char *find_libpath(void)
// add this v ----------------------------
if (file_exists("/usr/lib/crti.o"))
return "/usr/lib";
- Added
va_list
typedef to "stdarg.h". Apparentlymusl
doesn't ship a builtin version ofva_list
, and chibi would fall back to using gcc's builtin except that we're not using gcc, so we need to tell it to use the one in the "stdarg.h" header.
#ifndef va_list
// TODO: this is necessary on Alpine linux; there must be a better condition
// to check than if we have va_list defined at this point.
typedef va_list __builtin_va_list;
#endif
- Add "stdarg.h" to your build command when running tests. You'll need to
provide this include for your program for
va_args
.
-include include/stdarg.h
You can see the diff. I also added Dockerfile and VSCode devcontainer support.
With those changes, everything appears to be working that I've tested so far. A couple of these changes might need some tweaking to make sure that they don't affect other platforms, and perhaps I will do some general refactoring to clean up some of those platform-specific paths.