~daggerstab/stellarium/right-to-left

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/perl

use strict;
use Cwd;

my $inmt = "/usr/bin/install_name_tool";
my $ch_inmt = qq{$inmt -change \%s \%s \%s};
my $id_inmt = qq{$inmt -id \%s \%s};

my $otool = qq{/usr/bin/otool -L \%s};

my $appdir = shift(@ARGV);
chdir $appdir;
my $main_executable = shift(@ARGV);
my $frameworks_dir = shift(@ARGV);
my $current_arch = `/usr/bin/arch`;
chomp($current_arch);

if ( ! -e "$frameworks_dir/$current_arch" ) {
    `mkdir -p $frameworks_dir/$current_arch`;
}

&recurse( $main_executable, $frameworks_dir, $current_arch );

sub recurse {
    my($main_executable, $frameworks_dir, $current_arch) = @_;

    my $cmd1 = sprintf($otool, $main_executable);
    my($app, @names) = `$cmd1`;

    my $name;
  NAME_LOOP:
    foreach $name ( @names ) {
	chomp($name);
	$name =~ s,^\s*,,;
	$name =~ s,\s*\(compa.+$,,;

	## we've already dealt with this name on this executable, ie, itself
	if ( index($name, $main_executable) >= 0 ||
	     index($main_executable, $name) >= 0 ) {
	    next NAME_LOOP;
	}

	## leave sys libraries alone and dont include them
	if ( $name =~ m,^(/System/Library|/usr/lib|\@executable_name), &&
	     $name !~ m,^(/usr/lib/libiconv), && $name !~ m,^(/usr/lib/libintl),){ 
	    next NAME_LOOP; 
	}

	## a rooted Framework
	if ( $name =~ m,([^/]+\.framework)/(\S+)$, ) {
	    my $fwname = $1;
	    my $binary = $2;

	    my $absname = &locateFramework($fwname);
	    my $arch = &architecture("$absname/$binary");

	    if ( $arch eq $current_arch || $arch eq 'fat' ) {
		my $relPath = "\@executable_path/../Frameworks/$fwname/$binary";
		my $fwPath = "$frameworks_dir/$fwname/$binary";

		my $not_existed = 1;
		if ( ! -e $fwPath ) {
		    my $c = "cp -RP -p $absname $frameworks_dir/$fwname";
		    `$c`;
		} else {
		    $not_existed = 0;
		}

		my $c = sprintf($id_inmt, $relPath, $fwPath);
		`$c`;
		$c = sprintf($ch_inmt, $name, $relPath, $main_executable);
		`$c`;

		if ( $not_existed ) {
		    &recurse($fwPath, $frameworks_dir, $current_arch);
		}
	    } else {
		warn qq{$0: [1] for $main_executable: what to do about $absname being $arch!!!!!\n};
	    }
	    next NAME_LOOP;
	}

	## an unrooted Framework
	if ( $name =~ m,^([^/]+\.framework)/(\S+)$, ) {
	    my $fwname = $1;
	    my $binary = $2;

	    my $absname = &locateFramework($fwname);
	    my $arch = &architecture("$absname/$binary");

	    if ( $arch eq $current_arch || $arch eq 'fat' ) {
		my $relPath = "\@executable_path/../Frameworks/$fwname/$binary";
		my $fwPath = "$frameworks_dir/$fwname/$binary";

		my $not_existed = 1;
		if ( ! -e $fwPath ) {
		    my $c = "cp -RP -p $absname $frameworks_dir/$fwname";
		    `$c`;
		} else {
		    $not_existed = 0;
		}

		my $c = sprintf($id_inmt, $relPath, $fwPath);
		`$c`;
		$c = sprintf($ch_inmt, $name, $relPath, $main_executable);
		`$c`;

		if ( $not_existed ) {
		    &recurse($fwPath, $frameworks_dir, $current_arch);
		}
	    } else {
		warn qq{$0: [1] for $main_executable: what to do about $absname being $arch!!!!!\n};
	    }
	    next NAME_LOOP;
	}

	## a rooted dylib
	if ( $name =~ m,^/.+?([^/]+\.dylib)$, ) {
	    my $basename = $1;
	    my $absname = $name;

	    my $arch = &architecture($absname);

	    if ( $arch eq $current_arch || $arch eq 'fat' ) {
		my $relPath = "\@executable_path/../Frameworks/$current_arch/$basename";
		my $fwPath = "$frameworks_dir/$current_arch/$basename";

		my $not_existed = 1;
		if ( ! -e $fwPath ) {
		    my $c = "cp -P $absname $frameworks_dir/$current_arch";
		    `$c`;
		} else {
		    $not_existed = 0;
		}

		my $c = sprintf($id_inmt, $relPath, $fwPath);
		`$c`;
		$c = sprintf($ch_inmt, $absname, $relPath, $main_executable);
		`$c`;

		if ( $not_existed ) {
		    &recurse($fwPath, $frameworks_dir, $current_arch);
		}
	    } else {
		warn qq{$0: [2] for $main_executable: what to do about $name being $arch!!!\n};
	    }
	    next NAME_LOOP;
	}

	## an unrooted dylib; we only have one of these for Stellarium
	## right now, libstelMain.dylib, and its a special case.
	if ( $name =~ m,libstelMain.dylib, ) {

	    if ( $name =~ m/\@executable/ ) {
		# we've been here already
		next NAME_LOOP;
	    }

	    my $basename = $name;
	    my $absname = "MacOS/" . $name;

	    my $arch = &architecture($absname);

	    if ( $arch eq $current_arch || $arch eq 'fat') {
		my $relPath = "\@executable_path/$basename";

		my $c = sprintf($id_inmt, $relPath, $absname);
		`$c`;
		$c = sprintf($ch_inmt, $name, $relPath, $main_executable);
		`$c`;

		&recurse($absname, $frameworks_dir, $current_arch);
	    } else {
		warn qq{$0: [4] for $main_executable: what to do about $name being '$arch'!!!\n};
	    }
	    next NAME_LOOP;
	}
	

	## something else?
	warn qq{$0: [3] for $main_executable: what to do with $name?!?!?\n};
    }
}


sub architecture {
    my $file = shift;
    my(@output) = `/usr/bin/file $file`;

    if ( grep(m/^${file}: symbolic link to ([^\']+)/, @output) ) {
	my($r, @junk) = grep(m/^${file}: symbolic link to ([^\']+)/, @output);
	$r =~ m,^(.+)/([^/]+.dylib): symbolic link to ([^\']+),;
	my $root = $1;
	my $base = $2;
	my $target = $3;

	if ( $target =~ m,^/, ) {
	    return &architecture("$target");
	} else {
	    return &architecture("$root/$target");
	}
    }

    my $retval = undef;
    # file with 2 architectures

    if ( grep(m/(universal binary|fat file) with [0-9] architectures/i, @output)) {
	$retval =  'fat';
    }
    elsif ( grep(m/ppc/, @output) ) {
	$retval =  'ppc';
    }
    elsif ( grep(m/i386/, @output) ) {
	$retval =  'i386';
    }
    # warn qq{$0: $file isa '$retval' arch\n};
    return $retval;
}

sub locateFramework {
    my $fname = shift;
    my $lib;
    foreach $lib ( '~/Library/Frameworks', '/Library/Frameworks', '/usr/local/Trolltech/Qt-4.7.1/lib' ) {
	if ( -e "$lib/$fname" ) {
	    return "$lib/$fname";
	}
    }
    warn qq{$0: couldnt find $fname!!!!\n};
    return undef;
}