cmCTestBuildAndTestHandler.cxx 14 KB

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