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