makebundle.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/opt/local/bin/perl
  2. #
  3. # makebundle.pl - copies and makes portable a binary and all its dependencies
  4. # Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. #
  20. use strict;
  21. use warnings;
  22. # Script does some dangerous stuff - make sure a user will double-check everything before running it.
  23. print "This script is highly experimental. Modify it to suit your needs and take care to run it only in controlled environments.\n";
  24. # This line needs to be removed, the following lines adjusted.
  25. exit 1;
  26. my $OUTPUTDIR='/tmp/SuperTux.app/Contents/MacOS';
  27. my $EXECUTABLE='./supertux2';
  28. my %COPIED;
  29. my @TOCOPY;
  30. my %PATCHED;
  31. my @TOPATCH;
  32. system('mkdir '.$OUTPUTDIR);
  33. push (@TOCOPY, $EXECUTABLE);
  34. # first, copy all direct and indirect dependencies to OUTPUTDIR
  35. while (my $EXECUTABLE = pop @TOCOPY) {
  36. next if $COPIED{$EXECUTABLE};
  37. $COPIED{$EXECUTABLE} = 1;
  38. next unless $EXECUTABLE =~ /^(.*)\/(.*)$/;
  39. my $PATH = $1;
  40. my $FNAME = $2;
  41. print("copying ".$EXECUTABLE."\n");
  42. system('cp '.$EXECUTABLE.' '.$OUTPUTDIR.'/'.$FNAME);
  43. system('chmod u+w '.$OUTPUTDIR.'/'.$FNAME);
  44. # mark the copy as to-be-patched
  45. push(@TOPATCH, $OUTPUTDIR.'/'.$FNAME);
  46. $EXECUTABLE = $OUTPUTDIR.'/'.$FNAME;
  47. my @LIBS = qx{/usr/bin/otool -L $EXECUTABLE};
  48. while ($_ = pop @LIBS) {
  49. # parse output of otool
  50. next unless /^\t(.*) \(.*\).*$/;
  51. my $FULLPATH = $1;
  52. # skip dependencies on things that are no .dylib
  53. next unless $FULLPATH =~ /\.dylib$/;
  54. # skip dependencies that are already dynamic
  55. next if $FULLPATH =~ /^@/;
  56. # skip dependencies that don't have an absolute path
  57. next unless $FULLPATH =~ /^(.*)\/(.*)$/;
  58. my $PATH = $1;
  59. my $FNAME = $2;
  60. # skip dependencies on anything in /usr/lib
  61. next if $PATH =~ q{^/usr/lib$};
  62. # mark dependency as to-be-copied
  63. push(@TOCOPY, $FULLPATH);
  64. }
  65. }
  66. # now patch all binaries we copied
  67. while (my $EXECUTABLE = pop @TOPATCH) {
  68. next if $PATCHED{$EXECUTABLE};
  69. $PATCHED{$EXECUTABLE} = 1;
  70. print "patching ".$EXECUTABLE."\n";
  71. my @LIBS = qx{/usr/bin/otool -L $EXECUTABLE};
  72. while ($_ = pop @LIBS) {
  73. # parse output of otool
  74. next unless /^\t(.*) \(.*\).*$/;
  75. my $FULLPATH = $1;
  76. # skip dependencies that are not .dylibs
  77. next unless $FULLPATH =~ /\.dylib$/;
  78. # skip dependencies that are already dynamic
  79. next if $FULLPATH =~ /^@/;
  80. # skip dependencies without absolute paths
  81. next unless $FULLPATH =~ /^(.*)\/(.*)$/;
  82. my $PATH = $1;
  83. my $FNAME = $2;
  84. # skip dependencies on stuff in /usr/lib
  85. next if $PATH =~ q{^/usr/lib$};
  86. # patch binary to depend on copy in @executable_path instead
  87. system('install_name_tool -change '.$FULLPATH.' @executable_path/'.$FNAME.' '.$EXECUTABLE);
  88. }
  89. }