1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmSourceFileLocation.h"
14 #include "cmMakefile.h"
15 #include "cmLocalGenerator.h"
16 #include "cmGlobalGenerator.h"
17 #include "cmSystemTools.h"
19 //----------------------------------------------------------------------------
21 ::cmSourceFileLocation(cmMakefile* mf, const char* name): Makefile(mf)
23 this->AmbiguousDirectory = !cmSystemTools::FileIsFullPath(name);
24 this->AmbiguousExtension = true;
25 this->Directory = cmSystemTools::GetFilenamePath(name);
26 this->Name = cmSystemTools::GetFilenameName(name);
27 this->UpdateExtension(name);
30 //----------------------------------------------------------------------------
31 void cmSourceFileLocation::Update(const char* name)
33 if(this->AmbiguousDirectory)
35 this->UpdateDirectory(name);
37 if(this->AmbiguousExtension)
39 this->UpdateExtension(name);
43 //----------------------------------------------------------------------------
44 void cmSourceFileLocation::Update(cmSourceFileLocation const& loc)
46 if(this->AmbiguousDirectory && !loc.AmbiguousDirectory)
48 this->Directory = loc.Directory;
49 this->AmbiguousDirectory = false;
51 if(this->AmbiguousExtension && !loc.AmbiguousExtension)
53 this->Name = loc.Name;
54 this->AmbiguousExtension = false;
58 //----------------------------------------------------------------------------
59 void cmSourceFileLocation::DirectoryUseSource()
61 if(this->AmbiguousDirectory)
64 cmSystemTools::CollapseFullPath(
65 this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
66 this->AmbiguousDirectory = false;
70 //----------------------------------------------------------------------------
71 void cmSourceFileLocation::DirectoryUseBinary()
73 if(this->AmbiguousDirectory)
76 cmSystemTools::CollapseFullPath(
77 this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
78 this->AmbiguousDirectory = false;
82 //----------------------------------------------------------------------------
83 void cmSourceFileLocation::UpdateExtension(const char* name)
85 // Check the extension.
86 std::string ext = cmSystemTools::GetFilenameLastExtension(name);
87 if(!ext.empty()) { ext = ext.substr(1); }
89 // The global generator checks extensions of enabled languages.
90 cmGlobalGenerator* gg =
91 this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
92 cmMakefile* mf = this->Makefile;
93 const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
94 const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
95 if(gg->GetLanguageFromExtension(ext.c_str()) ||
96 std::find(srcExts.begin(), srcExts.end(), ext) != srcExts.end() ||
97 std::find(hdrExts.begin(), hdrExts.end(), ext) != hdrExts.end())
99 // This is a known extension. Use the given filename with extension.
100 this->Name = cmSystemTools::GetFilenameName(name);
101 this->AmbiguousExtension = false;
105 // This is not a known extension. See if the file exists on disk as
108 if(this->AmbiguousDirectory)
110 // Check the source tree only because a file in the build tree should
111 // be specified by full path at least once. We do not want this
112 // detection to depend on whether the project has already been built.
113 tryPath = this->Makefile->GetCurrentDirectory();
116 if(!this->Directory.empty())
118 tryPath += this->Directory;
121 tryPath += this->Name;
122 if(cmSystemTools::FileExists(tryPath.c_str(), true))
124 // We found a source file named by the user on disk. Trust it's
126 this->Name = cmSystemTools::GetFilenameName(name);
127 this->AmbiguousExtension = false;
129 // If the directory was ambiguous, it isn't anymore.
130 if(this->AmbiguousDirectory)
132 this->DirectoryUseSource();
138 //----------------------------------------------------------------------------
139 void cmSourceFileLocation::UpdateDirectory(const char* name)
141 // If a full path was given we know the directory.
142 if(cmSystemTools::FileIsFullPath(name))
144 this->Directory = cmSystemTools::GetFilenamePath(name);
145 this->AmbiguousDirectory = false;
149 //----------------------------------------------------------------------------
152 ::MatchesAmbiguousExtension(cmSourceFileLocation const& loc) const
154 // This location's extension is not ambiguous but loc's extension
155 // is. See if the names match as-is.
156 if(this->Name == loc.Name)
161 // Check if loc's name could possibly be extended to our name by
162 // adding an extension.
163 if(!(this->Name.size() > loc.Name.size() &&
164 this->Name.substr(0, loc.Name.size()) == loc.Name &&
165 this->Name[loc.Name.size()] == '.'))
170 // Only a fixed set of extensions will be tried to match a file on
171 // disk. One of these must match if loc refers to this source file.
172 std::string ext = this->Name.substr(loc.Name.size()+1);
173 cmMakefile* mf = this->Makefile;
174 const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
175 if(std::find(srcExts.begin(), srcExts.end(), ext) != srcExts.end())
179 const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
180 if(std::find(hdrExts.begin(), hdrExts.end(), ext) != hdrExts.end())
187 //----------------------------------------------------------------------------
188 bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc)
190 if(this->AmbiguousExtension && loc.AmbiguousExtension)
192 // Both extensions are ambiguous. Since only the old fixed set of
193 // extensions will be tried, the names must match at this point to
195 if(this->Name != loc.Name)
200 else if(this->AmbiguousExtension)
202 // Only "this" extension is ambiguous.
203 if(!loc.MatchesAmbiguousExtension(*this))
208 else if(loc.AmbiguousExtension)
210 // Only "loc" extension is ambiguous.
211 if(!this->MatchesAmbiguousExtension(loc))
218 // Neither extension is ambiguous.
219 if(this->Name != loc.Name)
225 if(!this->AmbiguousDirectory && !loc.AmbiguousDirectory)
227 // Both sides have absolute directories.
228 if(this->Directory != loc.Directory)
233 else if(this->AmbiguousDirectory && loc.AmbiguousDirectory &&
234 this->Makefile == loc.Makefile)
236 // Both sides have directories relative to the same location.
237 if(this->Directory != loc.Directory)
242 else if(this->AmbiguousDirectory && loc.AmbiguousDirectory)
244 // Each side has a directory relative to a different location.
245 // This can occur when referencing a source file from a different
246 // directory. This is not yet allowed.
247 this->Makefile->IssueMessage(
248 cmake::INTERNAL_ERROR,
249 "Matches error: Each side has a directory relative to a different "
250 "location. This can occur when referencing a source file from a "
251 "different directory. This is not yet allowed."
255 else if(this->AmbiguousDirectory)
257 // Compare possible directory combinations.
259 cmSystemTools::CollapseFullPath(
260 this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
262 cmSystemTools::CollapseFullPath(
263 this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
264 if(srcDir != loc.Directory &&
265 binDir != loc.Directory)
270 else if(loc.AmbiguousDirectory)
272 // Compare possible directory combinations.
274 cmSystemTools::CollapseFullPath(
275 loc.Directory.c_str(), loc.Makefile->GetCurrentDirectory());
277 cmSystemTools::CollapseFullPath(
278 loc.Directory.c_str(), loc.Makefile->GetCurrentOutputDirectory());
279 if(srcDir != this->Directory &&
280 binDir != this->Directory)
286 // File locations match.