summaryrefslogtreecommitdiff
path: root/unpack_lib_posix.sh
blob: 701207feb591010129e8d038966787782a8c0229 (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
#!/bin/bash -e
#
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This script is used to unpack a .a file into object files.
#
# Arguments:
#
# d - Output directory.
# a - List of possible locations of the archive.
# f - List of files to extract.
#

export LC_ALL=C

# Avoid things like -n messing up the grepping below.
unset GREP_OPTIONS

while getopts "d:a:f:r:" flag
do
  if [ "$flag" = "d" ]; then
    out_dir=$OPTARG
  elif [ "$flag" = "a" ]; then
    lib_files="$OPTARG $lib_files"
  elif [ "$flag" = "f" ]; then
    obj_files="$OPTARG $obj_files"
  elif [ "$flag" = "r" ]; then
    ar=$OPTARG
  fi
done

for f in $lib_files; do
  if [ -a $f ]; then
    lib_file=$f
    break
  fi
done

if [ -z "$lib_file" ]; then
  echo "Failed to locate a static library."
  false
  exit
fi

if [ ! -f "$ar" ]; then
  # Find the appropriate ar to use.
  ar="ar"
  if [ -n "$AR_target" ]; then
    ar=$AR_target
  elif [ -n "$AR" ]; then
    ar=$AR
  fi
fi

obj_list="$($ar t $lib_file | grep '\.o$')"

function extract_object {
  for f in $obj_list; do
    filename="${f##*/}"

    if [ -z "$(echo $filename | grep $1)" ]; then
      continue
    fi

    # Only echo this if debugging.
    # echo "Extract $filename from archive to $out_dir/$1."
    $ar p $lib_file $filename > $out_dir/$1
    [ -s $out_dir/$1 ] || exit 1
    break
  done
}

for f in $obj_files; do
  extract_object $f
done