From http://www.jwz.org/xscreensaver/xscreensaver-5.33.tar.gz
[xscreensaver] / OSX / build-fntable.pl
1 #!/usr/bin/perl -w
2 # Copyright © 2012-2014 Jamie Zawinski <jwz@jwz.org>
3 #
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation.  No representations are made about the suitability of this
9 # software for any purpose.  It is provided "as is" without express or 
10 # implied warranty.
11 #
12 # Generates a .c file that lists all the function tables we use, because
13 # CFBundleGetDataPointerForName doesn't work in "Archive" builds.
14 # What a crock of shit.
15 #
16 # There's no real way to integrate this into the Xcode build system, so
17 # run this manually each time a new saver is added to the iOS app.
18 #
19 # Created: 14-Jul-2012.
20
21 require 5;
22 #use diagnostics;       # Fails on some MacOS 10.5 systems
23 use strict;
24
25 my $progname = $0; $progname =~ s@.*/@@g;
26 my ($version) = ('$Revision: 1.3 $' =~ m/\s(\d[.\d]+)\s/s);
27
28 my $verbose = 1;
29
30 # List of savers not included in the iOS build.
31 #
32 my %disable = (
33    'extrusion'          => 1,
34    'lcdscrub'           => 1,
35    'lockward'           => 1,
36    'webcollage'         => 1,
37   );
38
39 # Parse the RETIRED_EXES variable from the Makefiles to populate %disable.
40 # Duplicated in ../hacks/munge-ad.pl.
41 #
42 sub parse_makefiles() {
43   foreach my $mf ( "../hacks/Makefile.in", "../hacks/glx/Makefile.in" ) {
44     open (my $in, '<', $mf) || error ("$mf: $!");
45     print STDERR "$progname: reading $mf\n" if ($verbose > 1);
46     local $/ = undef;  # read entire file
47     my $body = <$in>;
48     close $in;
49
50     $body =~ s/\\\n//gs;
51     my ($var)  = ($body =~ m/^RETIRED_EXES\s*=\s*(.*)$/mi);
52     my ($var2) = ($body =~ m/^RETIRED_GL_EXES\s*=\s*(.*)$/mi);
53     error ("no RETIRED_EXES in $mf") unless $var;
54     $var .= " $var2" if $var2;
55     foreach my $hack (split (/\s+/, $var)) {
56       $disable{$hack} = 2;
57     }
58   }
59 }
60
61
62 sub build_h($) {
63   my ($outfile) = @_;
64
65   parse_makefiles();
66
67   my @schemes = glob('xscreensaver.xcodeproj/xcuserdata/' .
68                      '*.xcuserdatad/xcschemes/*.xcscheme');
69   error ("no scheme files") unless (@schemes);
70
71   my %names = ();
72
73   foreach my $s (@schemes) {
74     open (my $in, '<', $s) || error ("$s: $!");
75     local $/ = undef;  # read entire file
76     my $body = <$in>;
77     close $in;
78     my ($name) = ($body =~ m@BuildableName *= *"([^\"<>]+?)\.saver"@s);
79     next unless $name;
80     $name = lc($name);
81     if ($disable{$name}) {
82       print STDERR "$progname: skipping $name\n" if ($verbose > 1);
83       next;
84     }
85     print STDERR "$progname: found $name\n" if ($verbose > 1);
86     $names{$name} = 1;
87   }
88
89   my @names = sort (keys %names);
90   error ("too few names") if (@names < 100);
91
92   my $suf = 'xscreensaver_function_table';
93
94   my $body = ("/* Generated file, do not edit.\n" .
95               "   Created: " . localtime() . " by $progname $version.\n" .
96               " */\n" .
97               "\n" .
98               "#import <Foundation/Foundation.h>\n" .
99               "#import <UIKit/UIKit.h>\n" .
100               "\n" .
101               "extern NSDictionary *make_function_table_dict(void);\n" .
102               "\n");
103
104   $body .= "extern struct $suf";
105   foreach my $s (@names) {
106     $body .= "\n *${s}_${suf},";
107   }
108   $body =~ s/,\s*$/;/s;
109
110   sub line($$) {
111     my ($s, $suf) = @_;
112     return "\t[NSValue valueWithPointer:&${s}_${suf}], @\"${s}\",\n";
113   }
114
115   $body .= ("\n\n" .
116             "NSDictionary *make_function_table_dict(void)\n{\n" .
117             "  return\n    [NSDictionary dictionaryWithObjectsAndKeys:\n" .
118             "\n" .
119             "#if defined(APPLE2_ONLY)\n" .
120             " " . line('apple2', $suf) .
121             "#elif defined(PHOSPHOR_ONLY)\n" .
122             " " . line('phosphor', $suf) .
123             "#else\n");
124   foreach my $s (@names) { $body .= line($s, $suf); }
125   $body .= ("#endif\n" .
126             "\tnil];\n" .
127             "}\n\n");
128
129   my $obody = '';
130   if (open (my $in, '<', $outfile)) {
131     local $/ = undef;  # read entire file
132     $obody = <$in>;
133     close $in;
134   }
135
136   # strip comments/date for diff.
137   my ($body2, $obody2) = ($body, $obody);
138   foreach ($body2, $obody2) { s@/\*.*?\*/@@gs; }
139
140   if ($body2 eq $obody2) {
141     print STDERR "$progname: $outfile: unchanged\n" if ($verbose > 1);
142   } else {
143     my $file_tmp = "$outfile.tmp";
144     open (my $out, '>', $file_tmp) || error ("$file_tmp: $!");
145     print $out $body || error ("$file_tmp: $!");
146     close $out || error ("$file_tmp: $!");
147
148     if (!rename ("$file_tmp", "$outfile")) {
149       unlink "$file_tmp";
150       error ("mv \"$file_tmp\" \"$outfile\": $!");
151     }
152     print STDERR "$progname: wrote $outfile\n" if ($verbose);
153   }
154 }
155
156
157 sub error($) {
158   my ($err) = @_;
159   print STDERR "$progname: $err\n";
160   exit 1;
161 }
162
163 sub usage() {
164   print STDERR "usage: $progname [--verbose] output.c\n";
165   exit 1;
166 }
167
168 sub main() {
169
170   my ($out);
171   while ($_ = $ARGV[0]) {
172     shift @ARGV;
173     if    (m/^--?verbose$/s)  { $verbose++; }
174     elsif (m/^-v+$/)          { $verbose += length($_)-1; }
175     elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
176     elsif (m/^-/s)            { usage(); }
177     elsif (! $out)            { $out = $_; }
178     else                      { usage(); }
179   }
180   usage() unless ($out);
181   build_h ($out);
182 }
183
184 main();
185 exit 0;