build: find dependencies for Go executables

To ensure that changing a dependency source file actually triggers
a rebuild of the core binaries when you type 'make', find their
dependencies and add them to the makefile's target dependencies.

Signed-off-by: Dan Williams <dcbw@redhat.com>
This commit is contained in:
Dan Williams 2017-03-31 12:57:34 -05:00
parent 9c44933b58
commit 95846211c9
2 changed files with 44 additions and 3 deletions

View file

@ -68,15 +68,15 @@ copyimg: .gopathok $(wildcard test/copyimg/*.go)
checkseccomp: .gopathok $(wildcard test/checkseccomp/*.go) checkseccomp: .gopathok $(wildcard test/checkseccomp/*.go)
go build -o test/checkseccomp/$@ $(PROJECT)/test/checkseccomp go build -o test/checkseccomp/$@ $(PROJECT)/test/checkseccomp
ocid: .gopathok ocid: .gopathok $(shell hack/find-godeps.sh $(GOPKGDIR) cmd/ocid $(PROJECT))
$(GO) build -o $@ \ $(GO) build -o $@ \
-tags "$(BUILDTAGS)" \ -tags "$(BUILDTAGS)" \
$(PROJECT)/cmd/ocid $(PROJECT)/cmd/ocid
ocic: .gopathok ocic: .gopathok $(shell hack/find-godeps.sh $(GOPKGDIR) cmd/ocic $(PROJECT))
$(GO) build -o $@ $(PROJECT)/cmd/ocic $(GO) build -o $@ $(PROJECT)/cmd/ocic
kpod: .gopathok kpod: .gopathok $(shell hack/find-godeps.sh $(GOPKGDIR) cmd/kpod $(PROJECT))
$(GO) build -o $@ $(PROJECT)/cmd/kpod $(GO) build -o $@ $(PROJECT)/cmd/kpod
ocid.conf: ocid ocid.conf: ocid

41
hack/find-godeps.sh Executable file
View file

@ -0,0 +1,41 @@
#!/bin/bash
#
# $1 - base path of the source tree
# $2 - subpath under $1 to find *.go dependencies for
# $3 - package name (eg, github.com/organization/project)
set -o errexit
set -o nounset
set -o pipefail
# might be called from makefile before basepath is set up; just return
# empty deps. The make target will then ensure that GOPATH is set up
# correctly, and go build will build everything the first time around
# anyway. Next time we get here everything will be fine.
if [ ! -d "$1/$2" ]; then
exit 0
fi
function find-deps() {
local basepath=$1
local srcdir=$2
local pkgname=$3
local deps=
# gather imports from cri-o
pkgs=$(cd ${basepath}/${srcdir} && go list -f "{{.Imports}}" . | tr ' ' '\n' | grep -v "/vendor/" | grep ${pkgname} | sed -e "s|${pkgname}/||g")
# add each Go import's sources to the deps list,
# and recursively get that imports's imports too
for dep in ${pkgs}; do
deps+="$(ls ${basepath}/${dep}/*.go | sed -e "s|${basepath}/||g") "
# add deps of this package too
deps+="$(find-deps ${basepath} ${dep} ${pkgname}) "
done
echo "${deps}" | sort | uniq
}
# add Go sources from the current package at the end
echo "$(find-deps "$1" "$2" "$3" | xargs) $(cd $1 && ls $2/*.go | xargs)"