3 # Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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.
26 # Script to check that source file extensions match file types in Xcode project.pbxproj files.
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.)
38 use File::Temp qw(tempfile);
41 # Map of Xcode file types to file extensions.
42 my %typeExtensionMap = qw(
47 sourcecode.cpp.cpp .cpp
48 sourcecode.cpp.objcpp .mm
49 sourcecode.exports .exp
50 sourcecode.javascript .js
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.
60 my $shouldFixIssues = 0;
61 my $printWarnings = 1;
64 my $getOptionsResult = GetOptions(
65 'f|fix' => \$shouldFixIssues,
66 'h|help' => \$showHelp,
67 'w|warnings!' => \$printWarnings,
70 if (scalar(@ARGV) == 0 && !$showHelp) {
71 print STDERR "ERROR: No Xcode project files (project.pbxproj) listed on command-line.\n";
72 undef $getOptionsResult;
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)
85 for my $projectFile (@ARGV) {
89 if (basename($projectFile) =~ /\.xcodeproj$/) {
90 $projectFile = File::Spec->catfile($projectFile, "project.pbxproj");
93 if (basename($projectFile) ne "project.pbxproj") {
94 print STDERR "WARNING: Not an Xcode project file: $projectFile\n" if $printWarnings;
98 open(IN, "< $projectFile") || die "Could not open $projectFile: $!";
100 my ($OUT, $tempFileName);
101 if ($shouldFixIssues) {
102 ($OUT, $tempFileName) = tempfile(
103 basename($projectFile) . "-XXXXXXXX",
104 DIR => dirname($projectFile),
108 # Clean up temp file in case of die()
109 $SIG{__DIE__} = sub {
112 unlink($tempFileName);
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$#;
122 while (my $line = <IN>) {
123 if ($line =~ m#^\Q/* End PBXFileReference section */\E$#) {
124 print $OUT $line if $shouldFixIssues;
128 if ($line =~ m#^\s*[A-Z0-9]{24} /\* (.+) \*/\s+=\s+\{.*\s+explicitFileType = (sourcecode[^;]*);.*\s+path = ([^;]+);.*\};$#) {
132 my (undef, undef, $fileExtension) = map { lc($_) } fileparse(basename($filePath), qr{\.[^.]+$});
134 if (!exists $typeExtensionMap{$fileType}) {
136 print STDERR "WARNING: Unknown file type '$fileType' for file '$filePath'.\n" if $printWarnings;
137 } elsif ($typeExtensionMap{$fileType} ne $fileExtension) {
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;
145 print $OUT $line if $shouldFixIssues;
148 # Output the rest of the file.
149 print $OUT <IN> if $shouldFixIssues;
153 if ($shouldFixIssues) {
156 unlink($projectFile) || die "Could not delete $projectFile: $!";
157 rename($tempFileName, $projectFile) || die "Could not rename $tempFileName to $projectFile: $!";
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;