189cced
#! /bin/sh
189cced
d22c0cb
# Fix multilib issue for header files.
657cae1
# Copyright (C) 2015-2016 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.
1a9b5b6
1a9b5b6
1a9b5b6
# Replace the multilib-unclean file with multilib-clean stub, while the
d22c0cb
# original file is moved to unique architecture-specific location.
189cced
657cae1
@LIB@
189cced
189cced
opt_buildroot=$(pwd)
9f438cf
opt_field_separator=-
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
{
f2fed85
    # The '#else' branch here is not needed!  We never install this header on
f2fed85
    # systems where this set of '#ifdef's is not enough (adding suggested e.g.
f2fed85
    # in rhbz#1242873).
f2fed85
01d964b
    # TODO: Shorten the #ifdef hell.  There's no need to have e.g. ifdef for
01d964b
    # x86_64 in ppc64's wrapper.
01d964b
9f438cf
    replacement=$filename$opt_field_separator
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
 *
5d02c91
 * We pay attention to include _only_ the original multilib-unclean
5d02c91
 * header file.  Including any other system-header file could cause
5d02c91
 * unpredictable include-ordering issues (rhbz#1412274, comment #16).
5d02c91
 *
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__)
9f438cf
#include "${replacement}x86_64.h"
189cced
#elif defined(__i386__)
9f438cf
#include "${replacement}i386.h"
189cced
#elif defined(__ppc64__) || defined(__powerpc64__)
9f438cf
#include "${replacement}ppc64.h"
189cced
#elif defined(__ppc__) || defined(__powerpc__)
9f438cf
#include "${replacement}ppc.h"
189cced
#elif defined(__s390x__)
9f438cf
#include "${replacement}s390x.h"
189cced
#elif defined(__s390__)
9f438cf
#include "${replacement}s390.h"
189cced
#elif defined(__sparc__) && defined(__arch64__)
9f438cf
#include "${replacement}sparc64.h"
189cced
#elif defined(__sparc__)
9f438cf
#include "${replacement}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
9f438cf
--field-separator   by default we move filename.h to filename<SEP><ARCH>.h; this
aeb305b
                option allows you to override the <SEP> part
189cced
--verbose       print some useful information
657cae1
--arch          override arch detection (mostly for testing purposes)
189cced
--help          show this help
189cced
EOF
189cced
189cced
    $_h_exit && exit "$1"
189cced
}
189cced
189cced
while test $# -gt 0
189cced
do
189cced
    _opt=$1 ; shift
189cced
    case $_opt in
9f438cf
        --buildroot|--arch|--file|--field-separator)
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
657cae1
fix_arch opt_arch
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)
9f438cf
multilib_file="$destdir/$filename$opt_field_separator$opt_arch$suffix"
189cced
189cced
test -f "$original_file" || die "can't find '$original_file'"
189cced
657cae1
is_multilib "$opt_arch"  || {
657cae1
  verbose "we don't need multilib haeder hack for '$opt_arch' architecture (no-op)"
657cae1
  exit 0
657cae1
}
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
: