xref: /netbsd-src/external/gpl3/gcc/dist/libtool-ldflags (revision 4d5abbe83f525258eb479e5fca29f25cb943f379)
14fee23f9Smrg#! /bin/sh
24fee23f9Smrg
34fee23f9Smrg# Script to translate LDFLAGS into a form suitable for use with libtool.
44fee23f9Smrg
5*4d5abbe8Smrg# Copyright (C) 2005-2014 Free Software Foundation, Inc.
64fee23f9Smrg#
74fee23f9Smrg# This file is free software; you can redistribute it and/or modify
84fee23f9Smrg# it under the terms of the GNU General Public License as published by
94fee23f9Smrg# the Free Software Foundation; either version 2 of the License, or
104fee23f9Smrg# (at your option) any later version.
114fee23f9Smrg#
124fee23f9Smrg# This program is distributed in the hope that it will be useful,
134fee23f9Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
144fee23f9Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
154fee23f9Smrg# GNU General Public License for more details.
164fee23f9Smrg#
174fee23f9Smrg# You should have received a copy of the GNU General Public License
184fee23f9Smrg# along with this program; if not, write to the Free Software
194fee23f9Smrg# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
204fee23f9Smrg# MA 02110-1301, USA.
214fee23f9Smrg
224fee23f9Smrg# Contributed by CodeSourcery, LLC.
234fee23f9Smrg
244fee23f9Smrg# This script is designed to be used from a Makefile that uses libtool
254fee23f9Smrg# to build libraries as follows:
264fee23f9Smrg#
274fee23f9Smrg#   LTLDFLAGS = $(shell libtool-ldflags $(LDFLAGS))
284fee23f9Smrg#
294fee23f9Smrg# Then, use (LTLDFLAGS) in place of $(LDFLAGS) in your link line.
304fee23f9Smrg
314fee23f9Smrg# The output of the script.  This string is built up as we process the
324fee23f9Smrg# arguments.
334fee23f9Smrgresult=
344fee23f9Smrgprev_arg=
354fee23f9Smrg
364fee23f9Smrgfor arg
374fee23f9Smrgdo
384fee23f9Smrg    case $arg in
39*4d5abbe8Smrg	-f*|--*|-static-lib*|-shared-lib*|-B*)
404fee23f9Smrg	    # Libtool does not ascribe any special meaning options
414fee23f9Smrg	    # that begin with -f or with a double-dash.  So, it will
424fee23f9Smrg	    # think these options are linker options, and prefix them
434fee23f9Smrg	    # with "-Wl,".  Then, the compiler driver will ignore the
444fee23f9Smrg	    # options.  So, we prefix these options with -Xcompiler to
454fee23f9Smrg	    # make clear to libtool that they are in fact compiler
46*4d5abbe8Smrg	    # options.  Similarly for e.g. -static-libstdc++, or
47*4d5abbe8Smrg	    # -B/some/path.
484fee23f9Smrg	    case $prev_arg in
494fee23f9Smrg		-Xpreprocessor|-Xcompiler|-Xlinker)
504fee23f9Smrg		    # This option is already prefixed; don't prefix it again.
514fee23f9Smrg		    ;;
524fee23f9Smrg		*)
534fee23f9Smrg		    result="$result -Xcompiler"
544fee23f9Smrg		    ;;
554fee23f9Smrg	    esac
564fee23f9Smrg	    ;;
574fee23f9Smrg	*)
584fee23f9Smrg	    # We do not want to add -Xcompiler to other options because
594fee23f9Smrg	    # that would prevent libtool itself from recognizing them.
604fee23f9Smrg	    ;;
614fee23f9Smrg    esac
624fee23f9Smrg    prev_arg=$arg
634fee23f9Smrg
644fee23f9Smrg    # If $(LDFLAGS) is (say):
654fee23f9Smrg    #   a "b'c d" e
664fee23f9Smrg    # then the user expects that:
674fee23f9Smrg    #   $(LD) $(LDFLAGS)
684fee23f9Smrg    # will pass three arguments to $(LD):
694fee23f9Smrg    #   1) a
704fee23f9Smrg    #   2) b'c d
714fee23f9Smrg    #   3) e
724fee23f9Smrg    # We must ensure, therefore, that the arguments are appropriately
734fee23f9Smrg    # quoted so that using:
744fee23f9Smrg    #   libtool --mode=link ... $(LTLDFLAGS)
754fee23f9Smrg    # will result in the same number of arguments being passed to
764fee23f9Smrg    # libtool.   In other words, when this script was invoked, the shell
774fee23f9Smrg    # removed one level of quoting, present in $(LDFLAGS); we have to put
784fee23f9Smrg    # it back.
794fee23f9Smrg
804fee23f9Smrg    # Quote any embedded single quotes.
814fee23f9Smrg    case $arg in
824fee23f9Smrg	*"'"*)
834fee23f9Smrg	    # The following command creates the script:
844fee23f9Smrg	    #   1s,^X,,;s|'|'"'"'|g
854fee23f9Smrg	    # which removes a leading X, and then quotes and embedded single
864fee23f9Smrg	    # quotes.
874fee23f9Smrg	    sed_script="1s,^X,,;s|'|'\"'\"'|g"
884fee23f9Smrg	    # Add a leading "X" so that if $arg starts with a dash,
894fee23f9Smrg	    # the echo command will not try to interpret the argument
904fee23f9Smrg	    # as a command-line option.
914fee23f9Smrg	    arg="X$arg"
924fee23f9Smrg	    # Generate the quoted string.
934fee23f9Smrg	    quoted_arg=`echo "$arg" | sed -e "$sed_script"`
944fee23f9Smrg	    ;;
954fee23f9Smrg	*)
964fee23f9Smrg	    quoted_arg=$arg
974fee23f9Smrg	    ;;
984fee23f9Smrg    esac
994fee23f9Smrg    # Surround the entire argument with single quotes.
1004fee23f9Smrg    quoted_arg="'"$quoted_arg"'"
1014fee23f9Smrg
1024fee23f9Smrg    # Add it to the string.
1034fee23f9Smrg    result="$result $quoted_arg"
1044fee23f9Smrgdone
1054fee23f9Smrg
1064fee23f9Smrg# Output the string we have built up.
1074fee23f9Smrgecho "$result"
108