• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -e
2#
3# Copyright (c) 2012 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# This script is used to unpack a .a file into object files.
8#
9# Arguments:
10#
11# d - Output directory.
12# a - List of possible locations of the archive.
13# f - List of files to extract.
14#
15
16export LC_ALL=C
17
18# Avoid things like -n messing up the grepping below.
19unset GREP_OPTIONS
20
21while getopts "d:a:f:r:" flag
22do
23  if [ "$flag" = "d" ]; then
24    out_dir=$OPTARG
25  elif [ "$flag" = "a" ]; then
26    lib_files="$OPTARG $lib_files"
27  elif [ "$flag" = "f" ]; then
28    obj_files="$OPTARG $obj_files"
29  elif [ "$flag" = "r" ]; then
30    ar=$OPTARG
31  fi
32done
33
34for f in $lib_files; do
35  if [ -a $f ]; then
36    lib_file=$f
37    break
38  fi
39done
40
41if [ -z "$lib_file" ]; then
42  echo "Failed to locate a static library."
43  false
44  exit
45fi
46
47if [ ! -f "$ar" ]; then
48  # Find the appropriate ar to use.
49  ar="ar"
50  if [ -n "$AR_target" ]; then
51    ar=$AR_target
52  elif [ -n "$AR" ]; then
53    ar=$AR
54  fi
55fi
56
57obj_list="$($ar t $lib_file | grep '\.o$')"
58
59function extract_object {
60  for f in $obj_list; do
61    filename="${f##*/}"
62
63    if [ -z "$(echo $filename | grep $1)" ]; then
64      continue
65    fi
66
67    # Only echo this if debugging.
68    # echo "Extract $filename from archive to $out_dir/$1."
69    $ar p $lib_file $filename > $out_dir/$1
70    [ -s $out_dir/$1 ] || exit 1
71    break
72  done
73}
74
75for f in $obj_files; do
76  extract_object $f
77done
78