libtool-ldflags 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #! /bin/sh
  2. # Script to translate LDFLAGS into a form suitable for use with libtool.
  3. # Copyright (C) 2005-2014 Free Software Foundation, Inc.
  4. #
  5. # This file is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. # Contributed by CodeSourcery, LLC.
  20. # This script is designed to be used from a Makefile that uses libtool
  21. # to build libraries as follows:
  22. #
  23. # LTLDFLAGS = $(shell libtool-ldflags $(LDFLAGS))
  24. #
  25. # Then, use (LTLDFLAGS) in place of $(LDFLAGS) in your link line.
  26. # The output of the script. This string is built up as we process the
  27. # arguments.
  28. result=
  29. prev_arg=
  30. for arg
  31. do
  32. case $arg in
  33. -f*|--*|-static-lib*|-shared-lib*|-B*)
  34. # Libtool does not ascribe any special meaning options
  35. # that begin with -f or with a double-dash. So, it will
  36. # think these options are linker options, and prefix them
  37. # with "-Wl,". Then, the compiler driver will ignore the
  38. # options. So, we prefix these options with -Xcompiler to
  39. # make clear to libtool that they are in fact compiler
  40. # options. Similarly for e.g. -static-libstdc++, or
  41. # -B/some/path.
  42. case $prev_arg in
  43. -Xpreprocessor|-Xcompiler|-Xlinker)
  44. # This option is already prefixed; don't prefix it again.
  45. ;;
  46. *)
  47. result="$result -Xcompiler"
  48. ;;
  49. esac
  50. ;;
  51. *)
  52. # We do not want to add -Xcompiler to other options because
  53. # that would prevent libtool itself from recognizing them.
  54. ;;
  55. esac
  56. prev_arg=$arg
  57. # If $(LDFLAGS) is (say):
  58. # a "b'c d" e
  59. # then the user expects that:
  60. # $(LD) $(LDFLAGS)
  61. # will pass three arguments to $(LD):
  62. # 1) a
  63. # 2) b'c d
  64. # 3) e
  65. # We must ensure, therefore, that the arguments are appropriately
  66. # quoted so that using:
  67. # libtool --mode=link ... $(LTLDFLAGS)
  68. # will result in the same number of arguments being passed to
  69. # libtool. In other words, when this script was invoked, the shell
  70. # removed one level of quoting, present in $(LDFLAGS); we have to put
  71. # it back.
  72. # Quote any embedded single quotes.
  73. case $arg in
  74. *"'"*)
  75. # The following command creates the script:
  76. # 1s,^X,,;s|'|'"'"'|g
  77. # which removes a leading X, and then quotes and embedded single
  78. # quotes.
  79. sed_script="1s,^X,,;s|'|'\"'\"'|g"
  80. # Add a leading "X" so that if $arg starts with a dash,
  81. # the echo command will not try to interpret the argument
  82. # as a command-line option.
  83. arg="X$arg"
  84. # Generate the quoted string.
  85. quoted_arg=`echo "$arg" | sed -e "$sed_script"`
  86. ;;
  87. *)
  88. quoted_arg=$arg
  89. ;;
  90. esac
  91. # Surround the entire argument with single quotes.
  92. quoted_arg="'"$quoted_arg"'"
  93. # Add it to the string.
  94. result="$result $quoted_arg"
  95. done
  96. # Output the string we have built up.
  97. echo "$result"