Blob Blame Raw
# Copyright (c) 2018 Nicolas Mailhot <>
# This file is distributed under the terms of GNU GPL license version 3, or
# any later version.
# This file contains macros needed at %%build %%install and %%check
# stage by Golang packages.
# The macros necessary at %%setup and srpm stage are in the sister file
# macros.go-srpm

# find directory filter to remove elements usually not needed to build other Go projects
%gofinddirfilter  -regextype egrep \! -iregex '.*/(.*[._-])?(example(s)?|test(_)?data)/.*' \! -ipath '*/vendor/*' \! -ipath '*/test cases/*'

# find filter to identify resources usually needed to build other Go projects
%gofindfilter     -regextype egrep -iregex '.*\\.(go|c|h|s|tmpl|proto|json)' \! -iname '*_test.go' %{gofinddirfilter}

# find filter to identify unit tests
%gofindtestfilter                  -iname '*.go' %{gofinddirfilter}

# Collect md files spread in subdirectories
%gocollectmd %{expand:
for mdfile in $(find . -iname '*.md' %{gofinddirfilter}) ; do
  suffix=$(dirname $mdfile | sed 's+^\./++g' | sed 's+/+·+g')
  if [[ $suffix != '.' ]] ; then
    cp -p "$mdfile" "$(echo $(basename $mdfile) | sed 's+\.md$++g')·${suffix}.md"

# Try to install Go package files in sensible locations, with strict directory
# ownership as required by Go autodeps
# %%goinstall takes a list of files as argument.
# %%goinstall will generate a file list that can be used in a %%files spec
# section. The default file list name is devel.file-list. It can be overriden
# by passing the -f argument to the macro with another filename.
# When invoked several times it will append to existing file lists not create
# a new one.
# When invoked several times with different file list names, it will attribute
# directories to the first file list that makes use of them only. This is
# intentional, to avoid triggering Go autodeps on the same Go directory in
# different subpackages. Therefore, splitting code in several subpackages
# requires careful though about %%goinstall invocation order.
%goinstall(f:) %{expand:
%global file_list %{?-f*}%{!?-f*:devel.file-list}
install -m 0755 -vd "%{buildroot}%{gopath}/src"
for file in %* ; do
  [[ -d "$file" && ! -L "$file" ]] && srcdir="$file" || srcdir=$(dirname "$file")
  while [[ ! -e "$dir" ]] ; do
    dirs=("$dir" "${dirs[@]}")
    dir=$(dirname "$dir")
  for dir in "${dirs[@]}" ; do
    install -m 0755 -vd "$dir"
    if $(echo "$dir" | grep -q "^%{buildroot}%{gopath}/src/%{goipath}") ; then
      touch -r           ".${dir#%{buildroot}%{gopath}/src/%{goipath}}" "$dir"
    echo "%%dir \"${dir#%{buildroot}}\"" >> %{file_list}
  [[ -L "$file" ]] && cp -vpa "$file" "$destdir/"
  [[ -f "$file" ]] && install -m 0644 -vp  "$file" "$destdir/"
  [[ -f "$file" || -L "$file" ]] && echo "%%{gopath}/src/%%{goipath}/$file" >> %{file_list}

# Create a local Go build root
# Useful in %%build and %%check
%gobuildroot() %{expand:
  %global gobuildpath "$GO_BUILD_PATH"
  install -m 0755 -vd "$(dirname %{gobuildpath}/src/%{goipath})"
  ln -fs "$PWD" "%{gobuildpath}/src/%{goipath}"
  install -m 0755 -vd _bin
  export GOPATH="%{gobuildpath}:%{gopath}"
  %{?commit:export LDFLAGS="-X %{goipath}/version.GitSHA=%{commit}"}

# Run “go test” on all subdirs except for those provided in parameters
# Examples:
#  – run all available unit tests
#    %gochecks
#  — run all available unit tests except in root
#    %gochecks .
#  — run all available unit tests except in root and foo subdir
#    %gochecks . foo
#  — run all available unit tests except in I love cake and _examples subdirectories
#    %gochecks 'I love cake' '_examples/*'
%gochecks() %{expand:
export PATH=$PATH:%{buildroot}%{_bindir}
set +x
for sub in $(find . %{gofindtestfilter} -exec dirname '\{\}' \\;|sort|uniq|sed s'+^./++') ; do
  declare -a 'excludes=('"%*"')'
  for exclude in "${excludes[@]}" ; do
    [[ $sub == $exclude ]] && process=false && break
  if [ "$process" = true ] ; then
    echo "Testing $sub…"
    pushd "%{gobuildpath}/src/%{goipath}/$sub"
    set -x
    set +x
set -x