all: build

CSI_SPEC := spec.md
CSI_PROTO := csi.proto
## Build go language bindings
CSI_A := csi.a
CSI_PKG := lib/go/csi

# This is the target for building the CSI protobuf file.
$(CSI_PROTO): $(CSI_SPEC) Makefile
	echo "// Code generated by make; DO NOT EDIT." > "$@"
	sed -n -e '/```protobuf$$/,/^```$$/ {//!p;}' $< >> "$@"

build: check build_cpp build_go

build_cpp:
	$(MAKE) -C lib/cxx

$(CSI_PKG)/%.pb.go: $(CSI_PROTO)
	$(MAKE) -C lib/go

$(CSI_A): $(CSI_PKG)/*.go
	go mod download
	go install ./$(CSI_PKG)
	go build -o "$@" ./$(CSI_PKG)

build_go: $(CSI_A)

clean:
	rm $(CSI_A)
	$(MAKE) -C lib/go $@

clobber: clean
	$(MAKE) -C lib/go $@
	rm -f $(CSI_PROTO)

# check generated files for violation of standards
check: $(CSI_PROTO)
	awk '{ if (length > 72) print NR, $$0 }' $? | diff - /dev/null

.PHONY: clean clobber check build_go build_cpp
