check-Xcode-source-file-types 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/perl -w
  2. # Copyright (C) 2007, 2008, 2009, 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. #
  14. # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  15. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  18. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. # Script to check that source file extensions match file types in Xcode project.pbxproj files.
  25. # TODO
  26. # - Add support for file types other than source code files.
  27. # - Can't differentiate between sourcecode.c.h and sourcecode.cpp.h.
  28. # (Hint: Use gcc -x c/objective-c/c++/objective-c++ -E. It will
  29. # take time to check each header using gcc, so make it a switch.)
  30. use strict;
  31. use File::Basename;
  32. use File::Spec;
  33. use File::Temp qw(tempfile);
  34. use Getopt::Long;
  35. # Map of Xcode file types to file extensions.
  36. my %typeExtensionMap = qw(
  37. sourcecode.c.c .c
  38. sourcecode.c.h .h
  39. sourcecode.c.objc .m
  40. sourcecode.cpp.h .h
  41. sourcecode.cpp.cpp .cpp
  42. sourcecode.cpp.objcpp .mm
  43. sourcecode.exports .exp
  44. sourcecode.javascript .js
  45. sourcecode.make .make
  46. sourcecode.mig .defs
  47. sourcecode.yacc .y
  48. );
  49. # Map of file extensions to Xcode file types.
  50. my %extensionTypeMap = map { $typeExtensionMap{$_} => $_ } keys %typeExtensionMap;
  51. $extensionTypeMap{'.h'} = 'sourcecode.c.h'; # See TODO list.
  52. my $shouldFixIssues = 0;
  53. my $printWarnings = 1;
  54. my $showHelp;
  55. my $getOptionsResult = GetOptions(
  56. 'f|fix' => \$shouldFixIssues,
  57. 'h|help' => \$showHelp,
  58. 'w|warnings!' => \$printWarnings,
  59. );
  60. if (scalar(@ARGV) == 0 && !$showHelp) {
  61. print STDERR "ERROR: No Xcode project files (project.pbxproj) listed on command-line.\n";
  62. undef $getOptionsResult;
  63. }
  64. if (!$getOptionsResult || $showHelp) {
  65. print STDERR <<__END__;
  66. Usage: @{[ basename($0) ]} [options] path/to/project.pbxproj [path/to/project.pbxproj ...]
  67. -f|--fix fix mismatched types in Xcode project file
  68. -h|--help show this help message
  69. -w|--[no-]warnings show or suppress warnings (default: show warnings)
  70. __END__
  71. exit 1;
  72. }
  73. for my $projectFile (@ARGV) {
  74. my $issuesFound = 0;
  75. my $issuesFixed = 0;
  76. if (basename($projectFile) =~ /\.xcodeproj$/) {
  77. $projectFile = File::Spec->catfile($projectFile, "project.pbxproj");
  78. }
  79. if (basename($projectFile) ne "project.pbxproj") {
  80. print STDERR "WARNING: Not an Xcode project file: $projectFile\n" if $printWarnings;
  81. next;
  82. }
  83. open(IN, "< $projectFile") || die "Could not open $projectFile: $!";
  84. my ($OUT, $tempFileName);
  85. if ($shouldFixIssues) {
  86. ($OUT, $tempFileName) = tempfile(
  87. basename($projectFile) . "-XXXXXXXX",
  88. DIR => dirname($projectFile),
  89. UNLINK => 0,
  90. );
  91. # Clean up temp file in case of die()
  92. $SIG{__DIE__} = sub {
  93. close(IN);
  94. close($OUT);
  95. unlink($tempFileName);
  96. };
  97. }
  98. # Fast-forward to "Begin PBXFileReference section".
  99. while (my $line = <IN>) {
  100. print $OUT $line if $shouldFixIssues;
  101. last if $line =~ m#^\Q/* Begin PBXFileReference section */\E$#;
  102. }
  103. while (my $line = <IN>) {
  104. if ($line =~ m#^\Q/* End PBXFileReference section */\E$#) {
  105. print $OUT $line if $shouldFixIssues;
  106. last;
  107. }
  108. if ($line =~ m#^\s*[A-Z0-9]{24} /\* (.+) \*/\s+=\s+\{.*\s+explicitFileType = (sourcecode[^;]*);.*\s+path = ([^;]+);.*\};$#) {
  109. my $fileName = $1;
  110. my $fileType = $2;
  111. my $filePath = $3;
  112. my (undef, undef, $fileExtension) = map { lc($_) } fileparse(basename($filePath), qr{\.[^.]+$});
  113. if (!exists $typeExtensionMap{$fileType}) {
  114. $issuesFound++;
  115. print STDERR "WARNING: Unknown file type '$fileType' for file '$filePath'.\n" if $printWarnings;
  116. } elsif ($typeExtensionMap{$fileType} ne $fileExtension) {
  117. $issuesFound++;
  118. print STDERR "WARNING: Incorrect file type '$fileType' for file '$filePath'.\n" if $printWarnings;
  119. $line =~ s/(\s+)explicitFileType( = )(sourcecode[^;]*);/$1lastKnownFileType$2$extensionTypeMap{$fileExtension};/;
  120. $issuesFixed++ if $shouldFixIssues;
  121. }
  122. }
  123. print $OUT $line if $shouldFixIssues;
  124. }
  125. # Output the rest of the file.
  126. print $OUT <IN> if $shouldFixIssues;
  127. close(IN);
  128. if ($shouldFixIssues) {
  129. close($OUT);
  130. unlink($projectFile) || die "Could not delete $projectFile: $!";
  131. rename($tempFileName, $projectFile) || die "Could not rename $tempFileName to $projectFile: $!";
  132. }
  133. if ($printWarnings) {
  134. printf STDERR "%s issues found for $projectFile.\n", ($issuesFound ? $issuesFound : "No");
  135. print STDERR "$issuesFixed issues fixed for $projectFile.\n" if $issuesFixed && $shouldFixIssues;
  136. print STDERR "NOTE: Open $projectFile in Xcode to let it have its way with the file.\n" if $issuesFixed;
  137. print STDERR "\n";
  138. }
  139. }
  140. exit 0;