cmCTestBuildAndTestHandler.cxx 13 KB

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