check-for-weak-vtables-and-externals 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/perl
  2. # Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. #
  8. # 1. Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # 3. Neither the name of Apple Inc. ("Apple") nor the names of
  14. # its contributors may be used to endorse or promote products derived
  15. # from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. # "check-for-weak-vtables-and-externals" script for WebKit Open Source Project
  28. # Intended to be invoked from an Xcode build step to check if there are
  29. # any weak vtables or weak externals in a target.
  30. use warnings;
  31. use strict;
  32. use File::Basename;
  33. sub touch($);
  34. my $arch = $ENV{'CURRENT_ARCH'};
  35. my $configuration = $ENV{'CONFIGURATION'};
  36. my $target = $ENV{'TARGET_NAME'};
  37. my $variant = $ENV{'CURRENT_VARIANT'};
  38. my $coverageBuild = $ENV{'WEBKIT_COVERAGE_BUILD'};
  39. my $debugRoot = $ENV{'WEBKIT_DEBUG_ROOT'};
  40. $arch = $ENV{'NATIVE_ARCH'} if !$arch; # for Xcode 2.1, which does not have CURRENT_ARCH
  41. $variant = "normal" if !$variant; # for Xcode 2.1, which does not have CURRENT_VARIANT
  42. my $executablePath = "$ENV{'TARGET_BUILD_DIR'}/$ENV{'EXECUTABLE_PATH'}";
  43. my $buildTimestampPath = $ENV{'TARGET_TEMP_DIR'} . "/" . basename($0) . ".timestamp";
  44. my $buildTimestampAge = -M $buildTimestampPath;
  45. my $executablePathAge = -M $executablePath;
  46. my $sawError = 0;
  47. if (!defined $executablePathAge || !defined $buildTimestampAge || $executablePathAge < $buildTimestampAge) {
  48. if (!open NM, "(nm -m '$executablePath' | c++filt | sed 's/^/STDOUT:/') 2>&1 |") {
  49. print "ERROR: Could not open $executablePath\n";
  50. $sawError = 1;
  51. next;
  52. }
  53. my @weakVTableClasses = ();
  54. my @weakExternalSymbols = ();
  55. while (<NM>) {
  56. if (/^STDOUT:/) {
  57. # Ignore undefined, RTTI and typeinfo symbols.
  58. next if /\bundefined\b/ or /\b__ZT[IS]/;
  59. if (/weak external vtable for (.*)$/) {
  60. push @weakVTableClasses, $1;
  61. } elsif (/weak external (.*)$/) {
  62. push @weakExternalSymbols, $1;
  63. }
  64. } else {
  65. print STDERR if $_ ne "nm: no name list\n";
  66. }
  67. }
  68. close NM;
  69. my $shortName = $executablePath;
  70. $shortName =~ s/.*\///;
  71. if (@weakVTableClasses) {
  72. print "ERROR: $shortName has a weak vtable in it ($executablePath)\n";
  73. print "ERROR: Fix by making sure the first virtual function in each of these classes is not an inline:\n";
  74. for my $class (sort @weakVTableClasses) {
  75. print "ERROR: class $class\n";
  76. }
  77. $sawError = 1;
  78. }
  79. if (@weakExternalSymbols) {
  80. print "ERROR: $shortName has a weak external symbol in it ($executablePath)\n";
  81. print "ERROR: A weak external symbol is generated when a symbol is defined in multiple compilation units and is also marked as being exported from the library.\n";
  82. print "ERROR: A common cause of weak external symbols is when an inline function is listed in the linker export file.\n";
  83. for my $symbol (sort @weakExternalSymbols) {
  84. print "ERROR: symbol $symbol\n";
  85. }
  86. $sawError = 1;
  87. }
  88. }
  89. if ($sawError and !$coverageBuild) {
  90. unlink $executablePath;
  91. exit 1;
  92. }
  93. touch($buildTimestampPath);
  94. exit 0;
  95. sub touch($)
  96. {
  97. my ($path) = @_;
  98. open(TOUCH, ">", $path) or die "$!";
  99. close(TOUCH);
  100. }