cmCablePackageCommand.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "cmCablePackageCommand.h"
  33. #include "cmCacheManager.h"
  34. #include "cmTarget.h"
  35. cmCablePackageCommand::~cmCablePackageCommand()
  36. {
  37. // If we are the owner of the cmCableData, we must delete it here.
  38. // For most cmCableCommands, the cmCableCommand destructor will take
  39. // care of this. If this package happens to be the last one, and is
  40. // the owner, then the destructor of cmCableData will call back to a method
  41. // in this class after the package part of it has been freed!
  42. if(m_CableData && m_CableData->OwnerIs(this))
  43. {
  44. delete m_CableData;
  45. // Make sure our superclass's destructor doesn't try to delete the
  46. // cmCableData too.
  47. m_CableData = NULL;
  48. }
  49. }
  50. // cmCablePackageCommand
  51. bool cmCablePackageCommand::Invoke(std::vector<std::string>& args)
  52. {
  53. if(args.size() != 2)
  54. {
  55. this->SetError("called with incorrect number of arguments");
  56. return false;
  57. }
  58. // setup this once. Really this should probably be moved somewhere else
  59. // at some point.
  60. {
  61. // We must add a custom rule to cause the cable_config.xml to be re-built
  62. // when it is removed. Rebuilding it means re-running CMake.
  63. std::string cMakeLists = m_Makefile->GetStartDirectory();
  64. cMakeLists += "/";
  65. cMakeLists += "CMakeLists.txt";
  66. cMakeLists = cmSystemTools::EscapeSpaces(cMakeLists.c_str());
  67. std::string command = "${CMAKE} "+cMakeLists;
  68. #if defined(_WIN32) && !defined(__CYGWIN__)
  69. command += " -DSP";
  70. #endif
  71. command += " -H\"";
  72. command += m_Makefile->GetHomeDirectory();
  73. command += "\" -S\"";
  74. command += m_Makefile->GetStartDirectory();
  75. command += "\" -O\"";
  76. command += m_Makefile->GetStartOutputDirectory();
  77. command += "\" -B\"";
  78. command += m_Makefile->GetHomeOutputDirectory();
  79. command += "\"";
  80. m_Makefile->ExpandVariablesInString(command);
  81. std::vector<std::string> depends;
  82. m_Makefile->AddCustomCommand(cMakeLists.c_str(),
  83. command.c_str(),
  84. depends,
  85. "cable_config.xml", args[1].c_str());
  86. }
  87. // This command needs to access the Cable data.
  88. this->SetupCableData();
  89. // The argument is the package name.
  90. m_PackageName = args[0];
  91. m_TargetName = args[1];
  92. // Ask the cable data to begin the package. This may call another
  93. // cmCablePackageCommand's WritePackageFooter(). This will call
  94. // this cmCablePackageCommand's WritePackageHeader().
  95. m_CableData->BeginPackage(this);
  96. // Tell the makefile that it needs the "cable" utility.
  97. m_Makefile->AddUtility("cable");
  98. // Add custom rules to the makefile to generate this package's source
  99. // files.
  100. {
  101. std::string command = "${CABLE}";
  102. m_Makefile->ExpandVariablesInString(command);
  103. std::vector<std::string> depends;
  104. depends.push_back(command);
  105. command = "\""+command+"\" cable_config.xml";
  106. std::vector<std::string> outputs;
  107. outputs.push_back("Cxx/"+m_PackageName+"_cxx.cxx");
  108. outputs.push_back("Cxx/"+m_PackageName+"_cxx.h");
  109. // A rule for the package's source files.
  110. m_Makefile->AddCustomCommand("cable_config.xml",
  111. command.c_str(),
  112. depends,
  113. outputs, m_TargetName.c_str());
  114. }
  115. // Add custom rules to the makefile to generate this package's xml files.
  116. {
  117. std::string command = "${GCCXML}";
  118. m_Makefile->ExpandVariablesInString(command);
  119. // Only add the rule if GCC-XML is available.
  120. if((command != "") && (command != "${GCCXML}"))
  121. {
  122. std::vector<std::string> depends;
  123. depends.push_back(command);
  124. std::string input = m_Makefile->GetStartOutputDirectory();
  125. input = input + "/Cxx/"+m_PackageName+"_cxx.cxx";
  126. std::string output = "Cxx/"+m_PackageName+"_cxx.xml";
  127. command = "\""+command+"\" ${CXX_FLAGS} -fsyntax-only -fxml=" + output + " -c " + input;
  128. std::vector<std::string> outputs;
  129. outputs.push_back("Cxx/"+m_PackageName+"_cxx.xml");
  130. // A rule for the package's source files.
  131. m_Makefile->AddCustomCommand(input.c_str(),
  132. command.c_str(),
  133. depends,
  134. outputs, m_TargetName.c_str());
  135. }
  136. }
  137. // add the source list to the target
  138. m_Makefile->GetTargets()[m_TargetName.c_str()].GetSourceLists().push_back(m_PackageName);
  139. return true;
  140. }
  141. void cmCablePackageCommand::FinalPass()
  142. {
  143. // Add a rule to build the generated package.
  144. std::string fileName = "Cxx/"+m_PackageName+"_cxx";
  145. std::string filePath = m_Makefile->GetStartOutputDirectory();
  146. cmSourceFile file;
  147. file.SetIsAnAbstractClass(false);
  148. file.SetIsAHeaderFileOnly(false);
  149. file.SetName(fileName.c_str(), filePath.c_str(), "cxx", false);
  150. m_Makefile->AddSource(file, m_PackageName.c_str());
  151. }
  152. /**
  153. * Write a CABLE package header.
  154. */
  155. void cmCablePackageCommand::WritePackageHeader() const
  156. {
  157. std::ostream& os = m_CableData->GetOutputStream();
  158. cmCableData::Indentation indent = m_CableData->GetIndentation();
  159. os << indent << "<Package name=\"" << m_PackageName.c_str() << "\">"
  160. << std::endl;
  161. m_CableData->Indent();
  162. }
  163. /**
  164. * Write a CABLE package footer.
  165. */
  166. void cmCablePackageCommand::WritePackageFooter() const
  167. {
  168. m_CableData->Unindent();
  169. std::ostream& os = m_CableData->GetOutputStream();
  170. cmCableData::Indentation indent = m_CableData->GetIndentation();
  171. os << indent << "</Package> <!-- \"" << m_PackageName.c_str() << "\" -->"
  172. << std::endl;
  173. }