cmSourceFile.cxx 6.6 KB

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