2024-02-02
Zig as a C/C++ compiler on the Steam Deck
I've been trying to get a development environment on my Steam Deck without entering Dev Mode and using Pacman or installing any flatpaks, and have been pretty impressed with Zig's drop-in compiler.
I only had to extract the zip file to somewhere in my home directory, add it to my $PATH, and create 2 executable shell scripts that call "zig cc" or "zig c++" and pass any arguments I want with "$@" - to keep typing short, I call the scripts "zcc" and "z++". Since I'm also working on projects that use CMake, I made another shell script called "zmake" that calls cmake with zcc and z++ as the main compilers.
Since the Deck's root filesystem is read-only, I keep all third party headers and libraries in $HOME/.local/include and $HOME/.local/lib/, respectively - next, I set $CPATH to the include directory and $LIBRARY_PATH to the lib directory. Finally, I just copied the "make" binary from another machine running Debian and put it in my directory added to $PATH. That's it! The Deck already comes with git and gdb, so I didn't need to get those from anywhere. One day I might try to get Autotools working.
Be warned that Zig's Clang backend will compile C/C++ code with UBSAN enabled by default, and will abort with an illegal instruction if it encounters UB - it will even #define _DEBUG unless you explicitly compile with release flags like -O2 or -O3. At least this got me to clean up Coal's codebase, which I haven't touched in a while. The big changes needed were padding to my arena allocator so that pointers begin at 8-byte address boundaries and casting voidto the last arguments of my glVertexAttribPointer and glDrawElements calls. I also had to update stb_sprintf and miniz to their latest versions - stb_vorbis is also getting an overflow crash, which I'm currently bypassing with a cast to long. If I ever release this damn game, you can be assured that Andrew Kelley approves of its code.