d2b6bed
# Copyright (c) 2018 Nicolas Mailhot <nim@fedoraproject.org>
d2b6bed
#
d2b6bed
# This file is distributed under the terms of GNU GPL license version 3, or
d2b6bed
# any later version.
d2b6bed
#
d2b6bed
# This file contains macros needed at %%build %%install and %%check
d2b6bed
# stage by Golang packages.
d2b6bed
# The macros necessary at %%setup and srpm stage are in the sister file
d2b6bed
# macros.go-srpm
d2b6bed
d2b6bed
# find directory filter to remove elements usually not needed to build other Go projects
d2b6bed
%gofinddirfilter  -regextype egrep \! -iregex '.*/(.*[._-])?(example(s)?|test(_)?data)/.*' \! -ipath '*/vendor/*' \! -ipath '*/test cases/*'
d2b6bed
d2b6bed
# find filter to identify resources usually needed to build other Go projects
d2b6bed
%gofindfilter     -regextype egrep -iregex '.*\\.(go|c|h|s|tmpl|proto|json)' \! -iname '*_test.go' %{gofinddirfilter}
d2b6bed
d2b6bed
# find filter to identify unit tests
d2b6bed
%gofindtestfilter                  -iname '*.go' %{gofinddirfilter}
d2b6bed
d2b6bed
# Collect md files spread in subdirectories
d2b6bed
%gocollectmd %{expand:
d2b6bed
for mdfile in $(find . -iname '*.md' %{gofinddirfilter}) ; do
d2b6bed
  suffix=$(dirname $mdfile | sed 's+^\./++g' | sed 's+/+·+g')
d2b6bed
  if [[ $suffix != '.' ]] ; then
d2b6bed
    cp -p "$mdfile" "$(echo $(basename $mdfile) | sed 's+\.md$++g')·${suffix}.md"
d2b6bed
  fi
d2b6bed
done}
d2b6bed
d2b6bed
# Try to install Go package files in sensible locations, with strict directory
d2b6bed
# ownership as required by Go autodeps
d2b6bed
#
d2b6bed
# %%goinstall takes a list of files as argument.
d2b6bed
#
d2b6bed
# %%goinstall will generate a file list that can be used in a %%files spec
d2b6bed
# section. The default file list name is devel.file-list. It can be overriden
d2b6bed
# by passing the -f argument to the macro with another filename.
d2b6bed
#
d2b6bed
# When invoked several times it will append to existing file lists not create
d2b6bed
# a new one.
d2b6bed
#
d2b6bed
# When invoked several times with different file list names, it will attribute
d2b6bed
# directories to the first file list that makes use of them only. This is
d2b6bed
# intentional, to avoid triggering Go autodeps on the same Go directory in
d2b6bed
# different subpackages. Therefore, splitting code in several subpackages
d2b6bed
# requires careful though about %%goinstall invocation order.
d2b6bed
%goinstall(f:) %{expand:
d2b6bed
%global file_list %{?-f*}%{!?-f*:devel.file-list}
d2b6bed
install -m 0755 -vd "%{buildroot}%{gopath}/src"
d2b6bed
for file in %* ; do
d2b6bed
  file="${file#./}"
d2b6bed
  [[ -d "$file" && ! -L "$file" ]] && srcdir="$file" || srcdir=$(dirname "$file")
d2b6bed
  destdir="%{buildroot}%{gopath}/src/%{goipath}/$srcdir"
d2b6bed
  destdir="${destdir%/.}"
d2b6bed
  dir="$destdir"
d2b6bed
  dirs=()
d2b6bed
  while [[ ! -e "$dir" ]] ; do
d2b6bed
    dirs=("$dir" "${dirs[@]}")
d2b6bed
    dir=$(dirname "$dir")
d2b6bed
  done
d2b6bed
  for dir in "${dirs[@]}" ; do
d2b6bed
    install -m 0755 -vd "$dir"
d2b6bed
    if $(echo "$dir" | grep -q "^%{buildroot}%{gopath}/src/%{goipath}") ; then
d2b6bed
      touch -r           ".${dir#%{buildroot}%{gopath}/src/%{goipath}}" "$dir"
d2b6bed
    fi
d2b6bed
    echo "%%dir \"${dir#%{buildroot}}\"" >> %{file_list}
d2b6bed
  done
d2b6bed
  [[ -L "$file" ]] && cp -vpa "$file" "$destdir/"
d2b6bed
  [[ -f "$file" ]] && install -m 0644 -vp  "$file" "$destdir/"
d2b6bed
  [[ -f "$file" || -L "$file" ]] && echo "%%{gopath}/src/%%{goipath}/$file" >> %{file_list}
d2b6bed
done}
d2b6bed
d2b6bed
# Create a local Go build root
d2b6bed
# Useful in %%build and %%check
d2b6bed
%gobuildroot() %{expand:
d2b6bed
  GO_BUILD_PATH="$PWD/_build"
d2b6bed
  %global gobuildpath "$GO_BUILD_PATH"
d2b6bed
  install -m 0755 -vd "$(dirname %{gobuildpath}/src/%{goipath})"
d2b6bed
  ln -fs "$PWD" "%{gobuildpath}/src/%{goipath}"
d2b6bed
  install -m 0755 -vd _bin
d2b6bed
  export GOPATH="%{gobuildpath}:%{gopath}"
d2b6bed
  %{?commit:export LDFLAGS="-X %{goipath}/version.GitSHA=%{commit}"}
d2b6bed
}
d2b6bed
d2b6bed
# Run “go test” on all subdirs except for those provided in parameters
d2b6bed
# Examples:
d2b6bed
#  – run all available unit tests
d2b6bed
#    %gochecks
d2b6bed
#  — run all available unit tests except in root
d2b6bed
#    %gochecks .
d2b6bed
#  — run all available unit tests except in root and foo subdir
d2b6bed
#    %gochecks . foo
d2b6bed
#  — run all available unit tests except in I love cake and _examples subdirectories
d2b6bed
#    %gochecks 'I love cake' '_examples/*'
d2b6bed
%gochecks() %{expand:
d2b6bed
%gobuildroot
d2b6bed
export PATH=$PATH:%{buildroot}%{_bindir}
d2b6bed
set +x
d2b6bed
for sub in $(find . %{gofindtestfilter} -exec dirname '\{\}' \\;|sort|uniq|sed s'+^./++') ; do
d2b6bed
  process=true
d2b6bed
  declare -a 'excludes=('"%*"')'
d2b6bed
  for exclude in "${excludes[@]}" ; do
d2b6bed
    [[ $sub == $exclude ]] && process=false && break
d2b6bed
  done
d2b6bed
  if [ "$process" = true ] ; then
d2b6bed
    echo "Testing $sub…"
d2b6bed
    pushd "%{gobuildpath}/src/%{goipath}/$sub"
d2b6bed
    set -x
d2b6bed
    %{gotest}
d2b6bed
    set +x
d2b6bed
    popd
d2b6bed
  fi
d2b6bed
done
d2b6bed
set -x
d2b6bed
}