firmware: Build firmware files with auto-generated version numbers

This resembles what we do for other firmware targets, such as simtrace2,
osmo-ccid-firmware, etc.

As we have multiple different firmware, hardware, gateware projects in
this repository, and they each may have separate tags, we now require
that there are project specific tags.  So for example, tags with a
prefix of icE1usb-fw for the firmware versions.

symlinks for easy use by scripts etc. are generated, always pointing to
the last-built versioned filename.

Change-Id: Ia308587e36a79c27fdb285e369a9c921ee65995f
diff --git a/firmware/ice40-riscv/icE1usb/Makefile b/firmware/ice40-riscv/icE1usb/Makefile
index b830051..1c92edb 100644
--- a/firmware/ice40-riscv/icE1usb/Makefile
+++ b/firmware/ice40-riscv/icE1usb/Makefile
@@ -9,6 +9,10 @@
 ICEPROG = iceprog
 DFU_UTIL = dfu-util
 
+TAG_PREFIX = icE1usb-fw
+GITVER = $(shell git describe --match '$(TAG_PREFIX)*')
+TARGET = $(GITVER)
+
 BOARD_DEFINE=BOARD_$(shell echo $(BOARD) | tr a-z\- A-Z_)
 CFLAGS=-Wall -Wextra -Wno-unused -Os -march=rv32i -mabi=ilp32 -ffreestanding -flto -nostartfiles -fomit-frame-pointer -Wl,--gc-section --specs=nano.specs -D$(BOARD_DEFINE) -I. -I../common
 
@@ -56,10 +60,10 @@
 	$(NULL)
 
 
-all: icE1usb-fw.bin
+all: $(GITVER).bin $(TAG_PREFIX).bin $(TAG_PREFIX).elf
 
 
-icE1usb-fw.elf: $(LNK) $(HEADERS_app) $(SOURCES_app) $(HEADERS_common) $(SOURCES_common)
+$(GITVER).elf: $(LNK) $(HEADERS_app) $(SOURCES_app) $(HEADERS_common) $(SOURCES_common)
 	$(CC) $(CFLAGS) -Wl,-Bstatic,-T,$(LNK),--strip-debug -o $@ $(SOURCES_common) $(SOURCES_app)
 
 
@@ -69,10 +73,16 @@
 %.bin: %.elf
 	$(OBJCOPY) -O binary $< $@
 
-prog: icE1usb-fw.bin
+$(TAG_PREFIX).bin: $(GITVER).bin
+	ln -sf $< $@
+
+$(TAG_PREFIX).elf: $(GITVER).elf
+	ln -sf $< $@
+
+prog: $(GITVER).bin
 	$(ICEPROG) -o 640k $<
 
-dfuprog: icE1usb-fw.bin
+dfuprog: $(GITVER).bin
 	$(DFU_UTIL) -R -a 1 -D $<