]> git.saurik.com Git - apple/libc.git/blob - xcodescripts/patch_headers_variants.pl
Libc-1439.100.3.tar.gz
[apple/libc.git] / xcodescripts / patch_headers_variants.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2006, 2007 Apple Inc. All rights reserved.
4 #
5 # @APPLE_LICENSE_HEADER_START@
6 #
7 # This file contains Original Code and/or Modifications of Original Code
8 # as defined in and that are subject to the Apple Public Source License
9 # Version 2.0 (the 'License'). You may not use this file except in
10 # compliance with the License. Please obtain a copy of the License at
11 # http://www.opensource.apple.com/apsl/ and read it before using this
12 # file.
13 #
14 # The Original Code and all software distributed under the License are
15 # distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 # EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 # INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 # Please see the License for the specific language governing rights and
20 # limitations under the License.
21 #
22 # @APPLE_LICENSE_HEADER_END@
23 #
24 # patchheaders srcdir destdir
25 #
26 # The last path component of srcdir is replicated in destdir, with the
27 # __DARWIN_ALIAS* and __DARWIN_EXTSN macro wrapped so that Libc can set
28 # the symbol decoration independently:
29 #
30 # #ifndef LIBC_ALIAS_FOO
31 # int foo(int) __DARWIN_ALIAS(foo);
32 # #else /* LIBC_ALIAS_FOO */
33 # int foo(int) LIBC_ALIAS(foo);
34 # #endif /* !LIBC_ALIAS_FOO */
35
36 use strict;
37 use IO::File;
38 use File::Basename ();
39 use File::Find ();
40 use File::Path ();
41 use File::Spec;
42
43 my $MyName = File::Basename::basename($0);
44 my $dest;
45
46 sub process {
47 my($path, $file) = @_;
48 local $_;
49 if (-e $file) {
50 my $dest_mtime = (stat($file))[9];
51 my $src_mtime = (stat($path))[9];
52
53 if ($dest_mtime > $src_mtime) {
54 return;
55 }
56 }
57
58 my($fname, $dirs, $suffix) = File::Basename::fileparse($file, ".h");
59 if ($suffix ne ".h") {
60 return;
61 }
62
63 my $p = IO::File->new($path, 'r');
64 die "$MyName: Can't open $path: $!\n" unless defined($p);
65 my $f = IO::File->new($file, 'w');
66 die "$MyName: Can't open $file: $!\n" unless defined($f);
67 my @save;
68 while(<$p>) {
69 if(/^\S/ or /^\s*$/) {
70 my $n = scalar(@save);
71 my $sym;
72 if($n > 0) {
73 my($sym) = ($save[$n - 1] =~ /__DARWIN_(?:10\d+|ALIAS|EXTSN|INODE64)[^(]*\(([^)]*)\)/);
74 if($save[$n - 1] =~ /__DARWIN_ALIAS_STARTING/) {
75 undef $sym;
76 }
77 if(defined($sym)) {
78 if(defined($path)) {
79 print " $path\n";
80 undef($path);
81 }
82 $sym =~ s/^\s+//;
83 $sym =~ s/\s+$//;
84 $sym =~ tr/a-z/A-Z/;
85 $f->print("#ifndef LIBC_ALIAS_$sym\n");
86 }
87 $f->print(@save);
88 if(defined($sym)) {
89 $save[$n - 1] =~ s/__DARWIN_(10\d+|ALIAS|EXTSN|INODE64)/LIBC_$1/;
90 $f->print("#else /* LIBC_ALIAS_$sym */\n");
91 $f->print(@save);
92 $f->print("#endif /* !LIBC_ALIAS_$sym */\n");
93 }
94 }
95 if(/^#/) {
96 $f->print($_);
97 @save = ();
98 } else {
99 @save = ($_);
100 }
101 } else {
102 push(@save, $_);
103 }
104 }
105 $f->print(@save);
106 }
107
108 sub usage {
109 die "Usage: $MyName srcdir dstdir\n";
110 }
111
112 sub wanted {
113 if(-d $File::Find::name) {
114 #print "DIR: $File::Find::name\n";
115 my $dir = File::Spec->join($dest, $File::Find::name);
116 File::Path::mkpath($dir, 0, 0755);
117 } else {
118 #print "FIL: $File::Find::name\n";
119 my $file = File::Spec->join($dest, $File::Find::name);
120 process($File::Find::name, $file);
121 }
122 }
123
124 usage() unless scalar(@ARGV) == 2;
125 my $start = File::Basename::dirname($ARGV[0]);
126 chdir($start) || die "$MyName: chdir($start): $!\n";
127 $dest = $ARGV[1];
128 File::Path::mkpath($dest, 0, 0755);
129 File::Find::find({wanted => \&wanted, no_chdir => 1}, File::Basename::basename($ARGV[0]));