cmCTestBuildAndTestHandler.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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.c_str(), resultingConfig, extraPaths,
  167. failed);
  168. if (!fullPath.empty() && !resultingConfig.empty()) {
  169. this->CTest->SetConfigType(resultingConfig.c_str());
  170. }
  171. out << "Using config sample with results: " << fullPath << " and "
  172. << resultingConfig << std::endl;
  173. }
  174. // we need to honor the timeout specified, the timeout include cmake, build
  175. // and test time
  176. auto clock_start = std::chrono::steady_clock::now();
  177. // make sure the binary dir is there
  178. out << "Internal cmake changing into directory: " << this->BinaryDir
  179. << std::endl;
  180. if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) {
  181. cmSystemTools::MakeDirectory(this->BinaryDir);
  182. }
  183. cmWorkingDirectory workdir(this->BinaryDir);
  184. if (workdir.Failed()) {
  185. auto msg = "Failed to change working directory to " + this->BinaryDir +
  186. " : " + std::strerror(workdir.GetLastResult()) + "\n";
  187. if (outstring) {
  188. *outstring = msg;
  189. } else {
  190. cmCTestLog(this->CTest, ERROR_MESSAGE, msg);
  191. }
  192. return 1;
  193. }
  194. if (this->BuildNoCMake) {
  195. // Make the generator available for the Build call below.
  196. cmGlobalGenerator* gen = cm.CreateGlobalGenerator(this->BuildGenerator);
  197. cm.SetGlobalGenerator(gen);
  198. if (!this->BuildGeneratorPlatform.empty()) {
  199. cmMakefile mf(gen, cm.GetCurrentSnapshot());
  200. if (!gen->SetGeneratorPlatform(this->BuildGeneratorPlatform, &mf)) {
  201. return 1;
  202. }
  203. }
  204. // Load the cache to make CMAKE_MAKE_PROGRAM available.
  205. cm.LoadCache(this->BinaryDir);
  206. } else {
  207. // do the cmake step, no timeout here since it is not a sub process
  208. if (this->RunCMake(outstring, out, cmakeOutString, &cm)) {
  209. return 1;
  210. }
  211. }
  212. // do the build
  213. if (this->BuildTargets.empty()) {
  214. this->BuildTargets.emplace_back();
  215. }
  216. for (std::string const& tar : this->BuildTargets) {
  217. cmDuration remainingTime = std::chrono::seconds(0);
  218. if (this->Timeout > cmDuration::zero()) {
  219. remainingTime =
  220. this->Timeout - (std::chrono::steady_clock::now() - clock_start);
  221. if (remainingTime <= std::chrono::seconds(0)) {
  222. if (outstring) {
  223. *outstring = "--build-and-test timeout exceeded. ";
  224. }
  225. return 1;
  226. }
  227. }
  228. std::string output;
  229. const char* config = nullptr;
  230. if (!this->CTest->GetConfigType().empty()) {
  231. config = this->CTest->GetConfigType().c_str();
  232. }
  233. #ifdef CMAKE_INTDIR
  234. if (!config) {
  235. config = CMAKE_INTDIR;
  236. }
  237. #endif
  238. if (!config) {
  239. config = "Debug";
  240. }
  241. int retVal = cm.GetGlobalGenerator()->Build(
  242. cmake::NO_BUILD_PARALLEL_LEVEL, this->SourceDir, this->BinaryDir,
  243. this->BuildProject, { tar }, output, this->BuildMakeProgram, config,
  244. !this->BuildNoClean, false, false, remainingTime);
  245. out << output;
  246. // if the build failed then return
  247. if (retVal) {
  248. if (outstring) {
  249. *outstring = out.str();
  250. }
  251. return 1;
  252. }
  253. }
  254. if (outstring) {
  255. *outstring = out.str();
  256. }
  257. // if no test was specified then we are done
  258. if (this->TestCommand.empty()) {
  259. return 0;
  260. }
  261. // now run the compiled test if we can find it
  262. // store the final location in fullPath
  263. std::string fullPath;
  264. std::string resultingConfig;
  265. std::vector<std::string> extraPaths;
  266. // if this->ExecutableDirectory is set try that as well
  267. if (!this->ExecutableDirectory.empty()) {
  268. std::string tempPath =
  269. cmStrCat(this->ExecutableDirectory, '/', 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. }