
TARGET=x86_64-linux-gnu
DYNAMIC_LINKER=/usr/lib/ld-linux-x86-64.so.2
LIBS=-lc


.PRECIOUS: %.ll
%.ll: %.c
	clang -emit-llvm -o $@ -S $<
	# lli $@


.PRECIOUS: %.opt.ll
%.opt.ll: %.ll
	opt -O2 -S -o $@ $<
	# https://www.youtube.com/watch?v=bUTXhcf_aNc


.PRECIOUS: %.bc
%.bc: %.ll
	llvm-as -o $@ $<
	# llvm-dis


.PRECIOUS: %.s
%.s: %.bc
	llc -mtriple="${TARGET}" -O2 -o $@ $<


.PRECIOUS: %.o
%.o: %.s
	as -o $@ $<


.PRECIOUS: %.elf
%.elf: %.o
	ld -o $@ -I${DYNAMIC_LINKER} /usr/lib64/crt1.o $< ${LIBS}
	#                            Reihenfolge crt1 $< -lc ist wichtig!


.PHONY: run
run: helloworld.opt.elf
	./helloworld.opt.elf


.PHONY: clean
clean:
	rm -f *.ll *.bc *.s *.o *.elf
