all: pb.py protobuf magic.py

.PHONY: all protobuf clean pb.py

proto := $(filter-out $(SRC_DIR)/protobuf/rpc.proto, $(sort $(wildcard $(SRC_DIR)/protobuf/*.proto)))
proto-py-modules := $(foreach m,$(proto),$(subst -,_,$(notdir $(m:.proto=_pb2))))

# We don't need rpc_pb2.py here, as it is not related to the
# images.
# Unfortunately, we can't drop ugly _pb2 suffixes here, because
# some _pb2 files depend on others _pb2 files.
protobuf:
	$(Q) protoc -I=$(SRC_DIR)/protobuf -I=/usr/include/ --python_out=./ $(proto)

magic.py: $(SRC_DIR)/scripts/magic-gen.py $(SRC_DIR)/include/magic.h
	$(E) "  GEN  " $@
	$(Q) python $^ $@

pb.py: protobuf
	$(Q) echo "# Autogenerated. Do not edit!" > $@
	$(Q) for m in $(proto-py-modules); do \
		echo "from $$m import *" >> $@ ;\
	done

clean:
	$(Q) $(RM) ./*_pb2.py ./*.pyc magic.py pb.py
