cmSourceFile.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. m_SourceName = name;
  25. std::string pathname = dir;
  26. // the name might include the full path already, so
  27. // check for this case
  28. if (name && (name[0] == '/' ||
  29. (name[0] != '\0' && name[1] == ':')))
  30. {
  31. pathname = "";
  32. }
  33. if(pathname != "")
  34. {
  35. pathname += "/";
  36. }
  37. // First try and see whether the listed file can be found
  38. // as is without extensions added on.
  39. pathname += name;
  40. std::string hname = pathname;
  41. if(cmSystemTools::FileExists(hname.c_str()))
  42. {
  43. m_SourceName = cmSystemTools::GetFilenamePath(name);
  44. if ( m_SourceName.size() > 0 )
  45. {
  46. m_SourceName += "/";
  47. }
  48. m_SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  49. std::string::size_type pos = hname.rfind('.');
  50. if(pos != std::string::npos)
  51. {
  52. m_SourceExtension = hname.substr(pos+1, hname.size()-pos);
  53. if ( cmSystemTools::FileIsFullPath(name) )
  54. {
  55. std::string::size_type pos2 = hname.rfind('/');
  56. if(pos2 != std::string::npos)
  57. {
  58. m_SourceName = hname.substr(pos2+1, pos - pos2-1);
  59. }
  60. }
  61. }
  62. // See if the file is a header file
  63. if(std::find( headerExts.begin(), headerExts.end(), m_SourceExtension ) ==
  64. headerExts.end())
  65. {
  66. this->SetProperty("HEADER_FILE_ONLY","0");
  67. }
  68. m_FullPath = hname;
  69. if ( m_SourceExtension == "obj" || m_SourceExtension == "o" ||
  70. m_SourceExtension == "lo" )
  71. {
  72. this->SetProperty("EXTERNAL_OBJECT", "1");
  73. }
  74. return;
  75. }
  76. // Next, try the various source extensions
  77. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  78. ext != sourceExts.end(); ++ext )
  79. {
  80. hname = pathname;
  81. hname += ".";
  82. hname += *ext;
  83. if(cmSystemTools::FileExists(hname.c_str()))
  84. {
  85. m_SourceExtension = *ext;
  86. this->SetProperty("HEADER_FILE_ONLY","0");
  87. m_FullPath = hname;
  88. return;
  89. }
  90. }
  91. // Finally, try the various header extensions
  92. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  93. ext != headerExts.end(); ++ext )
  94. {
  95. hname = pathname;
  96. hname += ".";
  97. hname += *ext;
  98. if(cmSystemTools::FileExists(hname.c_str()))
  99. {
  100. m_SourceExtension = *ext;
  101. m_FullPath = hname;
  102. return;
  103. }
  104. }
  105. std::string errorMsg = "\n\nTried";
  106. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  107. ext != sourceExts.end(); ++ext )
  108. {
  109. errorMsg += " .";
  110. errorMsg += *ext;
  111. }
  112. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  113. ext != headerExts.end(); ++ext )
  114. {
  115. errorMsg += " .";
  116. errorMsg += *ext;
  117. }
  118. cmSystemTools::Error("can not find file ", pathname.c_str(),
  119. errorMsg.c_str());
  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. m_SourceName = name;
  126. std::string pathname = dir;
  127. if(pathname != "")
  128. {
  129. pathname += "/";
  130. }
  131. pathname += m_SourceName;
  132. if(ext && strlen(ext))
  133. {
  134. pathname += ".";
  135. pathname += ext;
  136. }
  137. m_FullPath = pathname;
  138. m_SourceExtension = ext;
  139. return;
  140. }
  141. void cmSourceFile::Print() const
  142. {
  143. std::cerr << "m_FullPath: " << m_FullPath << "\n";
  144. std::cerr << "m_SourceName: " << m_SourceName << std::endl;
  145. std::cerr << "m_SourceExtension: " << m_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. m_Properties[prop] = value;
  158. }
  159. const char *cmSourceFile::GetProperty(const char* prop) const
  160. {
  161. std::map<cmStdString,cmStdString>::const_iterator i =
  162. m_Properties.find(prop);
  163. if (i != m_Properties.end())
  164. {
  165. return i->second.c_str();
  166. }
  167. return 0;
  168. }
  169. bool cmSourceFile::GetPropertyAsBool(const char* prop) const
  170. {
  171. std::map<cmStdString,cmStdString>::const_iterator i =
  172. m_Properties.find(prop);
  173. if (i != m_Properties.end())
  174. {
  175. return cmSystemTools::IsOn(i->second.c_str());
  176. }
  177. return false;
  178. }