1#!/bin/sh
2
3##
4# Wrapper around ar which behaves more like ar.
5# Problem is Rhapsody's ar doesn't work on a file that's been ranlib'ed
6# and some makefiles want to edit ranlib'ed archives.
7#
8# The interesting and functional routine here in unranlib().
9# The "main" code, which wraps ar, is a hack and may not parse the
10# arguments correctly, but seems to work for most uses of ar, where
11# the library is argv[2].
12##
13# Wilfredo Sanchez Jr. | wsanchez@apple.com
14# Copyright 1998 Apple Computer, Inc.
15##
16
17##
18# Set up PATH
19##
20
21MyPath=/usr/bin:/bin;
22
23if [ -z "${PATH}" ]; then
24    export PATH=${MyPath};
25else
26    export PATH=${PATH}:${MyPath};
27fi
28
29##
30# Functions
31##
32
33unranlib ()
34{
35    local archive;
36
37    for archive in $*; do
38
39	local   name="$(basename ${archive})";
40	local    dir="/tmp/unranlib.$$/${name}";
41	local ofiles="";
42	local  archs="$(file ${archive}			| \
43			grep '(for architecture'	| \
44			awk '{print $4}'		| \
45			sed 's/)://')";
46
47	for arch in ${archs}; do
48	    local archdir="${dir}/${arch}";
49	    mkdir -p "${archdir}";
50
51	    lipo -thin "${arch}" "${archive}" -o "${archdir}/${name}";
52
53	    ( cd "${archdir}" && ar -xo "./${name}"; );
54
55	    local ofile;
56	    for ofile in `find "${archdir}" -name \*.o`; do
57		ofiles="${ofiles} $(basename ${ofile})";
58	    done
59
60	done
61
62	ofiles=$(echo ${ofiles} | tr ' ' '\012' | sort | uniq);
63
64	local ofile;
65	for ofile in ${ofiles}; do
66	    lipo -create $(find "${dir}" -name "${ofile}" -print) -o "${dir}/${ofile}";
67	done
68
69	( cd "${dir}" && ar -cr "${name}" ${ofiles}; );
70
71	mv "${dir}/${name}" "${archive}";
72
73	rm -rf "${dir}";
74
75    done
76
77    rm -rf "/tmp/unranlib.$$";
78}
79
80##
81# Handle command line
82##
83
84# This is totally bogus, but enough for now.
85archive=$2;
86
87if [ -f "${archive}" ] &&
88   file "${archive}" | grep 'Mach-O universal binary' > /dev/null; then
89
90    # File is fat. Undo ranlib.
91    unranlib "${archive}";
92fi
93
94ar $*;
95ranlib $2;
96