summaryrefslogtreecommitdiffstats
path: root/mkmultiarch
blob: a4085a20fc4c60e6bed64d0da1bc7f411daec40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
#---------------------------------------------------------------
# Project         : Mandriva Linux
# Module          : multiarch-utils
# File            : mkmultiarch
# Version         : $Id$
# Author          : Gwenole Beauchesne
# Created On      : Wed Jan 12 10:54:10 CET 2005
#---------------------------------------------------------------

usage="Usage: $0 <binaries|includes> <file1> [<file2> ...]"
mplat=`multiarch-platform`
type=$1
case $type in
    binaries|includes) shift;;
    *) echo $usage; exit 1;;
esac

function error() {
    echo ${1+"$@"} > /dev/stderr
    exit 1
}

# read link on one level only
function read_link_1() {
    perl -e 'print readlink(shift)' $1
}

function dispatch_binaries() {
    local file=$1
    local bindir=`dirname $file`
    local archbindir=$bindir/$mplat
    [[ -d $archbindir ]] || mkdir -p $archbindir
    if [[ -L $file ]]; then
	link=`read_link_1 $file`
	case $link in
	    /*)
		mv $file $archbindir/
		;;
	    ../*)
		ln -s ../$link $archbindir/${file##*/}
		rm -f $file
		;;
	esac
    elif [[ -f $file ]]; then
	mv $file $archbindir/
    else
	error "Unsupported file type for $file"
    fi
    ln -s /usr/bin/multiarch-dispatch $file
}

function dispatch_includes() {
    local file=$1
    local incdir=`dirname $file`

    # handle circular inclusions
    local tag=$incdir/.multiarch-processing.${file##*/}
    [[ -f "$tag" ]] && return
    touch $tag

    # sanity checks, extract path parts
    echo $file | grep -q '/include/' || error "Unsupported includedir $incdir"
    local prefix=`echo $incdir | sed -n '/\(.*\/include\)\/.*/s//\1/p'`
    [[ -z "$prefix" ]] && prefix="$incdir"
    local suffix=`echo $incdir | sed -n '/.*\/include\/\(.*\)/s//\1/p'`
    [[ -n "$suffix" ]] && suffix="$suffix/"

    # dispatch nested includes expected in local directory
    sed -n '/^#[ \t]*include[ \t]*"\([^"][^"]*\)".*/s//\1/p' $file | \
    while read localfile; do
	[[ -f "$incdir/$localfile" ]] && dispatch_includes $incdir/$localfile
    done

    # dispatch selected include file, provided it's not already dispatched
    grep -q _MULTIARCH_HEADER $file || {
	local archincdir=$prefix/$mplat/$suffix
	[[ -d $archincdir ]] || mkdir -p $archincdir
	mv $file $archincdir/
	cat > $file << EOF
#define _MULTIARCH_HEADER $suffix${file##*/}
#include <multiarch-dispatch.h>
EOF
    }

    # done with this file
    rm -f $tag
}

while [[ $# -gt 0 ]]; do
    file=$1
    shift 1
    [[ -f $file ]] || error "$file does not exist!"
    dispatch_$type $file
done