rev 10291 - scripts
Modestas Vainius
modax-guest at alioth.debian.org
Sat Apr 26 22:44:43 UTC 2008
Author: modax-guest
Date: 2008-04-26 22:44:41 +0000 (Sat, 26 Apr 2008)
New Revision: 10291
Added:
scripts/autofixtll
Log:
* Initial version of the script that is supposed to automatically fix the most of veeeerrryy boring linking failures with cleaned up KDELibs and Qt4 deps.
* Read `autofixtll -h` for more information.
* I made some changes that I've not tested yet so it might not work. I'll test very soon though.
Added: scripts/autofixtll
===================================================================
--- scripts/autofixtll (rev 0)
+++ scripts/autofixtll 2008-04-26 22:44:41 UTC (rev 10291)
@@ -0,0 +1,449 @@
+#!/usr/bin/perl -w
+
+=head1 NAME
+
+B<autofixtll> - auto-correct CMake TARGET_LINK_LIBRARIES directive according to
+the information provided by GNU ld linker S<I<'undefined references'>> errors.
+
+=head1 SYNOPSIS
+
+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>]
+
+=head1 DESCRIPTION
+
+B<autofixtll> is capable of correcting most linking failures caused by
+S<'undefined references'> linker errors. The script should be executed from the
+extracted debian source tree with all build dependences installed in the
+environment. This script is a wrapper around S<`debian/rules build'> or
+whatever command you specify with B<--build-command> option.
+
+B<autofixtll> depends on B<quilt> to incrementally build up a patch of the
+changes it does. The script assumes that quilt patches are located in
+debian/patches. The default name of the patch is
+S<I<"97_fix_target_link_libraries.diff">> (it can be changed with the
+[B<--patch-name> option). This patch must have already pushed to the "quilt
+top" when this script is executed.
+
+First of all, the script loads dynamic symbol names of the libraries specified
+in the @LIBS array. Then it starts building process and keeps monitoring it
+while looking for the "undefined references" errors from the linker (only GNU
+ld syntax is supported). Then it tries to match undefined symbol names with the
+symbols loaded from the libraries in the @LIBS array. If matches are found, it
+tries to update TARGET_LINK_LIBRARIES command in the respective CMakeLists.txt
+file adding missing libraries. Finally, the build process is restarted.
+
+This loop continues until build completes successfully or the error which
+B<autofixtll> can't handle occurs. In latter case, a user will need to `quilt
+edit' the respective file manually or add more libs to the @LIBS array. Then
+the script can be restarted again (or use --invoke-edit option to invoke
+I<quilt edit> and restart build for you automatically).
+
+CMake verbose output must be enabled for B<autofixtll> to work reliably.
+
+B<autofixtll> was written to help maintainer resolve build failures of KDE
+applications which were introduced by the clean up of KDELibs recursive library
+dependences.
+
+=head1 OPTIONS
+
+=over 4
+
+=item B<-b> I<dir>, B<--build-dir>=I<dir>
+
+Specify a custom build directory. It must be relative the current (source)
+directory. Default is as returned by S<I<"obj-`dpkg-architecture
+-qDEB_BUILD_GNU_TYPE`>">.
+
+=item B<-c> I<command>, B<--build-command>=I<command>
+
+The command which should be run to build the source. If the command is not
+specific, `debian/rules build' will be executed from the B<source tree>.
+However, if you specify the command, it will be from the B<build tree>.
+
+=item B<-p>, B<--patch-name>=I<name>
+
+The name of the quilt patch in which all changes made by the script will be
+stored. Default is I<97_fix_target_link_libraries.diff>.
+
+=item B<--do-backups>, B<--backup>
+
+Whether to backup CMakeLists.txt as CMakeLists.txt.orig before auto-modifying
+it.
+
+=item B<--invoke-edit>, B<--edit>, B<-e>
+
+Invoke `quilt edit' on the respective CMakeLists.txt when undefined references
+cannot be automatically resolved and restart build process immediately when the
+editor is closed.
+
+=back
+
+=head1 LICENSE
+
+This program is free software: you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+On B<Debian> systems, the complete text of the GNU GPL v3 can be found in the
+file F</usr/share/common-licenses/GPL-3>
+
+=head1 AUTHORS
+
+Written by Modestas Vainius <modestas at vainius.eu>
+
+=cut
+
+use strict;
+use Cwd qw(getcwd realpath);
+use File::Spec;
+use File::Copy;
+use Getopt::Long;
+use Pod::Usage;
+use FileHandle;
+use IPC::Open2;
+
+#### Please add predefined libraries to load dynamic symbol from here.
+# new Library(name, cmake_target, [path]). If path is not specified,
+# it's path=/usr/lib/$name.so. Use '' quotes.
+my @LIBS = (
+ new Library('QtDBus', '${QT_QTDBUS_LIBRARY}'),
+ new Library('QtNetwork', '${QT_QTNETWORK_LIBRARY}'),
+ new Library('QtXml', '${QT_QTXML_LIBRARY}'),
+ new Library('QtSvg', '${QT_QTSVG_LIBRARY}'),
+ new Library('X11', '${X11_X11_LIB}'),
+ new Library('z', '${ZLIB_LIBRARY}'),
+);
+
+#### Some defaults
+my $QUILT = "QUILT_PATCHES=debian/patches quilt";
+my $MSG_PREFIX = "--=--";
+
+############### Implementation ###############################
+
+sub Library::new {
+ my ($cls, $name, $cmake_target, $path) = @_;
+ if (!defined $path) {
+ $path = "/usr/lib/lib$name.so";
+ }
+ return bless( { name => $name, path => $path, cmake_target => $cmake_target }, $cls);
+}
+
+sub Library::load {
+ my $self = shift;
+ my @symbols;
+ my @cpp_symbols;
+ if (-r $self->{path}) {
+ open(OBJDUMP, "objdump -T '$self->{path}' |") or die "Unable to run objdump";
+ while(<OBJDUMP>) {
+ # 0000000000021e80 g DF .text 00000000000000f9 Base _XSendClientPrefix
+ if (m/^[0-9a-f]+\s+g\s+DF\s+\.text\s+[0-9a-f]+\s+Base\s+(.*)$/) {
+ my $symbol = $1;
+ if ($symbol =~ m/^_Z/) {
+ # It is C++ symbol. Needs demangling.
+ push @cpp_symbols, $symbol;
+ } else {
+ # FIXME: use hashes?
+ push @symbols, $symbol;
+ }
+ }
+ }
+ close(OBJDUMP);
+ }
+ if (@cpp_symbols) {
+ open2(*OUT, *IN, "c++filt") or die "Unable to run c++filt";
+ for (@cpp_symbols) {
+ print IN "$_\n";
+ }
+ close(IN);
+ my $count = 0;
+ while(<OUT>) {
+ chomp;
+ # Don't care about everything after `('
+ s/\(.*$//;
+ s/^non-virtual thunk to\s+//;
+ push @symbols, $_;
+ $count++;
+ }
+ close(OUT);
+ print(STDERR "Lost a few C++ symbols (", (scalar(@cpp_symbols) - $count),
+ ") while demangling ", $self->to_string(), "\n") unless ($count == scalar(@cpp_symbols));
+ }
+ if (@symbols) {
+ $self->{symbols} = \@symbols;
+ return scalar(@symbols);
+ } else {
+ $self->{symbols} = [];
+ return 0;
+ }
+}
+
+sub Library::has_symbol {
+ my ($self, $symbol) = @_;
+
+ for my $sym (@{$self->{symbols}}) {
+ if ($symbol =~ m/^\Q$sym\E/) {
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+sub Library::to_string() {
+ my ($self) = @_;
+ return $self->{name} . " ( " . $self->{path} . " )";
+}
+
+
+sub determine_needed_libs {
+ my ($alllibs, $undefrefs) = @_;
+ my @_libs;
+ my @libs = ();
+
+ for my $ref (@$undefrefs) {
+ my $lib;
+ for my $lib (@$alllibs) {
+ if ($lib->has_symbol($ref)) {
+ push @_libs, $lib;
+ next;
+ }
+ }
+ }
+
+ # Kill dupes
+ my $prev = "";
+ for (sort { $a->{cmake_target} cmp $b->{cmake_target} } @_libs) {
+ if ($_ ne $prev) {
+ push @libs, $_;
+ $prev = $_;
+ }
+ }
+
+ return \@libs;
+}
+
+sub write_target_link_libs {
+ my ($dir, $target, $libs, $do_backups) = @_;
+ my $cmakelists = File::Spec->catfile($dir, "CMakeLists.txt");
+ my $strlibs = join(" ", map($_->{cmake_target}, @$libs));
+
+ if (-r $cmakelists) {
+ my @contents;
+ my $found = 0;
+
+ # Read and change
+ open(CMAKELISTS, "<$cmakelists");
+ while (<CMAKELISTS>) {
+ if (!$found && m/(target_link_libraries\s*\(\s*$target\s+)(.*?)(\s*\).*)?$/i) {
+ # Fix it
+ my $newline = $1;
+ my $end = $3;
+ $newline .= $2 if ($2);
+ $newline .= " " if ($newline !~ m/\s+$/);
+ $newline .= $strlibs;
+ $newline .= $end if ($end);
+ $newline .= "\n";
+ push @contents, $newline;
+ $found = $.;
+ } else {
+ push @contents, $_;
+ }
+ }
+ close(CMAKELISTS);
+
+ if (!$found) {
+ die "$MSG_PREFIX $cmakelists could not be corrected (needed '$strlibs' for target '$target'). Respective target_link_libraries() was not found $MSG_PREFIX";
+ } else {
+ # Write
+ system("$QUILT add '$cmakelists'");
+ open(CMAKELISTS, ">$cmakelists.tmp");
+ for (@contents) {
+ print CMAKELISTS $_;
+ }
+ close(CMAKELISTS);
+ if ($do_backups) {
+ File::Copy::move("$cmakelists", "$cmakelists.orig")
+ or die "Could not rename file '$cmakelists' -> '$cmakelists.org'";
+ }
+ File::Copy::move("$cmakelists.tmp", "$cmakelists")
+ or die "Could not rename file '$cmakelists.tmp' -> '$cmakelists'";
+ print "$MSG_PREFIX $cmakelists edited. Added libraries '$strlibs' for target $target\n";
+ }
+ } else {
+ die "$cmakelists for target $target could not be found. Something is wrong";
+ }
+}
+
+sub build_and_fix {
+ my ($sourcedir, $builddir, $buildcmd, $do_backups, $invoke_edit) = @_;
+
+ my $bdir;
+ my $btarget;
+ my $islderror = 0;
+ my @undefrefs;
+ my $link_cmd = 0;
+
+ if (defined $buildcmd) {
+ chdir $builddir;
+ open(MAKE, "$buildcmd 2>&1 |") or die "Unable to run `$buildcmd' in $builddir";
+ } else {
+ chdir $sourcedir;
+ open(MAKE, "debian/rules build 2>&1 |") or die "Unable to run `$buildcmd' in $builddir";
+ }
+
+ while (<MAKE>) {
+ # [ 39%] Building CXX object kwin/kcmkwin/kwindecoration/CMakeFiles
+ print $_;
+ chomp;
+ if ($link_cmd) {
+ #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
+ if (m#^cd (.*) && .*/cmake -E cmake_link_script CMakeFiles/(.*?)\.dir/#) {
+ $btarget = $2;
+ $bdir = File::Spec->abs2rel(Cwd::realpath($1), $builddir);
+ $link_cmd = 0;
+ } else {
+ die "Unrecognized link line";
+ }
+ } elsif (m/\[\s*\d+\%\] Building (.*) object (.*)/) {
+ $bdir = $2;
+ if ($bdir =~ m#CMakeFiles/(.*?)\.dir/#) {
+ $btarget = $1;
+ } else {
+ die "Could not extract target from $bdir";
+ }
+ $bdir =~ s#/CMakeFiles/.*##;
+ } elsif (m/^Linking (.*) (shared module|executable) /) {
+ $link_cmd = 1;
+ } elsif (m/undefined reference to `(.*)'$/) {
+ # undefined reference to `QDBusMessage::createSignal(QString const&, QString const&, QString const&)'
+ push @undefrefs, $1;
+ } elsif (m/collect\d+: ld returned \d+ exit status/) {
+ $islderror = 1;
+ }
+ }
+
+ close(MAKE);
+
+ chdir $sourcedir;
+
+ # Try to correct the error
+ if ($islderror && $bdir && $btarget && @undefrefs) {
+ my $libs = determine_needed_libs(\@LIBS, \@undefrefs);
+ if (@$libs) {
+ write_target_link_libs($bdir, $btarget, $libs, $do_backups);
+ return 0; # again
+ } else {
+ my $cmakelists = "$bdir/CMakeLists.txt";
+ print "$MSG_PREFIX Could not resolve linkage problem automatically. Undefined symbols have not been recognized\n";
+ print "$MSG_PREFIX Target: $btarget; CMakeLists.txt: $cmakelists", "\n";
+ my $quilt_cmd = "$QUILT edit $cmakelists";
+ if ($invoke_edit) {
+ print "$MSG_PREFIX Press any key to edit '$cmakelists' or ^C to cancel ...";
+ <>;
+ system($quilt_cmd);
+ } else {
+ print "$MSG_PREFIX You probably want to run the command to correct the problem yourself: \n",
+ "$MSG_PREFIX \$ $quilt_cmd\n";
+ return 2;
+ }
+ }
+ } else {
+ #print $islderror, ", ", $bdir, ", ", $btarget, ", ", @undefrefs, "\n";
+ print "$MSG_PREFIX Building completed successfully or unrecognized error\n";
+ return 1;
+ }
+}
+
+sub load_libraries {
+ my $LIBS = shift;
+
+ print "$MSG_PREFIX Reading dynamic symbol table of ", scalar(@$LIBS), " shared libraries... ", "\n";
+ my $count = 0;
+ for my $lib (@$LIBS) {
+ print "$MSG_PREFIX Loading ", $lib->to_string(), " ... ";
+ if ($lib->load()) {
+ print "success\n";
+ $count++;
+ } else {
+ print "failed (path is not readable or has no symbols)\n";
+ }
+ }
+ return $count;
+}
+
+sub check_environment {
+ my ($builddir, $patchname) = @_;
+
+ print "$MSG_PREFIX Script Version v$main::VERSION $MSG_PREFIX", "\n";
+
+ system("dh_testdir") == 0 or die "$MSG_PREFIX Please run this script from the debianized source tree $MSG_PREFIX\n";
+
+ my $toppatch = `$QUILT top`;
+ chomp $toppatch;
+ if (!defined $toppatch || $toppatch ne $patchname) {
+ die "$MSG_PREFIX Quilt top patch must be named '$patchname' when this script is run. Please either:\n" .
+ "$MSG_PREFIX \$ $QUILT push $patchname\n" .
+ "$MSG_PREFIX \$ $QUILT new $patchname\n";
+ }
+
+ if (! -d $builddir) {
+ die "Build directory '$builddir' does not exist";
+ }
+}
+
+sub get_gnu_build_type {
+ my $buildtype = `dpkg-architecture -qDEB_BUILD_GNU_TYPE`;
+ chomp $buildtype;
+ return $buildtype;
+}
+
+############## Main loop ##############################
+
+$main::VERSION = "0.2";
+
+my $sourcedir = Cwd::getcwd();
+my $builddir = "obj-" . get_gnu_build_type();
+my $buildcmd = undef;
+my $patchname = "97_fix_target_link_libraries.diff";
+my $do_backups = 0;
+my $show_help = 0;
+my $invoke_edit = 0;
+
+if (GetOptions(
+ "help|h|?" => \$show_help,
+ "build-dir|b=s" => \$builddir,
+ "build-command|c=s" => \$buildcmd,
+ "patch-name|p=s" => \$patchname,
+ "do-backups|backup!" => \$do_backups,
+ "invoke-edit|edit|e!" => \$invoke_edit,
+ )) {
+
+ pod2usage(-exitval => 1, -verbose => 2, -noperldoc => 1) if ($show_help);
+
+ $builddir = Cwd::realpath(File::Spec->catdir($sourcedir, $builddir));
+ check_environment($builddir, $patchname);
+
+ if (load_libraries(\@LIBS) == 0) {
+ die "Error: No dynamic symbols found in predefined libraries";
+ }
+
+ my $ret;
+ while (!($ret = build_and_fix($sourcedir, $builddir, $buildcmd, $do_backups, $invoke_edit))) {
+ print "$MSG_PREFIX Linkage problem fixed, rebuilding again\n";
+ }
+
+ if ($ret == 1) {
+ print "$MSG_PREFIX If build process is complete, you may want to run the following command to build/refresh the patch\n" .
+ "$MSG_PREFIX \$ $QUILT refresh --no-index\n";
+ if ($do_backups) {
+ print "$MSG_PREFIX Also run the following command to cleanup backup files:\n" .
+ "$MSG_PREFIX \$ find -name 'CMakeLists.txt.orig' -delete\n";
+ }
+ }
+
+ exit 0;
+} else {
+ podusage(-exitval => 2, -verbose => 1);
+}
+
Property changes on: scripts/autofixtll
___________________________________________________________________
Name: svn:executable
+ *
More information about the pkg-kde-commits
mailing list