# Copyright (c) 2018 Nicolas Mailhot <nim@fedoraproject.org>
#
# 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/*'
# find filter to identify resources usually needed to build other Go projects
%gofindfilter -regextype egrep -iregex '(.*\\.(go|c|h|s|tmpl|proto)|./Gopkg\\.(toml|lock))' \! -iname '*_test.go' \! -iregex '.*/(.*[._-])?test(([._-])?case)?(s)?/.*' \! -iregex '.*/[._].*' %{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"
fi
done}
# 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
file="${file#./}"
[[ -d "${file}" && ! -L "${file}" ]] && srcdir="${file}" || srcdir=$(dirname "${file}")
destdir="%{buildroot}%{gopath}/src/%{goipath}/${srcdir}"
destdir="${destdir%/.}"
dir="${destdir}"
dirs=()
while [[ ! -e "${dir}" ]] ; do
dirs=("$dir" "${dirs[@]}")
dir=$(dirname "${dir}")
done
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}"
fi
echo "%%dir \"${dir#%{buildroot}}\"" >> %{file_list}
done
if [[ -L "$file" ]] ; then
ln -s $(readlink "${file}") "${destdir}/$(basename ${file})"
touch -h -r "${file}" "${destdir}/$(basename ${file})"
fi
[[ -f "$file" && ! -L "$file" ]] && install -m 0644 -vp "${file}" "${destdir}/"
[[ -f "$file" || -L "$file" ]] && echo "%%{gopath}/src/%%{goipath}/${file}" >> %{file_list} || :
done}
# Create a local Go build root
# Useful in %%build and %%check
%gobuildroot() %{expand:
GO_BUILD_PATH="$PWD/_build"
%global gobuildpath "$GO_BUILD_PATH"
install -m 0755 -vd "$(dirname %{gobuildpath}/src/%{goipath})"
ln -fs "$PWD" "%{gobuildpath}/src/%{goipath}"
cd "%{gobuildpath}/src/%{goipath}"
install -m 0755 -vd _bin
export GOPATH="%{gobuildpath}:%{gopath}"
export LDFLAGS="${LDFLAGS:-}%{?commit: -X %{goipath}/version.commit=%{commit}}%{?tag: -X %{goipath}/version.tag=%{tag}}%{?version: -X %{goipath}/version=%{version}}"
}
# Run %%{gotest} on all subdirectories except for those provided in parameters.
# THIS MACRO IS OPT-OUT.
# It only allows excluding specific subdirectories from the test run, with the
# following syntax :
# — to exclude the tests in a specific subdirectory, not recursively:
# pass it as parameter. Use . for the project root.
# Example:
# %gochecks . subdir1 sub2/dir
# — to exclude the tests in a whole subtree:
# use the -R or --root switch with the subtree root as argument.
# Example:
# %gochecks -R sub1/tree/root -root 'sub2/fixtures'
# — to exclude using free-form egrep-style regexes:
# use the -r or --regex switch with the regex as argument.
# Example:
# %gochecks --regex '.*/(.*[._-])?test(([._-])?case)?(s)?/.*'
# — you can mix and match:
# %gochecks . subdir1 -R sub/tree/root sub/dir2 -r './testcase(s)?/.*'
# — arguments containing spaces or escaped quotes are not supported
%gochecks(rotegxR-) %{expand:%{lua:
local parameters = {}
-- Quite sufficient for our needs, otherwise look at rex.gmatch
for p in string.gmatch(rpm.expand("%**"), "%S+") do
-- p = string.gsub(p, '"(%S+)"', '%1')
-- p = string.gsub(p, "'(%S+)'", '%1')
table.insert(parameters, p)
end
local function notsubpath(path)
if not string.match(path, "^%./") then
path = "./" .. path
end
return "\! -regex '" .. path .. "'"
end
local function regexfilter(regex)
return "\! -regex '" .. regex .. "'"
end
local function rootfilter(root)
return notsubpath(root .. "/.*")
end
local function dirfilter(dir)
return notsubpath(dir .. "/[^/]*")
end
local argparse = require "argparse"
local parser = argparse()
parser:option ("-r --regex")
:count ("*")
:args ("1")
:convert (regexfilter)
parser:option ("-R --root")
:count ("*")
:args ("1")
:convert (rootfilter)
parser:argument ("dir")
:args ("*")
:convert (dirfilter)
local args = parser:parse(parameters)
local gofindtestfilter = rpm.expand("%{?gofindtestfilter}")
gofindtestfilter = gofindtestfilter .. " -regextype egrep " ..
table.concat(args.regex, " ") .. " " ..
table.concat(args.root, " ") .. " " ..
table.concat(args.dir, " ")
rpm.define("checkfilter " .. gofindtestfilter)
}
# The following relies on things like environment variable and other macros
# easier to use in shell
%gobuildroot
export PATH=$PATH:%{buildroot}%{_bindir}
find . %{checkfilter} -exec dirname '\{\}' \\;|sort|uniq|while read sub ; do
set +x
sub="${sub#./}"
echo "Testing ${sub}…"
pushd "%{gobuildpath}/src/%{goipath}/${sub}" >/dev/null
set -x
%{gotest}
set +x
popd >/dev/null
done
}