ces

Generating static binutils binaries (various archs) using zig + musl

In the romforth project I've created a "gradually implementable Forth" where you can follow a step wise approach to port Forth to any architecture of your choice.

I've been using binutils for generating the assembler/disassembler/objdump binaries for the architectures for which I've already worked on a port.

Recently while moving distros I needed to rebuild the binaries and decided that rather than having to rebuild it any time I need to move, I'd rather build a static binary once and for all which can be run anywhere (x86).

Using the superpowers afforded by Zig + Musl, it is trivially easy to do this.

For example to build binutils for the SPARC, here are the steps required: (I'm using zig version 0.11.0)

target=sparc-unknown-linux-gnu
BINUTILS= # directory where you have saved the code for binutils
cd $BINUTILS
mkdir -p ../build/$target && cd ../build/$target
CC="zig cc -target x86_64-linux-musl" ~-/configure \
        --target=$target --prefix $PWD
# install the required binutils prerequisite dependencies such as
# bison flex gcc ...
make
make install

Note: I used bash to run the above commands, YMMV

The binaries will be installed in the build directory. To build a similar set of binaries for SPARC64, you only need to just change the target to sparc64-unknown-linux-gnu and rebuild as above. Running --help lists the remaining architectures supported by binutils.

Note: This is just a "note to self" since I had to do some research to get it working and I'm hoping that having it on my own blog makes it trivial to find