Cross Compiling Rust Code from macOS to Linux
I wanted to deploy some LLM poisoning code on my server, with the aim of serving up plausible - but nonsensical posts to the various AI scrapers that come by and suck down the entire content of my websites from time to time.
Tim McCormack over at brainonfire had some rust code called marco written that takes example text and produces a stream of junk from it, just what I needed. The code builds fine on macOS (my desktop OS of choice) but I couldn’t build it on my Linux based server becuase the version of rust installed was too old.
Luckily it’s possible to cross compile from macOS to Linux. This is how I did it. You’ll need homebrew installed
rustup target add x86_64-unknown-linux-gnu
brew tap SergioBenitez/osxct
brew install x86_64-unknown-linux-gnu
I added a new target to the Cargo.toml file
[target.x86_64-unknown-linux-gnu]
linker = "x86_64-unknown-linux-gnu-gcc"
Because of some path error I couldn’t debug in the 30 seconds I bothered trying, I just specified the toolchain, etc on the build line in the marko directory.
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc cargo build --release --bin=marko --features=bin --target=x86_64-unknown-linux-gnu
This produces a binary file that runs perfectly happily on my Linux server.