I spent the better part of the day trying to figure this out. I want to use make for building a golang project with different os targets and multiple binaries. I think I want to use gnu make
My stripped down Makefile looks like:
main= mailworker websocket worker init
os= linux freebsd darwin
all: $(main)
$(main): ensure_build_path
@echo "Build $@ Version: $(GIT_REV)"
@rm -rf $(BUILD_PREFIX)/$@
GO111MODULE=on GOARCH=amd64 $(GO) build -o $(BUILD_PREFIX)/$@ ./bin/$@/$@.go
Now I want to add GOOS=$(os) to the build command / generate a target for every main X os combination. My test / lern Makefile looks like:
os=linux freebsd darwin
main=mailworker websocket worker init
define build_template =
t:=$(addprefix $(1), $(2))
$(info $(t))
$(t):
@echo $(1) $(2) $$@
endef
$(foreach P, $(main), $(foreach O, $(os), $(eval $$(call build_template, $O, $P))) )
There are at least a dozen variants of this I tried. I think my main problem is how to declare t inside the template. I tried many "concat" methods I found regarding make. Also := or = for t. I think := is the right one. But I am not sure about anything anymore ;)
To be clear, I want make to "generate" targets that look like:
mailworker_linux:
GO111MODULE=on GOOS=linux GOARCH=amd64 $(GO) build mailworker
mailworker_darwin:
GO111MODULE=on GOOS=darwin GOARCH=amd64 $(GO) build mailworker
Is this a good way doing this? And if so, where is my misunderstanding.
Thanks a lot