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