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($);
40
41my $arch = $ENV{'CURRENT_ARCH'};
42my $configuration = $ENV{'CONFIGURATION'};
43my $target = $ENV{'TARGET_NAME'};
44my $variant = $ENV{'CURRENT_VARIANT'};
45my $coverageBuild = $ENV{'WEBKIT_COVERAGE_BUILD'};
46my $debugRoot = $ENV{'WEBKIT_DEBUG_ROOT'};
47
48$arch = $ENV{'NATIVE_ARCH'} if !$arch; # for Xcode 2.1, which does not have CURRENT_ARCH
49$variant = "normal" if !$variant; # for Xcode 2.1, which does not have CURRENT_VARIANT
50
51my $executablePath = "$ENV{'TARGET_BUILD_DIR'}/$ENV{'EXECUTABLE_PATH'}";
52
53my $buildTimestampPath = $ENV{'TARGET_TEMP_DIR'} . "/" . basename($0) . ".timestamp";
54my $buildTimestampAge = -M $buildTimestampPath;
55my $scriptAge = -M $0;
56
57my $list = $ENV{"LINK_FILE_LIST_${variant}_${arch}"};
58
59if (!open LIST, $list) {
60    print "ERROR: Could not open $list\n";
61    exit 1;
62}
63
64my @files = <LIST>;
65chomp @files;
66close LIST;
67
68my $sawError = 0;
69
70for my $file (sort @files) {
71    if (defined $buildTimestampAge && $buildTimestampAge < $scriptAge) {
72        my $fileAge = -M $file;
73        next if defined $fileAge && $fileAge > $buildTimestampAge;
74    }
75    if (!open NM, "(nm '$file' | sed 's/^/STDOUT:/') 2>&1 |") {
76        print "ERROR: Could not open $file\n";
77        $sawError = 1;
78        next;
79    }
80    my $sawGlobal = 0;
81    while (<NM>) {
82        if (/^STDOUT:/) {
83            $sawGlobal = 1 if /__GLOBAL__I/;
84        } else {
85            print STDERR if $_ ne "nm: no name list\n";
86        }
87    }
88    close NM;
89    if ($sawGlobal) {
90        my $shortName = $file;
91        $shortName =~ s/.*\///;
92
93        # Special cases for files that have initializers in debug builds.
94        if ($configuration eq "Debug" or $variant eq "debug" or $debugRoot) {
95            if ($target eq "JavaScriptCore") {
96                next if $shortName eq "AllInOneFile.o";
97                next if $shortName eq "Opcode.o";
98                next if $shortName eq "Structure.o";
99                next if $shortName eq "nodes.o";
100            }
101            if ($target eq "WebCore") {
102                next if $shortName eq "CachedPage.o";
103                next if $shortName eq "CachedResource.o";
104                next if $shortName eq "Frame.o";
105                next if $shortName eq "JSCustomSQLTransactionCallback.o";
106                next if $shortName eq "JSLazyEventListener.o";
107                next if $shortName eq "Node.o";
108                next if $shortName eq "Page.o";
109                next if $shortName eq "Range.o";
110                next if $shortName eq "BidiRun.o";
111                next if $shortName eq "RenderObject.o";
112                next if $shortName eq "SubresourceLoader.o";
113                next if $shortName eq "SVGElementInstance.o";
114                next if $shortName eq "XMLHttpRequest.o";
115            }
116            if ($target eq "WebKit") {
117                next if $shortName eq "HostedNetscapePluginStream.o";
118                next if $shortName eq "NetscapePluginInstanceProxy.o";
119            }
120        }
121
122        print "ERROR: $shortName has a global initializer in it! ($file)\n";
123        $sawError = 1;
124    }
125}
126
127if ($sawError and !$coverageBuild) {
128    unlink $executablePath;
129    exit 1;
130}
131
132touch($buildTimestampPath);
133exit 0;
134
135sub touch($)
136{
137    my ($path) = @_;
138    open(TOUCH, ">", $path) or die "$!";
139    close(TOUCH);
140}
141