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-exit-time-destructors" script for WebKit Open Source Project
30
31# Intended to be invoked from an Xcode build step to check if there are
32# any exit-time destructors in a target.
33
34use warnings;
35use strict;
36
37use File::Basename;
38
39sub touch($);
40sub printFunctions($$);
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 $sawAtExit = 0;
82    while (<NM>) {
83        if (/^STDOUT:/) {
84            $sawAtExit = 1 if /___cxa_atexit/;
85        } else {
86            print STDERR if $_ ne "nm: no name list\n";
87        }
88    }
89    close NM;
90    next unless $sawAtExit;
91
92    my $shortName = $file;
93    $shortName =~ s/.*\///;
94
95    $sawError = 1 if printFunctions($shortName, $file);
96}
97
98if ($sawError and !$coverageBuild) {
99    print "ERROR: Use DEFINE_STATIC_LOCAL from <wtf/StdLibExtras.h>\n";
100    unlink $executablePath;
101    exit 1;
102}
103
104touch($buildTimestampPath);
105exit 0;
106
107sub touch($)
108{
109    my ($path) = @_;
110    open(TOUCH, ">", $path) or die "$!";
111    close(TOUCH);
112}
113
114sub demangle($)
115{
116    my ($symbol) = @_;
117    if (!open FILT, "c++filt $symbol |") {
118        print "ERROR: Could not open c++filt\n";
119        return;
120    }
121    my $result = <FILT>;
122    close FILT;
123    chomp $result;
124    return $result;
125}
126
127sub printFunctions($$)
128{
129    my ($shortName, $path) = @_;
130    if (!open OTOOL, "otool -tV '$path' |") {
131        print "WARNING: Could not open $path\n";
132        return 0;
133    }
134    my %functions;
135    my $currentSymbol = "";
136    while (<OTOOL>) {
137        $currentSymbol = $1 if /^(\w+):$/;
138        next unless $currentSymbol;
139        $functions{demangle($currentSymbol)} = 1 if /___cxa_atexit/;
140    }
141    close OTOOL;
142    my $result = 0;
143    for my $function (sort keys %functions) {
144        if (!$result) {
145            print "ERROR: $shortName has exit time destructors in it! ($path)\n";
146            $result = 1;
147        }
148        print "ERROR: In function $function\n";
149    }
150    return $result;
151}
152