cmCTestBuildAndTestHandler.cxx 13 KB

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