summaryrefslogtreecommitdiff
blob: ae56e5a44b9316225e7b1ca0d0c99f75c1c9118f (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash

# Libtool does not support spaces in dependency_libs entries so we won't worry
# overly about them either.

NEWLINE="
"

has() {
	[[ " ${*:2} " == *" $1 "* ]]
}

fix_la_files() {
	local lafile
	for lafile in "${@:2}"
	do
		local has_inh_link_flags="no"
		local new_inh_link_flags=""
		local dependency_libs=""
		local inh_link_flags=""
		local has_dep_libs="no"
		local new_dep_libs=""
		local remove_lib=""
		local contents="$(<"${lafile}")"
		local libladir=""
		local librpath=""
		local dep_libs=""
		local line=""
		local lib=""


		save_IFS="${IFS}"
		IFS="$NEWLINE"
		for line in $contents
		do
			if [[ "${line#dependency_libs=\'}" != "${line}" ]]
			then
				[[ "$has_dep_libs" == "no" ]] || { echo "dependency_libs= assigned more than once in $lafile" && return 1 ; }
				line="${line#dependency_libs=\'}"
				dep_libs="${line%\'}"
				has_dep_libs="yes"

			elif [[ "${line#inherited_linker_flags=\'}" != "${line}" ]]
			then

				[[ "$has_inh_link_flags" == "no" ]] || { echo "inherited_linker_flags= assigned more than once in $lafile" && return 1 ; }
				line="${line#inherited_linker_flags=\'}"
				inh_link_flags="${line%\'}"
				new_inh_link_flags="${inh_link_flags}"
				has_inh_link_flags="yes"
			fi
		done
		IFS="$save_IFS"

		if [[ "$has_dep_libs" == "no" ]]
		then
			printf '%s\n' "$lafile is not a .la file. Skipping."
			continue
		fi

		for entry in $dep_libs
		do
			case $entry in
				-l*)
					has ${entry} ${new_dep_libs} || new_dep_libs="${new_dep_libs} ${entry}"
					;;
				*.la)

					if [[ "${entry##*\/lib}" == "${entry}" ]]
					then
						has ${entry} ${new_dep_libs} || new_dep_libs="${new_dep_libs} ${entry}"
					else
						lib="${entry##*\/lib}"
						lib="${lib%.la}"
						lib="-l${lib}"
						has ${lib} ${new_dep_libs} || new_dep_libs="${new_dep_libs} ${lib}"
						has -L${entry%/*.la} ${libladir} || libladir="${libladir} -L${entry%/*.la}"
					fi
					;;
				-L*)
					[[ ${entry/X11R6\/lib} != ${entry} ]] && entry="${entry/X11R6\/}"
					[[ ${entry/local\/lib} != ${entry} ]] && entry="${entry/local\/}"
					[[ ${entry/usr\/lib*\/pkgconfig\/..\/..} != ${entry} ]] && entry="${entry/\/lib*\/pkgconfig\/..\/..}"
					[[ ${entry/usr\/lib*\/pkgconfig\/..} != ${entry} ]] && entry="${entry/\/pkgconfig\/..}"
					has ${entry} ${libladir} || libladir="${libladir} ${entry}"
					;;
				-R*)
					has ${entry} ${librpath} || librpath="${librpath} ${entry}"
					;;
				-mt|-mthreads|-kthread|-Kthread|-pthread|-pthreads|--thread-safe|-threads)
					if [[ "${has_inh_link_flags}" == "yes" ]]
					then
						has ${entry} ${new_inh_link_flags} || new_inh_link_flags="${new_inh_link_flags} ${entry}"
					else
						has ${entry} ${new_dep_libs} || new_dep_libs="${new_dep_libs} ${entry}"
					fi
					;;
				*)
					echo "Debug information:"
					echo $lafile
					echo "${entry}"
					echo "Holy Moley, Dorothy, we ain't in Kansas and Luke, I'm not your father"
					return 1
					;;
			esac
		done

		[[ "${dep_libs}" == "${librpath}${libladir}${new_dep_libs}" && "${new_inh_link_flags}" == "${inh_link_flags}" ]] && { echo "$lafile already clean, skipping update."; continue; }
		echo "${lafile}: Updating..."
		contents="${contents/${NEWLINE}dependency_libs=\'${dep_libs}\'${NEWLINE}/${NEWLINE}dependency_libs='${librpath}${libladir}${new_dep_libs}'${NEWLINE}}"
		[[ "${has_inh_link_flags}" == "yes" ]] && \
			contents="${contents/${NEWLINE}inherited_linker_flags=\'${inh_link_flags}\'${NEWLINE}/${NEWLINE}inherited_linker_flags='${new_inh_link_flags}'${NEWLINE}}"

		printf '%s' "$contents" > "${lafile}"
	done
}

case "$1" in
	-h|--help)
		cat <<- EOF
		lafilefixer (C) 2009 Peter Alfredsen <loki_val@gentoo.org>
		Released under the MIT/X11 license.

		Usage: lafilefixer [OPTION] [FILE|DIR]...
		Fix .la libtool archives to list libraries, not .la files in dependency_libs and
		do some minor fixups, moving -pthread to inherited_linker_flags if available and
		eliminating duplicate library listings.

		By default, lafilefixer is recursive, fixing all .la files in all subdirectories
		so if you want to fix only a single file, it must be specified in full.

		Options:
		  -h, --help            Display this text and exit.
		      --justfixit       Choose some reasonable dirs, such as /usr/lib*, etc. ,
		                        find all .la files and fix them to not use .la files
		                        for linking
		      --license	        Display the license and exit.
		EOF
		;;
	--justfixit)
		declare dirlist=""
		declare files=()
		for dir in {/usr/lib,/usr/qt/3/lib,/usr/kde/3.5/lib,/opt/lib,/lib}{,32,64}
		do
			[[ -d "${dir}" ]] && dirlist="${dirlist} ${dir}"
		done

		while read -r line
		do
			files+=( "$line" )
		done< <( find ${dirlist} -name '*.la' -type f )

		fix_la_files --cleanup "${files[@]}"
		;;
	--license)
		cat <<- EOF
		Copyright (c) 2009 Peter Alfredsen <loki_val@gentoo.org>

		Permission is hereby granted, free of charge, to any person obtaining a copy of
		this software and associated documentation files (the "Software"), to deal in
		the Software without restriction, including without limitation the rights to
		use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
		of the Software, and to permit persons to whom the Software is furnished to
		do so, subject to the following conditions:

		The above copyright notice and this permission notice shall be included in all
		copies or substantial portions of the Software.

		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
		IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
		FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
		COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
		IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
		CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
		EOF
		;;
	*)
		declare arglist=()
		declare files=()
		declare arg=""

		for arg in "${@}"
		do
			if [[ -d "${arg}" || -f "${arg}" ]]
			then
				arglist+=( "${arg}" )
			else
				printf '%s\n' "${arg} is not a valid directory or file, skipping."
			fi
		done

		while read -r line
		do
			files+=( "$line" )
		done< <( find "${arglist[@]}" -name '*.la' -type f )

		fix_la_files --cleanup "${files[@]}"
		;;
esac