cmCTestConfigureCommand.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestConfigureCommand.h"
  4. #include <chrono>
  5. #include <cstdlib>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9. #include <cm/memory>
  10. #include <cmext/string_view>
  11. #include "cmArgumentParser.h"
  12. #include "cmCTest.h"
  13. #include "cmDuration.h"
  14. #include "cmExecutionStatus.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmInstrumentation.h"
  18. #include "cmInstrumentationQuery.h"
  19. #include "cmList.h"
  20. #include "cmMakefile.h"
  21. #include "cmStringAlgorithms.h"
  22. #include "cmSystemTools.h"
  23. #include "cmValue.h"
  24. #include "cmXMLWriter.h"
  25. #include "cmake.h"
  26. bool cmCTestConfigureCommand::ExecuteConfigure(ConfigureArguments const& args,
  27. cmExecutionStatus& status) const
  28. {
  29. cmMakefile& mf = status.GetMakefile();
  30. std::string const buildDirectory = !args.Build.empty()
  31. ? args.Build
  32. : mf.GetDefinition("CTEST_BINARY_DIRECTORY");
  33. if (buildDirectory.empty()) {
  34. status.SetError("called with no build directory specified. "
  35. "Either use the BUILD argument or set the "
  36. "CTEST_BINARY_DIRECTORY variable.");
  37. return false;
  38. }
  39. std::string configureCommand = mf.GetDefinition("CTEST_CONFIGURE_COMMAND");
  40. if (configureCommand.empty()) {
  41. cmValue cmakeGenerator = mf.GetDefinition("CTEST_CMAKE_GENERATOR");
  42. if (!cmNonempty(cmakeGenerator)) {
  43. status.SetError(
  44. "called with no configure command specified. "
  45. "If this is a \"built with CMake\" project, set "
  46. "CTEST_CMAKE_GENERATOR. If not, set CTEST_CONFIGURE_COMMAND.");
  47. return false;
  48. }
  49. std::string const sourceDirectory = !args.Source.empty()
  50. ? args.Source
  51. : mf.GetDefinition("CTEST_SOURCE_DIRECTORY");
  52. if (sourceDirectory.empty() ||
  53. !cmSystemTools::FileExists(sourceDirectory + "/CMakeLists.txt")) {
  54. status.SetError("called with invalid source directory. "
  55. "CTEST_SOURCE_DIRECTORY must be set to a directory "
  56. "that contains CMakeLists.txt.");
  57. return false;
  58. }
  59. bool const multiConfig = [&]() -> bool {
  60. cmake* cm = mf.GetCMakeInstance();
  61. auto gg = cm->CreateGlobalGenerator(cmakeGenerator);
  62. return gg && gg->IsMultiConfig();
  63. }();
  64. bool const buildTypeInOptions =
  65. args.Options.find("CMAKE_BUILD_TYPE=") != std::string::npos ||
  66. args.Options.find("CMAKE_BUILD_TYPE:STRING=") != std::string::npos;
  67. configureCommand = cmStrCat('"', cmSystemTools::GetCMakeCommand(), '"');
  68. auto const options = cmList(args.Options);
  69. for (std::string const& option : options) {
  70. configureCommand += " \"";
  71. configureCommand += option;
  72. configureCommand += "\"";
  73. }
  74. cmValue cmakeBuildType = mf.GetDefinition("CTEST_CONFIGURATION_TYPE");
  75. if (!multiConfig && !buildTypeInOptions && cmNonempty(cmakeBuildType)) {
  76. configureCommand += " \"-DCMAKE_BUILD_TYPE:STRING=";
  77. configureCommand += cmakeBuildType;
  78. configureCommand += "\"";
  79. }
  80. if (mf.IsOn("CTEST_USE_LAUNCHERS")) {
  81. configureCommand += " \"-DCTEST_USE_LAUNCHERS:BOOL=TRUE\"";
  82. }
  83. configureCommand += " \"-G";
  84. configureCommand += cmakeGenerator;
  85. configureCommand += "\"";
  86. cmValue cmakeGeneratorPlatform =
  87. mf.GetDefinition("CTEST_CMAKE_GENERATOR_PLATFORM");
  88. if (cmNonempty(cmakeGeneratorPlatform)) {
  89. configureCommand += " \"-A";
  90. configureCommand += *cmakeGeneratorPlatform;
  91. configureCommand += "\"";
  92. }
  93. cmValue cmakeGeneratorToolset =
  94. mf.GetDefinition("CTEST_CMAKE_GENERATOR_TOOLSET");
  95. if (cmNonempty(cmakeGeneratorToolset)) {
  96. configureCommand += " \"-T";
  97. configureCommand += *cmakeGeneratorToolset;
  98. configureCommand += "\"";
  99. }
  100. configureCommand += " \"-S";
  101. configureCommand += sourceDirectory;
  102. configureCommand += "\"";
  103. configureCommand += " \"-B";
  104. configureCommand += buildDirectory;
  105. configureCommand += "\"";
  106. }
  107. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "Configure project\n",
  108. args.Quiet);
  109. if (this->CTest->GetShowOnly()) {
  110. cmCTestOptionalLog(this->CTest, DEBUG,
  111. "Configure with command: " << configureCommand << '\n',
  112. args.Quiet);
  113. if (!args.ReturnValue.empty()) {
  114. mf.AddDefinition(args.ReturnValue, "0");
  115. }
  116. return true;
  117. }
  118. if (!cmSystemTools::MakeDirectory(buildDirectory)) {
  119. status.SetError(cmStrCat("cannot create directory ", buildDirectory));
  120. return false;
  121. }
  122. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  123. "Configure with command: " << configureCommand << '\n',
  124. args.Quiet);
  125. int const submitIndex =
  126. args.SubmitIndex.empty() ? 0 : std::atoi(args.SubmitIndex.c_str());
  127. cmGeneratedFileStream logFile;
  128. this->CTest->StartLogFile("Configure", submitIndex, logFile);
  129. auto const startTime = std::chrono::system_clock::now();
  130. auto const startDateTime = this->CTest->CurrentTime();
  131. std::string output;
  132. int retVal = 0;
  133. bool const res = this->CTest->RunMakeCommand(configureCommand, output,
  134. &retVal, buildDirectory.c_str(),
  135. cmDuration::zero(), logFile);
  136. auto const endTime = std::chrono::system_clock::now();
  137. auto const endDateTime = this->CTest->CurrentTime();
  138. auto const elapsedMinutes =
  139. std::chrono::duration_cast<std::chrono::minutes>(endTime - startTime);
  140. cmCTestOptionalLog(this->CTest, DEBUG, "End\n", args.Quiet);
  141. if (!res || retVal) {
  142. cmCTestLog(this->CTest, ERROR_MESSAGE,
  143. "Error(s) when configuring the project\n");
  144. }
  145. if (!args.ReturnValue.empty()) {
  146. mf.AddDefinition(args.ReturnValue, std::to_string(retVal));
  147. }
  148. if (cmValue value = mf.GetDefinition("CTEST_CHANGE_ID")) {
  149. this->CTest->SetCTestConfiguration("ChangeId", *value, args.Quiet);
  150. }
  151. if (cmValue value = mf.GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
  152. this->CTest->SetCTestConfiguration("LabelsForSubprojects", *value,
  153. args.Quiet);
  154. }
  155. cmGeneratedFileStream xmlFile;
  156. if (!this->CTest->StartResultingXML(cmCTest::PartConfigure, "Configure",
  157. submitIndex, xmlFile)) {
  158. cmCTestLog(this->CTest, ERROR_MESSAGE,
  159. "Cannot open configure file" << std::endl);
  160. return false;
  161. }
  162. cmXMLWriter xml(xmlFile);
  163. this->CTest->StartXML(xml, mf.GetCMakeInstance(), args.Append);
  164. this->CTest->GenerateSubprojectsOutput(xml);
  165. xml.StartElement("Configure");
  166. xml.Element("StartDateTime", startDateTime);
  167. xml.Element("StartConfigureTime", startTime);
  168. xml.Element("ConfigureCommand", configureCommand);
  169. xml.Element("Log", output);
  170. xml.Element("ConfigureStatus", retVal);
  171. xml.Element("EndDateTime", endDateTime);
  172. xml.Element("EndConfigureTime", endTime);
  173. xml.Element("ElapsedMinutes", elapsedMinutes.count());
  174. this->CTest->GetInstrumentation().CollectTimingData(
  175. cmInstrumentationQuery::Hook::PrepareForCDash);
  176. this->CTest->ConvertInstrumentationSnippetsToXML(xml, "configure");
  177. xml.EndElement(); // Configure
  178. this->CTest->EndXML(xml);
  179. return res;
  180. }
  181. bool cmCTestConfigureCommand::InitialPass(std::vector<std::string> const& args,
  182. cmExecutionStatus& status) const
  183. {
  184. using Args = ConfigureArguments;
  185. static auto const parser =
  186. cmArgumentParser<Args>{ MakeHandlerParser<Args>() } //
  187. .Bind("OPTIONS"_s, &ConfigureArguments::Options);
  188. return this->Invoke(parser, args, status, [&](ConfigureArguments& a) {
  189. return this->ExecuteConfigure(a, status);
  190. });
  191. }