• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (c) 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Reads etc/ld.so.conf and/or etc/ld.so.conf.d/*.conf and returns the
7# appropriate linker flags.
8#
9#  sysroot_ld_path.sh /abspath/to/sysroot
10#
11
12log_error_and_exit() {
13  echo $0: $@
14  exit 1
15}
16
17process_entry() {
18  if [ -z "$1" ] || [ -z "$2" ]; then
19    log_error_and_exit "bad arguments to process_entry()"
20  fi
21  local root="$1"
22  local localpath="$2"
23
24  echo $localpath | grep -qs '^/'
25  if [ $? -ne 0 ]; then
26    log_error_and_exit $localpath does not start with /
27  fi
28  local entry="$root$localpath"
29  echo $entry
30}
31
32process_ld_so_conf() {
33  if [ -z "$1" ] || [ -z "$2" ]; then
34    log_error_and_exit "bad arguments to process_ld_so_conf()"
35  fi
36  local root="$1"
37  local ld_so_conf="$2"
38
39  # ld.so.conf may include relative include paths. pushd is a bashism.
40  local saved_pwd=$(pwd)
41  cd $(dirname "$ld_so_conf")
42
43  cat "$ld_so_conf" | \
44    while read ENTRY; do
45      echo "$ENTRY" | grep -qs ^include
46      if [ $? -eq 0 ]; then
47        local included_files=$(echo "$ENTRY" | sed 's/^include //')
48        echo "$included_files" | grep -qs ^/
49        if [ $? -eq 0 ]; then
50          if ls $root$included_files >/dev/null 2>&1 ; then
51            for inc_file in $root$included_files; do
52              process_ld_so_conf "$root" "$inc_file"
53            done
54          fi
55        else
56          if ls $(pwd)/$included_files >/dev/null 2>&1 ; then
57            for inc_file in $(pwd)/$included_files; do
58              process_ld_so_conf "$root" "$inc_file"
59            done
60          fi
61        fi
62        continue
63      fi
64
65      echo "$ENTRY" | grep -qs ^/
66      if [ $? -eq 0 ]; then
67        process_entry "$root" "$ENTRY"
68      fi
69    done
70
71  # popd is a bashism
72  cd "$saved_pwd"
73}
74
75# Main
76if [ $# -ne 1 ]; then
77  echo "Usage $0 abspath to sysroot"
78  exit 1
79fi
80
81echo $1 | grep -qs ' '
82if [ $? -eq 0 ]; then
83  log_error_and_exit $1 contains whitespace.
84fi
85
86LD_SO_CONF="$1/etc/ld.so.conf"
87LD_SO_CONF_D="$1/etc/ld.so.conf.d"
88
89if [ -e "$LD_SO_CONF" ]; then
90  process_ld_so_conf "$1" "$LD_SO_CONF" | xargs echo
91elif [ -e "$LD_SO_CONF_D" ]; then
92  find "$LD_SO_CONF_D" -maxdepth 1 -name '*.conf' -print -quit > /dev/null
93  if [ $? -eq 0 ]; then
94    for entry in $LD_SO_CONF_D/*.conf; do
95      process_ld_so_conf "$1" "$entry"
96    done | xargs echo
97  fi
98fi
99