1
0

patch-specs.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env bash
  2. DIR="$1"
  3. if [ -d "$DIR" ]; then
  4. DIR="$(cd "$DIR"; pwd)"
  5. else
  6. echo "Usage: $0 toolchain-dir"
  7. exit 1
  8. fi
  9. echo -n "Locating cpp ... "
  10. for bin in bin usr/bin usr/local/bin; do
  11. for cmd in "$DIR/$bin/"*-cpp; do
  12. if [ -x "$cmd" ]; then
  13. echo "$cmd"
  14. CPP="$cmd"
  15. break
  16. fi
  17. done
  18. done
  19. if [ ! -x "$CPP" ]; then
  20. echo "Can't locate a cpp executable in '$DIR' !"
  21. exit 1
  22. fi
  23. patch_specs() {
  24. local found=0
  25. for lib in $(STAGING_DIR="$DIR" "$CPP" -x c -v /dev/null 2>&1 | sed -ne 's#:# #g; s#^LIBRARY_PATH=##p'); do
  26. if [ -d "$lib" ]; then
  27. grep -qs "STAGING_DIR" "$lib/specs" && rm -f "$lib/specs"
  28. if [ $found -lt 1 ]; then
  29. echo -n "Patching specs ... "
  30. STAGING_DIR="$DIR" "$CPP" -dumpspecs | awk '
  31. mode ~ "link" {
  32. sub(/(%@?\{L.\})/, "& -L %:getenv(STAGING_DIR /usr/lib) -rpath-link %:getenv(STAGING_DIR /usr/lib)")
  33. }
  34. mode ~ "cpp" {
  35. $0 = $0 " -idirafter %:getenv(STAGING_DIR /usr/include)"
  36. }
  37. {
  38. print $0
  39. mode = ""
  40. }
  41. /^\*cpp:/ {
  42. mode = "cpp"
  43. }
  44. /^\*link.*:/ {
  45. mode = "link"
  46. }
  47. ' > "$lib/specs"
  48. echo "ok"
  49. found=1
  50. fi
  51. fi
  52. done
  53. [ $found -gt 0 ]
  54. return $?
  55. }
  56. VERSION="$(STAGING_DIR="$DIR" "$CPP" --version | sed -ne 's/^.* (.*) //; s/ .*$//; 1p')"
  57. VERSION="${VERSION:-unknown}"
  58. case "${VERSION##* }" in
  59. 2.*|3.*|4.0.*|4.1.*|4.2.*)
  60. echo "The compiler version does not support getenv() in spec files."
  61. echo -n "Wrapping binaries instead ... "
  62. if "${0%/*}/ext-toolchain.sh" --toolchain "$DIR" --wrap "${CPP%/*}"; then
  63. echo "ok"
  64. exit 0
  65. else
  66. echo "failed"
  67. exit $?
  68. fi
  69. ;;
  70. *)
  71. if patch_specs; then
  72. echo "Toolchain successfully patched."
  73. exit 0
  74. else
  75. echo "Failed to locate library directory!"
  76. exit 1
  77. fi
  78. ;;
  79. esac