cmSourceFile.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 += m_SourceName;
  40. std::string hname = pathname;
  41. if(cmSystemTools::FileExists(hname.c_str()))
  42. {
  43. std::string::size_type pos = hname.rfind('.');
  44. if(pos != std::string::npos)
  45. {
  46. m_SourceExtension = hname.substr(pos+1, hname.size()-pos);
  47. std::string::size_type pos2 = hname.rfind('/');
  48. if(pos2 != std::string::npos)
  49. {
  50. m_SourceName = hname.substr(pos2+1, pos - pos2-1);
  51. }
  52. else
  53. {
  54. m_SourceName = hname.substr(0, pos);
  55. }
  56. }
  57. // See if the file is a header file
  58. if(std::find( headerExts.begin(), headerExts.end(), m_SourceExtension ) ==
  59. headerExts.end())
  60. {
  61. this->SetProperty("HEADER_FILE_ONLY","0");
  62. }
  63. m_FullPath = hname;
  64. return;
  65. }
  66. // Next, try the various source extensions
  67. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  68. ext != sourceExts.end(); ++ext )
  69. {
  70. hname = pathname;
  71. hname += ".";
  72. hname += *ext;
  73. if(cmSystemTools::FileExists(hname.c_str()))
  74. {
  75. m_SourceExtension = *ext;
  76. this->SetProperty("HEADER_FILE_ONLY","0");
  77. m_FullPath = hname;
  78. return;
  79. }
  80. }
  81. // Finally, try the various header extensions
  82. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  83. ext != headerExts.end(); ++ext )
  84. {
  85. hname = pathname;
  86. hname += ".";
  87. hname += *ext;
  88. if(cmSystemTools::FileExists(hname.c_str()))
  89. {
  90. m_SourceExtension = *ext;
  91. m_FullPath = hname;
  92. return;
  93. }
  94. }
  95. std::string errorMsg = "\n\nTried";
  96. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  97. ext != sourceExts.end(); ++ext )
  98. {
  99. errorMsg += " .";
  100. errorMsg += *ext;
  101. }
  102. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  103. ext != headerExts.end(); ++ext )
  104. {
  105. errorMsg += " .";
  106. errorMsg += *ext;
  107. }
  108. cmSystemTools::Error("can not find file ", pathname.c_str(),
  109. errorMsg.c_str());
  110. }
  111. void cmSourceFile::SetName(const char* name, const char* dir, const char *ext,
  112. bool hfo)
  113. {
  114. this->SetProperty("HEADER_FILE_ONLY",(hfo ? "1" : "0"));
  115. m_SourceName = name;
  116. std::string pathname = dir;
  117. if(pathname != "")
  118. {
  119. pathname += "/";
  120. }
  121. pathname += m_SourceName;
  122. if(ext && strlen(ext))
  123. {
  124. pathname += ".";
  125. pathname += ext;
  126. }
  127. m_FullPath = pathname;
  128. m_SourceExtension = ext;
  129. return;
  130. }
  131. void cmSourceFile::Print() const
  132. {
  133. std::cerr << "m_FullPath: " << m_FullPath << "\n";
  134. std::cerr << "m_SourceName: " << m_SourceName << std::endl;
  135. std::cerr << "m_SourceExtension: " << m_SourceExtension << "\n";
  136. }
  137. void cmSourceFile::SetProperty(const char* prop, const char* value)
  138. {
  139. if (!prop)
  140. {
  141. return;
  142. }
  143. if (!value)
  144. {
  145. value = "NOTFOUND";
  146. }
  147. m_Properties[prop] = value;
  148. }
  149. const char *cmSourceFile::GetProperty(const char* prop) const
  150. {
  151. std::map<cmStdString,cmStdString>::const_iterator i =
  152. m_Properties.find(prop);
  153. if (i != m_Properties.end())
  154. {
  155. return i->second.c_str();
  156. }
  157. return 0;
  158. }
  159. bool cmSourceFile::GetPropertyAsBool(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 cmSystemTools::IsOn(i->second.c_str());
  166. }
  167. return false;
  168. }