diff --git a/Makefile b/Makefile index 1fc76e87d47..392ce028385 100644 --- a/Makefile +++ b/Makefile @@ -62,6 +62,10 @@ GOLDFLAGS ?= KUBE_GOLDFLAGS = $(GOLDFLAGS) export KUBE_GOLDFLAGS GOLDFLAGS +GOGCFLAGS ?= +KUBE_GOGCFLAGS = $(GOGCFLAGS) +export KUBE_GOGCFLAGS GOGCFLAGS + # Build code. # # Args: @@ -69,12 +73,17 @@ export KUBE_GOLDFLAGS GOLDFLAGS # package, the build will produce executable files under $(OUT_DIR)/go/bin. # If not specified, "everything" will be built. # GOFLAGS: Extra flags to pass to 'go' when building. -# GOLDFLAGS: Extra linking 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. # # Example: # make # make all # make all WHAT=cmd/kubelet GOFLAGS=-v +# make all GOGCFLAGS="-N -l" +# Note: Use the -N -l options to disable compiler optimizations an inlining. +# Using these build options allows you to subsequently use source +# debugging tools like delve. .PHONY: all all: generated_files hack/make-rules/build.sh $(WHAT) @@ -107,6 +116,7 @@ verify: # TESTS: Same as WHAT. # GOFLAGS: Extra flags to pass to 'go' when building. # GOLDFLAGS: Extra linking flags to pass to 'go' when building. +# GOGCFLAGS: Additional go compile flags passed to 'go' when building. # # Example: # make check diff --git a/docs/devel/development.md b/docs/devel/development.md index 594dfcdf588..ac2b3bb3397 100644 --- a/docs/devel/development.md +++ b/docs/devel/development.md @@ -75,8 +75,14 @@ binaries): make ``` -You may pass build options and packages to the script as necessary. To build -binaries for all platforms: +You may pass build options and packages to the script as necessary. For example, +to build with optimizations disabled for enabling use of source debug tools: + +```sh + make GOGCFLAGS="-N -l" +``` + +To build binaries for all platforms: ```sh make cross diff --git a/hack/lib/golang.sh b/hack/lib/golang.sh index 4f01d81d1fc..b0a23d7c789 100755 --- a/hack/lib/golang.sh +++ b/hack/lib/golang.sh @@ -426,6 +426,7 @@ kube::golang::build_kube_toolchain() { kube::log::status "Building the toolchain targets:" "${binaries[@]}" go install "${goflags[@]:+${goflags[@]}}" \ + -gcflags "${gogcflags}" \ -ldflags "${goldflags}" \ "${binaries[@]:+${binaries[@]}}" } @@ -482,6 +483,7 @@ kube::golang::build_binaries_for_platform() { local outfile=$(kube::golang::output_filename_for_binary "${binary}" "${platform}") CGO_ENABLED=0 go build -o "${outfile}" \ "${goflags[@]:+${goflags[@]}}" \ + -gcflags "${gogcflags}" \ -ldflags "${goldflags}" \ "${binary}" kube::log::progress "*" @@ -490,6 +492,7 @@ kube::golang::build_binaries_for_platform() { local outfile=$(kube::golang::output_filename_for_binary "${binary}" "${platform}") go build -o "${outfile}" \ "${goflags[@]:+${goflags[@]}}" \ + -gcflags "${gogcflags}" \ -ldflags "${goldflags}" \ "${binary}" kube::log::progress "*" @@ -499,11 +502,13 @@ kube::golang::build_binaries_for_platform() { # Use go install. if [[ "${#nonstatics[@]}" != 0 ]]; then go install "${goflags[@]:+${goflags[@]}}" \ + -gcflags "${gogcflags}" \ -ldflags "${goldflags}" \ "${nonstatics[@]:+${nonstatics[@]}}" fi if [[ "${#statics[@]}" != 0 ]]; then CGO_ENABLED=0 go install -installsuffix cgo "${goflags[@]:+${goflags[@]}}" \ + -gcflags "${gogcflags}" \ -ldflags "${goldflags}" \ "${statics[@]:+${statics[@]}}" fi @@ -536,12 +541,14 @@ kube::golang::build_binaries_for_platform() { # returns true (always stale). And that's why we need to install the # test package. go install "${goflags[@]:+${goflags[@]}}" \ + -gcflags "${gogcflags}" \ -ldflags "${goldflags}" \ "${testpkg}" mkdir -p "$(dirname ${outfile})" go test -c \ "${goflags[@]:+${goflags[@]}}" \ + -gcflags "${gogcflags}" \ -ldflags "${goldflags}" \ -o "${outfile}" \ "${testpkg}" @@ -595,9 +602,10 @@ kube::golang::build_binaries() { host_platform=$(kube::golang::host_platform) # Use eval to preserve embedded quoted strings. - local goflags goldflags + local goflags goldflags gogcflags eval "goflags=(${KUBE_GOFLAGS:-})" goldflags="${KUBE_GOLDFLAGS:-} $(kube::version::ldflags)" + gogcflags="${KUBE_GOGCFLAGS:-}" local use_go_build local -a targets=()