| 1 |
modax-guest |
10291 |
#!/usr/bin/perl -w |
| 2 |
|
|
|
| 3 |
|
|
=head1 NAME |
| 4 |
|
|
|
| 5 |
|
|
B<autofixtll> - auto-correct CMake TARGET_LINK_LIBRARIES directive according to |
| 6 |
|
|
the information provided by GNU ld linker S<I<'undefined references'>> errors. |
| 7 |
|
|
|
| 8 |
|
|
=head1 SYNOPSIS |
| 9 |
|
|
|
| 10 |
|
|
B<autofixtll> [B<--invoke-edit|-e>] [B<--build-dir|-b>=I<dir>] [B<--build-command|-c>=I<command>] [B<--patch-name|-p>=I<name>] [B<--do-backups>] |
| 11 |
|
|
|
| 12 |
|
|
=head1 DESCRIPTION |
| 13 |
|
|
|
| 14 |
|
|
B<autofixtll> is capable of correcting most linking failures caused by |
| 15 |
|
|
S<'undefined references'> linker errors. The script should be executed from the |
| 16 |
|
|
extracted debian source tree with all build dependences installed in the |
| 17 |
|
|
environment. This script is a wrapper around S<`debian/rules build'> or |
| 18 |
|
|
whatever command you specify with B<--build-command> option. |
| 19 |
|
|
|
| 20 |
|
|
B<autofixtll> depends on B<quilt> to incrementally build up a patch of the |
| 21 |
|
|
changes it does. The script assumes that quilt patches are located in |
| 22 |
|
|
debian/patches. The default name of the patch is |
| 23 |
|
|
S<I<"97_fix_target_link_libraries.diff">> (it can be changed with the |
| 24 |
|
|
[B<--patch-name> option). This patch must have already pushed to the "quilt |
| 25 |
|
|
top" when this script is executed. |
| 26 |
|
|
|
| 27 |
|
|
First of all, the script loads dynamic symbol names of the libraries specified |
| 28 |
|
|
in the @LIBS array. Then it starts building process and keeps monitoring it |
| 29 |
|
|
while looking for the "undefined references" errors from the linker (only GNU |
| 30 |
|
|
ld syntax is supported). Then it tries to match undefined symbol names with the |
| 31 |
|
|
symbols loaded from the libraries in the @LIBS array. If matches are found, it |
| 32 |
|
|
tries to update TARGET_LINK_LIBRARIES command in the respective CMakeLists.txt |
| 33 |
|
|
file adding missing libraries. Finally, the build process is restarted. |
| 34 |
|
|
|
| 35 |
|
|
This loop continues until build completes successfully or the error which |
| 36 |
|
|
B<autofixtll> can't handle occurs. In latter case, a user will need to `quilt |
| 37 |
|
|
edit' the respective file manually or add more libs to the @LIBS array. Then |
| 38 |
|
|
the script can be restarted again (or use --invoke-edit option to invoke |
| 39 |
|
|
I<quilt edit> and restart build for you automatically). |
| 40 |
|
|
|
| 41 |
|
|
CMake verbose output must be enabled for B<autofixtll> to work reliably. |
| 42 |
|
|
|
| 43 |
|
|
B<autofixtll> was written to help maintainer resolve build failures of KDE |
| 44 |
|
|
applications which were introduced by the clean up of KDELibs recursive library |
| 45 |
|
|
dependences. |
| 46 |
|
|
|
| 47 |
|
|
=head1 OPTIONS |
| 48 |
|
|
|
| 49 |
|
|
=over 4 |
| 50 |
|
|
|
| 51 |
|
|
=item B<-b> I<dir>, B<--build-dir>=I<dir> |
| 52 |
|
|
|
| 53 |
|
|
Specify a custom build directory. It must be relative the current (source) |
| 54 |
|
|
directory. Default is as returned by S<I<"obj-`dpkg-architecture |
| 55 |
|
|
-qDEB_BUILD_GNU_TYPE`>">. |
| 56 |
|
|
|
| 57 |
|
|
=item B<-c> I<command>, B<--build-command>=I<command> |
| 58 |
|
|
|
| 59 |
|
|
The command which should be run to build the source. If the command is not |
| 60 |
|
|
specific, `debian/rules build' will be executed from the B<source tree>. |
| 61 |
|
|
However, if you specify the command, it will be from the B<build tree>. |
| 62 |
|
|
|
| 63 |
modax-guest |
10329 |
=item B<-i>, B<--exec-in-build-dir> |
| 64 |
|
|
|
| 65 |
|
|
Run build command in the build directory. Default is to run in the source |
| 66 |
|
|
directory. |
| 67 |
|
|
|
| 68 |
modax-guest |
10291 |
=item B<-p>, B<--patch-name>=I<name> |
| 69 |
|
|
|
| 70 |
|
|
The name of the quilt patch in which all changes made by the script will be |
| 71 |
|
|
stored. Default is I<97_fix_target_link_libraries.diff>. |
| 72 |
|
|
|
| 73 |
|
|
=item B<--do-backups>, B<--backup> |
| 74 |
|
|
|
| 75 |
|
|
Whether to backup CMakeLists.txt as CMakeLists.txt.orig before auto-modifying |
| 76 |
|
|
it. |
| 77 |
|
|
|
| 78 |
|
|
=item B<--invoke-edit>, B<--edit>, B<-e> |
| 79 |
|
|
|
| 80 |
|
|
Invoke `quilt edit' on the respective CMakeLists.txt when undefined references |
| 81 |
|
|
cannot be automatically resolved and restart build process immediately when the |
| 82 |
|
|
editor is closed. |
| 83 |
|
|
|
| 84 |
modax-guest |
10655 |
=item B<--quilt>=I<command>, B<-q> I<command> |
| 85 |
|
|
|
| 86 |
|
|
Use the specified command to invoke quilt instead of default |
| 87 |
|
|
`QUILT_PATCHES=debian/patches quilt' |
| 88 |
|
|
|
| 89 |
modax-guest |
10291 |
=back |
| 90 |
|
|
|
| 91 |
|
|
=head1 LICENSE |
| 92 |
|
|
|
| 93 |
|
|
This program is free software: you can redistribute it and/or modify it under |
| 94 |
|
|
the terms of the GNU General Public License as published by the Free Software |
| 95 |
|
|
Foundation, either version 3 of the License, or (at your option) any later |
| 96 |
|
|
version. |
| 97 |
|
|
|
| 98 |
|
|
On B<Debian> systems, the complete text of the GNU GPL v3 can be found in the |
| 99 |
|
|
file F</usr/share/common-licenses/GPL-3> |
| 100 |
|
|
|
| 101 |
|
|
=head1 AUTHORS |
| 102 |
|
|
|
| 103 |
|
|
Written by Modestas Vainius <modestas@vainius.eu> |
| 104 |
|
|
|
| 105 |
|
|
=cut |
| 106 |
|
|
|
| 107 |
|
|
use strict; |
| 108 |
|
|
use Cwd qw(getcwd realpath); |
| 109 |
|
|
use File::Spec; |
| 110 |
|
|
use File::Copy; |
| 111 |
|
|
use Getopt::Long; |
| 112 |
|
|
use Pod::Usage; |
| 113 |
|
|
use FileHandle; |
| 114 |
|
|
use IPC::Open2; |
| 115 |
|
|
|
| 116 |
|
|
#### Please add predefined libraries to load dynamic symbol from here. |
| 117 |
modax-guest |
10348 |
# new Library(name, cmake_target, condition => 'condition', [path]). If path is not specified, |
| 118 |
modax-guest |
10311 |
# it's /usr/lib/$name.so. Use '' quotes to avoid escaping $. |
| 119 |
modax-guest |
10291 |
my @LIBS = ( |
| 120 |
|
|
new Library('QtDBus', '${QT_QTDBUS_LIBRARY}'), |
| 121 |
|
|
new Library('QtNetwork', '${QT_QTNETWORK_LIBRARY}'), |
| 122 |
|
|
new Library('QtXml', '${QT_QTXML_LIBRARY}'), |
| 123 |
|
|
new Library('QtSvg', '${QT_QTSVG_LIBRARY}'), |
| 124 |
modax-guest |
10329 |
new Library('QtGui', '${QT_QTGUI_LIBRARY}'), |
| 125 |
|
|
# new Library('pthread', '${CMAKE_THREAD_LIBS_INIT}', '/lib/libpthread.so.0'), |
| 126 |
modax-guest |
10348 |
new Library('X11', '${X11_X11_LIB}', condition => 'X11_FOUND'), |
| 127 |
|
|
new Library('Xext', '${X11_Xext_LIB}', condition => 'X11_Xext_FOUND'), |
| 128 |
modax-guest |
10291 |
new Library('z', '${ZLIB_LIBRARY}'), |
| 129 |
modax-guest |
10329 |
new Library('solid', '${KDE4_SOLID_LIBS}'), |
| 130 |
modax-guest |
10441 |
new Library('kdecore', '${KDE4_KDECORE_LIBS}'), |
| 131 |
|
|
new Library('kdeui', '${KDE4_KDEUI_LIBS}'), |
| 132 |
wdgt-guest |
11651 |
new Library('kparts', '${KDE4_KPARTS_LIBS}'), |
| 133 |
|
|
new Library('ktexteditor', '${KDE4_KTEXTEDITOR_LIBS}'), |
| 134 |
|
|
new Library('kio', '${KDE4_KIO_LIBS}'), |
| 135 |
modax-guest |
10441 |
new Library('kde3support', '${KDE4_KDE3SUPPORT_LIBRARY}'), |
| 136 |
modax-guest |
10291 |
); |
| 137 |
|
|
|
| 138 |
|
|
#### Some defaults |
| 139 |
modax-guest |
10655 |
my $MSG_PREFIX = "--=--"; |
| 140 |
modax-guest |
10291 |
my $QUILT = "QUILT_PATCHES=debian/patches quilt"; |
| 141 |
|
|
|
| 142 |
|
|
############### Implementation ############################### |
| 143 |
|
|
|
| 144 |
|
|
sub Library::new { |
| 145 |
modax-guest |
10348 |
my ($cls, $name, $cmake_target, %other) = @_; |
| 146 |
|
|
my $path = $other{path}; |
| 147 |
modax-guest |
10358 |
my $condition = (exists $other{condition}) ? $other{condition} : undef; |
| 148 |
modax-guest |
10291 |
if (!defined $path) { |
| 149 |
|
|
$path = "/usr/lib/lib$name.so"; |
| 150 |
|
|
} |
| 151 |
modax-guest |
10348 |
return bless( { name => $name, path => $path, cmake_target => $cmake_target, condition => $condition }, $cls); |
| 152 |
modax-guest |
10291 |
} |
| 153 |
|
|
|
| 154 |
|
|
sub Library::load { |
| 155 |
|
|
my $self = shift; |
| 156 |
|
|
my @symbols; |
| 157 |
modax-guest |
10329 |
my %symbhash; |
| 158 |
modax-guest |
10291 |
my @cpp_symbols; |
| 159 |
|
|
if (-r $self->{path}) { |
| 160 |
|
|
open(OBJDUMP, "objdump -T '$self->{path}' |") or die "Unable to run objdump"; |
| 161 |
|
|
while(<OBJDUMP>) { |
| 162 |
|
|
# 0000000000021e80 g DF .text 00000000000000f9 Base _XSendClientPrefix |
| 163 |
|
|
if (m/^[0-9a-f]+\s+g\s+DF\s+\.text\s+[0-9a-f]+\s+Base\s+(.*)$/) { |
| 164 |
|
|
my $symbol = $1; |
| 165 |
|
|
if ($symbol =~ m/^_Z/) { |
| 166 |
|
|
# It is C++ symbol. Needs demangling. |
| 167 |
|
|
push @cpp_symbols, $symbol; |
| 168 |
|
|
} else { |
| 169 |
|
|
push @symbols, $symbol; |
| 170 |
modax-guest |
10329 |
$symbhash{$symbol} = 1; |
| 171 |
modax-guest |
10291 |
} |
| 172 |
|
|
} |
| 173 |
|
|
} |
| 174 |
|
|
close(OBJDUMP); |
| 175 |
|
|
} |
| 176 |
|
|
if (@cpp_symbols) { |
| 177 |
|
|
open2(*OUT, *IN, "c++filt") or die "Unable to run c++filt"; |
| 178 |
modax-guest |
10329 |
my $count = 0; |
| 179 |
modax-guest |
10291 |
for (@cpp_symbols) { |
| 180 |
|
|
print IN "$_\n"; |
| 181 |
modax-guest |
10329 |
$_ = <OUT>; |
| 182 |
modax-guest |
10291 |
chomp; |
| 183 |
modax-guest |
10329 |
# Don't care about anything after `(' |
| 184 |
modax-guest |
10291 |
s/\(.*$//; |
| 185 |
|
|
s/^non-virtual thunk to\s+//; |
| 186 |
|
|
push @symbols, $_; |
| 187 |
modax-guest |
10329 |
$symbhash{$_} = 1; |
| 188 |
modax-guest |
10291 |
$count++; |
| 189 |
|
|
} |
| 190 |
modax-guest |
10329 |
close(IN); |
| 191 |
modax-guest |
10291 |
close(OUT); |
| 192 |
|
|
print(STDERR "Lost a few C++ symbols (", (scalar(@cpp_symbols) - $count), |
| 193 |
|
|
") while demangling ", $self->to_string(), "\n") unless ($count == scalar(@cpp_symbols)); |
| 194 |
|
|
} |
| 195 |
|
|
if (@symbols) { |
| 196 |
|
|
$self->{symbols} = \@symbols; |
| 197 |
modax-guest |
10329 |
$self->{symbhash} = \%symbhash; |
| 198 |
modax-guest |
10291 |
return scalar(@symbols); |
| 199 |
|
|
} else { |
| 200 |
|
|
$self->{symbols} = []; |
| 201 |
modax-guest |
10329 |
$self->{symbhash} = {}; |
| 202 |
modax-guest |
10291 |
return 0; |
| 203 |
|
|
} |
| 204 |
|
|
} |
| 205 |
|
|
|
| 206 |
|
|
sub Library::has_symbol { |
| 207 |
|
|
my ($self, $symbol) = @_; |
| 208 |
|
|
|
| 209 |
|
|
for my $sym (@{$self->{symbols}}) { |
| 210 |
|
|
if ($symbol =~ m/^\Q$sym\E/) { |
| 211 |
|
|
return 1; |
| 212 |
|
|
} |
| 213 |
|
|
} |
| 214 |
|
|
return 0; |
| 215 |
|
|
} |
| 216 |
|
|
|
| 217 |
modax-guest |
10329 |
sub Library::has_symbol_fast { |
| 218 |
|
|
my ($self, $symbol) = @_; |
| 219 |
|
|
|
| 220 |
|
|
# Don't care about anything after `(' |
| 221 |
|
|
$symbol =~ s/\(.*$//; |
| 222 |
|
|
|
| 223 |
|
|
return (exists $self->{symbhash}{$symbol}); |
| 224 |
|
|
} |
| 225 |
|
|
|
| 226 |
modax-guest |
10291 |
sub Library::to_string() { |
| 227 |
|
|
my ($self) = @_; |
| 228 |
|
|
return $self->{name} . " ( " . $self->{path} . " )"; |
| 229 |
|
|
} |
| 230 |
|
|
|
| 231 |
modax-guest |
10311 |
sub IgnoreStack::new { |
| 232 |
|
|
return bless( { stack => [] }, shift() ); |
| 233 |
|
|
} |
| 234 |
modax-guest |
10291 |
|
| 235 |
modax-guest |
10311 |
sub IgnoreStack::process_line { |
| 236 |
|
|
my ($self, $line) = @_; |
| 237 |
|
|
my $stack = $self->{stack}; |
| 238 |
|
|
|
| 239 |
|
|
if ($line =~ m/^\s*((end)?(foreach|function|if|macro|while))\s*[(]/i) { |
| 240 |
|
|
my $isend = defined $2; |
| 241 |
|
|
my $cmd = uc($3); |
| 242 |
|
|
|
| 243 |
|
|
if ($isend) { |
| 244 |
|
|
if (@$stack) { |
| 245 |
|
|
my $s = pop @$stack; |
| 246 |
|
|
print STDERR "$MSG_PREFIX There is something wrong with IgnoreStack:\n", |
| 247 |
|
|
"$MSG_PREFIX stack top ($s) does not match end command ($cmd)\n" if $s ne $cmd; |
| 248 |
|
|
} else { |
| 249 |
|
|
print STDERR "$MSG_PREFIX IgnoreStack is empty but got 'end$cmd'. A bug probably.\n"; |
| 250 |
|
|
} |
| 251 |
|
|
} else { |
| 252 |
|
|
push @$stack, $cmd; |
| 253 |
|
|
} |
| 254 |
|
|
return 1; # Processed |
| 255 |
|
|
} else { |
| 256 |
|
|
return 0; |
| 257 |
|
|
} |
| 258 |
|
|
} |
| 259 |
|
|
|
| 260 |
|
|
sub IgnoreStack::is_empty { |
| 261 |
|
|
return scalar(@{shift()->{stack}}) == 0; |
| 262 |
|
|
} |
| 263 |
|
|
|
| 264 |
|
|
sub IgnoreStack::dump_stack { |
| 265 |
|
|
my $self = shift; |
| 266 |
|
|
print "Ignore stack dump:\n"; |
| 267 |
|
|
for my $e (@{$self->{stack}}) { |
| 268 |
|
|
print " $e\n"; |
| 269 |
|
|
} |
| 270 |
|
|
} |
| 271 |
|
|
|
| 272 |
modax-guest |
10291 |
sub determine_needed_libs { |
| 273 |
|
|
my ($alllibs, $undefrefs) = @_; |
| 274 |
|
|
my @_libs; |
| 275 |
|
|
my @libs = (); |
| 276 |
modax-guest |
10329 |
my @notfound; |
| 277 |
modax-guest |
10291 |
|
| 278 |
modax-guest |
10329 |
# Try fast search first |
| 279 |
modax-guest |
10348 |
nextfastref: |
| 280 |
modax-guest |
10291 |
for my $ref (@$undefrefs) { |
| 281 |
|
|
my $lib; |
| 282 |
|
|
for my $lib (@$alllibs) { |
| 283 |
modax-guest |
10329 |
if ($lib->has_symbol_fast($ref)) { |
| 284 |
|
|
push @_libs, $lib; |
| 285 |
modax-guest |
10348 |
next nextfastref; |
| 286 |
modax-guest |
10329 |
} |
| 287 |
|
|
} |
| 288 |
|
|
push @notfound, $ref; |
| 289 |
|
|
} |
| 290 |
|
|
|
| 291 |
|
|
# Then try slow one |
| 292 |
modax-guest |
10348 |
nextslowref: |
| 293 |
modax-guest |
10329 |
for my $ref (@notfound) { |
| 294 |
|
|
my $lib; |
| 295 |
|
|
for my $lib (@$alllibs) { |
| 296 |
modax-guest |
10291 |
if ($lib->has_symbol($ref)) { |
| 297 |
|
|
push @_libs, $lib; |
| 298 |
modax-guest |
10348 |
next nextslowref; |
| 299 |
modax-guest |
10291 |
} |
| 300 |
|
|
} |
| 301 |
|
|
} |
| 302 |
|
|
|
| 303 |
|
|
# Kill dupes |
| 304 |
|
|
my $prev = ""; |
| 305 |
|
|
for (sort { $a->{cmake_target} cmp $b->{cmake_target} } @_libs) { |
| 306 |
|
|
if ($_ ne $prev) { |
| 307 |
|
|
push @libs, $_; |
| 308 |
|
|
$prev = $_; |
| 309 |
|
|
} |
| 310 |
|
|
} |
| 311 |
|
|
|
| 312 |
|
|
return \@libs; |
| 313 |
|
|
} |
| 314 |
|
|
|
| 315 |
|
|
sub write_target_link_libs { |
| 316 |
|
|
my ($dir, $target, $libs, $do_backups) = @_; |
| 317 |
|
|
my $cmakelists = File::Spec->catfile($dir, "CMakeLists.txt"); |
| 318 |
|
|
|
| 319 |
modax-guest |
10348 |
|
| 320 |
modax-guest |
10358 |
my $normlibs = ""; # Unconditional (normal) libs |
| 321 |
modax-guest |
10348 |
my $condlibs = ""; # Conditional linking |
| 322 |
modax-guest |
10358 |
my $strlibs = join(" ", map($_->{cmake_target}, @$libs)); # Both |
| 323 |
modax-guest |
10348 |
for (@$libs) { |
| 324 |
modax-guest |
10358 |
if (defined $_->{condition}) { |
| 325 |
modax-guest |
10348 |
$condlibs .= sprintf("if (%s)\n target_link_libraries($target %s)\nendif (%s)\n", |
| 326 |
|
|
$_->{condition}, $_->{cmake_target}, $_->{condition}); |
| 327 |
|
|
} else { |
| 328 |
modax-guest |
10358 |
$normlibs .= " " if ($normlibs); |
| 329 |
|
|
$normlibs .= $_->{cmake_target}; |
| 330 |
modax-guest |
10348 |
} |
| 331 |
|
|
} |
| 332 |
|
|
|
| 333 |
modax-guest |
10291 |
if (-r $cmakelists) { |
| 334 |
|
|
my @contents; |
| 335 |
modax-guest |
10311 |
my @ignored; |
| 336 |
modax-guest |
10291 |
my $found = 0; |
| 337 |
modax-guest |
10311 |
# Ignore directive inside if/endif, while/endwhile etc. blocks |
| 338 |
|
|
my $ignstack = new IgnoreStack; |
| 339 |
modax-guest |
10291 |
|
| 340 |
|
|
# Read and change |
| 341 |
|
|
open(CMAKELISTS, "<$cmakelists"); |
| 342 |
|
|
while (<CMAKELISTS>) { |
| 343 |
modax-guest |
10311 |
if (!$found && !$ignstack->process_line($_) && |
| 344 |
modax-guest |
10363 |
m/^\s*((?:target_link_libraries|kdepim4_link_unique_libraries)\s*\(\s*$target\s+)(.*?)(\s*\).*)?$/i) { |
| 345 |
modax-guest |
10311 |
|
| 346 |
modax-guest |
10348 |
my $newline; |
| 347 |
modax-guest |
10358 |
if ($normlibs) { |
| 348 |
modax-guest |
10348 |
# Fix it |
| 349 |
|
|
$newline = $1; |
| 350 |
|
|
my $end = $3; |
| 351 |
|
|
$newline .= $2 if ($2); |
| 352 |
|
|
$newline .= " " if ($newline !~ m/\s+$/); |
| 353 |
modax-guest |
10358 |
$newline .= $normlibs; |
| 354 |
modax-guest |
10348 |
$newline .= $end if ($end); |
| 355 |
|
|
$newline .= "\n"; |
| 356 |
|
|
} else { |
| 357 |
|
|
$newline = $_; |
| 358 |
|
|
} |
| 359 |
|
|
$newline .= "\n" . $condlibs . "\n" if ($condlibs); |
| 360 |
modax-guest |
10311 |
|
| 361 |
|
|
if ($ignstack->is_empty()) { |
| 362 |
|
|
push @contents, $newline; |
| 363 |
|
|
$found = $.; |
| 364 |
|
|
} else { |
| 365 |
|
|
# Save for later use |
| 366 |
|
|
push @ignored, { found => $., newline => $newline }; |
| 367 |
|
|
push @contents, $_; |
| 368 |
|
|
} |
| 369 |
modax-guest |
10291 |
} else { |
| 370 |
|
|
push @contents, $_; |
| 371 |
|
|
} |
| 372 |
|
|
} |
| 373 |
|
|
close(CMAKELISTS); |
| 374 |
|
|
|
| 375 |
modax-guest |
10311 |
if (!$ignstack->is_empty()) { |
| 376 |
|
|
$ignstack->dump_stack(); |
| 377 |
|
|
print STDERR "$cmakelists has been processed but ignore stack is not empty. Probably a bug!\n"; |
| 378 |
|
|
} |
| 379 |
|
|
|
| 380 |
|
|
if (!$found && @ignored == 1) { |
| 381 |
|
|
# That's probably it as there were no other candidates. Replace |
| 382 |
|
|
$found = ${ignored[0]}->{found}; |
| 383 |
|
|
my $newline = ${ignored[0]}->{newline}; |
| 384 |
|
|
$contents[$found-1] = $newline; |
| 385 |
modax-guest |
10329 |
} elsif (!$found && @ignored > 1) { |
| 386 |
modax-guest |
10311 |
print "$MSG_PREFIX More (", scalar(@ignored), ") than 1 target_link_libraries() found in the IF/WHILE etc. blocks\n"; |
| 387 |
|
|
} |
| 388 |
|
|
|
| 389 |
modax-guest |
10291 |
if (!$found) { |
| 390 |
modax-guest |
10334 |
print "$MSG_PREFIX $cmakelists could not be corrected (needed '$strlibs' for target '$target'). Respective target_link_libraries() was not found $MSG_PREFIX\n"; |
| 391 |
|
|
return 0; |
| 392 |
modax-guest |
10291 |
} else { |
| 393 |
|
|
# Write |
| 394 |
|
|
system("$QUILT add '$cmakelists'"); |
| 395 |
|
|
open(CMAKELISTS, ">$cmakelists.tmp"); |
| 396 |
|
|
for (@contents) { |
| 397 |
|
|
print CMAKELISTS $_; |
| 398 |
|
|
} |
| 399 |
|
|
close(CMAKELISTS); |
| 400 |
|
|
if ($do_backups) { |
| 401 |
|
|
File::Copy::move("$cmakelists", "$cmakelists.orig") |
| 402 |
|
|
or die "Could not rename file '$cmakelists' -> '$cmakelists.org'"; |
| 403 |
|
|
} |
| 404 |
|
|
File::Copy::move("$cmakelists.tmp", "$cmakelists") |
| 405 |
|
|
or die "Could not rename file '$cmakelists.tmp' -> '$cmakelists'"; |
| 406 |
|
|
print "$MSG_PREFIX $cmakelists edited. Added libraries '$strlibs' for target $target\n"; |
| 407 |
modax-guest |
10334 |
return 1; |
| 408 |
modax-guest |
10291 |
} |
| 409 |
|
|
} else { |
| 410 |
|
|
die "$cmakelists for target $target could not be found. Something is wrong"; |
| 411 |
|
|
} |
| 412 |
|
|
} |
| 413 |
|
|
|
| 414 |
modax-guest |
10334 |
sub invoke_edit { |
| 415 |
|
|
my ($cmakelists, $do_invoke) = @_; |
| 416 |
|
|
my $quilt_cmd = "$QUILT edit '$cmakelists'"; |
| 417 |
|
|
|
| 418 |
|
|
if ($do_invoke) { |
| 419 |
|
|
print "$MSG_PREFIX Press ENTER to edit '$cmakelists' or ^C to cancel ..."; |
| 420 |
|
|
<>; |
| 421 |
|
|
return (system($quilt_cmd) == 0) ? 0 : 2; |
| 422 |
|
|
} else { |
| 423 |
|
|
print "$MSG_PREFIX You probably want to run the command to correct the problem yourself: \n", |
| 424 |
|
|
"$MSG_PREFIX \$ $quilt_cmd\n"; |
| 425 |
|
|
return 2; |
| 426 |
|
|
} |
| 427 |
|
|
} |
| 428 |
|
|
|
| 429 |
modax-guest |
10291 |
sub build_and_fix { |
| 430 |
modax-guest |
10329 |
my ($sourcedir, $builddir, $buildcmd, $exec_in_build_dir, $do_backups, $invoke_edit) = @_; |
| 431 |
modax-guest |
10291 |
|
| 432 |
|
|
my $bdir; |
| 433 |
|
|
my $btarget; |
| 434 |
|
|
my $islderror = 0; |
| 435 |
|
|
my @undefrefs; |
| 436 |
|
|
my $link_cmd = 0; |
| 437 |
|
|
|
| 438 |
modax-guest |
10329 |
if ($exec_in_build_dir) { |
| 439 |
modax-guest |
10291 |
chdir $builddir; |
| 440 |
|
|
} else { |
| 441 |
|
|
chdir $sourcedir; |
| 442 |
|
|
} |
| 443 |
|
|
|
| 444 |
modax-guest |
10329 |
open(MAKE, "$buildcmd 2>&1 |") or die "Unable to run `$buildcmd' in $builddir"; |
| 445 |
|
|
|
| 446 |
modax-guest |
10291 |
while (<MAKE>) { |
| 447 |
|
|
# [ 39%] Building CXX object kwin/kcmkwin/kwindecoration/CMakeFiles |
| 448 |
|
|
print $_; |
| 449 |
|
|
chomp; |
| 450 |
|
|
if ($link_cmd) { |
| 451 |
|
|
#cd /buildd/kdebase-workspace/obj-x86_64-linux-gnu/kwin/kcmkwin/kwindecoration && /usr/bin/cmake -E cmake_link_script CMakeFiles/kcm_kwindecoration.dir/link.txt $MSG_PREFIXverbose=1 |
| 452 |
modax-guest |
10654 |
if (m#^(?:cd ["']?(.*?)["']? && )?.*/cmake -E cmake_link_script CMakeFiles/(.*?)\.dir/#) { |
| 453 |
modax-guest |
10291 |
$btarget = $2; |
| 454 |
modax-guest |
10654 |
$bdir = ($1) ? File::Spec->abs2rel(Cwd::realpath($1), $builddir) : "."; |
| 455 |
modax-guest |
10291 |
$link_cmd = 0; |
| 456 |
|
|
} else { |
| 457 |
|
|
die "Unrecognized link line"; |
| 458 |
|
|
} |
| 459 |
modax-guest |
10535 |
} elsif (m/\[\s*\d+\%\] Building (.*) object ["']?(.*?)["']?\s*$/) { |
| 460 |
modax-guest |
10291 |
$bdir = $2; |
| 461 |
|
|
if ($bdir =~ m#CMakeFiles/(.*?)\.dir/#) { |
| 462 |
|
|
$btarget = $1; |
| 463 |
|
|
} else { |
| 464 |
|
|
die "Could not extract target from $bdir"; |
| 465 |
|
|
} |
| 466 |
|
|
$bdir =~ s#/CMakeFiles/.*##; |
| 467 |
modax-guest |
10311 |
} elsif (m/^Linking (.*) (shared (module|library)|executable) /) { |
| 468 |
modax-guest |
10291 |
$link_cmd = 1; |
| 469 |
|
|
} elsif (m/undefined reference to `(.*)'$/) { |
| 470 |
|
|
# undefined reference to `QDBusMessage::createSignal(QString const&, QString const&, QString const&)' |
| 471 |
|
|
push @undefrefs, $1; |
| 472 |
|
|
} elsif (m/collect\d+: ld returned \d+ exit status/) { |
| 473 |
|
|
$islderror = 1; |
| 474 |
|
|
} |
| 475 |
|
|
} |
| 476 |
|
|
|
| 477 |
|
|
close(MAKE); |
| 478 |
|
|
|
| 479 |
|
|
chdir $sourcedir; |
| 480 |
|
|
|
| 481 |
|
|
# Try to correct the error |
| 482 |
modax-guest |
10311 |
# print $MSG_PREFIX, $islderror, $bdir, $btarget, @undefrefs; |
| 483 |
modax-guest |
10291 |
if ($islderror && $bdir && $btarget && @undefrefs) { |
| 484 |
|
|
my $libs = determine_needed_libs(\@LIBS, \@undefrefs); |
| 485 |
|
|
if (@$libs) { |
| 486 |
modax-guest |
10334 |
if (write_target_link_libs($bdir, $btarget, $libs, $do_backups)) { |
| 487 |
|
|
return 0; # again |
| 488 |
|
|
} else { |
| 489 |
|
|
return invoke_edit("$bdir/CMakeLists.txt", $invoke_edit); |
| 490 |
|
|
} |
| 491 |
modax-guest |
10291 |
} else { |
| 492 |
|
|
my $cmakelists = "$bdir/CMakeLists.txt"; |
| 493 |
|
|
print "$MSG_PREFIX Could not resolve linkage problem automatically. Undefined symbols have not been recognized\n"; |
| 494 |
|
|
print "$MSG_PREFIX Target: $btarget; CMakeLists.txt: $cmakelists", "\n"; |
| 495 |
modax-guest |
10334 |
return invoke_edit($cmakelists, $invoke_edit); |
| 496 |
modax-guest |
10291 |
} |
| 497 |
|
|
} else { |
| 498 |
|
|
#print $islderror, ", ", $bdir, ", ", $btarget, ", ", @undefrefs, "\n"; |
| 499 |
|
|
print "$MSG_PREFIX Building completed successfully or unrecognized error\n"; |
| 500 |
|
|
return 1; |
| 501 |
|
|
} |
| 502 |
|
|
} |
| 503 |
|
|
|
| 504 |
|
|
sub load_libraries { |
| 505 |
|
|
my $LIBS = shift; |
| 506 |
|
|
|
| 507 |
|
|
print "$MSG_PREFIX Reading dynamic symbol table of ", scalar(@$LIBS), " shared libraries... ", "\n"; |
| 508 |
|
|
my $count = 0; |
| 509 |
|
|
for my $lib (@$LIBS) { |
| 510 |
|
|
print "$MSG_PREFIX Loading ", $lib->to_string(), " ... "; |
| 511 |
|
|
if ($lib->load()) { |
| 512 |
|
|
print "success\n"; |
| 513 |
|
|
$count++; |
| 514 |
|
|
} else { |
| 515 |
|
|
print "failed (path is not readable or has no symbols)\n"; |
| 516 |
|
|
} |
| 517 |
|
|
} |
| 518 |
|
|
return $count; |
| 519 |
|
|
} |
| 520 |
|
|
|
| 521 |
|
|
sub check_environment { |
| 522 |
modax-guest |
10655 |
my ($builddir, $buildcmd, $patchname) = @_; |
| 523 |
modax-guest |
10291 |
|
| 524 |
|
|
print "$MSG_PREFIX Script Version v$main::VERSION $MSG_PREFIX", "\n"; |
| 525 |
|
|
|
| 526 |
modax-guest |
10655 |
if ($buildcmd =~ m/^debian/) { |
| 527 |
|
|
system("dh_testdir") == 0 or die "$MSG_PREFIX Please run this script from the debianized source tree $MSG_PREFIX\n"; |
| 528 |
|
|
} |
| 529 |
modax-guest |
10291 |
|
| 530 |
|
|
my $toppatch = `$QUILT top`; |
| 531 |
|
|
chomp $toppatch; |
| 532 |
|
|
if (!defined $toppatch || $toppatch ne $patchname) { |
| 533 |
|
|
die "$MSG_PREFIX Quilt top patch must be named '$patchname' when this script is run. Please either:\n" . |
| 534 |
|
|
"$MSG_PREFIX \$ $QUILT push $patchname\n" . |
| 535 |
|
|
"$MSG_PREFIX \$ $QUILT new $patchname\n"; |
| 536 |
|
|
} |
| 537 |
|
|
} |
| 538 |
|
|
|
| 539 |
|
|
sub get_gnu_build_type { |
| 540 |
|
|
my $buildtype = `dpkg-architecture -qDEB_BUILD_GNU_TYPE`; |
| 541 |
|
|
chomp $buildtype; |
| 542 |
|
|
return $buildtype; |
| 543 |
|
|
} |
| 544 |
|
|
|
| 545 |
|
|
############## Main loop ############################## |
| 546 |
|
|
|
| 547 |
modax-guest |
10655 |
$main::VERSION = "0.5.6"; |
| 548 |
modax-guest |
10291 |
|
| 549 |
|
|
my $sourcedir = Cwd::getcwd(); |
| 550 |
|
|
my $builddir = "obj-" . get_gnu_build_type(); |
| 551 |
modax-guest |
10329 |
my $exec_in_build_dir = 0; |
| 552 |
|
|
my $buildcmd = "debian/rules build"; |
| 553 |
modax-guest |
10291 |
my $patchname = "97_fix_target_link_libraries.diff"; |
| 554 |
|
|
my $do_backups = 0; |
| 555 |
|
|
my $show_help = 0; |
| 556 |
|
|
my $invoke_edit = 0; |
| 557 |
|
|
|
| 558 |
|
|
if (GetOptions( |
| 559 |
|
|
"help|h|?" => \$show_help, |
| 560 |
|
|
"build-dir|b=s" => \$builddir, |
| 561 |
|
|
"build-command|c=s" => \$buildcmd, |
| 562 |
modax-guest |
10329 |
"invoke-edit|edit|e!" => \$invoke_edit, |
| 563 |
|
|
"exec-in-build-dir|i!" => \$exec_in_build_dir, |
| 564 |
modax-guest |
10291 |
"patch-name|p=s" => \$patchname, |
| 565 |
|
|
"do-backups|backup!" => \$do_backups, |
| 566 |
modax-guest |
10655 |
"quilt|q=s" => \$QUILT, |
| 567 |
modax-guest |
10291 |
)) { |
| 568 |
|
|
|
| 569 |
|
|
pod2usage(-exitval => 1, -verbose => 2, -noperldoc => 1) if ($show_help); |
| 570 |
|
|
|
| 571 |
|
|
$builddir = Cwd::realpath(File::Spec->catdir($sourcedir, $builddir)); |
| 572 |
modax-guest |
10655 |
check_environment($builddir, $buildcmd, $patchname); |
| 573 |
modax-guest |
10291 |
|
| 574 |
|
|
if (load_libraries(\@LIBS) == 0) { |
| 575 |
|
|
die "Error: No dynamic symbols found in predefined libraries"; |
| 576 |
|
|
} |
| 577 |
|
|
|
| 578 |
|
|
my $ret; |
| 579 |
modax-guest |
10329 |
while (!($ret = build_and_fix($sourcedir, $builddir, $buildcmd, $exec_in_build_dir, $do_backups, $invoke_edit))) { |
| 580 |
modax-guest |
10291 |
print "$MSG_PREFIX Linkage problem fixed, rebuilding again\n"; |
| 581 |
|
|
} |
| 582 |
|
|
|
| 583 |
|
|
if ($ret == 1) { |
| 584 |
|
|
print "$MSG_PREFIX If build process is complete, you may want to run the following command to build/refresh the patch\n" . |
| 585 |
fabo |
10524 |
"$MSG_PREFIX \$ $QUILT refresh -p ab --no-timestamps --no-index\n"; |
| 586 |
modax-guest |
10291 |
if ($do_backups) { |
| 587 |
|
|
print "$MSG_PREFIX Also run the following command to cleanup backup files:\n" . |
| 588 |
|
|
"$MSG_PREFIX \$ find -name 'CMakeLists.txt.orig' -delete\n"; |
| 589 |
|
|
} |
| 590 |
|
|
} |
| 591 |
|
|
|
| 592 |
|
|
exit 0; |
| 593 |
|
|
} else { |
| 594 |
|
|
podusage(-exitval => 2, -verbose => 1); |
| 595 |
|
|
} |
| 596 |
|
|
|