Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created December 2, 2013 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tluyben/7748353 to your computer and use it in GitHub Desktop.
Save tluyben/7748353 to your computer and use it in GitHub Desktop.
Lddcollect is a small tool which packs up and installs binaries, libraries and related files no a linux (possibly unix) machine.
#!/usr/bin/perl
use strict;
use warnings;
my $cmd = $ARGV[0];
if (scalar(@ARGV) < 2 or ($cmd ne "c" and $cmd ne "i" and $cmd ne "r")) {
print "Usage: lddcollect [cir] [binary/tgz/dir] [extra]\n\n";
print "Lddcollect is a small tool which packs up and installs binaries, libraries and related files.\n";
print "The tool was created to pack up cross compiled software in a small package and unpack and install it on the target machine.\n";
print "The target machine being so small memory/disk wise and slow normal distro's or packaging does not apply.\n";
print "\nArguments: c for creating and archive, i for installing an archive, r for running (in place) an archive.\n";
print "At least the binary or the location to run or install needs to be provided, and if there is more, like data files etc, they can be provided on the rest of the commandline.\n";
print "\nLicenced under the GPL. Created by T. Luyben 2000.\n";
exit;
}
my $tmpdir = "/tmp/binpack";
my %found = ();
sub findLibs {
my ($bin) = @_;
my @libs = `ldd $bin`;
foreach(@libs) {
chomp;
if (my ($want, $have) = /(.*?)=>(.*)/) {
$want =~ s/^\s+|\s+$//g ;
if ($have =~ /(.*)\s+\(0x.*\)/) {
$have = $1;
}
$have =~ s/^\s+|\s+$//g ;
if ($have =~ /not found/) {
print "Cannot find $want, please resolve this before continuing!\n";
exit;
}
if (!$found{$have}) {
print "Found required lib: $have, copying to $tmpdir/libs\n";
print `cp $have $tmpdir/libs`;
# recurse
findLibs($have);
$found{$have} = 1;
}
}
}
}
# creating an archive
if ($cmd eq "c") {
`rm -fR $tmpdir 2>&1; mkdir $tmpdir; mkdir $tmpdir/libs`;
my $file = $ARGV[1];
if (not -e $file) {
print "File $file does not exist!\n";
exit;
}
`cp $file $tmpdir/runme`;
# now we resolve all libs; ze fun part!
my @libs = ();
findLibs($file);
# pack it up!
my $tgz = "$file.tgz";
if ($file =~ /^.*\/(.*)$/) {
$tgz = "$1.tgz";
}
my $pwd = `pwd`;
chomp($pwd);
print `cd $tmpdir; tar czf $pwd/$tgz .`;
} elsif ($cmd eq "r") {
my $dir = $ARGV[1];
if (not -d $dir) {
print "Directory $dir to run in does not exist!\n";
exit;
}
print `cd $dir; LD_LIBRARY_PATH=./libs; ./runme`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment