cmCTestBuildAndTestHandler.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 "cmCTestBuildAndTestHandler.h"
  4. #include <chrono>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <ratio>
  8. #include "cmsys/Process.h"
  9. #include "cmCTest.h"
  10. #include "cmCTestTestHandler.h"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmState.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #include "cmWorkingDirectory.h"
  17. #include "cmake.h"
  18. cmCTestBuildAndTestHandler::cmCTestBuildAndTestHandler()
  19. {
  20. this->BuildTwoConfig = false;
  21. this->BuildNoClean = false;
  22. this->BuildNoCMake = false;
  23. this->Timeout = cmDuration::zero();
  24. }
  25. void cmCTestBuildAndTestHandler::Initialize()
  26. {
  27. this->BuildTargets.clear();
  28. this->Superclass::Initialize();
  29. }
  30. const char* cmCTestBuildAndTestHandler::GetOutput()
  31. {
  32. return this->Output.c_str();
  33. }
  34. int cmCTestBuildAndTestHandler::ProcessHandler()
  35. {
  36. this->Output.clear();
  37. std::string output;
  38. cmSystemTools::ResetErrorOccuredFlag();
  39. int retv = this->RunCMakeAndTest(&this->Output);
  40. cmSystemTools::ResetErrorOccuredFlag();
  41. return retv;
  42. }
  43. int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
  44. std::ostringstream& out,
  45. std::string& cmakeOutString,
  46. cmake* cm)
  47. {
  48. std::vector<std::string> args;
  49. args.push_back(cmSystemTools::GetCMakeCommand());
  50. args.push_back(this->SourceDir);
  51. if (!this->BuildGenerator.empty()) {
  52. args.push_back("-G" + this->BuildGenerator);
  53. }
  54. if (!this->BuildGeneratorPlatform.empty()) {
  55. args.push_back("-A" + this->BuildGeneratorPlatform);
  56. }
  57. if (!this->BuildGeneratorToolset.empty()) {
  58. args.push_back("-T" + this->BuildGeneratorToolset);
  59. }
  60. const char* config = nullptr;
  61. if (!this->CTest->GetConfigType().empty()) {
  62. config = this->CTest->GetConfigType().c_str();
  63. }
  64. #ifdef CMAKE_INTDIR
  65. if (!config) {
  66. config = CMAKE_INTDIR;
  67. }
  68. #endif
  69. if (config) {
  70. args.push_back("-DCMAKE_BUILD_TYPE:STRING=" + std::string(config));
  71. }
  72. if (!this->BuildMakeProgram.empty() &&
  73. (this->BuildGenerator.find("Make") != std::string::npos ||
  74. this->BuildGenerator.find("Ninja") != std::string::npos)) {
  75. args.push_back("-DCMAKE_MAKE_PROGRAM:FILEPATH=" + this->BuildMakeProgram);
  76. }
  77. for (std::string const& opt : this->BuildOptions) {
  78. args.push_back(opt);
  79. }
  80. if (cm->Run(args) != 0) {
  81. out << "Error: cmake execution failed\n";
  82. out << cmakeOutString << "\n";
  83. if (outstring) {
  84. *outstring = out.str();
  85. } else {
  86. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  87. }
  88. return 1;
  89. }
  90. // do another config?
  91. if (this->BuildTwoConfig) {
  92. if (cm->Run(args) != 0) {
  93. out << "Error: cmake execution failed\n";
  94. out << cmakeOutString << "\n";
  95. if (outstring) {
  96. *outstring = out.str();
  97. } else {
  98. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  99. }
  100. return 1;
  101. }
  102. }
  103. out << "======== CMake output ======\n";
  104. out << cmakeOutString;
  105. out << "======== End CMake output ======\n";
  106. return 0;
  107. }
  108. class cmCTestBuildAndTestCaptureRAII
  109. {
  110. cmake& CM;
  111. public:
  112. cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s)
  113. : CM(cm)
  114. {
  115. cmSystemTools::SetMessageCallback(
  116. [&s](const std::string& msg, const char* /*unused*/) {
  117. s += msg;
  118. s += "\n";
  119. });
  120. cmSystemTools::SetStdoutCallback([&s](std::string const& m) { s += m; });
  121. cmSystemTools::SetStderrCallback([&s](std::string const& m) { s += m; });
  122. this->CM.SetProgressCallback([&s](const std::string& msg, float prog) {
  123. if (prog < 0) {
  124. s += msg;
  125. s += "\n";
  126. }
  127. });
  128. }
  129. ~cmCTestBuildAndTestCaptureRAII()
  130. {
  131. this->CM.SetProgressCallback(nullptr);
  132. cmSystemTools::SetStderrCallback(nullptr);
  133. cmSystemTools::SetStdoutCallback(nullptr);
  134. cmSystemTools::SetMessageCallback(nullptr);
  135. }
  136. cmCTestBuildAndTestCaptureRAII(const cmCTestBuildAndTestCaptureRAII&) =
  137. delete;
  138. cmCTestBuildAndTestCaptureRAII& operator=(
  139. const cmCTestBuildAndTestCaptureRAII&) = delete;
  140. };
  141. int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
  142. {
  143. // if the generator and make program are not specified then it is an error
  144. if (this->BuildGenerator.empty()) {
  145. if (outstring) {
  146. *outstring = "--build-and-test requires that the generator "
  147. "be provided using the --build-generator "
  148. "command line option. ";
  149. }
  150. return 1;
  151. }
  152. cmake cm(cmake::RoleProject, cmState::Project);
  153. cm.SetHomeDirectory("");
  154. cm.SetHomeOutputDirectory("");
  155. std::string cmakeOutString;
  156. cmCTestBuildAndTestCaptureRAII captureRAII(cm, cmakeOutString);
  157. static_cast<void>(captureRAII);
  158. std::ostringstream out;
  159. if (this->CTest->GetConfigType().empty() && !this->ConfigSample.empty()) {
  160. // use the config sample to set the ConfigType
  161. std::string fullPath;
  162. std::string resultingConfig;
  163. std::vector<std::string> extraPaths;
  164. std::vector<std::string> failed;
  165. fullPath = cmCTestTestHandler::FindExecutable(
  166. this->CTest, this->ConfigSample, resultingConfig, extraPaths, failed);
  167. if (!fullPath.empty() && !resultingConfig.empty()) {
  168. this->CTest->SetConfigType(resultingConfig);
  169. }
  170. out << "Using config sample with results: " << fullPath << " and "
  171. << resultingConfig << std::endl;
  172. }
  173. // we need to honor the timeout specified, the timeout include cmake, build
  174. // and test time
  175. auto clock_start = std::chrono::steady_clock::now();
  176. // make sure the binary dir is there
  177. out << "Internal cmake changing into directory: " << this->BinaryDir
  178. << std::endl;
  179. if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) {
  180. cmSystemTools::MakeDirectory(this->BinaryDir);
  181. }
  182. cmWorkingDirectory workdir(this->BinaryDir);
  183. if (workdir.Failed()) {
  184. auto msg = "Failed to change working directory to " + this->BinaryDir +
  185. " : " + std::strerror(workdir.GetLastResult()) + "\n";
  186. if (outstring) {
  187. *outstring = msg;
  188. } else {
  189. cmCTestLog(this->CTest, ERROR_MESSAGE, msg);
  190. }
  191. return 1;
  192. }
  193. if (this->BuildNoCMake) {
  194. // Make the generator available for the Build call below.
  195. cm.SetGlobalGenerator(cm.CreateGlobalGenerator(this->BuildGenerator));
  196. if (!this->BuildGeneratorPlatform.empty()) {
  197. cmMakefile mf(cm.GetGlobalGenerator(), cm.GetCurrentSnapshot());
  198. if (!cm.GetGlobalGenerator()->SetGeneratorPlatform(
  199. this->BuildGeneratorPlatform, &mf)) {
  200. return 1;
  201. }
  202. }
  203. // Load the cache to make CMAKE_MAKE_PROGRAM available.
  204. cm.LoadCache(this->BinaryDir);
  205. } else {
  206. // do the cmake step, no timeout here since it is not a sub process
  207. if (this->RunCMake(outstring, out, cmakeOutString, &cm)) {
  208. return 1;
  209. }
  210. }
  211. // do the build
  212. if (this->BuildTargets.empty()) {
  213. this->BuildTargets.emplace_back();
  214. }
  215. for (std::string const& tar : this->BuildTargets) {
  216. cmDuration remainingTime = std::chrono::seconds(0);
  217. if (this->Timeout > cmDuration::zero()) {
  218. remainingTime =
  219. this->Timeout - (std::chrono::steady_clock::now() - clock_start);
  220. if (remainingTime <= std::chrono::seconds(0)) {
  221. if (outstring) {
  222. *outstring = "--build-and-test timeout exceeded. ";
  223. }
  224. return 1;
  225. }
  226. }
  227. std::string output;
  228. const char* config = nullptr;
  229. if (!this->CTest->GetConfigType().empty()) {
  230. config = this->CTest->GetConfigType().c_str();
  231. }
  232. #ifdef CMAKE_INTDIR
  233. if (!config) {
  234. config = CMAKE_INTDIR;
  235. }
  236. #endif
  237. if (!config) {
  238. config = "Debug";
  239. }
  240. int retVal = cm.GetGlobalGenerator()->Build(
  241. cmake::NO_BUILD_PARALLEL_LEVEL, this->SourceDir, this->BinaryDir,
  242. this->BuildProject, { tar }, output, this->BuildMakeProgram, config,
  243. !this->BuildNoClean, false, false, remainingTime);
  244. out << output;
  245. // if the build failed then return
  246. if (retVal) {
  247. if (outstring) {
  248. *outstring = out.str();
  249. }
  250. return 1;
  251. }
  252. }
  253. if (outstring) {
  254. *outstring = out.str();
  255. }
  256. // if no test was specified then we are done
  257. if (this->TestCommand.empty()) {
  258. return 0;
  259. }
  260. // now run the compiled test if we can find it
  261. // store the final location in fullPath
  262. std::string fullPath;
  263. std::string resultingConfig;
  264. std::vector<std::string> extraPaths;
  265. // if this->ExecutableDirectory is set try that as well
  266. if (!this->ExecutableDirectory.empty()) {
  267. std::string tempPath =
  268. cmStrCat(this->ExecutableDirectory, '/', this->TestCommand);
  269. extraPaths.push_back(tempPath);
  270. }
  271. std::vector<std::string> failed;
  272. fullPath = cmCTestTestHandler::FindExecutable(
  273. this->CTest, this->TestCommand, resultingConfig, extraPaths, failed);
  274. if (!cmSystemTools::FileExists(fullPath)) {
  275. out << "Could not find path to executable, perhaps it was not built: "
  276. << this->TestCommand << "\n";
  277. out << "tried to find it in these places:\n";
  278. out << fullPath << "\n";
  279. for (std::string const& fail : failed) {
  280. out << fail << "\n";
  281. }
  282. if (outstring) {
  283. *outstring = out.str();
  284. } else {
  285. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
  286. }
  287. return 1;
  288. }
  289. std::vector<const char*> testCommand;
  290. testCommand.push_back(fullPath.c_str());
  291. for (std::string const& testCommandArg : this->TestCommandArgs) {
  292. testCommand.push_back(testCommandArg.c_str());
  293. }
  294. testCommand.push_back(nullptr);
  295. std::string outs;
  296. int retval = 0;
  297. // run the test from the this->BuildRunDir if set
  298. if (!this->BuildRunDir.empty()) {
  299. out << "Run test in directory: " << this->BuildRunDir << "\n";
  300. if (!workdir.SetDirectory(this->BuildRunDir)) {
  301. out << "Failed to change working directory : "
  302. << std::strerror(workdir.GetLastResult()) << "\n";
  303. if (outstring) {
  304. *outstring = out.str();
  305. } else {
  306. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
  307. }
  308. return 1;
  309. }
  310. }
  311. out << "Running test command: \"" << fullPath << "\"";
  312. for (std::string const& testCommandArg : this->TestCommandArgs) {
  313. out << " \"" << testCommandArg << "\"";
  314. }
  315. out << "\n";
  316. // how much time is remaining
  317. cmDuration remainingTime = std::chrono::seconds(0);
  318. if (this->Timeout > cmDuration::zero()) {
  319. remainingTime =
  320. this->Timeout - (std::chrono::steady_clock::now() - clock_start);
  321. if (remainingTime <= std::chrono::seconds(0)) {
  322. if (outstring) {
  323. *outstring = "--build-and-test timeout exceeded. ";
  324. }
  325. return 1;
  326. }
  327. }
  328. int runTestRes = this->CTest->RunTest(testCommand, &outs, &retval, nullptr,
  329. remainingTime, nullptr);
  330. if (runTestRes != cmsysProcess_State_Exited || retval != 0) {
  331. out << "Test command failed: " << testCommand[0] << "\n";
  332. retval = 1;
  333. }
  334. out << outs << "\n";
  335. if (outstring) {
  336. *outstring = out.str();
  337. } else {
  338. cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
  339. }
  340. return retval;
  341. }
  342. int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
  343. const std::string& currentArg, size_t& idx,
  344. const std::vector<std::string>& allArgs)
  345. {
  346. // --build-and-test options
  347. if (cmHasLiteralPrefix(currentArg, "--build-and-test") &&
  348. idx < allArgs.size() - 1) {
  349. if (idx + 2 < allArgs.size()) {
  350. idx++;
  351. this->SourceDir = allArgs[idx];
  352. idx++;
  353. this->BinaryDir = allArgs[idx];
  354. // dir must exist before CollapseFullPath is called
  355. cmSystemTools::MakeDirectory(this->BinaryDir);
  356. this->BinaryDir = cmSystemTools::CollapseFullPath(this->BinaryDir);
  357. this->SourceDir = cmSystemTools::CollapseFullPath(this->SourceDir);
  358. } else {
  359. cmCTestLog(this->CTest, ERROR_MESSAGE,
  360. "--build-and-test must have source and binary dir"
  361. << std::endl);
  362. return 0;
  363. }
  364. }
  365. if (cmHasLiteralPrefix(currentArg, "--build-target") &&
  366. idx < allArgs.size() - 1) {
  367. idx++;
  368. this->BuildTargets.push_back(allArgs[idx]);
  369. }
  370. if (cmHasLiteralPrefix(currentArg, "--build-nocmake")) {
  371. this->BuildNoCMake = true;
  372. }
  373. if (cmHasLiteralPrefix(currentArg, "--build-run-dir") &&
  374. idx < allArgs.size() - 1) {
  375. idx++;
  376. this->BuildRunDir = allArgs[idx];
  377. }
  378. if (cmHasLiteralPrefix(currentArg, "--build-two-config")) {
  379. this->BuildTwoConfig = true;
  380. }
  381. if (cmHasLiteralPrefix(currentArg, "--build-exe-dir") &&
  382. idx < allArgs.size() - 1) {
  383. idx++;
  384. this->ExecutableDirectory = allArgs[idx];
  385. }
  386. if (cmHasLiteralPrefix(currentArg, "--test-timeout") &&
  387. idx < allArgs.size() - 1) {
  388. idx++;
  389. this->Timeout = cmDuration(atof(allArgs[idx].c_str()));
  390. }
  391. if (currentArg == "--build-generator" && idx < allArgs.size() - 1) {
  392. idx++;
  393. this->BuildGenerator = allArgs[idx];
  394. }
  395. if (currentArg == "--build-generator-platform" && idx < allArgs.size() - 1) {
  396. idx++;
  397. this->BuildGeneratorPlatform = allArgs[idx];
  398. }
  399. if (currentArg == "--build-generator-toolset" && idx < allArgs.size() - 1) {
  400. idx++;
  401. this->BuildGeneratorToolset = allArgs[idx];
  402. }
  403. if (cmHasLiteralPrefix(currentArg, "--build-project") &&
  404. idx < allArgs.size() - 1) {
  405. idx++;
  406. this->BuildProject = allArgs[idx];
  407. }
  408. if (cmHasLiteralPrefix(currentArg, "--build-makeprogram") &&
  409. idx < allArgs.size() - 1) {
  410. idx++;
  411. this->BuildMakeProgram = allArgs[idx];
  412. }
  413. if (cmHasLiteralPrefix(currentArg, "--build-config-sample") &&
  414. idx < allArgs.size() - 1) {
  415. idx++;
  416. this->ConfigSample = allArgs[idx];
  417. }
  418. if (cmHasLiteralPrefix(currentArg, "--build-noclean")) {
  419. this->BuildNoClean = true;
  420. }
  421. if (cmHasLiteralPrefix(currentArg, "--build-options")) {
  422. while (idx + 1 < allArgs.size() && allArgs[idx + 1] != "--build-target" &&
  423. allArgs[idx + 1] != "--test-command") {
  424. ++idx;
  425. this->BuildOptions.push_back(allArgs[idx]);
  426. }
  427. }
  428. if (cmHasLiteralPrefix(currentArg, "--test-command") &&
  429. idx < allArgs.size() - 1) {
  430. ++idx;
  431. this->TestCommand = allArgs[idx];
  432. while (idx + 1 < allArgs.size()) {
  433. ++idx;
  434. this->TestCommandArgs.push_back(allArgs[idx]);
  435. }
  436. }
  437. return 1;
  438. }