Attachment 'updatela.pl'

Download

   1 #!/usr/bin/perl
   2 # 
   3 # Copyright (C) 2010 Sébastien Granjoux  <seb.sfo@free.fr>
   4 # 
   5 # This program is free software; you can redistribute it and/or modify
   6 # it under the terms of the GNU General Public License as published by
   7 # the Free Software Foundation; either version 2 of the License, or
   8 # (at your option) any later version.
   9 #
  10 # This program is distributed in the hope that it will be useful,
  11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 # GNU Library General Public License for more details.
  14 #
  15 # You should have received a copy of the GNU General Public License
  16 # along with this program; if not, write to the Free Software
  17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18 
  19 #
  20 # This program reads all libtool library files (*.la) in the directory used as
  21 # argument. Then copy and update all needed libtool library files to make sure
  22 # that the libraries in the directory are used.
  23 
  24 use strict;
  25 use File::Basename;
  26 use File::Copy;
  27 use Getopt::Long;
  28 
  29 # Get command line argument
  30 my $dry_run = 0;
  31 my $help = 0;
  32 
  33 GetOptions ("n|dry-run" => \$dry_run, "h|help" => \$help);
  34 
  35 my $LibraryDirectory = $ARGV[0];
  36 
  37 if (!defined($LibraryDirectory))
  38 {
  39 	print "updatela V0.1\n";
  40 	print "\nUsage: updatela [-n|--dry-run] local_library_directory";
  41 	print "\nUpdate all libtool .la file in local directory to use local libraries\n";
  42 	print "\n\t-n, --dry-run\t\tDo not change anything on disk\n";
  43 	exit 0;
  44 }
  45 
  46 print "\nSearching Local libraries...\n";
  47 my %LocalLibraries = GetLibraries($LibraryDirectory);
  48 my %LocalDependencies = %LocalLibraries;
  49 my $lib;
  50 foreach $lib (keys(%LocalLibraries))
  51 {
  52 	print "\t$lib\n";
  53 }
  54 
  55 print "\nSearching Local Dependencies...\n";
  56 my %Dependencies = %LocalLibraries;
  57 my @NewDependencies = ();
  58 my $new_local = 1;
  59 while ($new_local || @NewDependencies)
  60 {
  61 	my @CheckDependencies;
  62 
  63 	if ($new_local)
  64 	{
  65 		@CheckDependencies = values(%Dependencies);
  66 		$new_local = 0;
  67 	}
  68 	else
  69 	{
  70 		@CheckDependencies = @NewDependencies;
  71 	}
  72 
  73 	@NewDependencies = ();	
  74 	foreach $lib (@CheckDependencies)
  75 	{
  76 		my $dep;
  77 		my @LibraryDependencies = GetDependencies ($lib);
  78 
  79 		foreach $dep (@LibraryDependencies)
  80 		{
  81 			if (!exists $Dependencies{basename($dep)})
  82 			{
  83 				push (@NewDependencies, $dep);
  84 				$Dependencies{basename($dep)} = $dep;
  85 			}
  86 		}
  87 
  88 		if (!exists $LocalDependencies{basename($lib)})
  89 		{
  90 			foreach $dep (@LibraryDependencies)
  91 			{
  92 				if (exists $LocalDependencies{basename($dep)})
  93 				{
  94 					my $basename = basename($dep);
  95 					print "\t$lib\n\t\t$basename\n";
  96 					$LocalDependencies{basename($lib)} = $lib;
  97 					$new_local = 1;
  98 					last;
  99 				}
 100 			}
 101 		}
 102 	}
 103 }
 104 
 105 print "\nCopying Local Dependencies...\n";
 106 foreach $lib (values(%LocalDependencies))
 107 {
 108 	if (!exists $LocalLibraries{basename($lib)})
 109 	{
 110 		print "\t$lib\n";
 111 		if (!$dry_run)
 112 		{
 113 			copy($lib, $LibraryDirectory . "/" . basename($lib));
 114 		}
 115 	}
 116 }
 117 
 118 print "\nUpdating Local Libraries...\n";
 119 foreach $lib (keys(%LocalDependencies))
 120 {
 121 	UpdateLibrary ($LibraryDirectory . "/" . $lib, \%LocalDependencies, $LibraryDirectory, $dry_run);	
 122 }
 123 
 124 
 125 exit;
 126 
 127 # Get all dependencies from a .la file
 128 sub GetDependencies
 129 {
 130 	my $filename = shift;
 131 	my @depend = ();
 132 	my $LA;
 133 	my $line;
 134 
 135 	open (LA, $filename) || die "unable to read $filename";
 136 	for $line (<LA>)
 137 	{
 138 		if ($line =~ /^\s*dependency_libs\s*=\s*['"]?(.*?)['"]?\s*$/)
 139 		{
 140 			my $lib;
 141 
 142 			for $lib (split(/\s/, $1))
 143 			{
 144 				if ($lib =~ /^[-A-Za-z0-9_\/\.]+\.la$/)
 145 				{
 146 					push @depend, $lib;
 147 				}
 148 			}
 149 		}
 150 	}
 151 	close (LA);
 152 
 153 	return @depend;
 154 }
 155 
 156 # Get all la file in a directory indexed by the basename
 157 sub GetLibraries
 158 {
 159 	my $directory = shift;
 160 	my %libs =();
 161 	my $lib;
 162 
 163 	for $lib (glob($directory . "/*.la"))
 164 	{
 165 		$libs{basename ($lib)} = $lib;
 166 	}
 167 
 168 	return %libs;
 169 }
 170 
 171 # Replace local dependencies in la file
 172 sub UpdateLibrary
 173 {
 174 	my $filename = shift;
 175 	my $dependencies = shift;
 176 	my $local_path = shift;
 177 	my $dry_run = shift;
 178 	my $line;
 179 	my @new_la = ();
 180 	my $updated = 0;
 181 
 182 
 183 	open (LA, $filename) || die "unable to read $filename";
 184 	for $line (<LA>)
 185 	{
 186 		if ($line =~ /^(\s*dependency_libs\s*=\s*['"]?)(.*?)(['"]?\s*$)/)
 187 		{
 188 			my $prefix = $1;
 189 			my $list = $2;
 190 			my $suffix = $3;
 191 			my $new_list = "";
 192 
 193 			while ($list)
 194 			{
 195 				if ($list =~ s/^(\s+)//)
 196 				{
 197 					$new_list = $new_list . $1;
 198 				}
 199 				elsif ($list =~ s/^([^\s]+)//)
 200 				{
 201 					my $lib = $1;
 202 					if ($lib =~ /^[-A-Za-z0-9_\/\.]+\.la$/)
 203 					{
 204 						if ((exists $dependencies->{basename($lib)}) && ($local_path . "/" . basename($lib) ne $lib))
 205 						{
 206 							if (!$updated)
 207 							{
 208 								print "\t$filename\n";
 209 							}
 210 							print "\t\t$lib\n";	
 211 							$new_list = $new_list . $local_path . "/". basename($lib);
 212 							$updated = 1;
 213 						}
 214 						else
 215 						{
 216 							$new_list = $new_list . $lib;
 217 						}
 218 					}
 219 					else
 220 					{
 221 						$new_list = $new_list . $lib;
 222 					}
 223 				}
 224 			}
 225 			$line = $prefix . $new_list . $suffix;
 226 		}
 227 		push @new_la, $line;
 228 	}
 229 	close (LA);
 230 
 231 	if ($updated)
 232 	{
 233 		my $line;
 234 
 235 		if (!$dry_run)
 236 		{
 237 			copy ($filename, $filename . ".bak");
 238 
 239 			open (LA, ">" . $filename) || die "unable to create $filename";
 240 			foreach $line (@new_la)
 241 			{
 242 				print LA $line;
 243 			}
 244 			close LA;
 245 		}
 246 	}
 247 }	

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-02-25 09:42:36, 5.1 KB) [[attachment:updatela.pl]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.