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