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 <[email protected]>
  • Loading branch information
skitt committed Dec 4, 2024
1 parent 9d2e96a commit 7a80cf3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ 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*' \)) 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


Expand Down

0 comments on commit 7a80cf3

Please sign in to comment.