cmSourceFile.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "cmStandardIncludes.h"
  15. #include "cmSystemTools.h"
  16. // Set the name of the class and the full path to the file.
  17. // The class must be found in dir and end in name.cxx, name.txx,
  18. // name.c or it will be considered a header file only class
  19. // and not included in the build process
  20. void cmSourceFile::SetName(const char* name, const char* dir,
  21. const std::vector<std::string>& sourceExts,
  22. const std::vector<std::string>& headerExts)
  23. {
  24. this->SetProperty("HEADER_FILE_ONLY","1");
  25. m_SourceName = name;
  26. std::string pathname = dir;
  27. // the name might include the full path already, so
  28. // check for this case
  29. if (name && (name[0] == '/' ||
  30. (name[0] != '\0' && name[1] == ':')))
  31. {
  32. pathname = "";
  33. }
  34. if(pathname != "")
  35. {
  36. pathname += "/";
  37. }
  38. // First try and see whether the listed file can be found
  39. // as is without extensions added on.
  40. pathname += m_SourceName;
  41. std::string hname = pathname;
  42. if(cmSystemTools::FileExists(hname.c_str()))
  43. {
  44. std::string::size_type pos = hname.rfind('.');
  45. if(pos != std::string::npos)
  46. {
  47. m_SourceExtension = hname.substr(pos+1, hname.size()-pos);
  48. std::string::size_type pos2 = hname.rfind('/');
  49. if(pos2 != std::string::npos)
  50. {
  51. m_SourceName = hname.substr(pos2+1, pos - pos2-1);
  52. }
  53. else
  54. {
  55. m_SourceName = hname.substr(0, pos);
  56. }
  57. }
  58. // See if the file is a header file
  59. if(std::find( headerExts.begin(), headerExts.end(), m_SourceExtension ) ==
  60. headerExts.end())
  61. {
  62. this->SetProperty("HEADER_FILE_ONLY","0");
  63. }
  64. m_FullPath = hname;
  65. return;
  66. }
  67. // Next, try the various source extensions
  68. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  69. ext != sourceExts.end(); ++ext )
  70. {
  71. hname = pathname;
  72. hname += ".";
  73. hname += *ext;
  74. if(cmSystemTools::FileExists(hname.c_str()))
  75. {
  76. m_SourceExtension = *ext;
  77. this->SetProperty("HEADER_FILE_ONLY","0");
  78. m_FullPath = hname;
  79. return;
  80. }
  81. }
  82. // Finally, try the various header extensions
  83. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  84. ext != headerExts.end(); ++ext )
  85. {
  86. hname = pathname;
  87. hname += ".";
  88. hname += *ext;
  89. if(cmSystemTools::FileExists(hname.c_str()))
  90. {
  91. m_SourceExtension = *ext;
  92. m_FullPath = hname;
  93. return;
  94. }
  95. }
  96. std::string errorMsg = "\n\nTried";
  97. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  98. ext != sourceExts.end(); ++ext )
  99. {
  100. errorMsg += " .";
  101. errorMsg += *ext;
  102. }
  103. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  104. ext != headerExts.end(); ++ext )
  105. {
  106. errorMsg += " .";
  107. errorMsg += *ext;
  108. }
  109. cmSystemTools::Error("can not find file ", pathname.c_str(),
  110. errorMsg.c_str());
  111. }
  112. void cmSourceFile::SetName(const char* name, const char* dir, const char *ext,
  113. bool hfo)
  114. {
  115. this->SetProperty("HEADER_FILE_ONLY",(hfo ? "1" : "0"));
  116. m_SourceName = name;
  117. std::string pathname = dir;
  118. if(pathname != "")
  119. {
  120. pathname += "/";
  121. }
  122. pathname += m_SourceName;
  123. if(ext && strlen(ext))
  124. {
  125. pathname += ".";
  126. pathname += ext;
  127. }
  128. m_FullPath = pathname;
  129. m_SourceExtension = ext;
  130. return;
  131. }
  132. void cmSourceFile::Print() const
  133. {
  134. std::cerr << "m_FullPath: " << m_FullPath << "\n";
  135. std::cerr << "m_SourceName: " << m_SourceName << std::endl;
  136. std::cerr << "m_SourceExtension: " << m_SourceExtension << "\n";
  137. }
  138. void cmSourceFile::SetProperty(const char* prop, const char* value)
  139. {
  140. if (!prop)
  141. {
  142. return;
  143. }
  144. if (!value)
  145. {
  146. value = "NOTFOUND";
  147. }
  148. m_Properties[prop] = value;
  149. }
  150. const char *cmSourceFile::GetProperty(const char* prop) const
  151. {
  152. std::map<cmStdString,cmStdString>::const_iterator i =
  153. m_Properties.find(prop);
  154. if (i != m_Properties.end())
  155. {
  156. return i->second.c_str();
  157. }
  158. return 0;
  159. }
  160. bool cmSourceFile::GetPropertyAsBool(const char* prop) const
  161. {
  162. std::map<cmStdString,cmStdString>::const_iterator i =
  163. m_Properties.find(prop);
  164. if (i != m_Properties.end())
  165. {
  166. return cmSystemTools::IsOn(i->second.c_str());
  167. }
  168. return false;
  169. }