cmSourceFile.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "cmSourceFile.h"
  14. #include "cmSystemTools.h"
  15. // Set the name of the class and the full path to the file.
  16. // The class must be found in dir and end in name.cxx, name.txx,
  17. // name.c or it will be considered a header file only class
  18. // and not included in the build process
  19. void cmSourceFile::SetName(const char* name, const char* dir,
  20. const std::vector<std::string>& sourceExts,
  21. const std::vector<std::string>& headerExts)
  22. {
  23. this->SetProperty("HEADER_FILE_ONLY","1");
  24. this->SourceNameWithoutLastExtension = "";
  25. // Save the original name given.
  26. this->SourceName = name;
  27. // Convert the name to a full path in case the given name is a
  28. // relative path.
  29. std::string pathname = cmSystemTools::CollapseFullPath(name, dir);
  30. // First try and see whether the listed file can be found
  31. // as is without extensions added on.
  32. std::string hname = pathname;
  33. if(cmSystemTools::FileExists(hname.c_str()))
  34. {
  35. this->SourceName = cmSystemTools::GetFilenamePath(name);
  36. if ( this->SourceName.size() > 0 )
  37. {
  38. this->SourceName += "/";
  39. }
  40. this->SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  41. std::string::size_type pos = hname.rfind('.');
  42. if(pos != std::string::npos)
  43. {
  44. this->SourceExtension = hname.substr(pos+1, hname.size()-pos);
  45. if ( cmSystemTools::FileIsFullPath(name) )
  46. {
  47. std::string::size_type pos2 = hname.rfind('/');
  48. if(pos2 != std::string::npos)
  49. {
  50. this->SourceName = hname.substr(pos2+1, pos - pos2-1);
  51. }
  52. }
  53. }
  54. // See if the file is a header file
  55. if(std::find( headerExts.begin(), headerExts.end(), this->SourceExtension ) ==
  56. headerExts.end())
  57. {
  58. this->SetProperty("HEADER_FILE_ONLY","0");
  59. }
  60. this->FullPath = hname;
  61. // Mark this as an external object file if it has the proper
  62. // extension. THIS CODE IS DUPLICATED IN THE OTHER SetName METHOD.
  63. // THESE METHODS SHOULD BE MERGED.
  64. if ( this->SourceExtension == "obj" || this->SourceExtension == "o" ||
  65. this->SourceExtension == "lo" )
  66. {
  67. this->SetProperty("EXTERNAL_OBJECT", "1");
  68. }
  69. return;
  70. }
  71. // Next, try the various source extensions
  72. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  73. ext != sourceExts.end(); ++ext )
  74. {
  75. hname = pathname;
  76. hname += ".";
  77. hname += *ext;
  78. if(cmSystemTools::FileExists(hname.c_str()))
  79. {
  80. this->SourceExtension = *ext;
  81. this->SetProperty("HEADER_FILE_ONLY","0");
  82. this->FullPath = hname;
  83. return;
  84. }
  85. }
  86. // Finally, try the various header extensions
  87. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  88. ext != headerExts.end(); ++ext )
  89. {
  90. hname = pathname;
  91. hname += ".";
  92. hname += *ext;
  93. if(cmSystemTools::FileExists(hname.c_str()))
  94. {
  95. this->SourceExtension = *ext;
  96. this->FullPath = hname;
  97. return;
  98. }
  99. }
  100. std::string errorMsg = "\n\nTried";
  101. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  102. ext != sourceExts.end(); ++ext )
  103. {
  104. errorMsg += " .";
  105. errorMsg += *ext;
  106. }
  107. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  108. ext != headerExts.end(); ++ext )
  109. {
  110. errorMsg += " .";
  111. errorMsg += *ext;
  112. }
  113. cmSystemTools::Error("can not find file ", pathname.c_str(),
  114. errorMsg.c_str());
  115. }
  116. void cmSourceFile::SetName(const char* name, const char* dir, const char *ext,
  117. bool hfo)
  118. {
  119. this->SetProperty("HEADER_FILE_ONLY",(hfo ? "1" : "0"));
  120. this->SourceNameWithoutLastExtension = "";
  121. this->SourceName = name;
  122. std::string fname = this->SourceName;
  123. if(ext && strlen(ext))
  124. {
  125. fname += ".";
  126. fname += ext;
  127. }
  128. this->FullPath = cmSystemTools::CollapseFullPath(fname.c_str(), dir);
  129. cmSystemTools::ConvertToUnixSlashes(this->FullPath);
  130. this->SourceExtension = ext;
  131. // Mark this as an external object file if it has the proper
  132. // extension. THIS CODE IS DUPLICATED IN THE OTHER SetName METHOD.
  133. // THESE METHODS SHOULD BE MERGED.
  134. if ( this->SourceExtension == "obj" || this->SourceExtension == "o" ||
  135. this->SourceExtension == "lo" )
  136. {
  137. this->SetProperty("EXTERNAL_OBJECT", "1");
  138. }
  139. return;
  140. }
  141. void cmSourceFile::Print() const
  142. {
  143. std::cerr << "this->FullPath: " << this->FullPath << "\n";
  144. std::cerr << "this->SourceName: " << this->SourceName << std::endl;
  145. std::cerr << "this->SourceExtension: " << this->SourceExtension << "\n";
  146. }
  147. void cmSourceFile::SetProperty(const char* prop, const char* value)
  148. {
  149. if (!prop)
  150. {
  151. return;
  152. }
  153. if (!value)
  154. {
  155. value = "NOTFOUND";
  156. }
  157. this->Properties[prop] = value;
  158. }
  159. const char *cmSourceFile::GetProperty(const char* prop) const
  160. {
  161. // watch for special "computed" properties that are dependent on other
  162. // properties or variables, always recompute them
  163. if (!strcmp(prop,"LOCATION"))
  164. {
  165. return this->FullPath.c_str();
  166. }
  167. std::map<cmStdString,cmStdString>::const_iterator i =
  168. this->Properties.find(prop);
  169. if (i != this->Properties.end())
  170. {
  171. return i->second.c_str();
  172. }
  173. return 0;
  174. }
  175. bool cmSourceFile::GetPropertyAsBool(const char* prop) const
  176. {
  177. std::map<cmStdString,cmStdString>::const_iterator i =
  178. this->Properties.find(prop);
  179. if (i != this->Properties.end())
  180. {
  181. return cmSystemTools::IsOn(i->second.c_str());
  182. }
  183. return false;
  184. }
  185. void cmSourceFile::SetCustomCommand(cmCustomCommand* cc)
  186. {
  187. if(this->CustomCommand)
  188. {
  189. delete this->CustomCommand;
  190. }
  191. this->CustomCommand = cc;
  192. }
  193. const std::string& cmSourceFile::GetSourceNameWithoutLastExtension()
  194. {
  195. if ( this->SourceNameWithoutLastExtension.empty() )
  196. {
  197. this->SourceNameWithoutLastExtension =
  198. cmSystemTools::GetFilenameWithoutLastExtension(this->FullPath);
  199. }
  200. return this->SourceNameWithoutLastExtension;
  201. }