Skip to content

Commit

Permalink
Construct package dependencies using Go
Browse files Browse the repository at this point in the history
For best results, Make should be told about all the files a given
target depends on. For Go programs, this is the program's main Go
file, and all non-test Go files in directories corresponding to
packages the main Go file depends upon (including transitive
dependencies).

This adds a Make macro using go list to retrieve a package's
dependencies, and constructs the corresponding list of files to use as
target prerequisites.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt committed Dec 9, 2024
1 parent 84d01fa commit e0a130d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ bin/protoc:
unzip protoc-$(PROTOC_VERSION)-linux-x86_64.zip 'bin/*' 'include/*'
rm -f protoc-$(PROTOC_VERSION)-linux-x86_64.zip

bin/%/submariner-gateway: main.go $(shell find pkg -not \( -path 'pkg/globalnet*' -o -path 'pkg/routeagent*' -o -path 'pkg/await_node_ready*' \)) pkg/natdiscovery/proto/natdiscovery.pb.go
basepkg = github.com/submariner-io/submariner
pkgdeps = $(shell find $$(go list -json $(1) | jq -r '.Imports[] | select(startswith("$(basepkg)")) | sub("$(basepkg)"; ".")') -name '*.go' -not -name '*_test.go')

# natdiscovery.pb.go must be listed explicitly because it might not exist when Make evaluates pkgdeps
bin/%/submariner-gateway: main.go $(call pkgdeps,.) pkg/natdiscovery/proto/natdiscovery.pb.go
GOARCH=$(call dockertogoarch,$(patsubst bin/linux/%/,%,$(dir $@))) ${SCRIPTS_DIR}/compile.sh $@ .

bin/%/submariner-route-agent: $(shell find pkg/routeagent_driver)
bin/%/submariner-route-agent: $(call pkgdeps,./pkg/routeagent_driver)
GOARCH=$(call dockertogoarch,$(patsubst bin/linux/%/,%,$(dir $@))) ${SCRIPTS_DIR}/compile.sh $@ ./pkg/routeagent_driver

bin/%/submariner-globalnet: $(shell find pkg/globalnet)
bin/%/submariner-globalnet: $(call pkgdeps,./pkg/globalnet)
GOARCH=$(call dockertogoarch,$(patsubst bin/linux/%/,%,$(dir $@))) ${SCRIPTS_DIR}/compile.sh $@ ./pkg/globalnet

bin/%/await-node-ready: $(shell find pkg/await_node_ready)
bin/%/await-node-ready: $(call pkgdeps,./pkg/await_node_ready)
GOARCH=$(call dockertogoarch,$(patsubst bin/linux/%/,%,$(dir $@))) ${SCRIPTS_DIR}/compile.sh $@ ./pkg/await_node_ready

nullstring :=
Expand Down

0 comments on commit e0a130d

Please sign in to comment.