]> git.saurik.com Git - apple/libc.git/blob - xcodescripts/generate_features.pl
ac5479559ac3ea2c10a3f8033622be14a12dcfc6
[apple/libc.git] / xcodescripts / generate_features.pl
1 #!/usr/bin/perl
2
3 # Generates the libc-features.h files used to control #ifdef behaviour in Libc
4 use warnings;
5 use Data::Dumper;
6 use File::Path qw(mkpath);
7
8 #printf Dumper(\%ENV);
9
10 my $unifdef = 0;
11 my %unifdefs = ();
12 my $bash = 0;
13 if (scalar(@ARGV) > 0) {
14 $unifdef = 1 if $ARGV[0] =~ /--unifdef/;
15 $bash = 1 if $ARGV[0] =~ /--bash/;
16 }
17
18 for my $arch (split(/ /, $ENV{"ARCHS"}))
19 {
20 # set ENV{"CURRENT_ARCH"} so we can predicate on it
21 $ENV{"CURRENT_ARCH"} = $arch;
22
23 my $platformName = $ENV{"PLATFORM_NAME"};
24 $platformName =~ s/simulator/os/;
25
26 my $platformPath = $ENV{"SRCROOT"} . "/Platforms/" . $platformName . "/Makefile.inc";
27 my $featuresHeaderDir = $ENV{"DERIVED_FILES_DIR"}."/".$arch;
28 my $featuresHeader = $featuresHeaderDir."/libc-features.h";
29
30 open FEATURESFILE, "<$platformPath" or die "Unable to open: $platformPath";
31
32 my %features = ();
33 my $skip = 0;
34 my $nested = 0;
35
36 while (<FEATURESFILE>) {
37 next if $_ =~ /\s*#/;
38
39 if ($_ =~ /^.endif/) {
40 $skip-- if $skip > 0;
41 $nested--;
42 }
43
44 elsif ($_ =~ /^\.if\s+(\S+)\s+(\S+)/) {
45 # an if statement, very rudimentary regex against envvar
46 my $envvar = $1;
47 my $regex = $2;
48
49 $nested++;
50 if (!defined($ENV{$envvar}) || ($ENV{$envvar} !~ /$regex/)) {
51 $skip += 1;
52 }
53 }
54
55 elsif ($_ =~ /^\s*([^= ]+)\s*=\s*(\d)/) {
56 if ($skip == 0) {
57 if ($2 == 1) {
58 $features{$1} = $2;
59 } elsif (defined($features{$1})) {
60 delete $features{$1};
61 }
62 }
63 }
64 }
65
66 close FEATURESFILE;
67
68 if ($bash == 1) {
69 for my $f (keys %features) {
70 print "$f=$features{$f} ";
71 }
72 printf "\n";
73 exit 0;
74 }
75
76 elsif ($unifdef == 1) {
77 if ($platformName eq "macosx") {
78 $unifdefs{"__OSX_OPEN_SOURCE__"} = 1;
79 }
80 # assume FEATURE_BLOCKS was on by default
81 $unifdefs{"UNIFDEF_BLOCKS"} = 1;
82 $unifdefs{"UNIFDEF_LEGACY_64_APIS"} = defined($features{"FEATURE_LEGACY_64_APIS"});
83 $unifdefs{"UNIFDEF_LEGACY_RUNE_APIS"} = defined($features{"FEATURE_LEGACY_RUNE_APIS"});
84 $unifdefs{"UNIFDEF_LEGACY_UTMP_APIS"} = defined($features{"FEATURE_LEGACY_UTMP_APIS"});
85 $unifdefs{"UNIFDEF_MOVE_LOCALTIME"} = defined($features{"FEATURE_MOVE_LOCALTIME"});
86 $unifdefs{"UNIFDEF_TZDIR_SYMLINK"} = defined($features{"FEATURE_TZDIR_SYMLINK"});
87
88 my $output = "";
89 for my $d (keys %unifdefs) {
90 $output .= " " . ($unifdefs{$d} == 1 ? "-D" : "-U") . $d;
91 }
92
93 chomp $output;
94 print "$output\n";
95 exit 0;
96 }
97
98 elsif ($unifdef == 0) {
99 # If we touch this file on every build, then every other iterative build in Xcode will rebuild *everything*
100 my $platform_mtime = (stat($platformPath))[9];
101 my $header_mtime = (stat($featuresHeader))[9];
102
103 if (defined($header_mtime) && defined($platform_mtime) && ($header_mtime > $platform_mtime)) {
104 exit 0;
105 }
106
107 printf $arch." features:\n";
108 printf Dumper(\%features);
109
110 if ($nested != 0) {
111 die "Unbalanced .if/.endif directive";
112 }
113
114 # And the meat, new header options should go under here
115 if (! -d $featuresHeaderDir) {
116 mkpath $featuresHeaderDir or die "Unable to mkdir: $featuresHeaderDir";
117 }
118 open HEADER, ">$featuresHeader" or die "Unable to open (for writing): $featuresHeader";
119
120 printf HEADER "#ifndef _LIBC_FEATURES_H_\n";
121 printf HEADER "#define _LIBC_FEATURES_H_\n\n";
122
123 my $shortarch = $arch;
124 $shortarch =~ s/armv\d+[a-z]?/arm/g;
125
126 printf HEADER "#if !defined(__".$shortarch."__)\n";
127 printf HEADER "#error Mismatched libc-features.h architecture\n";
128 printf HEADER "#endif\n\n";
129
130 if (defined($features{"FEATURE_LEGACY_RUNE_APIS"})) {
131 printf HEADER "#define UNIFDEF_LEGACY_RUNE_APIS 1\n";
132 } else {
133 printf HEADER "/* #undef UNIFDEF_LEGACY_RUNE_APIS */\n";
134 }
135
136 if (defined($features{"FEATURE_LEGACY_CRT1_ENVIRON"})) {
137 printf HEADER "#define LEGACY_CRT1_ENVIRON 1\n";
138 } else {
139 printf HEADER "/* #undef LEGACY_CRT1_ENVIRON */\n";
140 }
141
142 if (defined($features{"FEATURE_LEGACY_UTMP_APIS"})) {
143 printf HEADER "#define UNIFDEF_LEGACY_UTMP_APIS 1\n";
144 } else {
145 printf HEADER "/* #undef UNIFDEF_LEGACY_UTMP_APIS */\n";
146 }
147
148 if (defined($features{"FEATURE_MOVE_LOCALTIME"})) {
149 printf HEADER "#define UNIFDEF_MOVE_LOCALTIME 1\n";
150 } else {
151 printf HEADER "/* #undef UNIFDEF_MOVE_LOCALTIME */\n";
152 }
153
154 if (defined($features{"FEATURE_TZDIR_SYMLINK"})) {
155 printf HEADER "#define UNIFDEF_TZDIR_SYMLINK 1\n";
156 } else {
157 printf HEADER "/* #undef UNIFDEF_TZDIR_SYMLINK */\n";
158 }
159
160 if (defined($features{"FEATURE_ONLY_1050_VARIANTS"})) {
161 printf HEADER "#if !__DARWIN_ONLY_VERS_1050\n";
162 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_VERS_1050 == 0\n";
163 printf HEADER "#endif /* !__DARWIN_ONLY_VERS_1050 */\n";
164 } else {
165 printf HEADER "#if __DARWIN_ONLY_VERS_1050\n";
166 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_VERS_1050 == 1\n";
167 printf HEADER "#endif /* __DARWIN_ONLY_VERS_1050 */\n";
168 }
169
170 if (defined($features{"FEATURE_ONLY_UNIX_CONFORMANCE"})) {
171 printf HEADER "#if !__DARWIN_ONLY_UNIX_CONFORMANCE\n";
172 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_UNIX_CONFORMANCE == 0\n";
173 printf HEADER "#endif /* !__DARWIN_ONLY_UNIX_CONFORMANCE */\n";
174 } else {
175 printf HEADER "#if __DARWIN_ONLY_UNIX_CONFORMANCE\n";
176 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_UNIX_CONFORMANCE == 1\n";
177 printf HEADER "#endif /* __DARWIN_ONLY_UNIX_CONFORMANCE */\n";
178 }
179
180 if (defined($features{"FEATURE_ONLY_64_BIT_INO_T"})) {
181 printf HEADER "#if !__DARWIN_ONLY_64_BIT_INO_T\n";
182 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_64_BIT_INO_T == 0\n";
183 printf HEADER "#endif /* !__DARWIN_ONLY_64_BIT_INO_T */\n";
184 } else {
185 printf HEADER "#if __DARWIN_ONLY_64_BIT_INO_T\n";
186 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_64_BIT_INO_T == 1\n";
187 printf HEADER "#endif /* __DARWIN_ONLY_64_BIT_INO_T */\n";
188 }
189
190 if (defined($features{"FEATURE_PATCH_3417676"})) {
191 printf HEADER "#define __APPLE_PR3417676_HACK__ 1\n";
192 } else {
193 printf HEADER "/* #undef __APPLE_PR3417676_HACK__ */\n";
194 }
195
196 if (defined($features{"FEATURE_PLOCKSTAT"})) {
197 printf HEADER "#define PLOCKSTAT 1\n";
198 } else {
199 printf HEADER "/* #undef PLOCKSTAT */\n";
200 }
201
202 if (defined($features{"FEATURE_TIMEZONE_CHANGE_NOTIFICATION"})) {
203 printf HEADER "#define NOTIFY_TZ 1\n";
204 } else {
205 printf HEADER "/* #undef NOTIFY_TZ */\n";
206 }
207
208 if (defined($features{"FEATURE_NO_LIBCRASHREPORTERCLIENT"})) {
209 printf HEADER "#define LIBC_NO_LIBCRASHREPORTERCLIENT 1\n";
210 } else {
211 printf HEADER "/* #undef LIBC_NO_LIBCRASHREPORTERCLIENT */\n";
212 }
213
214 if (defined($features{"FEATURE_SMALL_STDIOBUF"})) {
215 printf HEADER "#define FEATURE_SMALL_STDIOBUF 1\n";
216 } else {
217 printf HEADER "/* #undef FEATURE_SMALL_STDIOBUF */\n";
218 }
219
220 if (defined($features{"FEATURE_XPRINTF_PERF"})) {
221 printf HEADER "#define XPRINTF_PERF 1\n";
222 } else {
223 printf HEADER "/* #undef XPRINTF_PERF */\n";
224 }
225
226 if (defined($features{"FEATURE_SIGNAL_RESTRICTION"})) {
227 printf HEADER "#define FEATURE_SIGNAL_RESTRICTION 1\n";
228 } else {
229 printf HEADER "/* #undef FEATURE_SIGNAL_RESTRICTION */\n";
230 }
231
232 printf HEADER "#endif // _LIBC_FEATURES_H_\n";
233 close HEADER;
234 }
235 }
236
237 exit 0;