You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.2 KiB

  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSourceFileLocation.h"
  14. #include "cmMakefile.h"
  15. #include "cmSystemTools.h"
  16. //----------------------------------------------------------------------------
  17. cmSourceFileLocation
  18. ::cmSourceFileLocation(cmMakefile* mf, const char* name): Makefile(mf)
  19. {
  20. this->AmbiguousDirectory = !cmSystemTools::FileIsFullPath(name);
  21. this->AmbiguousExtension = true;
  22. this->Directory = cmSystemTools::GetFilenamePath(name);
  23. this->Name = cmSystemTools::GetFilenameName(name);
  24. this->UpdateExtension(name);
  25. }
  26. //----------------------------------------------------------------------------
  27. void cmSourceFileLocation::Update(const char* name)
  28. {
  29. if(this->AmbiguousDirectory)
  30. {
  31. this->UpdateDirectory(name);
  32. }
  33. if(this->AmbiguousExtension)
  34. {
  35. this->UpdateExtension(name);
  36. }
  37. }
  38. //----------------------------------------------------------------------------
  39. void cmSourceFileLocation::Update(cmSourceFileLocation const& loc)
  40. {
  41. if(this->AmbiguousDirectory && !loc.AmbiguousDirectory)
  42. {
  43. this->Directory = loc.Directory;
  44. this->AmbiguousDirectory = false;
  45. }
  46. if(this->AmbiguousExtension && !loc.AmbiguousExtension)
  47. {
  48. this->Name = loc.Name;
  49. this->AmbiguousExtension = false;
  50. }
  51. }
  52. //----------------------------------------------------------------------------
  53. void cmSourceFileLocation::DirectoryUseSource()
  54. {
  55. if(this->AmbiguousDirectory)
  56. {
  57. this->Directory =
  58. cmSystemTools::CollapseFullPath(
  59. this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
  60. this->AmbiguousDirectory = false;
  61. }
  62. }
  63. //----------------------------------------------------------------------------
  64. void cmSourceFileLocation::DirectoryUseBinary()
  65. {
  66. if(this->AmbiguousDirectory)
  67. {
  68. this->Directory =
  69. cmSystemTools::CollapseFullPath(
  70. this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
  71. this->AmbiguousDirectory = false;
  72. }
  73. }
  74. //----------------------------------------------------------------------------
  75. void cmSourceFileLocation::UpdateExtension(const char* name)
  76. {
  77. // Check the extension.
  78. std::string ext = cmSystemTools::GetFilenameLastExtension(name);
  79. if(!ext.empty()) { ext = ext.substr(1); }
  80. // TODO: Let enable-language specify extensions for each language.
  81. cmMakefile const* mf = this->Makefile;
  82. const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
  83. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  84. if(std::find(srcExts.begin(), srcExts.end(), ext) != srcExts.end() ||
  85. std::find(hdrExts.begin(), hdrExts.end(), ext) != hdrExts.end())
  86. {
  87. // This is a known extension. Use the given filename with extension.
  88. this->Name = cmSystemTools::GetFilenameName(name);
  89. this->AmbiguousExtension = false;
  90. }
  91. }
  92. //----------------------------------------------------------------------------
  93. void cmSourceFileLocation::UpdateDirectory(const char* name)
  94. {
  95. // If a full path was given we know the directory.
  96. if(cmSystemTools::FileIsFullPath(name))
  97. {
  98. this->Directory = cmSystemTools::GetFilenamePath(name);
  99. this->AmbiguousDirectory = false;
  100. }
  101. }
  102. //----------------------------------------------------------------------------
  103. bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc)
  104. {
  105. if(this->AmbiguousExtension || loc.AmbiguousExtension)
  106. {
  107. // Need to compare without the file extension.
  108. std::string thisName;
  109. if(this->AmbiguousExtension)
  110. {
  111. thisName = this->Name;
  112. }
  113. else
  114. {
  115. thisName = cmSystemTools::GetFilenameWithoutLastExtension(this->Name);
  116. }
  117. std::string locName;
  118. if(loc.AmbiguousExtension)
  119. {
  120. locName = loc.Name;
  121. }
  122. else
  123. {
  124. locName = cmSystemTools::GetFilenameWithoutLastExtension(loc.Name);
  125. }
  126. if(thisName != locName)
  127. {
  128. return false;
  129. }
  130. }
  131. else
  132. {
  133. // Compare with extension.
  134. if(this->Name != loc.Name)
  135. {
  136. return false;
  137. }
  138. }
  139. if(!this->AmbiguousDirectory && !loc.AmbiguousDirectory)
  140. {
  141. // Both sides have absolute directories.
  142. if(this->Directory != loc.Directory)
  143. {
  144. return false;
  145. }
  146. }
  147. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory &&
  148. this->Makefile == loc.Makefile)
  149. {
  150. // Both sides have directories relative to the same location.
  151. if(this->Directory != loc.Directory)
  152. {
  153. return false;
  154. }
  155. }
  156. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory)
  157. {
  158. // Each side has a directory relative to a different location.
  159. // This can occur when referencing a source file from a different
  160. // directory. This is not yet allowed.
  161. abort();
  162. }
  163. else if(this->AmbiguousDirectory)
  164. {
  165. // Compare possible directory combinations.
  166. std::string srcDir =
  167. cmSystemTools::CollapseFullPath(
  168. this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
  169. std::string binDir =
  170. cmSystemTools::CollapseFullPath(
  171. this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
  172. if(srcDir != loc.Directory &&
  173. binDir != loc.Directory)
  174. {
  175. return false;
  176. }
  177. }
  178. else if(loc.AmbiguousDirectory)
  179. {
  180. // Compare possible directory combinations.
  181. std::string srcDir =
  182. cmSystemTools::CollapseFullPath(
  183. loc.Directory.c_str(), loc.Makefile->GetCurrentDirectory());
  184. std::string binDir =
  185. cmSystemTools::CollapseFullPath(
  186. loc.Directory.c_str(), loc.Makefile->GetCurrentOutputDirectory());
  187. if(srcDir != this->Directory &&
  188. binDir != this->Directory)
  189. {
  190. return false;
  191. }
  192. }
  193. // File locations match.
  194. this->Update(loc);
  195. return true;
  196. }