From ed5e549cde797a12b85dc1fcb4ed88943d041fb3 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sat, 26 Feb 2022 23:28:49 -0800 Subject: [PATCH 1/2] Makefile: Add a DBG flag to build debug binaries Now `make DBG=1` will produce binaries with no optimizaions and no inlining, but with symbols and DWARF information. --- build/root/Makefile | 2 ++ hack/lib/golang.sh | 53 ++++++++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/build/root/Makefile b/build/root/Makefile index 2499df28caf..e66bddd2409 100644 --- a/build/root/Makefile +++ b/build/root/Makefile @@ -73,6 +73,8 @@ define ALL_HELP_INFO # GOFLAGS: Extra flags to pass to 'go' when building. # GOLDFLAGS: Extra linking flags passed to 'go' when building. # GOGCFLAGS: Additional go compile flags passed to 'go' when building. +# DBG: If set to "1", build with optimizations disabled for easier +# debugging. Any other value is ignored. # # Example: # make diff --git a/hack/lib/golang.sh b/hack/lib/golang.sh index 2b83d3912be..9575c689329 100755 --- a/hack/lib/golang.sh +++ b/hack/lib/golang.sh @@ -714,12 +714,12 @@ kube::golang::build_binaries_for_platform() { local -a build_args if [[ "${#statics[@]}" != 0 ]]; then build_args=( - -installsuffix static + -installsuffix=static ${goflags:+"${goflags[@]}"} - -gcflags "${gogcflags:-}" - -asmflags "${goasmflags:-}" - -ldflags "${goldflags:-}" - -tags "${gotags:-}" + -gcflags="${gogcflags}" + -asmflags="${goasmflags}" + -ldflags="${goldflags}" + -tags="${gotags:-}" ) CGO_ENABLED=0 kube::golang::build_some_binaries "${statics[@]}" fi @@ -727,10 +727,10 @@ kube::golang::build_binaries_for_platform() { if [[ "${#nonstatics[@]}" != 0 ]]; then build_args=( ${goflags:+"${goflags[@]}"} - -gcflags "${gogcflags:-}" - -asmflags "${goasmflags:-}" - -ldflags "${goldflags:-}" - -tags "${gotags:-}" + -gcflags="${gogcflags}" + -asmflags="${goasmflags}" + -ldflags="${goldflags}" + -tags="${gotags:-}" ) kube::golang::build_some_binaries "${nonstatics[@]}" fi @@ -743,10 +743,10 @@ kube::golang::build_binaries_for_platform() { mkdir -p "$(dirname "${outfile}")" go test -c \ ${goflags:+"${goflags[@]}"} \ - -gcflags "${gogcflags:-}" \ - -asmflags "${goasmflags:-}" \ - -ldflags "${goldflags:-}" \ - -tags "${gotags:-}" \ + -gcflags="${gogcflags}" \ + -asmflags="${goasmflags}" \ + -ldflags="${goldflags}" \ + -tags="${gotags:-}" \ -o "${outfile}" \ "${testpkg}" done @@ -798,17 +798,26 @@ kube::golang::build_binaries() { local host_platform host_platform=$(kube::golang::host_platform) + # These are "local" but are visible to and relied on by functions this + # function calls. They are effectively part of the calling API to + # build_binaries_for_platform. local goflags goldflags goasmflags gogcflags gotags - # If GOLDFLAGS is unset, then set it to the a default of "-s -w". - # Disable SC2153 for this, as it will throw a warning that the local - # variable goldflags will exist, and it suggest changing it to this. - # shellcheck disable=SC2153 - goldflags="${GOLDFLAGS=-s -w} $(kube::version::ldflags)" - goasmflags="-trimpath=${KUBE_ROOT}" - gogcflags="${GOGCFLAGS:-} -trimpath=${KUBE_ROOT}" - # extract tags if any specified in GOFLAGS - # shellcheck disable=SC2001 + goasmflags="-trimpath=${KUBE_ROOT}" + + gogcflags="-trimpath=${KUBE_ROOT} ${GOGCFLAGS:-}" + if [[ "${DBG:-}" == 1 ]]; then + # Debugging - disable optimizations and inlining. + gogcflags="${gogcflags} -N -l" + fi + + goldflags="$(kube::version::ldflags) ${GOLDFLAGS:-}" + if [[ "${DBG:-}" != 1 ]]; then + # Not debugging - disable symbols and DWARF. + goldflags="${goldflags} -s -w" + fi + + # Extract tags if any specified in GOFLAGS gotags="selinux,notest,$(echo "${GOFLAGS:-}" | sed -ne 's|.*-tags=\([^-]*\).*|\1|p')" local -a targets=() From bf27cad2561f1dd3370ddc1c96a904526a2ec8b0 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sat, 26 Feb 2022 23:28:49 -0800 Subject: [PATCH 2/2] Build flags: use `all=` syntax This has somewhat subtle implications. For a concrete example, this changes the `-trimpath` behavior from only affecting the named pkg to affecting all pkgs, which broke ginkgo, which seems to try to strip its own `pwd` prefix. But since that runs in run-in-gopath, and not in KUBE_ROOT, it fails to strip anything. e.g. before this, strings in the binary would be like /home/user/kube/_output/local/go/src/k8s.io/kubernetes/vendor/github.com/onsi/ginkgo/... Ginkgo would find its own root as /home/user/kube/_output/local/go/src/k8s.io/kubernetes/ so it would produce vendor/github.com/onsi/ginkgo/... in logs. after this, strings in the binary strip the KUBE_ROOT and be like: _output/local/go/src/k8s.io/kubernetes/vendor/github.com/onsi/ginkgo/... Ginkgo would find its own root as /home/user/kube/_output/local/go/src/k8s.io/kubernetes/ so it would not strip anything, and produce _output/local/go/src/k8s.io/kubernetes/vendor/github.com/onsi/ginkgo/... in logs. --- hack/lib/golang.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/hack/lib/golang.sh b/hack/lib/golang.sh index 9575c689329..0be13e6caa2 100755 --- a/hack/lib/golang.sh +++ b/hack/lib/golang.sh @@ -803,15 +803,20 @@ kube::golang::build_binaries() { # build_binaries_for_platform. local goflags goldflags goasmflags gogcflags gotags - goasmflags="-trimpath=${KUBE_ROOT}" + # This is $(pwd) because we use run-in-gopath to build. Once that is + # excised, this can become ${KUBE_ROOT}. + local trimroot # two lines to appease shellcheck SC2155 + trimroot=$(pwd) - gogcflags="-trimpath=${KUBE_ROOT} ${GOGCFLAGS:-}" + goasmflags="all=-trimpath=${trimroot}" + + gogcflags="all=-trimpath=${trimroot} ${GOGCFLAGS:-}" if [[ "${DBG:-}" == 1 ]]; then # Debugging - disable optimizations and inlining. gogcflags="${gogcflags} -N -l" fi - goldflags="$(kube::version::ldflags) ${GOLDFLAGS:-}" + goldflags="all=$(kube::version::ldflags) ${GOLDFLAGS:-}" if [[ "${DBG:-}" != 1 ]]; then # Not debugging - disable symbols and DWARF. goldflags="${goldflags} -s -w"