From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 189531581C1 for ; Tue, 16 Jul 2024 09:36:57 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 16BB22BC0E7; Tue, 16 Jul 2024 09:36:00 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id AA2232BC0E1 for ; Tue, 16 Jul 2024 09:35:59 +0000 (UTC) From: Florian Schmaus To: gentoo-dev@lists.gentoo.org Cc: Anna Vyalkova , Matthew Smith , Florian Schmaus Subject: [gentoo-dev] [PATCH 1/2] rebar.eclass: factor out common functions into rebar-utils.eclass Date: Tue, 16 Jul 2024 11:35:30 +0200 Message-ID: <20240716093532.349861-2-flow@gentoo.org> X-Mailer: git-send-email 2.44.2 In-Reply-To: <20240716093532.349861-1-flow@gentoo.org> References: <20240716093532.349861-1-flow@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: 19faadfd-57d5-4c6b-bff9-06388e553817 X-Archives-Hash: 022a861db10ac7edae5730187a7b7888 In preperation for rebar3.eclass, factor out common functions into rebar-utils.eclass. Signed-off-by: Florian Schmaus --- eclass/rebar-utils.eclass | 160 ++++++++++++++++++++++++++++++++++++++ eclass/rebar.eclass | 112 +------------------------- 2 files changed, 163 insertions(+), 109 deletions(-) create mode 100644 eclass/rebar-utils.eclass diff --git a/eclass/rebar-utils.eclass b/eclass/rebar-utils.eclass new file mode 100644 index 000000000000..5657908eb8e9 --- /dev/null +++ b/eclass/rebar-utils.eclass @@ -0,0 +1,160 @@ +# Copyright 1999-2024 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# @ECLASS: rebar-utils.eclass +# @MAINTAINER: +# Florian Schmaus +# @AUTHOR: +# Amadeusz Żołnowski +# @SUPPORTED_EAPIS: 7 8 +# @BLURB: Auxiliary functions for using dev-util/rebar. +# @DESCRIPTION: +# This eclass provides a set of axiliary functions commonly needed +# when building Erlang/OTP packages with rebar. + +case ${EAPI} in + 7|8) ;; + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; +esac + +if [[ -z ${_REBAR_UTILS_ECLASS} ]]; then +_REBAR_UTILS_ECLASS=1 + +# @ECLASS_VARIABLE: REBAR_APP_SRC +# @DESCRIPTION: +# Relative path to .app.src description file. Defaults to +# 'src/${PN}.app.src'. +: "${REBAR_APP_SRC:=src/${PN}.app.src}" + +# @FUNCTION: get_erl_libs +# @RETURN: the path to Erlang lib directory +# @DESCRIPTION: +# Get the full path without EPREFIX to Erlang lib directory. +get_erl_libs() { + echo "/usr/$(get_libdir)/erlang/lib" +} + +# @FUNCTION: _rebar_find_dep +# @INTERNAL +# @USAGE: +# @RETURN: 0 success, 1 dependency not found, 2 multiple versions found +# @DESCRIPTION: +# Find a Erlang package/project by name in Erlang lib directory. Project +# directory is usually suffixed with version. It is matched to '' +# or '-*'. +_rebar_find_dep() { + local pn="${1}" + local p + local result + + pushd "${EPREFIX}$(get_erl_libs)" >/dev/null || return 1 + for p in ${pn} ${pn}-*; do + if [[ -d ${p} ]]; then + # Ensure there's at most one matching. + [[ ${result} ]] && return 2 + result="${p}" + fi + done + popd >/dev/null || die + + [[ ${result} ]] || return 1 + echo "${result}" +} + +# @FUNCTION: rebar_disable_coverage +# @USAGE: [] +# @DESCRIPTION: +# Disable coverage in rebar.config. This is a workaround for failing coverage. +# Coverage is not relevant in this context, so there's no harm to disable it, +# although the issue should be fixed. +rebar_disable_coverage() { + debug-print-function ${FUNCNAME} "${@}" + + local rebar_config="${1:-rebar.config}" + + sed -e 's/{cover_enabled, true}/{cover_enabled, false}/' \ + -i "${rebar_config}" \ + || die "failed to disable coverage in ${rebar_config}" +} + +# @FUNCTION: rebar_fix_include_path +# @USAGE: [] +# @DESCRIPTION: +# Fix path in rebar.config to 'include' directory of dependent project/package, +# so it points to installation in system Erlang lib rather than relative 'deps' +# directory. +# +# is optional. Default is 'rebar.config'. +# +# The function dies on failure. +rebar_fix_include_path() { + debug-print-function ${FUNCNAME} "${@}" + + local pn="${1}" + local rebar_config="${2:-rebar.config}" + local erl_libs="${EPREFIX}$(get_erl_libs)" + local p + + p="$(_rebar_find_dep "${pn}")" \ + || die "failed to unambiguously resolve dependency of '${pn}'" + + gawk -i inplace \ + -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" ' +/^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ { + pattern = "\"(./)?deps/" pn "/include\""; + if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) { + sub(pattern, "\"" erl_libs "/" p "/include\""); + } + print $0; + next; +} +1 +' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'" +} + +# @FUNCTION: rebar_remove_deps +# @USAGE: [] +# @DESCRIPTION: +# Remove dependencies list from rebar.config and deceive build rules that any +# dependencies are already fetched and built. Otherwise rebar tries to fetch +# dependencies and compile them. +# +# is optional. Default is 'rebar.config'. +# +# The function dies on failure. +rebar_remove_deps() { + debug-print-function ${FUNCNAME} "${@}" + + local rebar_config="${1:-rebar.config}" + + mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die + gawk -i inplace ' +/^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ { + if ($0 ~ /}[[:space:]]*\.$/) { + print "{deps, []}."; + } + next; +} +1 +' "${rebar_config}" || die "failed to remove deps from ${rebar_config}" +} + +# @FUNCTION: rebar_set_vsn +# @USAGE: [] +# @DESCRIPTION: +# Set version in project description file if it's not set. +# +# is optional. Default is PV stripped from version suffix. +# +# The function dies on failure. +rebar_set_vsn() { + debug-print-function ${FUNCNAME} "${@}" + + local version="${1:-${PV%_*}}" + + sed -e "s/vsn, git/vsn, \"${version}\"/" \ + -i "${S}/${REBAR_APP_SRC}" \ + || die "failed to set version in src/${PN}.app.src" +} + +fi diff --git a/eclass/rebar.eclass b/eclass/rebar.eclass index 97638c761814..1e4e845a9adb 100644 --- a/eclass/rebar.eclass +++ b/eclass/rebar.eclass @@ -7,6 +7,7 @@ # @AUTHOR: # Amadeusz Żołnowski # @SUPPORTED_EAPIS: 7 8 +# @PROVIDES: rebar-utils # @BLURB: Build Erlang/OTP projects using dev-util/rebar. # @DESCRIPTION: # An eclass providing functions to build Erlang/OTP projects using @@ -27,6 +28,8 @@ esac if [[ -z ${_REBAR_ECLASS} ]]; then _REBAR_ECLASS=1 +inherit rebar-utils + RDEPEND="dev-lang/erlang:=" DEPEND="${RDEPEND}" BDEPEND=" @@ -34,19 +37,6 @@ BDEPEND=" >=sys-apps/gawk-4.1 " -# @ECLASS_VARIABLE: REBAR_APP_SRC -# @DESCRIPTION: -# Relative path to .app.src description file. -REBAR_APP_SRC="${REBAR_APP_SRC-src/${PN}.app.src}" - -# @FUNCTION: get_erl_libs -# @RETURN: the path to Erlang lib directory -# @DESCRIPTION: -# Get the full path without EPREFIX to Erlang lib directory. -get_erl_libs() { - echo "/usr/$(get_libdir)/erlang/lib" -} - # @FUNCTION: _rebar_find_dep # @INTERNAL # @USAGE: @@ -74,22 +64,6 @@ _rebar_find_dep() { echo "${result}" } -# @FUNCTION: rebar_disable_coverage -# @USAGE: [] -# @DESCRIPTION: -# Disable coverage in rebar.config. This is a workaround for failing coverage. -# Coverage is not relevant in this context, so there's no harm to disable it, -# although the issue should be fixed. -rebar_disable_coverage() { - debug-print-function ${FUNCNAME} "${@}" - - local rebar_config="${1:-rebar.config}" - - sed -e 's/{cover_enabled, true}/{cover_enabled, false}/' \ - -i "${rebar_config}" \ - || die "failed to disable coverage in ${rebar_config}" -} - # @FUNCTION: erebar # @USAGE: # @DESCRIPTION: @@ -105,86 +79,6 @@ erebar() { rebar -v skip_deps=true "$@" || die -n "rebar $@ failed" } -# @FUNCTION: rebar_fix_include_path -# @USAGE: [] -# @DESCRIPTION: -# Fix path in rebar.config to 'include' directory of dependent project/package, -# so it points to installation in system Erlang lib rather than relative 'deps' -# directory. -# -# is optional. Default is 'rebar.config'. -# -# The function dies on failure. -rebar_fix_include_path() { - debug-print-function ${FUNCNAME} "${@}" - - local pn="${1}" - local rebar_config="${2:-rebar.config}" - local erl_libs="${EPREFIX}$(get_erl_libs)" - local p - - p="$(_rebar_find_dep "${pn}")" \ - || die "failed to unambiguously resolve dependency of '${pn}'" - - gawk -i inplace \ - -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" ' -/^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ { - pattern = "\"(./)?deps/" pn "/include\""; - if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) { - sub(pattern, "\"" erl_libs "/" p "/include\""); - } - print $0; - next; -} -1 -' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'" -} - -# @FUNCTION: rebar_remove_deps -# @USAGE: [] -# @DESCRIPTION: -# Remove dependencies list from rebar.config and deceive build rules that any -# dependencies are already fetched and built. Otherwise rebar tries to fetch -# dependencies and compile them. -# -# is optional. Default is 'rebar.config'. -# -# The function dies on failure. -rebar_remove_deps() { - debug-print-function ${FUNCNAME} "${@}" - - local rebar_config="${1:-rebar.config}" - - mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die - gawk -i inplace ' -/^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ { - if ($0 ~ /}[[:space:]]*\.$/) { - print "{deps, []}."; - } - next; -} -1 -' "${rebar_config}" || die "failed to remove deps from ${rebar_config}" -} - -# @FUNCTION: rebar_set_vsn -# @USAGE: [] -# @DESCRIPTION: -# Set version in project description file if it's not set. -# -# is optional. Default is PV stripped from version suffix. -# -# The function dies on failure. -rebar_set_vsn() { - debug-print-function ${FUNCNAME} "${@}" - - local version="${1:-${PV%_*}}" - - sed -e "s/vsn, git/vsn, \"${version}\"/" \ - -i "${S}/${REBAR_APP_SRC}" \ - || die "failed to set version in src/${PN}.app.src" -} - # @FUNCTION: rebar_src_prepare # @DESCRIPTION: # Prevent rebar from fetching and compiling dependencies. Set version in -- 2.44.2