Files
my-rook-config/build/makelib/golang.mk
T
Bassam Tabbara 75523ccb17 build: add support debug builds and backtraces
we now build with debug information on linux even for release builds. Debug
information is save in a separate .debug file that can be loaded by gdb.

switched to using the golangs's facilities for cgo backtraces. had to add
C++ demangling to it but its now on par with the default backtrace facility
that ceph had. This approach works with static builds and relies on the
symbol table alone -- no need for rdynamic or DWARF sections in the binary.

Update the build container so that all packages now build with debug, also
fixed zlib package which had a broken link.

Fixes #135
2017-02-09 19:52:27 -08:00

190 lines
7.8 KiB
Makefile

# Copyright 2016 The Rook Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ====================================================================================
# Makefile helper functions for golang
#
ifeq ($(GO_PROJECT),)
$(error the variable $$GO_PROJECT must be set prior to including golang.mk)
endif
# These targets will statically or dynamically linked depending on whether they
# import the standard net, os/user packages.
GO_NONSTATIC_PACKAGES ?=
# These targets will statically or dynamically linked depending on whether they
# import the standard net, os/user packages. Buildmode PIE will be enabled.
GO_NONSTATIC_PIE_PACKAGES ?=
# These targets do not use cgo or SWIG and contain all pure go code. They will
# be forced to link statically even if they import the net or os/user packages.
GO_STATIC_PACKAGES ?=
# These targets are a mix of go and non-go code. They will be linked statically
# or dynamically based on the linker flags passed through ldflags.
GO_STATIC_CGO_PACKAGES ?=
ifeq ($(GO_NONSTATIC_PACKAGES)$(GO_NONSTATIC_PIE_PACKAGES)$(GO_STATIC_PACKAGES)$(GO_STATIC_CGO_PACKAGES),)
$(error please set GO_NONSTATIC_PACKAGES, GO_NONSTATIC_PIE_PACKAGES, GO_STATIC_PACKAGES and/or GO_STATIC_CGO_PACKAGES prior to including golang.mk)
endif
# Optional. These are sudirs that we look for all go files to test, vet, and fmt
GO_SUBDIRS ?= cmd pkg
# Optional directories (relative to CURDIR)
GO_BIN_DIR ?= bin
GO_TOOLS_DIR ?= tools
GO_VENDOR_DIR ?= vendor
GO_PKG_DIR ?=
# Optional build flags passed to go tools
GO_BUILDFLAGS ?=
GO_LDFLAGS ?=
GO_TAGS ?=
# Optional CGO flags directories
CGO_CFLAGS ?=
CGO_CXXFLAGS ?=
CGO_LDFLAGS ?=
# Optional prerequisities for CGO builds
CGO_PREREQS ?=
# Optional OS and ARCH to build
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
# ====================================================================================
# Setup go environment
GO_SUPPORTED_VERSIONS ?= 1.6|1.7
GOROOT = $(shell go env GOROOT)
GOPATH = $(shell go env GOPATH)
GOHOSTOS = $(shell go env GOHOSTOS)
GOHOSTARCH = $(shell go env GOHOSTARCH)
GO_ALL_PACKAGES := $(foreach t,$(GO_SUBDIRS),$(GO_PROJECT)/$(t)/...)
unexport CGO_ENABLED
export CGO_CFLAGS CGO_CPPFLAGS CGO_LDFLAGS GLIDE_HOME
# setup glide
GLIDE_HOME := $(abspath .glide)
GLIDE := $(GO_TOOLS_DIR)/glide
GO := go
GOHOST := GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) go
GO_OUT_DIR := $(abspath $(GO_BIN_DIR)/$(GOOS)_$(GOARCH))
ifeq ($(GOOS),windows)
GO_OUT_EXT := .exe
endif
ifneq ($(GO_PKG_DIR),)
GO_PKG_FLAGS := -pkgdir $(abspath $(GO_PKG_DIR)/$(GOOS)_$(GOARCH))
endif
# these configurations are matched with the ones in build/cross-image/Dockerfile
GO_NONSTATIC_FLAGS = $(GO_BUILDFLAGS) $(GO_PKG_FLAGS) -tags '$(GO_TAGS)' -ldflags '$(GO_LDFLAGS)'
GO_NONSTATIC_PIE_FLAGS = $(GO_BUILDFLAGS) $(GO_PKG_FLAGS) -installsuffix pie -buildmode pie -tags '$(GO_TAGS)' -ldflags '$(GO_LDFLAGS)'
# note the we remove the symbol table since its not needed for pure golang binaries. for backtraces go
# uses its own ELF section and does not depend on the symbol tables.
GO_STATIC_FLAGS = $(GO_BUILDFLAGS) $(GO_PKG_FLAGS) -installsuffix nocgo -tags '$(GO_TAGS)' -ldflags '-s $(GO_LDFLAGS)'
# for cgo binaries we need the symbol table to support backtraces, so no -s here.
GO_STATIC_CGO_FLAGS = $(GO_BUILDFLAGS) $(GO_PKG_FLAGS) -installsuffix netgo -tags '$(GO_TAGS) netgo' -ldflags '$(GO_LDFLAGS) -extldflags "-static"'
# ====================================================================================
# Targets
ifeq ($(filter help clean distclean, $(MAKECMDGOALS)),)
.PHONY: go.check
go.check:
ifneq ($(shell $(GO) version | grep -q -E '\bgo($(GO_SUPPORTED_VERSIONS))\b' && echo 0 || echo 1), 0)
$(error unsupported go version. Please make install one of the following supported version: '$(GO_SUPPORTED_VERSIONS)')
endif
ifneq ($(realpath ../../../..), $(realpath $(GOPATH)))
$(warning WARNING: the source directory is not relative to the GOPATH at $(GOPATH) or you are you using symlinks. The build might run into issue. Please move the source directory to be at $(GOPATH)/src/$(GO_PROJECT))
endif
-include go.check
.PHONY: go.init
go.init: $(GO_VENDOR_DIR)/vendor.stamp
-include go.init
endif
.PHONY: go.build
go.build: go.vet go.fmt $(CGO_PREREQS)
@$(foreach p,$(GO_NONSTATIC_PACKAGES),CGO_ENABLED=1 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_NONSTATIC_FLAGS) $(p);)
@$(foreach p,$(GO_NONSTATIC_PIE_PACKAGES),CGO_ENABLED=1 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_NONSTATIC_PIE_FLAGS) $(p);)
@$(foreach p,$(GO_STATIC_PACKAGES),CGO_ENABLED=0 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_FLAGS) $(p);)
@$(foreach p,$(GO_STATIC_CGO_PACKAGES),CGO_ENABLED=1 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_CGO_FLAGS) $(p);)
ifeq ($(RELEASEBUILD),1)
ifeq ($(GOOS),linux)
# @$(foreach p,$(GO_STATIC_CGO_PACKAGES) $(GO_NONSTATIC_PACKAGES) $(GO_NONSTATIC_PIE_PACKAGES),dwz $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT);)
@$(foreach p,$(GO_STATIC_CGO_PACKAGES) $(GO_NONSTATIC_PACKAGES) $(GO_NONSTATIC_PIE_PACKAGES),$(OBJCOPY) --only-keep-debug $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p))).debug;)
@$(foreach p,$(GO_STATIC_CGO_PACKAGES) $(GO_NONSTATIC_PACKAGES) $(GO_NONSTATIC_PIE_PACKAGES),$(OBJCOPY) --strip-debug $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT);)
@$(foreach p,$(GO_STATIC_CGO_PACKAGES) $(GO_NONSTATIC_PACKAGES) $(GO_NONSTATIC_PIE_PACKAGES),$(OBJCOPY) --add-gnu-debuglink=$(GO_OUT_DIR)/$(lastword $(subst /, ,$(p))).debug $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT);)
endif
endif
.PHONY: go.install
go.install: go.vet go.fmt $(CGO_PREREQS)
@$(foreach p,$(GO_NONSTATIC_PACKAGES),CGO_ENABLED=1 $(GO) install -v $(GO_NONSTATIC_FLAGS) $(p);)
@$(foreach p,$(GO_NONSTATIC_PIE_PACKAGES),CGO_ENABLED=1 $(GO) install -v $(GO_NONSTATIC_PIE_FLAGS) $(p);)
@$(foreach p,$(GO_STATIC_PACKAGES),CGO_ENABLED=0 $(GO) install -v $(GO_STATIC_FLAGS) $(p);)
@$(foreach p,$(GO_STATIC_CGO_PACKAGES),CGO_ENABLED=1 $(GO) install -v $(GO_STATIC_CGO_FLAGS) $(p);)
.PHONY: go.test
go.test: go.vet go.fmt $(CGO_PREREQS)
# this is disabled since it looks like there's a bug in go test where it attempts to install cmd/cgo
# @$(GOHOST) test -v -i $(GO_PKG_FLAGS) $(GO_NONSTATIC_FLAGS) $(GO_ALL_PACKAGES)
@$(GOHOST) test -cover $(GO_PKG_FLAGS) $(GO_NONSTATIC_FLAGS) $(GO_ALL_PACKAGES)
.PHONY: go.vet
go.vet:
@$(GOHOST) vet $(GO_NONSTATIC_FLAGS) $(GO_ALL_PACKAGES)
.PHONY: go.fmt
go.fmt:
@$(GOHOST) fmt $(GO_ALL_PACKAGES)
.PHONY: go.vendor
go.vendor $(GO_VENDOR_DIR)/vendor.stamp: $(GO_TOOLS_DIR)/glide
@mkdir -p $(GLIDE_HOME)
@$(GLIDE) install
@touch $(GO_VENDOR_DIR)/vendor.stamp
$(GO_TOOLS_DIR)/glide:
@echo "installing glide"
@mkdir -p $(GO_TOOLS_DIR)
@curl -sL https://github.com/Masterminds/glide/releases/download/v0.12.3/glide-v0.12.3-$(GOHOSTOS)-$(GOHOSTARCH).tar.gz | tar -xz -C $(GO_TOOLS_DIR)
@mv $(GO_TOOLS_DIR)/$(GOHOSTOS)-$(GOHOSTARCH)/glide $(GO_TOOLS_DIR)/glide
@rm -r $(GO_TOOLS_DIR)/$(GOHOSTOS)-$(GOHOSTARCH)
.PHONY: go.clean
go.clean: ;
@rm -rf $(GO_BIN_DIR)/*
ifneq ($(GO_PKG_DIR),)
@rm -rf $(GO_PKG_DIR)
endif
.PHONY: go.distclean
go.distclean: go.clean
@rm -rf $(GO_TOOLS_DIR) $(GO_VENDOR_DIR) $(GLIDE_HOME)