makerust

In 2023 I learned about (GNU) make's .ONESHELL. Put this in your Makefile:

SHELL = python3

.ONESHELL: date
date:
	from datetime import datetime
	print(datetime.now())
; make date
from datetime import datetime
print(datetime.now())
2026-04-23 12:32:27.193732

So for obvious reasons I put together an example so you can write Rust in your Makefile:

.SHELLFLAGS = -c 'echo $$1 > tmp.rs && rustc tmp.rs -o out.bin && ./out.bin' --
.ONESHELL: all
all:
	@fn main() {
		println!("hello make!")
	}
; make all
hello make!

But what if you want to add dependencies? That made me write makerust. I never wrote it up as a blog post, so here it is, now that I updated it to use cargo script (needs Rust nightly, ).

So now you can write:

include makerust.mk

info: dep.xshell dep.anyhow
	@
	use anyhow::Result;
	use xshell::{cmd, Shell};
	
	fn main() -> Result<()> {
		let sh = Shell::new()?;
		let branch = "main";
		let commit_hash = cmd!(sh, "git rev-parse {branch}").read()?;
		println!("makerust running on commit {commit_hash}");
		Ok(())
	}

And then it works:

; make info
makerust running on commit 438da52bae8e0afbcb46d19852cb0def6f21d619

Why?

make is good. Rust is good. In combination it can only get better.

But really ... why?

We don't ask this question around here.

Should I really?

Check with your team first, but I can't see a reason why not.

This is a joke, right?

I don't make jokes on the internet.


THE SOFTWARE IS PROVIDED "AS IS", BUT ALSO I TAKE FULL RESPONSIBILITY FOR THIS UNUTTERABLE HORROR AND YOU SHALL RECITE MY NAME FOR FOREVER ALONGSIDE IT.