by williamdclt · 9y ago · 0 comments
I never used Go so the following may not be technically correct, but in principle:
- The `build` rule should be `build: *.go` so that it builds only if a file has changed. - The `run` rule should have the `build` rule as dependency, so as to not rebuild if nothing has changed. Plus, it avoid to repeat the almost-identical build command (which is not very DRY)
Plus, I'm not convinced of the usefulness of having a $GOCMD variable (or $GOGET, $GOBUILD...). But again, never used go, no idea if the tooling is as tweakable as C (for which this kind of variable is used a lot in makefiles)
My quibble with the example Makefile as written is that it doesn't utilise 'go install,' which I much prefer to 'go build.'
IGNORE_FILES := "Makefile\|.gitignore\|.dockerignore"
DOCKER_CONTEXT := $(shell git ls-files -cmo --exclude-standard \
--exclude-from=.dockerignore \
| grep -v $(IGNORE_FILES))
.PHONY: build
build: .build/docker_image
.build/docker_image: .build $(DOCKER_CONTEXT)
docker build -t $(IMAGE) .
docker images -q $(IMAGE) > $@
.build:
@mkdir -p $@
I'm still figuring this stuff out, so I would love any feedback. :) ${GOCMD} list -f "{{ .ImportPath }} {{join .Deps \" \" }}" ./cmd/$command | \
xargs ${GOCMD} list -f "{{ if not .Standard }}build/mac/${command}: {{ range .GoFiles }}{{ \$.Dir }}/{{ . }} {{ end }}{{ end }}" ./cmd/${command} | \
sed "s: /go/src/com.test/your/package/: :g"
...and then including the output of that in the Makefile. There are ways to make sure it gets rebuilt if anything changes, but you'll pay a performance price. It's rather similar to what happens with C, as far as I remember.It was quite evil, somewhat redundant, and I eventually found make to complicate things for my application more than it was helpful, but sure, you can map modified go files to whatever tasks you might want to do.
EDIT: note that, unlike in C, go (usually) has a single static output file, so without the .o stage, the dependencies are quite dynamic - a change to a go file not included directly might be cause for a recompile. You can build intermediates, (I think either Buck or Bazel do that?) and link by hand, but that's very non-standard.