1#!/usr/bin/perl
2
3# Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9# 1.  Redistributions of source code must retain the above copyright
10#     notice, this list of conditions and the following disclaimer. 
11# 2.  Redistributions in binary form must reproduce the above copyright
12#     notice, this list of conditions and the following disclaimer in the
13#     documentation and/or other materials provided with the distribution. 
14# 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15#     its contributors may be used to endorse or promote products derived
16#     from this software without specific prior written permission. 
17#
18# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29# "check-for-global-initializers" script for WebKit Open Source Project
30
31# Intended to be invoked from an Xcode build step to check if there are
32# any global initializers in a target.
33
34use warnings;
35use strict;
36
37use File::Basename;
38
39sub touch($);
40sub demangle($);
41
42my $arch = $ENV{'CURRENT_ARCH'};
43my $configuration = $ENV{'CONFIGURATION'};
44my $target = $ENV{'TARGET_NAME'};
45my $variant = $ENV{'CURRENT_VARIANT'};
46my $coverageBuild = $ENV{'WEBKIT_COVERAGE_BUILD'};
47my $debugRoot = $ENV{'WEBKIT_DEBUG_ROOT'};
48
49$arch = $ENV{'NATIVE_ARCH'} if !$arch; # for Xcode 2.1, which does not have CURRENT_ARCH
50$variant = "normal" if !$variant; # for Xcode 2.1, which does not have CURRENT_VARIANT
51
52my $executablePath = "$ENV{'TARGET_BUILD_DIR'}/$ENV{'EXECUTABLE_PATH'}";
53
54my $buildTimestampPath = $ENV{'TARGET_TEMP_DIR'} . "/" . basename($0) . ".timestamp";
55my $buildTimestampAge = -M $buildTimestampPath;
56my $scriptAge = -M $0;
57
58my $list = $ENV{"LINK_FILE_LIST_${variant}_${arch}"};
59
60if (!open LIST, $list) {
61    print "ERROR: Could not open $list\n";
62    exit 1;
63}
64
65my @files = <LIST>;
66chomp @files;
67close LIST;
68
69my $sawError = 0;
70
71for my $file (sort @files) {
72    if (defined $buildTimestampAge && $buildTimestampAge < $scriptAge) {
73        my $fileAge = -M $file;
74        next if defined $fileAge && $fileAge > $buildTimestampAge;
75    }
76    if (!open NM, "(nm '$file' | sed 's/^/STDOUT:/') 2>&1 |") {
77        print "ERROR: Could not open $file\n";
78        $sawError = 1;
79        next;
80    }
81    my $sawGlobal = 0;
82    my @globals;
83    while (<NM>) {
84        if (/^STDOUT:/) {
85            my $line = $_;
86            if ($line =~ /__GLOBAL__I(.+)$/) {
87                $sawGlobal = 1;
88                push(@globals, demangle($1));
89            }
90        } else {
91            print STDERR if $_ ne "nm: no name list\n";
92        }
93    }
94    close NM;
95    if ($sawGlobal) {
96        my $shortName = $file;
97        $shortName =~ s/.*\///;
98
99        # Special cases for files that have initializers in debug builds.
100        if ($configuration eq "Debug" or $variant eq "debug" or $debugRoot) {
101            if ($target eq "JavaScriptCore") {
102                next if $shortName eq "AllInOneFile.o";
103                next if $shortName eq "Opcode.o";
104                next if $shortName eq "Structure.o";
105                next if $shortName eq "nodes.o";
106            }
107            if ($target eq "WebCore") {
108                next if $shortName eq "BidiRun.o";
109                next if $shortName eq "CachedPage.o";
110                next if $shortName eq "CachedResource.o";
111                next if $shortName eq "FEGaussianBlur.o";
112                next if $shortName eq "Frame.o";
113                next if $shortName eq "JSCustomSQLTransactionCallback.o";
114                next if $shortName eq "JSLazyEventListener.o";
115                next if $shortName eq "Node.o";
116                next if $shortName eq "Page.o";
117                next if $shortName eq "Range.o";
118                next if $shortName eq "RenderObject.o";
119                next if $shortName eq "SVGElementInstance.o";
120                next if $shortName eq "SubresourceLoader.o";
121                next if $shortName eq "XMLHttpRequest.o";
122            }
123            if ($target eq "WebKit") {
124                next if $shortName eq "HostedNetscapePluginStream.o";
125                next if $shortName eq "NetscapePluginInstanceProxy.o";
126            }
127            if ($target eq "WebKit2") {
128                next if $shortName eq "WebContext.o";
129                next if $shortName eq "WebFrame.o";
130                next if $shortName eq "WebPage.o";
131                next if $shortName eq "WebPageProxy.o";
132            }
133        }
134
135        print "ERROR: $shortName has one or more global initializers in it! ($file), near @globals\n";
136        $sawError = 1;
137    }
138}
139
140if ($sawError and !$coverageBuild) {
141    unlink $executablePath;
142    exit 1;
143}
144
145touch($buildTimestampPath);
146exit 0;
147
148sub touch($)
149{
150    my ($path) = @_;
151    open(TOUCH, ">", $path) or die "$!";
152    close(TOUCH);
153}
154
155sub demangle($)
156{
157    my ($symbol) = @_;
158    if (!open FILT, "c++filt $symbol |") {
159        print "ERROR: Could not open c++filt\n";
160        return;
161    }
162    my $result = <FILT>;
163    close FILT;
164    chomp $result;
165    return $result;
166}
167
168