From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 0A9A5138262 for ; Sat, 21 May 2016 23:20:09 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 879F214203; Sat, 21 May 2016 23:19:59 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6FBF2234003 for ; Sat, 21 May 2016 23:19:58 +0000 (UTC) Received: from localhost (cpc92302-cmbg19-2-0-cust189.5-4.cable.virginm.net [82.1.208.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: aidecoe) by smtp.gentoo.org (Postfix) with ESMTPSA id ABB23340C9A; Sat, 21 May 2016 23:19:56 +0000 (UTC) From: aidecoe@gentoo.org To: gentoo-dev@lists.gentoo.org Cc: =?UTF-8?q?Amadeusz=20=C5=BBo=C5=82nowski?= Subject: [gentoo-dev] [PATCH] rebar.eclass: Build Erlang/OTP projects using dev-util/rebar Date: Sun, 22 May 2016 00:19:49 +0100 Message-Id: <1463872789-18083-1-git-send-email-aidecoe@gentoo.org> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1463610918-4062-1-git-send-email-aidecoe@gentoo.org> References: <1463610918-4062-1-git-send-email-aidecoe@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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: d48f5ab9-b91c-4bd9-b49a-4ec9f7ef3acd X-Archives-Hash: f0e9d057f115d64f74d42ee623ea5c13 From: Amadeusz Żołnowski It is an eclass providing functions to build Erlang/OTP projects using dev-util/rebar. All packages in upcoming category dev-erlang are going to use this eclass. --- eclass/rebar.eclass | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 eclass/rebar.eclass diff --git a/eclass/rebar.eclass b/eclass/rebar.eclass new file mode 100644 index 0000000..1a4eaba --- /dev/null +++ b/eclass/rebar.eclass @@ -0,0 +1,223 @@ +# Copyright 1999-2016 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id$ + +# @ECLASS: rebar.eclass +# @MAINTAINER: +# Amadeusz Żołnowski +# @AUTHOR: +# Amadeusz Żołnowski +# @BLURB: Build Erlang/OTP projects using dev-util/rebar. +# @DESCRIPTION: +# An eclass providing functions to build Erlang/OTP projects using +# dev-util/rebar. +# +# rebar is a tool which tries to resolve dependencies itself which is by +# cloning remote git repositories. Dependant projects are usually expected to +# be in sub-directory 'deps' rather than looking at system Erlang lib +# directory. Projects relying on rebar usually don't have 'install' make +# targets. The eclass workarounds some of these problems. It handles +# installation in a generic way for Erlang/OTP structured projects. + +case "${EAPI:-0}" in + 0|1|2|3|4|5) + die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}" + ;; + 6) + ;; + *) + die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}" + ;; +esac + +EXPORT_FUNCTIONS src_prepare src_compile src_install + +RDEPEND="dev-lang/erlang" +DEPEND="${RDEPEND} + dev-util/rebar + >=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: +# @RETURN: full path with EPREFIX to a Erlang package/project on success, +# code 1 when dependency is not found and code 2 if multiple versions of +# dependency are 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: erebar +# @USAGE: +# @DESCRIPTION: +# Run rebar with verbose flag. Die on failure. +erebar() { + debug-print-function ${FUNCNAME} "${@}" + + (( $# > 0 )) || die "erebar: at least one target is required" + + local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)" + rebar -v skip_deps=true "$@" || die "rebar $@ failed" +} + +# @FUNCTION: rebar_fix_include_path +# @USAGE: +# @DESCRIPTION: +# Fix path in rebar.config to 'include' directory of dependant project/package, +# so it points to installation in system Erlang lib rather than relative 'deps' +# directory. +# +# The function dies on failure. +rebar_fix_include_path() { + debug-print-function ${FUNCNAME} "${@}" + + local pn="$1" + 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 +# @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. +# +# The function dies on failure. +rebar_remove_deps() { + debug-print-function ${FUNCNAME} "${@}" + + 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 in compiling dependencies. Set version in project +# description file if it's not set. +# +# Existence of rebar.config is optional, but file description file must exist +# at 'src/${PN}.app.src'. +rebar_src_prepare() { + debug-print-function ${FUNCNAME} "${@}" + + default + rebar_set_vsn + [[ -f rebar.config ]] && rebar_remove_deps +} + +# @FUNCTION: rebar_src_configure +# @DESCRIPTION: +# Configure with ERL_LIBS set. +rebar_src_configure() { + debug-print-function ${FUNCNAME} "${@}" + + local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)" + default +} + +# @FUNCTION: rebar_src_compile +# @DESCRIPTION: +# Compile project with rebar. +rebar_src_compile() { + debug-print-function ${FUNCNAME} "${@}" + + erebar compile +} + +# @FUNCTION: rebar_src_install +# @DESCRIPTION: +# Install BEAM files, include headers, executables and native libraries. +# Install standard docs like README or defined in DOCS variable. +# +# Function expects that project conforms to Erlang/OTP structure. +rebar_src_install() { + debug-print-function ${FUNCNAME} "${@}" + + local bin + local dest="$(get_erl_libs)/${P}" + + insinto "${dest}" + doins -r ebin + [[ -d include ]] && doins -r include + [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done + + if [[ -d priv ]]; then + cp -pR priv "${ED}${dest}/" || die "failed to install priv/" + fi + + einstalldocs +} -- 2.8.2