check-for-inappropriate-objc-class-names 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/perl
  2. # Copyright (C) 2006, 2007, 2008, 2010, 2011 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. # 1. Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # 2. Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. #
  13. # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. # THE POSSIBILITY OF SUCH DAMAGE.
  24. # "check-for-inappropriate-objc-class-names" script for WebKit Open Source Project
  25. # Intended to be invoked from an Xcode build step to check if a framework
  26. # defines any Objective-C class whose name does not have one of the prefixes
  27. # the framework is allowed to use.
  28. use warnings;
  29. use strict;
  30. use File::Basename;
  31. sub touch($);
  32. my @allowedPrefixes = @ARGV;
  33. # Xcode will automatically link ObjC binaries against libarclite in some cases, which defines a class called __ARCLite__.
  34. push(@allowedPrefixes, "__ARCLite");
  35. die "No allowed prefixes passed on the command line" if !@allowedPrefixes;
  36. my $arch = $ENV{'CURRENT_ARCH'};
  37. my $target = $ENV{'TARGET_NAME'};
  38. my $variant = $ENV{'CURRENT_VARIANT'};
  39. my $coverageBuild = $ENV{'WEBKIT_COVERAGE_BUILD'};
  40. my $executablePath = "$ENV{'TARGET_BUILD_DIR'}/$ENV{'EXECUTABLE_PATH'}";
  41. my $buildTimestampPath = $ENV{'TARGET_TEMP_DIR'} . "/" . basename($0) . join('-', @allowedPrefixes) . ".timestamp";
  42. my $buildTimestampAge = -M $buildTimestampPath;
  43. my $executablePathAge = -M $executablePath;
  44. my $scriptAge = -M $0;
  45. my $pattern = "^(" . join('|', @allowedPrefixes) . ")";
  46. my $sawError = 0;
  47. if (!defined $executablePathAge || !defined $buildTimestampAge || $executablePathAge < $buildTimestampAge || $scriptAge < $buildTimestampAge) {
  48. if (!open NM, "(nm -Ugjp '$executablePath' | sed 's/^/STDOUT:/') 2>&1 |") {
  49. print "ERROR: Could not open $executablePath\n";
  50. $sawError = 1;
  51. next;
  52. }
  53. my @badNames;
  54. while (<NM>) {
  55. if (/^STDOUT:/) {
  56. next unless /^STDOUT:_OBJC_CLASS_\$_/;
  57. chomp;
  58. my $className = substr($_, 21);
  59. push(@badNames, $className) unless $className =~ /$pattern/;
  60. } else {
  61. print STDERR if $_ ne "nm: no name list\n";
  62. }
  63. }
  64. close NM;
  65. if (@badNames) {
  66. my $shortName = $executablePath;
  67. $shortName =~ s/.*\///;
  68. print "ERROR: $shortName defines one or more Objective-C classes with inappropriate names. ($executablePath)\n";
  69. for my $className (@badNames) {
  70. print "ERROR: Inappropriate Objective-C class name: $className.\n";
  71. }
  72. if (@allowedPrefixes > 1) {
  73. print "ERROR: Objective-C class names in $target must have one of these prefixes: " . join(", ", map('"' . $_ . '"', @allowedPrefixes)) . ".\n";
  74. } else {
  75. print "ERROR: Objective-C class names in $target must have the prefix \"" . $allowedPrefixes[0] . "\".\n";
  76. }
  77. $sawError = 1;
  78. }
  79. }
  80. if ($sawError and !$coverageBuild) {
  81. unlink $executablePath;
  82. exit 1;
  83. }
  84. touch($buildTimestampPath);
  85. exit 0;
  86. sub touch($)
  87. {
  88. my ($path) = @_;
  89. open(TOUCH, ">", $path) or die "$!";
  90. close(TOUCH);
  91. }