sysroot_ld_path.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/sh
  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. # Reads etc/ld.so.conf and/or etc/ld.so.conf.d/*.conf and returns the
  6. # appropriate linker flags.
  7. #
  8. # sysroot_ld_path.sh /abspath/to/sysroot
  9. #
  10. log_error_and_exit() {
  11. echo $0: $@
  12. exit 1
  13. }
  14. process_entry() {
  15. if [ -z "$1" ] || [ -z "$2" ]; then
  16. log_error_and_exit "bad arguments to process_entry()"
  17. fi
  18. local root="$1"
  19. local localpath="$2"
  20. echo $localpath | grep -qs '^/'
  21. if [ $? -ne 0 ]; then
  22. log_error_and_exit $localpath does not start with /
  23. fi
  24. local entry="$root$localpath"
  25. echo -L$entry
  26. echo -Wl,-rpath-link=$entry
  27. }
  28. process_ld_so_conf() {
  29. if [ -z "$1" ] || [ -z "$2" ]; then
  30. log_error_and_exit "bad arguments to process_ld_so_conf()"
  31. fi
  32. local root="$1"
  33. local ld_so_conf="$2"
  34. # ld.so.conf may include relative include paths. pushd is a bashism.
  35. local saved_pwd=$(pwd)
  36. cd $(dirname "$ld_so_conf")
  37. cat "$ld_so_conf" | \
  38. while read ENTRY; do
  39. echo "$ENTRY" | grep -qs ^include
  40. if [ $? -eq 0 ]; then
  41. local included_files=$(echo "$ENTRY" | sed 's/^include //')
  42. echo "$included_files" | grep -qs ^/
  43. if [ $? -eq 0 ]; then
  44. if ls $root$included_files >/dev/null 2>&1 ; then
  45. for inc_file in $root$included_files; do
  46. process_ld_so_conf "$root" "$inc_file"
  47. done
  48. fi
  49. else
  50. if ls $(pwd)/$included_files >/dev/null 2>&1 ; then
  51. for inc_file in $(pwd)/$included_files; do
  52. process_ld_so_conf "$root" "$inc_file"
  53. done
  54. fi
  55. fi
  56. continue
  57. fi
  58. echo "$ENTRY" | grep -qs ^/
  59. if [ $? -eq 0 ]; then
  60. process_entry "$root" "$ENTRY"
  61. fi
  62. done
  63. # popd is a bashism
  64. cd "$saved_pwd"
  65. }
  66. # Main
  67. if [ $# -ne 1 ]; then
  68. echo Usage $0 /abspath/to/sysroot
  69. exit 1
  70. fi
  71. echo $1 | grep -qs ' '
  72. if [ $? -eq 0 ]; then
  73. log_error_and_exit $1 contains whitespace.
  74. fi
  75. LD_SO_CONF="$1/etc/ld.so.conf"
  76. LD_SO_CONF_D="$1/etc/ld.so.conf.d"
  77. if [ -e "$LD_SO_CONF" ]; then
  78. process_ld_so_conf "$1" "$LD_SO_CONF" | xargs echo
  79. elif [ -e "$LD_SO_CONF_D" ]; then
  80. find "$LD_SO_CONF_D" -maxdepth 1 -name '*.conf' -print -quit > /dev/null
  81. if [ $? -eq 0 ]; then
  82. for entry in $LD_SO_CONF_D/*.conf; do
  83. process_ld_so_conf "$1" "$entry"
  84. done | xargs echo
  85. fi
  86. fi