4ba658f
#!/bin/bash
4ba658f
f22d4de
# Copyright (c) 2015-2016 Sandro Mani <manisandro@gmail.com>
4ba658f
#
4ba658f
# Permission is hereby granted, free of charge, to any person obtaining a copy
4ba658f
# of this software and associated documentation files (the "Software"), to deal
4ba658f
# in the Software without restriction, including without limitation the rights
4ba658f
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
4ba658f
# copies of the Software, and to permit persons to whom the Software is
4ba658f
# furnished to do so, subject to the following conditions:
4ba658f
#
4ba658f
# The above copyright notice and this permission notice shall be included in
4ba658f
# all copies or substantial portions of the Software.
4ba658f
#
4ba658f
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4ba658f
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4ba658f
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4ba658f
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4ba658f
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
4ba658f
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
4ba658f
# THE SOFTWARE
4ba658f
4ba658f
a945791
buildroot="$RPM_BUILD_ROOT"
4ba658f
if [ ! -d "$buildroot" ]; then
4ba658f
  >&2 echo "Invalid buildroot"
4ba658f
  exit 1
4ba658f
fi
4ba658f
6cb8095
# Remaining arguments are passed to elfdeps
6cb8095
shift
6cb8095
elfdepsargs="$@"
4ba658f
4ba658f
# Search for all MPI implementations:
4ba658f
# - Search all modules which start with mpi
5ad6ade
# - For each module, store $MPI_DIR -> $MPI_COMPILER in assoc. array
debf66a
declare -A mpi_compiler_for_dir
4ba658f
if [ -e /etc/profile.d/modules.sh ]; then
4ba658f
  . /etc/profile.d/modules.sh;
4ba658f
4ba658f
  # Add moduledirs in buildroot also for cases where an MPI implementation
4ba658f
  # package is being built, meaning that its module files are not yet installed
4ba658f
  IFS=':' read -ra _MODULEPATH <<< "$MODULEPATH"
4ba658f
  for i in "${!_MODULEPATH[@]}"; do
4ba658f
    _MODULEPATH[$i]=$buildroot${_MODULEPATH[$i]}
4ba658f
  done
4ba658f
  export MODULEPATH="$MODULEPATH:$(IFS=:; echo "${_MODULEPATH[*]}")"
4ba658f
4ba658f
  for module in $(module -t avail 2>&1 | grep "^mpi/"); do
4ba658f
    module load $module
debf66a
    mpi_compiler_for_dir[${MPI_HOME}]=${MPI_COMPILER}
debf66a
    mpi_compiler_for_dir[${MPI_FORTRAN_MOD_DIR}]=${MPI_COMPILER}
ed9755f
    mpi_compiler_for_dir[${MPI_PYTHON2_SITEARCH}]=${MPI_COMPILER}
ed9755f
    mpi_compiler_for_dir[${MPI_PYTHON3_SITEARCH}]=${MPI_COMPILER}
4ba658f
    module unload $module
4ba658f
  done
4ba658f
fi
4ba658f
4ba658f
# Read file list from stdin
4ba658f
filelist=$(sed "s/['\"]/\\\&/g")
4ba658f
4ba658f
# List provides:
4ba658f
if [ -x /usr/lib/rpm/elfdeps -a -n "$filelist" ]; then
4ba658f
  for file in $(echo $filelist | tr '[:blank:]' \\n); do
4ba658f
4ba658f
    # Get the default provides string from elfdeps
6cb8095
    prov=$(echo $file | /usr/lib/rpm/elfdeps --provides $elfdepsargs)
f22d4de
    if [ -z "$prov" ]; then
4ba658f
      continue
4ba658f
    fi
4ba658f
5ad6ade
    # If the path to the scanned binary starts with a known mpi binary directory,
5ad6ade
    # append the corresponding ($MPI_COMPILER) to the provides string
debf66a
    for mpi_dir in "${!mpi_compiler_for_dir[@]}"; do
5ad6ade
      if [[ "$file" == "$buildroot$mpi_dir"* ]]; then
debf66a
        prov="${prov}(${mpi_compiler_for_dir[$mpi_dir]})"
4ba658f
        break
4ba658f
      fi
4ba658f
    done
4ba658f
4ba658f
    echo "$prov"
4ba658f
  done
4ba658f
fi