]> git.saurik.com Git - apple/libc.git/blame - xcodescripts/patch_headers_variants.pl
Libc-825.26.tar.gz
[apple/libc.git] / xcodescripts / patch_headers_variants.pl
CommitLineData
224c7076
A
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
36use strict;
37use IO::File;
38use File::Basename ();
39use File::Find ();
40use File::Path ();
41use File::Spec;
42
43my $MyName = File::Basename::basename($0);
44my $dest;
45
46sub process {
47 my($path, $file) = @_;
48 local $_;
ad3c9f2a
A
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
224c7076
A
58 my $p = IO::File->new($path, 'r');
59 die "$MyName: Can't open $path: $!\n" unless defined($p);
60 my $f = IO::File->new($file, 'w');
61 die "$MyName: Can't open $file: $!\n" unless defined($f);
62 my @save;
63 while(<$p>) {
ad3c9f2a
A
64 if(/^\S/ or /^\s*$/) {
65 my $n = scalar(@save);
66 my $sym;
67 if($n > 0) {
68 my($sym) = ($save[$n - 1] =~ /__DARWIN_(?:10\d+|ALIAS|EXTSN|INODE64)[^(]*\(([^)]*)\)/);
69 if(defined($sym)) {
70 if(defined($path)) {
71 print " $path\n";
72 undef($path);
73 }
74 $sym =~ s/^\s+//;
75 $sym =~ s/\s+$//;
76 $sym =~ tr/a-z/A-Z/;
77 $f->print("#ifndef LIBC_ALIAS_$sym\n");
78 }
79 $f->print(@save);
80 if(defined($sym)) {
81 $save[$n - 1] =~ s/__DARWIN_(10\d+|ALIAS|EXTSN|INODE64)/LIBC_$1/;
82 $f->print("#else /* LIBC_ALIAS_$sym */\n");
83 $f->print(@save);
84 $f->print("#endif /* !LIBC_ALIAS_$sym */\n");
85 }
86 }
87 if(/^#/) {
88 $f->print($_);
89 @save = ();
224c7076 90 } else {
ad3c9f2a 91 @save = ($_);
224c7076
A
92 }
93 } else {
94 push(@save, $_);
95 }
ad3c9f2a
A
96}
97$f->print(@save);
224c7076
A
98}
99
100sub usage {
101 die "Usage: $MyName srcdir dstdir\n";
102}
103
104sub wanted {
105 if(-d $File::Find::name) {
ad3c9f2a
A
106 #print "DIR: $File::Find::name\n";
107 my $dir = File::Spec->join($dest, $File::Find::name);
108 File::Path::mkpath($dir, 0, 0755);
224c7076 109 } else {
ad3c9f2a
A
110 #print "FIL: $File::Find::name\n";
111 my $file = File::Spec->join($dest, $File::Find::name);
112 process($File::Find::name, $file);
224c7076
A
113 }
114}
115
116usage() unless scalar(@ARGV) == 2;
117my $start = File::Basename::dirname($ARGV[0]);
118chdir($start) || die "$MyName: chdir($start): $!\n";
119$dest = $ARGV[1];
120File::Path::mkpath($dest, 0, 0755);
121File::Find::find({wanted => \&wanted, no_chdir => 1}, File::Basename::basename($ARGV[0]));