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