Blob Blame History Raw
# Copyright © 2015-2018 Jakub Cajka <jcajka@redhat.com>,
#                       Jan Chaloupka <jchaloup@redhat.com>,
#                       Nicolas Mailhot <nim@fedoraproject.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# This file contains macros for building projects in golang for packages
# with golang compiler or gcc-go compiler based on an architecture.
#
# SPDX-License-Identifier: GPL-3.0-or-later

# All flags to pass "go build" other than -ldflags
# BACKWARDS COMPATIBILITY:
#     $BUILDTAGS is supported as a deprecated alias to $GO_BUILDTAGS to add additional
#     arguments to `go build -tags "..."`.
#     Please set $GO_BUILDTAGS instead of $BUILDTAGS.
%gobuild_baseflags %{gocompilerflags} -tags="rpm_crashtraceback ${GO_BUILDTAGS-${BUILDTAGS-}}" -a -v -x
# A shell-escaped version of %gobuild_baseflags.
# Can be used to set an environment or make variable.
# Example from the aerc package:
#     %make_build BUILD_OPTS=%{gobuild_baseflags_shescaped} aerc
# NOTE: This cannot be passed to the GOFLAGS environment variable due to
# <https://github.com/golang/go/issues/26849>.
# Upstream build scripts must expose a separate variable to pass arguments
# directly to the "go build" invocation.
%gobuild_baseflags_shescaped "$(cat <<EOF\
%{gobuild_baseflags}\
EOF\
)"

# Argument to pass to "go build"'s -ldflags option
# BACKWARDS COMPATIBILITY:
#     $LDFLAGS is supported as a deprecated alias to $GO_LDFLAGS to add additional
#     arguments to `go build -ldflags "..."`.
#     Please set $GO_LDFLAGS instead of $LDFLAGS.
%gobuild_ldflags ${GO_LDFLAGS-${LDFLAGS-}} %{?currentgoldflags} -B 0x$(echo "%{name}-%{version}-%{release}-${SOURCE_DATE_EPOCH:-}" | sha1sum | cut -d ' ' -f1) -compressdwarf=false -linkmode=external -extldflags '%{build_ldflags} %{?__golang_extldflags}'
# A shell-escaped version of %gobuild_ldflags.
# Can be used to set an environment or make variable.
# Example from the aerc package:
#     %make_build \
#       BUILD_OPTS=%{gobuild_baseflags_shescaped} \
#       GO_LDFLAGS=%{gobuild_ldflags_shescaped} \
#       aerc
# NOTE: This cannot be passed to the GOLDFLAGS environment variable due to
# <https://github.com/golang/go/issues/26849>.
# Upstream build scripts must expose a separate variable for that.
# For projects that don't provide a separate flag to set -ldflags, use
# %gobuildflags_shescapes which combines %gobuild_baseflags_shescaped and
# %gobuild_ldflags_shescaped.
%gobuild_ldflags_shescaped "$(cat <<EOF\
%{gobuild_ldflags}\
EOF\
)"

# Arguments to pass to "go build."
# Unlike the %gobuild_baseflags, this also includes the -ldflags option.
%gobuildflags() %{expand:%{gobuild_baseflags} -ldflags "%{gobuild_ldflags}"}
# See previous macros' documentation
%gobuildflags_shescaped() "$(cat <<EOF\
%{gobuildflags}\
EOF\
)"

# Turn off Go modules
%gomodulesmode GO111MODULE=off

# Define commands for building
# BUILD_ID can be generated for golang build no matter of debuginfo
%gobuild(o:) %{expand:
  # https://pagure.io/go-rpm-macros/pull-request/38
  # Most of the default LDFLAGS for Fedora are not supported so we don't want
  # LDFLAGS to be automatically initialized with the Fedora flags.
  %undefine _auto_set_build_flags
  # https://bugzilla.redhat.com/show_bug.cgi?id=995136#c12
  %global _dwz_low_mem_die_limit 0
  %{?gobuilddir:GOPATH="%{gobuilddir}:${GOPATH:+${GOPATH}:}%{?gopath}"} %{?gomodulesmode} \\
  go build %{gobuildflags} %{?**};
}
${workroot}${GOPATH:+:${GOPATH}}

# Define commands for testing
%gotestflags      %{gocompilerflags}
%gotestextldflags %{build_ldflags} %{?__golang_extldflags}
%gotest() %{expand:
  %undefine _auto_set_build_flags
  %{?gomodulesmode} go test %{gotestflags} -ldflags "${GO_LDFLAGS-${LDFLAGS-}} %{?currentgoldflags} -extldflags '%{gotestextldflags}'" %{?**};
}