1#!/bin/sh 2 3# 4# Copyright (C) 2009 Google Inc. All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are 8# met: 9# 10# * Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# * Redistributions in binary form must reproduce the above 13# copyright notice, this list of conditions and the following disclaimer 14# in the documentation and/or other materials provided with the 15# distribution. 16# * Neither the name of Google Inc. nor the names of its 17# contributors may be used to endorse or promote products derived from 18# this software without specific prior written permission. 19# 20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31# 32 33# usage: adjust_visibility.sh INPUT OUTPUT WORK_DIR 34# 35# Transforms a static library at INPUT by marking all of its symbols 36# private_extern. The output is placed at OUTPUT. WORK_DIR is used as a 37# scratch directory, which need not exist before this script is invoked, 38# and which will be left behind when the script exits. 39 40set -e 41 42if [ $# -ne 3 ] ; then 43 echo "usage: ${0} INPUT OUTPUT WORK_DIR" >& 2 44 exit 1 45fi 46 47INPUT="${1}" 48OUTPUT="${2}" 49WORK_DIR="${3}" 50 51# Start with a clean slate. 52rm -f "${OUTPUT}" 53rm -rf "${WORK_DIR}" 54mkdir -p "${WORK_DIR}" 55 56# ar doesn't operate on fat files. Figure out what architectures are 57# involved. 58ARCHS=$(file "${INPUT}" | sed -Ene 's/^.*\(for architecture (.+)\):.*$/\1/p') 59if [ -z "${ARCHS}" ] ; then 60 ARCHS=self 61fi 62 63OUTPUT_NAME="output.a" 64 65for ARCH in ${ARCHS} ; do 66 # Get a thin version of fat input by running lipo. If the input is already 67 # thin, just copy it into place. The extra copy isn't strictly necessary 68 # but it simplifies the script. 69 ARCH_DIR="${WORK_DIR}/${ARCH}" 70 mkdir -p "${ARCH_DIR}" 71 INPUT_NAME=input.a 72 ARCH_INPUT="${ARCH_DIR}/${INPUT_NAME}" 73 if [ "${ARCHS}" = "self" ] ; then 74 cp "${INPUT}" "${ARCH_INPUT}" 75 else 76 lipo -thin "${ARCH}" "${INPUT}" -output "${ARCH_INPUT}" 77 fi 78 79 # Change directories to extract the archive to ensure correct pathnames. 80 (cd "${ARCH_DIR}" && ar -x "${INPUT_NAME}") 81 82 # Use ld -r to relink each object that was in the archive. Providing an 83 # empty -exported_symbols_list will transform all symbols to private_extern; 84 # these symbols are retained with -keep_private_externs. 85 for OBJECT in "${ARCH_DIR}/"*.o ; do 86 NEW_OBJECT="${OBJECT}.new" 87 ld -o "${NEW_OBJECT}" -r "${OBJECT}" \ 88 -exported_symbols_list /dev/null -keep_private_externs 89 mv "${NEW_OBJECT}" "${OBJECT}" 90 done 91 92 # Build an architecture-specific archive from the modified object files. 93 ARCH_OUTPUT="${ARCH_DIR}/${OUTPUT_NAME}" 94 (cd "${ARCH_DIR}" && ar -rc "${OUTPUT_NAME}" *.o) 95 ranlib "${ARCH_OUTPUT}" 96 97 # Toss the object files out now that they're in the archive. 98 rm -f "${ARCH_DIR}/"*.o 99done 100 101# Create a fat archive from the architecture-specific archives if needed. 102# If the input was thin, leave the output thin by copying the only output 103# archive to the destination. 104if [ "${ARCHS}" = "self" ] ; then 105 cp "${WORK_DIR}/self/${OUTPUT_NAME}" "${OUTPUT}" 106else 107 lipo -create -output "${OUTPUT}" "${WORK_DIR}/"*"/${OUTPUT_NAME}" 108fi 109