summaryrefslogtreecommitdiff
path: root/mk/wrapper/arg-source
blob: 111bb4d63f1493aab5d0f72470a4c3bee87b396c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# $NetBSD: arg-source,v 1.15 2007/09/19 13:08:19 rillig Exp $
#
# Copyright (c) 2004 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Johnny C. Lam.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by the NetBSD
#        Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# This script manages a buffer through which arguments from the command
# line are funneled.  Along the way, the arguments are potentially
# merged and split in various ways to canonicalize their representation.
# This script should be sourced within a loop and upon exiting returns
# an argument in $arg.
#
# This is done by pushing the next command-line argument onto a queue,
# then using a processing loop to pop an argument off of the top of
# the queue, transform it, and possibly push more arguments back onto
# the queue.

# Push arguments from the command line into the argument buffer.
while $test $# -gt 0; do
	arg="$1"; shift
	case $arg in
	##############################################################
	# Merge "-I /dir" into a single "-I/dir".  Same for -L, -R.
	##############################################################
	-[DILR])
		nextarg="$1"; shift
		case "$nextarg" in
		-*) msg_log $wrapperlog "WARNING: [arg-source] An $arg option must not be followed by another option, $nextarg." ;;
		esac
		append_queue argbuf "$arg$nextarg"
		$debug_log $wrapperlog "    (arg-source) push: $arg$nextarg"
		;;
	##############################################################
	# Split -Wl,option1,option2 into -Wl,option1 -Wl,option2 and
	# process them again.
	#
	# XXX: The sunpro manual page says: A comma can be part of an
	# argument by escaping it with an immediately preceding
	# backslash. The manual pages for gcc and mipspro do not
	# mention commas in those options at all.
	##############################################################
	-Wl,*,*)
		$debug_log $wrapperlog "    (arg-source) before-split: $arg $*"
		saved_IFS="$IFS"
		new_args=""
		IFS=","
		for o in ${arg#-Wl,}; do
			IFS="$saved_IFS"
			shquote "$o"; o="$shquoted"
			new_args="$new_args -Wl,$o"
		done
		IFS="$saved_IFS"
		eval "set args $new_args \"\$@\""; shift
		$debug_log $wrapperlog "    (arg-source) after-split: $*"
		continue
		;;

	##############################################################
	# Split "-Wl,-R/dir1:/dir2" into "-Wl,-R/dir1 -Wl,-R/dir2".
	# Same for -R and -Wl,-rpath and -Wl,-rpath-link.
	##############################################################
	-R*:*|-Wl,-R*:*|\
	-Wl,-rpath,*:*|-Wl,-rpath-link,*:*|-Wl,--rpath,*:*)
		case $arg in
		-R*)			R="-R" ;;
		-Wl,-R*)		R="-Wl,-R" ;;
		-Wl,-rpath,*)		R="-Wl,-rpath," ;;
		-Wl,-rpath-link,*)	R="-Wl,-rpath-link," ;;
		-Wl,--rpath,*)		R="-Wl,--rpath," ;;
		esac
		list="${arg#$R}"
		save_IFS="${IFS}"; IFS=":"
		for dir in $list; do
			IFS="${save_IFS}"
			append_queue argbuf "$R$dir"
			$debug_log $wrapperlog "    (arg-source) push: $R$dir"
		done
		IFS="${save_IFS}"
		;;
	##############################################################
	# Merge and split "-Wl,-R -Wl,/dir1:/dir2" into
	# "-Wl,-R/dir1 -Wl,-R/dir2".  Same for -Wl,-rpath and
	# -Wl,-rpath-link.
	##############################################################
	-Wl,-R|-Wl,-rpath|-Wl,-rpath-link|-Wl,--rpath)
		nextarg="$1"; shift
		case $arg in
		-Wl,-R)			R="-Wl,-R" ;;
		-Wl,-rpath)		R="-Wl,-rpath," ;;
		-Wl,-rpath-link)	R="-Wl,-rpath-link," ;;
		-Wl,--rpath)		R="-Wl,--rpath," ;;
		esac
		nextarg="${nextarg#-Wl,}"
		case $nextarg in
		*:*)
			save_IFS="${IFS}"; IFS=":"
			for dir in $nextarg; do
				IFS="${save_IFS}"
				append_queue argbuf "$R$dir"
				$debug_log $wrapperlog "    (arg-source) push: $R$dir"
			done
			IFS="${save_IFS}"
			;;
		*)
			append_queue argbuf "$R$nextarg"
			$debug_log $wrapperlog "    (arg-source) push: $R$nextarg"
			;;
		esac
		;;
	##############################################################
	# "-Xlinker arg" is the equivalent of "-Wl,arg" so convert it
	# here before queueing it up.
	##############################################################
	-Xlinker)
		nextarg="$1"; shift
		case $nextarg in
		-Wl,*)
			append_queue argbuf "$nextarg"
			$debug_log $wrapperlog "    (arg-source) push: $nextarg"
			;;
		*)
			append_queue argbuf "-Wl,$nextarg"
			$debug_log $wrapperlog "    (arg-source) push: -Wl,$nextarg"
			;;
		esac
		;;
	##############################################################
	# Remember whether running the linker is desired.
	##############################################################
	-c|-S|-E)
		dont_link=yes
		append_queue argbuf "$arg"
		$debug_log $wrapperlog "    (arg-source) push: $arg"
		;;
	##############################################################
	# For everything else, just queue it up.
	##############################################################
	*)
		append_queue argbuf "$arg"
		$debug_log $wrapperlog "    (arg-source) push: $arg"
		;;
	esac
done