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