cmCTestBuildAndTestHandler.cxx 14 KB

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