cmCTestBuildAndTestHandler.cxx 14 KB

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