1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSourceFileLocation.cxx,v $
6 Date: $Date: 2007-06-18 15:59:23 $
7 Version: $Revision: 1.2 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmSourceFileLocation.h"
19 #include "cmMakefile.h"
20 #include "cmSystemTools.h"
22 //----------------------------------------------------------------------------
24 ::cmSourceFileLocation(cmMakefile
* mf
, const char* name
): Makefile(mf
)
26 this->AmbiguousDirectory
= !cmSystemTools::FileIsFullPath(name
);
27 this->AmbiguousExtension
= true;
28 this->Directory
= cmSystemTools::GetFilenamePath(name
);
29 this->Name
= cmSystemTools::GetFilenameName(name
);
30 this->UpdateExtension(name
);
33 //----------------------------------------------------------------------------
34 void cmSourceFileLocation::Update(const char* name
)
36 if(this->AmbiguousDirectory
)
38 this->UpdateDirectory(name
);
40 if(this->AmbiguousExtension
)
42 this->UpdateExtension(name
);
46 //----------------------------------------------------------------------------
47 void cmSourceFileLocation::Update(cmSourceFileLocation
const& loc
)
49 if(this->AmbiguousDirectory
&& !loc
.AmbiguousDirectory
)
51 this->Directory
= loc
.Directory
;
52 this->AmbiguousDirectory
= false;
54 if(this->AmbiguousExtension
&& !loc
.AmbiguousExtension
)
56 this->Name
= loc
.Name
;
57 this->AmbiguousExtension
= false;
61 //----------------------------------------------------------------------------
62 void cmSourceFileLocation::DirectoryUseSource()
64 if(this->AmbiguousDirectory
)
67 cmSystemTools::CollapseFullPath(
68 this->Directory
.c_str(), this->Makefile
->GetCurrentDirectory());
69 this->AmbiguousDirectory
= false;
73 //----------------------------------------------------------------------------
74 void cmSourceFileLocation::DirectoryUseBinary()
76 if(this->AmbiguousDirectory
)
79 cmSystemTools::CollapseFullPath(
80 this->Directory
.c_str(), this->Makefile
->GetCurrentOutputDirectory());
81 this->AmbiguousDirectory
= false;
85 //----------------------------------------------------------------------------
86 void cmSourceFileLocation::UpdateExtension(const char* name
)
88 // Check the extension.
89 std::string ext
= cmSystemTools::GetFilenameLastExtension(name
);
90 if(!ext
.empty()) { ext
= ext
.substr(1); }
92 // TODO: Let enable-language specify extensions for each language.
93 cmMakefile
const* mf
= this->Makefile
;
94 const std::vector
<std::string
>& srcExts
= mf
->GetSourceExtensions();
95 const std::vector
<std::string
>& hdrExts
= mf
->GetHeaderExtensions();
96 if(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 //----------------------------------------------------------------------------
106 void cmSourceFileLocation::UpdateDirectory(const char* name
)
108 // If a full path was given we know the directory.
109 if(cmSystemTools::FileIsFullPath(name
))
111 this->Directory
= cmSystemTools::GetFilenamePath(name
);
112 this->AmbiguousDirectory
= false;
116 //----------------------------------------------------------------------------
117 bool cmSourceFileLocation::Matches(cmSourceFileLocation
const& loc
)
119 if(this->AmbiguousExtension
|| loc
.AmbiguousExtension
)
121 // Need to compare without the file extension.
122 std::string thisName
;
123 if(this->AmbiguousExtension
)
125 thisName
= this->Name
;
129 thisName
= cmSystemTools::GetFilenameWithoutLastExtension(this->Name
);
132 if(loc
.AmbiguousExtension
)
138 locName
= cmSystemTools::GetFilenameWithoutLastExtension(loc
.Name
);
140 if(thisName
!= locName
)
147 // Compare with extension.
148 if(this->Name
!= loc
.Name
)
154 if(!this->AmbiguousDirectory
&& !loc
.AmbiguousDirectory
)
156 // Both sides have absolute directories.
157 if(this->Directory
!= loc
.Directory
)
162 else if(this->AmbiguousDirectory
&& loc
.AmbiguousDirectory
&&
163 this->Makefile
== loc
.Makefile
)
165 // Both sides have directories relative to the same location.
166 if(this->Directory
!= loc
.Directory
)
171 else if(this->AmbiguousDirectory
&& loc
.AmbiguousDirectory
)
173 // Each side has a directory relative to a different location.
174 // This can occur when referencing a source file from a different
175 // directory. This is not yet allowed.
178 else if(this->AmbiguousDirectory
)
180 // Compare possible directory combinations.
182 cmSystemTools::CollapseFullPath(
183 this->Directory
.c_str(), this->Makefile
->GetCurrentDirectory());
185 cmSystemTools::CollapseFullPath(
186 this->Directory
.c_str(), this->Makefile
->GetCurrentOutputDirectory());
187 if(srcDir
!= loc
.Directory
&&
188 binDir
!= loc
.Directory
)
193 else if(loc
.AmbiguousDirectory
)
195 // Compare possible directory combinations.
197 cmSystemTools::CollapseFullPath(
198 loc
.Directory
.c_str(), loc
.Makefile
->GetCurrentDirectory());
200 cmSystemTools::CollapseFullPath(
201 loc
.Directory
.c_str(), loc
.Makefile
->GetCurrentOutputDirectory());
202 if(srcDir
!= this->Directory
&&
203 binDir
!= this->Directory
)
209 // File locations match.