cmCTestBuildAndTestHandler.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 "cmCTest.h"
  5. #include "cmCTestTestHandler.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmState.h"
  9. #include "cmSystemTools.h"
  10. #include "cmWorkingDirectory.h"
  11. #include "cmake.h"
  12. #include "cmsys/Process.h"
  13. #include <chrono>
  14. #include <cstring>
  15. #include <ratio>
  16. #include <stdlib.h>
  17. cmCTestBuildAndTestHandler::cmCTestBuildAndTestHandler()
  18. {
  19. this->BuildTwoConfig = false;
  20. this->BuildNoClean = false;
  21. this->BuildNoCMake = false;
  22. this->Timeout = cmDuration::zero();
  23. }
  24. void cmCTestBuildAndTestHandler::Initialize()
  25. {
  26. this->BuildTargets.clear();
  27. this->Superclass::Initialize();
  28. }
  29. const char* cmCTestBuildAndTestHandler::GetOutput()
  30. {
  31. return this->Output.c_str();
  32. }
  33. int cmCTestBuildAndTestHandler::ProcessHandler()
  34. {
  35. this->Output.clear();
  36. std::string output;
  37. cmSystemTools::ResetErrorOccuredFlag();
  38. int retv = this->RunCMakeAndTest(&this->Output);
  39. cmSystemTools::ResetErrorOccuredFlag();
  40. return retv;
  41. }
  42. int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
  43. std::ostringstream& out,
  44. std::string& cmakeOutString,
  45. cmake* cm)
  46. {
  47. std::vector<std::string> args;
  48. args.push_back(cmSystemTools::GetCMakeCommand());
  49. args.push_back(this->SourceDir);
  50. if (!this->BuildGenerator.empty()) {
  51. args.push_back("-G" + this->BuildGenerator);
  52. }
  53. if (!this->BuildGeneratorPlatform.empty()) {
  54. args.push_back("-A" + this->BuildGeneratorPlatform);
  55. }
  56. if (!this->BuildGeneratorToolset.empty()) {
  57. args.push_back("-T" + this->BuildGeneratorToolset);
  58. }
  59. const char* config = nullptr;
  60. if (!this->CTest->GetConfigType().empty()) {
  61. config = this->CTest->GetConfigType().c_str();
  62. }
  63. #ifdef CMAKE_INTDIR
  64. if (!config) {
  65. config = CMAKE_INTDIR;
  66. }
  67. #endif
  68. if (config) {
  69. args.push_back("-DCMAKE_BUILD_TYPE:STRING=" + std::string(config));
  70. }
  71. if (!this->BuildMakeProgram.empty() &&
  72. (this->BuildGenerator.find("Make") != std::string::npos ||
  73. this->BuildGenerator.find("Ninja") != std::string::npos)) {
  74. args.push_back("-DCMAKE_MAKE_PROGRAM:FILEPATH=" + this->BuildMakeProgram);
  75. }
  76. for (std::string const& opt : this->BuildOptions) {
  77. args.push_back(opt);
  78. }
  79. if (cm->Run(args) != 0) {
  80. out << "Error: cmake execution failed\n";
  81. out << cmakeOutString << "\n";
  82. if (outstring) {
  83. *outstring = out.str();
  84. } else {
  85. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  86. }
  87. return 1;
  88. }
  89. // do another config?
  90. if (this->BuildTwoConfig) {
  91. if (cm->Run(args) != 0) {
  92. out << "Error: cmake execution failed\n";
  93. out << cmakeOutString << "\n";
  94. if (outstring) {
  95. *outstring = out.str();
  96. } else {
  97. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  98. }
  99. return 1;
  100. }
  101. }
  102. out << "======== CMake output ======\n";
  103. out << cmakeOutString;
  104. out << "======== End CMake output ======\n";
  105. return 0;
  106. }
  107. class cmCTestBuildAndTestCaptureRAII
  108. {
  109. cmake& CM;
  110. public:
  111. cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s)
  112. : CM(cm)
  113. {
  114. cmSystemTools::SetMessageCallback(
  115. [&s](const std::string& msg, const char* /*unused*/) {
  116. s += msg;
  117. s += "\n";
  118. });
  119. cmSystemTools::SetStdoutCallback([&s](std::string const& m) { s += m; });
  120. cmSystemTools::SetStderrCallback([&s](std::string const& m) { s += m; });
  121. this->CM.SetProgressCallback([&s](const std::string& msg, float prog) {
  122. if (prog < 0) {
  123. s += msg;
  124. s += "\n";
  125. }
  126. });
  127. }
  128. ~cmCTestBuildAndTestCaptureRAII()
  129. {
  130. this->CM.SetProgressCallback(nullptr);
  131. cmSystemTools::SetStderrCallback(nullptr);
  132. cmSystemTools::SetStdoutCallback(nullptr);
  133. cmSystemTools::SetMessageCallback(nullptr);
  134. }
  135. cmCTestBuildAndTestCaptureRAII(const cmCTestBuildAndTestCaptureRAII&) =
  136. delete;
  137. cmCTestBuildAndTestCaptureRAII& operator=(
  138. const cmCTestBuildAndTestCaptureRAII&) = delete;
  139. };
  140. int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
  141. {
  142. // if the generator and make program are not specified then it is an error
  143. if (this->BuildGenerator.empty()) {
  144. if (outstring) {
  145. *outstring = "--build-and-test requires that the generator "
  146. "be provided using the --build-generator "
  147. "command line option. ";
  148. }
  149. return 1;
  150. }
  151. cmake cm(cmake::RoleProject, cmState::Project);
  152. cm.SetHomeDirectory("");
  153. cm.SetHomeOutputDirectory("");
  154. std::string cmakeOutString;
  155. cmCTestBuildAndTestCaptureRAII captureRAII(cm, cmakeOutString);
  156. static_cast<void>(captureRAII);
  157. std::ostringstream out;
  158. if (this->CTest->GetConfigType().empty() && !this->ConfigSample.empty()) {
  159. // use the config sample to set the ConfigType
  160. std::string fullPath;
  161. std::string resultingConfig;
  162. std::vector<std::string> extraPaths;
  163. std::vector<std::string> failed;
  164. fullPath = cmCTestTestHandler::FindExecutable(
  165. this->CTest, this->ConfigSample.c_str(), resultingConfig, extraPaths,
  166. failed);
  167. if (!fullPath.empty() && !resultingConfig.empty()) {
  168. this->CTest->SetConfigType(resultingConfig.c_str());
  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. cmGlobalGenerator* gen = cm.CreateGlobalGenerator(this->BuildGenerator);
  196. cm.SetGlobalGenerator(gen);
  197. if (!this->BuildGeneratorPlatform.empty()) {
  198. cmMakefile mf(gen, cm.GetCurrentSnapshot());
  199. if (!gen->SetGeneratorPlatform(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 = this->ExecutableDirectory;
  268. tempPath += "/";
  269. tempPath += this->TestCommand;
  270. extraPaths.push_back(tempPath);
  271. }
  272. std::vector<std::string> failed;
  273. fullPath =
  274. cmCTestTestHandler::FindExecutable(this->CTest, this->TestCommand.c_str(),
  275. resultingConfig, extraPaths, failed);
  276. if (!cmSystemTools::FileExists(fullPath)) {
  277. out << "Could not find path to executable, perhaps it was not built: "
  278. << this->TestCommand << "\n";
  279. out << "tried to find it in these places:\n";
  280. out << fullPath << "\n";
  281. for (std::string const& fail : failed) {
  282. out << fail << "\n";
  283. }
  284. if (outstring) {
  285. *outstring = out.str();
  286. } else {
  287. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
  288. }
  289. return 1;
  290. }
  291. std::vector<const char*> testCommand;
  292. testCommand.push_back(fullPath.c_str());
  293. for (std::string const& testCommandArg : this->TestCommandArgs) {
  294. testCommand.push_back(testCommandArg.c_str());
  295. }
  296. testCommand.push_back(nullptr);
  297. std::string outs;
  298. int retval = 0;
  299. // run the test from the this->BuildRunDir if set
  300. if (!this->BuildRunDir.empty()) {
  301. out << "Run test in directory: " << this->BuildRunDir << "\n";
  302. if (!workdir.SetDirectory(this->BuildRunDir)) {
  303. out << "Failed to change working directory : "
  304. << std::strerror(workdir.GetLastResult()) << "\n";
  305. if (outstring) {
  306. *outstring = out.str();
  307. } else {
  308. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
  309. }
  310. return 1;
  311. }
  312. }
  313. out << "Running test command: \"" << fullPath << "\"";
  314. for (std::string const& testCommandArg : this->TestCommandArgs) {
  315. out << " \"" << testCommandArg << "\"";
  316. }
  317. out << "\n";
  318. // how much time is remaining
  319. cmDuration remainingTime = std::chrono::seconds(0);
  320. if (this->Timeout > cmDuration::zero()) {
  321. remainingTime =
  322. this->Timeout - (std::chrono::steady_clock::now() - clock_start);
  323. if (remainingTime <= std::chrono::seconds(0)) {
  324. if (outstring) {
  325. *outstring = "--build-and-test timeout exceeded. ";
  326. }
  327. return 1;
  328. }
  329. }
  330. int runTestRes = this->CTest->RunTest(testCommand, &outs, &retval, nullptr,
  331. remainingTime, nullptr);
  332. if (runTestRes != cmsysProcess_State_Exited || retval != 0) {
  333. out << "Test command failed: " << testCommand[0] << "\n";
  334. retval = 1;
  335. }
  336. out << outs << "\n";
  337. if (outstring) {
  338. *outstring = out.str();
  339. } else {
  340. cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
  341. }
  342. return retval;
  343. }
  344. int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
  345. const std::string& currentArg, size_t& idx,
  346. const std::vector<std::string>& allArgs)
  347. {
  348. // --build-and-test options
  349. if (currentArg.find("--build-and-test", 0) == 0 &&
  350. idx < allArgs.size() - 1) {
  351. if (idx + 2 < allArgs.size()) {
  352. idx++;
  353. this->SourceDir = allArgs[idx];
  354. idx++;
  355. this->BinaryDir = allArgs[idx];
  356. // dir must exist before CollapseFullPath is called
  357. cmSystemTools::MakeDirectory(this->BinaryDir);
  358. this->BinaryDir = cmSystemTools::CollapseFullPath(this->BinaryDir);
  359. this->SourceDir = cmSystemTools::CollapseFullPath(this->SourceDir);
  360. } else {
  361. cmCTestLog(this->CTest, ERROR_MESSAGE,
  362. "--build-and-test must have source and binary dir"
  363. << std::endl);
  364. return 0;
  365. }
  366. }
  367. if (currentArg.find("--build-target", 0) == 0 && idx < allArgs.size() - 1) {
  368. idx++;
  369. this->BuildTargets.push_back(allArgs[idx]);
  370. }
  371. if (currentArg.find("--build-nocmake", 0) == 0) {
  372. this->BuildNoCMake = true;
  373. }
  374. if (currentArg.find("--build-run-dir", 0) == 0 && idx < allArgs.size() - 1) {
  375. idx++;
  376. this->BuildRunDir = allArgs[idx];
  377. }
  378. if (currentArg.find("--build-two-config", 0) == 0) {
  379. this->BuildTwoConfig = true;
  380. }
  381. if (currentArg.find("--build-exe-dir", 0) == 0 && idx < allArgs.size() - 1) {
  382. idx++;
  383. this->ExecutableDirectory = allArgs[idx];
  384. }
  385. if (currentArg.find("--test-timeout", 0) == 0 && idx < allArgs.size() - 1) {
  386. idx++;
  387. this->Timeout = cmDuration(atof(allArgs[idx].c_str()));
  388. }
  389. if (currentArg == "--build-generator" && idx < allArgs.size() - 1) {
  390. idx++;
  391. this->BuildGenerator = allArgs[idx];
  392. }
  393. if (currentArg == "--build-generator-platform" && idx < allArgs.size() - 1) {
  394. idx++;
  395. this->BuildGeneratorPlatform = allArgs[idx];
  396. }
  397. if (currentArg == "--build-generator-toolset" && idx < allArgs.size() - 1) {
  398. idx++;
  399. this->BuildGeneratorToolset = allArgs[idx];
  400. }
  401. if (currentArg.find("--build-project", 0) == 0 && idx < allArgs.size() - 1) {
  402. idx++;
  403. this->BuildProject = allArgs[idx];
  404. }
  405. if (currentArg.find("--build-makeprogram", 0) == 0 &&
  406. idx < allArgs.size() - 1) {
  407. idx++;
  408. this->BuildMakeProgram = allArgs[idx];
  409. }
  410. if (currentArg.find("--build-config-sample", 0) == 0 &&
  411. idx < allArgs.size() - 1) {
  412. idx++;
  413. this->ConfigSample = allArgs[idx];
  414. }
  415. if (currentArg.find("--build-noclean", 0) == 0) {
  416. this->BuildNoClean = true;
  417. }
  418. if (currentArg.find("--build-options", 0) == 0) {
  419. while (idx + 1 < allArgs.size() && allArgs[idx + 1] != "--build-target" &&
  420. allArgs[idx + 1] != "--test-command") {
  421. ++idx;
  422. this->BuildOptions.push_back(allArgs[idx]);
  423. }
  424. }
  425. if (currentArg.find("--test-command", 0) == 0 && idx < allArgs.size() - 1) {
  426. ++idx;
  427. this->TestCommand = allArgs[idx];
  428. while (idx + 1 < allArgs.size()) {
  429. ++idx;
  430. this->TestCommandArgs.push_back(allArgs[idx]);
  431. }
  432. }
  433. return 1;
  434. }