189cced
#! /bin/sh
189cced
d22c0cb
# Fix multilib issue for header files.
d22c0cb
# Copyright (C) 2015 Red Hat, Inc.
d22c0cb
# Written by Pavel Raiskup <praiskup@redhat.com>
189cced
#
d22c0cb
# This program is free software; you can redistribute it and/or modify
d22c0cb
# it under the terms of the GNU General Public License as published by
d22c0cb
# the Free Software Foundation; either version 2 of the License, or
d22c0cb
# (at your option) any later version.
189cced
#
d22c0cb
# This program is distributed in the hope that it will be useful,
d22c0cb
# but WITHOUT ANY WARRANTY; without even the implied warranty of
d22c0cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d22c0cb
# GNU General Public License for more details.
189cced
#
d22c0cb
# You should have received a copy of the GNU General Public License along
d22c0cb
# with this program; if not, write to the Free Software Foundation, Inc.,
d22c0cb
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
d22c0cb
# Replace the multilib-unclean header file with multilib-clean stub, while the
d22c0cb
# original file is moved to unique architecture-specific location.
189cced
189cced
progname=$(basename "$0")
189cced
189cced
opt_arch=$(uname -i)
189cced
# See rhbz#1242873 for more info.
189cced
test "$opt_arch" = ppc64p7 && opt_arch=ppc64
189cced
189cced
opt_buildroot=$(pwd)
189cced
opt_verbose=:
189cced
opt_additional_suffix=
189cced
189cced
# TODO: we could pretty easily implement other then 'cpp-header' stubs, if the
189cced
# target file type allows some kind of "transparent" file inclusion.  For
ccd34dc
# example shell scripts might use '. "${destdir}/${filename}_x86_64.sh'.
d22c0cb
# The solution is taken from Fedora PostgreSQL RPM package.
189cced
print_stub ()
189cced
{
189cced
cat <
189cced
/*
189cced
 * Kluge to support multilib installation of both 32- and 64-bit RPMS:
189cced
 * we need to arrange that header files that appear in both RPMs are
189cced
 * identical.  Hence, this file is architecture-independent and calls
189cced
 * in an arch-dependent file that will appear in just one RPM.
189cced
 *
189cced
 * To avoid breaking arches not explicitly supported by Red Hat, we
189cced
 * use this indirection file *only* on known multilib arches.
189cced
 *
189cced
 * Note: this may well fail if user tries to use gcc's -I- option.
189cced
 * But that option is deprecated anyway.
189cced
 */
189cced
#if defined(__x86_64__)
ccd34dc
#include "${filename}${opt_additional_suffix}_x86_64.h"
189cced
#elif defined(__i386__)
ccd34dc
#include "${filename}${opt_additional_suffix}_i386.h"
189cced
#elif defined(__ppc64__) || defined(__powerpc64__)
ccd34dc
#include "${filename}${opt_additional_suffix}_ppc64.h"
189cced
#elif defined(__ppc__) || defined(__powerpc__)
ccd34dc
#include "${filename}${opt_additional_suffix}_ppc.h"
189cced
#elif defined(__s390x__)
ccd34dc
#include "${filename}${opt_additional_suffix}_s390x.h"
189cced
#elif defined(__s390__)
ccd34dc
#include "${filename}${opt_additional_suffix}_s390.h"
189cced
#elif defined(__sparc__) && defined(__arch64__)
ccd34dc
#include "${filename}${opt_additional_suffix}_sparc64.h"
189cced
#elif defined(__sparc__)
ccd34dc
#include "${filename}${opt_additional_suffix}_sparc.h"
189cced
#endif
189cced
EOF
189cced
}
189cced
189cced
print_help ()
189cced
{
189cced
    _h_exit=false
189cced
    test -n "$1" && _h_exit=:
189cced
189cced
    cat <
189cced
Usage: $progname [OPTIONS]
189cced
189cced
Replace the multilib-unclean header file with multilib-clean stub, while the
189cced
original file is moved to unique architecture-specific location.  This should be
189cced
absolutely safe operation (effects of '#include <HEADER.h>' are unchanged.
189cced
189cced
To allow us to do incompatible changes in this script, packagers should use this
ccd34dc
script only through available RPM macros.
189cced
189cced
--buildroot     prefix (directory where we play with installed files, usually
189cced
                after 'make install DESTDIR=buildroot')
ccd34dc
--file          for example /some/path/test.h
189cced
--additional-suffix     we usually move 'test.h' to 'test_\$ARCH.h'.  However
ccd34dc
                this file could already exist.  With this option the multilib
189cced
                file will be named 'test_\$ARCH\$SUFFIX.h'
189cced
--verbose       print some useful information
189cced
--help          show this help
189cced
EOF
189cced
189cced
    $_h_exit && exit "$1"
189cced
}
189cced
189cced
verbose ()
189cced
{
189cced
    $opt_verbose && echo "INFO: $progname: $*"
189cced
}
189cced
189cced
die ()
189cced
{
189cced
    echo >&2 " # $*"
189cced
    print_help 1
189cced
}
189cced
189cced
error ()
189cced
{
189cced
    error_occurred=:
189cced
    echo >&2 " ! $*"
189cced
}
189cced
189cced
error_occurred=false
189cced
189cced
while test $# -gt 0
189cced
do
189cced
    _opt=$1 ; shift
189cced
    case $_opt in
ccd34dc
        --buildroot|--arch|--additional-suffix|--file)
ccd34dc
            _raw_opt=$(echo "$_opt" | sed -e 's/^--//' -e 's/-/_/g')
ccd34dc
            eval "opt_$_raw_opt=\$1"
ccd34dc
            shift || die "$_opt requires argument"
189cced
            ;;
189cced
        --help)
189cced
            print_help 0
189cced
            ;;
189cced
        *)
189cced
            error "unexpected '$_opt' program argument"
189cced
            ;;
189cced
    esac
189cced
done
189cced
$error_occurred && print_help 1
189cced
ccd34dc
for i in arch buildroot file
189cced
do
189cced
    eval "test -z \"\$opt_$i\"" && error "--$i needs to be set"
189cced
done
189cced
$error_occurred && print_help 1
189cced
ccd34dc
# --> /buildroot/usr/include/test.h
ccd34dc
original_file="$opt_buildroot$opt_file"
ccd34dc
ccd34dc
# --> /buildroot/usr/include
ccd34dc
destdir=$(dirname "$original_file")
ccd34dc
ccd34dc
# --> test.h
ccd34dc
orig_basename=$(basename "$original_file")
ccd34dc
ccd34dc
# --> test
ccd34dc
filename=${orig_basename%%.[a-zA-Z0-9_]}
ccd34dc
ccd34dc
# --> .h
ccd34dc
suffix=${orig_basename##${filename}}
ccd34dc
ccd34dc
# --> ../test_x86_64.h (on x86_64)
ccd34dc
multilib_file="$destdir/$filename${opt_additional_suffix}_$opt_arch$suffix"
189cced
189cced
test -f "$original_file" || die "can't find '$original_file'"
189cced
189cced
case $opt_arch in
189cced
  # we only apply this to known Red Hat multilib arches, per bug #177564
189cced
  i386|x86_64|ppc|ppc64|s390|s390x|sparc|sparc64)
189cced
      ;;
189cced
  *)
189cced
      verbose "we don't need multilib haeder hack for '$opt_arch' architecture (no-op)"
189cced
      exit 0
189cced
      ;;
189cced
esac
189cced
189cced
verbose "moving: '$original_file' to '$multilib_file'"
189cced
189cced
mv "$original_file" "$multilib_file" || exit 1
189cced
if print_stub > "$original_file" && chmod 644 "$original_file"; then
189cced
    :
189cced
else
189cced
    die "can't write into '$original_file'"
189cced
fi
189cced
189cced
: