cmConfigureGccXmlCommand.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmConfigureGccXmlCommand.h"
  33. #include "cmCacheManager.h"
  34. cmConfigureGccXmlCommand::cmConfigureGccXmlCommand()
  35. {
  36. }
  37. cmConfigureGccXmlCommand::~cmConfigureGccXmlCommand()
  38. {
  39. }
  40. // cmConfigureGccXmlCommand
  41. bool cmConfigureGccXmlCommand::InitialPass(std::vector<std::string>& args)
  42. {
  43. if(args.size() != 2)
  44. {
  45. this->SetError("called with incorrect number of arguments");
  46. return false;
  47. }
  48. // If the cache entry already exists, we are done.
  49. std::string cacheName = args[1];
  50. const char* cacheValue =
  51. m_Makefile->GetDefinition(cacheName.c_str());
  52. if(cacheValue && (std::string(cacheValue) != ""))
  53. { return true; }
  54. // Get the gccxml support directory location. This is based on the
  55. // executable location.
  56. if(!this->GetSupportDirectory(args[0].c_str()))
  57. { return false; }
  58. #if defined(_WIN32) && !defined(__CYGWIN__)
  59. // On Windows, we will just look at VcInclude/FLAGS.txt for now.
  60. if(!this->FindVcIncludeFlags())
  61. { return false; }
  62. #else
  63. // On UNIX, we have to determine which compiler is being used, and
  64. // attempt to use that compiler's support directory.
  65. if(this->CompilerIsGCC())
  66. {
  67. if(!this->FindGccIncludeFlags())
  68. { return false; }
  69. }
  70. else if(this->CompilerIsMipsPro())
  71. {
  72. if(!this->FindMproIncludeFlags())
  73. { return false; }
  74. }
  75. else
  76. {
  77. this->SetError("Compiler is not supported by GCC-XML!\n");
  78. return false;
  79. }
  80. #endif
  81. // Add the cache entry with the flags found.
  82. m_Makefile->AddCacheDefinition(
  83. cacheName.c_str(),
  84. m_Flags.c_str(),
  85. "Flags to GCC-XML to get it to parse the native compiler's headers.",
  86. cmCacheManager::STRING);
  87. return true;
  88. }
  89. /**
  90. * Given the location of the GCC-XML executable, find the root of the
  91. * support library tree. Subdirectories of the returned location should
  92. * contain the compiler-specific support libraries.
  93. */
  94. bool cmConfigureGccXmlCommand::GetSupportDirectory(const char* exeLoc)
  95. {
  96. std::string gccxml = exeLoc;
  97. m_Makefile->ExpandVariablesInString(gccxml);
  98. if(!cmSystemTools::FileExists(gccxml.c_str()))
  99. {
  100. std::string err = "Can't find GCC-XML at given path: ";
  101. err += gccxml;
  102. this->SetError(err.c_str());
  103. return false;
  104. }
  105. std::string dir;
  106. std::string file;
  107. // Get the directory (also converts to unix slashes).
  108. cmSystemTools::SplitProgramPath(gccxml.c_str(), dir, file);
  109. #if !defined(_WIN32) || defined(__CYGWIN__)
  110. // On UNIX platforms, we must replace the "/bin" suffix with
  111. // "/share/GCC_XML". If there is no "/bin" suffix, we will assume
  112. // that the user has put everything in one directory, and not change
  113. // the path.
  114. if(dir.substr(dir.length()-4, 4) == "/bin")
  115. {
  116. dir = dir.substr(0, dir.length()-4) + "/share/GCC_XML";
  117. }
  118. #endif
  119. m_SupportDir = dir;
  120. return true;
  121. }
  122. /**
  123. * Find the flags needed to use the Visual C++ support library.
  124. */
  125. bool cmConfigureGccXmlCommand::FindVcIncludeFlags()
  126. {
  127. std::string fname = m_SupportDir+"/VcInclude/FLAGS.txt";
  128. std::ifstream flagsFile(fname.c_str());
  129. if(!flagsFile)
  130. {
  131. std::string err = "Cannot open GCC-XML flags file \""+fname+"\".";
  132. this->SetError(err.c_str());
  133. return false;
  134. }
  135. // TODO: Replace this with a real implementation.
  136. char buf[4096];
  137. flagsFile.getline(buf, 4096);
  138. if(!flagsFile)
  139. {
  140. std::string err = "Error reading from GCC-XML flags file \""+fname+"\".";
  141. this->SetError(err.c_str());
  142. return false;
  143. }
  144. m_Flags = buf;
  145. return true;
  146. }
  147. /**
  148. * Find the flags needed to use the GCC support library.
  149. */
  150. bool cmConfigureGccXmlCommand::FindGccIncludeFlags()
  151. {
  152. std::string supportDir = m_SupportDir+"/GccInclude";
  153. if(!cmSystemTools::FileIsDirectory(supportDir.c_str()))
  154. {
  155. std::string err = "No GCC support library for GCC-XML. Couldn't find directory \""+supportDir+"\".";
  156. this->SetError(err.c_str());
  157. return false;
  158. }
  159. // Try to run the find_gcc_options command.
  160. std::string command = supportDir+"/find_gcc_options";
  161. std::string flags;
  162. if(!cmSystemTools::RunCommand(command.c_str(), flags))
  163. {
  164. this->SetError("Could not run find_gcc_options!");
  165. return false;
  166. }
  167. // Use the result of the command as the flags.
  168. m_Flags = flags;
  169. return true;
  170. }
  171. /**
  172. * Find the flags needed to use the MIPSpro support library.
  173. */
  174. bool cmConfigureGccXmlCommand::FindMproIncludeFlags()
  175. {
  176. std::string supportDir = m_SupportDir+"/MproInclude";
  177. if(!cmSystemTools::FileIsDirectory(supportDir.c_str()))
  178. {
  179. std::string err = "No MIPSpro support library for GCC-XML. Couldn't find directory \""+supportDir+"\".";
  180. this->SetError(err.c_str());
  181. return false;
  182. }
  183. // Try to run the find_mpro_options command.
  184. std::string command = supportDir+"/find_mpro_options";
  185. std::string flags;
  186. if(!cmSystemTools::RunCommand(command.c_str(), flags))
  187. {
  188. this->SetError("Could not run find_mpro_options!");
  189. return false;
  190. }
  191. // Use the result of the command as the flags. Also prefix on the
  192. // include path flag for the support directory.
  193. m_Flags = "-I"+supportDir+" "+flags;
  194. return true;
  195. }
  196. /**
  197. * Determine whether the compiler is GCC.
  198. */
  199. bool cmConfigureGccXmlCommand::CompilerIsGCC() const
  200. {
  201. const char* isGNU = m_Makefile->GetDefinition("CMAKE_COMPILER_IS_GNUCXX");
  202. return (isGNU && !cmSystemTools::IsOff(isGNU));
  203. }
  204. /**
  205. * Determine whether the compiler is MipsPro.
  206. */
  207. bool cmConfigureGccXmlCommand::CompilerIsMipsPro() const
  208. {
  209. const char* compiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  210. if(!compiler) { return false; }
  211. std::string command = compiler;
  212. command += " -version 2>&1";
  213. std::string output;
  214. if(!cmSystemTools::RunCommand(command.c_str(), output, false))
  215. { return false; }
  216. if(output.find("MIPSpro") != std::string::npos)
  217. {
  218. return true;
  219. }
  220. return false;
  221. }