]> git.saurik.com Git - apple/libc.git/blob - xcodescripts/generate_features.pl
ce710f4b96b37448219b543fb619c3ab2b19afe8
[apple/libc.git] / xcodescripts / generate_features.pl
1 #!/usr/bin/perl
2
3 if ($ENV{"ACTION"} eq "installhdrs") {
4 exit 0;
5 }
6
7 # Generates the libc-features.h files used to control #ifdef behaviour in Libc
8 use warnings;
9 use Data::Dumper;
10 use File::Path qw(mkpath);
11
12 #printf Dumper(\%ENV);
13
14 my $unifdef = 0;
15 my %unifdefs = ();
16 my $bash = 0;
17 if (scalar(@ARGV) > 0) {
18 $unifdef = 1 if $ARGV[0] =~ /--unifdef/;
19 $bash = 1 if $ARGV[0] =~ /--bash/;
20 }
21
22 for my $arch (split(/ /, $ENV{"ARCHS"}))
23 {
24 # set ENV{"CURRENT_ARCH"} so we can predicate on it
25 $ENV{"CURRENT_ARCH"} = $arch;
26
27 my $platformName = $ENV{"PLATFORM_NAME"};
28 $platformName = "iphoneos" if ($platformName eq "iphonesimulator");
29
30 my $platformPath = $ENV{"SRCROOT"} . "/Platforms/" . $platformName . "/Makefile.inc";
31 my $featuresHeaderDir = $ENV{"DERIVED_FILES_DIR"}."/".$arch;
32 my $featuresHeader = $featuresHeaderDir."/libc-features.h";
33
34 open FEATURESFILE, "<$platformPath" or die "Unable to open: $platformPath";
35
36 my %features = ();
37 my $skip = 0;
38 my $nested = 0;
39
40 while (<FEATURESFILE>) {
41 next if $_ =~ /\s*#/;
42
43 if ($_ =~ /^.endif/) {
44 $skip-- if $skip > 0;
45 $nested--;
46 }
47
48 elsif ($_ =~ /^\.if\s+(\S+)\s+(\S+)/) {
49 # an if statement, very rudimentary regex against envvar
50 my $envvar = $1;
51 my $regex = $2;
52
53 $nested++;
54 if ($ENV{$envvar} !~ /$regex/) {
55 $skip += 1;
56 }
57 }
58
59 elsif ($_ =~ /^\s*([^= ]+)\s*=\s*(\d)/) {
60 if ($skip == 0) {
61 if ($2 == 1) {
62 $features{$1} = $2;
63 } elsif (defined($features{$1})) {
64 delete $features{$1};
65 }
66 }
67 }
68 }
69
70 close FEATURESFILE;
71
72 if ($bash == 1) {
73 for my $f (keys %features) {
74 print "$f=$features{$f} ";
75 }
76 printf "\n";
77 exit 0;
78 }
79
80 elsif ($unifdef == 1) {
81 # assume FEATURE_BLOCKS was on by default
82 $unifdefs{"UNIFDEF_BLOCKS"} = 1;
83 $unifdefs{"UNIFDEF_LEGACY_64_APIS"} = defined($features{"FEATURE_LEGACY_64_APIS"});
84 $unifdefs{"UNIFDEF_LEGACY_RUNE_APIS"} = defined($features{"FEATURE_LEGACY_RUNE_APIS"});
85 $unifdefs{"UNIFDEF_LEGACY_UTMP_APIS"} = defined($features{"FEATURE_LEGACY_UTMP_APIS"});
86 $unifdefs{"UNIFDEF_MOVE_LOCALTIME"} = defined($features{"FEATURE_MOVE_LOCALTIME"});
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 ($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_ONLY_1050_VARIANTS"})) {
155 printf HEADER "#if !__DARWIN_ONLY_VERS_1050\n";
156 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_VERS_1050 == 0\n";
157 printf HEADER "#endif /* !__DARWIN_ONLY_VERS_1050 */\n";
158 } else {
159 printf HEADER "#if __DARWIN_ONLY_VERS_1050\n";
160 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_VERS_1050 == 1\n";
161 printf HEADER "#endif /* __DARWIN_ONLY_VERS_1050 */\n";
162 }
163
164 if (defined($features{"FEATURE_ONLY_UNIX_CONFORMANCE"})) {
165 printf HEADER "#if !__DARWIN_ONLY_UNIX_CONFORMANCE\n";
166 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_UNIX_CONFORMANCE == 0\n";
167 printf HEADER "#endif /* !__DARWIN_ONLY_UNIX_CONFORMANCE */\n";
168 } else {
169 printf HEADER "#if __DARWIN_ONLY_UNIX_CONFORMANCE\n";
170 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_UNIX_CONFORMANCE == 1\n";
171 printf HEADER "#endif /* __DARWIN_ONLY_UNIX_CONFORMANCE */\n";
172 }
173
174 if (defined($features{"FEATURE_ONLY_64_BIT_INO_T"})) {
175 printf HEADER "#if !__DARWIN_ONLY_64_BIT_INO_T\n";
176 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_64_BIT_INO_T == 0\n";
177 printf HEADER "#endif /* !__DARWIN_ONLY_64_BIT_INO_T */\n";
178 } else {
179 printf HEADER "#if __DARWIN_ONLY_64_BIT_INO_T\n";
180 printf HEADER "# error Feature mismatch: __DARWIN_ONLY_64_BIT_INO_T == 1\n";
181 printf HEADER "#endif /* __DARWIN_ONLY_64_BIT_INO_T */\n";
182 }
183
184 if (defined($features{"FEATURE_PATCH_3417676"})) {
185 printf HEADER "#define __APPLE_PR3417676_HACK__ 1\n";
186 } else {
187 printf HEADER "/* #undef __APPLE_PR3417676_HACK__ */\n";
188 }
189
190 if (defined($features{"FEATURE_PATCH_5243343"})) {
191 printf HEADER "#define PR_5243343 1\n";
192 } else {
193 printf HEADER "/* #undef PR_5243343 */\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_MEMORYSTATUS"})) {
215 printf HEADER "#define CONFIG_MEMORYSTATUS 1\n";
216 } else {
217 printf HEADER "/* #undef CONFIG_MEMORYSTATUS */\n";
218 }
219
220 printf HEADER "#endif // _LIBC_FEATURES_H_\n";
221 close HEADER;
222 }
223 }
224
225 exit 0;