cmCTestBuildAndTestHandler.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <stdlib.h>
  13. cmCTestBuildAndTestHandler::cmCTestBuildAndTestHandler()
  14. {
  15. this->BuildTwoConfig = false;
  16. this->BuildNoClean = false;
  17. this->BuildNoCMake = false;
  18. this->Timeout = 0;
  19. }
  20. void cmCTestBuildAndTestHandler::Initialize()
  21. {
  22. this->BuildTargets.clear();
  23. this->Superclass::Initialize();
  24. }
  25. const char* cmCTestBuildAndTestHandler::GetOutput()
  26. {
  27. return this->Output.c_str();
  28. }
  29. int cmCTestBuildAndTestHandler::ProcessHandler()
  30. {
  31. this->Output.clear();
  32. std::string output;
  33. cmSystemTools::ResetErrorOccuredFlag();
  34. int retv = this->RunCMakeAndTest(&this->Output);
  35. cmSystemTools::ResetErrorOccuredFlag();
  36. return retv;
  37. }
  38. int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
  39. std::ostringstream& out,
  40. std::string& cmakeOutString,
  41. cmake* cm)
  42. {
  43. std::vector<std::string> args;
  44. args.push_back(cmSystemTools::GetCMakeCommand());
  45. args.push_back(this->SourceDir);
  46. if (!this->BuildGenerator.empty()) {
  47. std::string generator = "-G";
  48. generator += this->BuildGenerator;
  49. args.push_back(generator);
  50. }
  51. if (!this->BuildGeneratorPlatform.empty()) {
  52. std::string platform = "-A";
  53. platform += this->BuildGeneratorPlatform;
  54. args.push_back(platform);
  55. }
  56. if (!this->BuildGeneratorToolset.empty()) {
  57. std::string toolset = "-T";
  58. toolset += this->BuildGeneratorToolset;
  59. args.push_back(toolset);
  60. }
  61. const char* config = nullptr;
  62. if (!this->CTest->GetConfigType().empty()) {
  63. config = this->CTest->GetConfigType().c_str();
  64. }
  65. #ifdef CMAKE_INTDIR
  66. if (!config) {
  67. config = CMAKE_INTDIR;
  68. }
  69. #endif
  70. if (config) {
  71. std::string btype = "-DCMAKE_BUILD_TYPE:STRING=" + std::string(config);
  72. args.push_back(btype);
  73. }
  74. for (std::string const& opt : this->BuildOptions) {
  75. args.push_back(opt);
  76. }
  77. if (cm->Run(args) != 0) {
  78. out << "Error: cmake execution failed\n";
  79. out << cmakeOutString << "\n";
  80. if (outstring) {
  81. *outstring = out.str();
  82. } else {
  83. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  84. }
  85. return 1;
  86. }
  87. // do another config?
  88. if (this->BuildTwoConfig) {
  89. if (cm->Run(args) != 0) {
  90. out << "Error: cmake execution failed\n";
  91. out << cmakeOutString << "\n";
  92. if (outstring) {
  93. *outstring = out.str();
  94. } else {
  95. cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
  96. }
  97. return 1;
  98. }
  99. }
  100. out << "======== CMake output ======\n";
  101. out << cmakeOutString;
  102. out << "======== End CMake output ======\n";
  103. return 0;
  104. }
  105. void CMakeMessageCallback(const char* m, const char* /*unused*/,
  106. bool& /*unused*/, void* s)
  107. {
  108. std::string* out = static_cast<std::string*>(s);
  109. *out += m;
  110. *out += "\n";
  111. }
  112. void CMakeProgressCallback(const char* msg, float /*unused*/, void* s)
  113. {
  114. std::string* out = static_cast<std::string*>(s);
  115. *out += msg;
  116. *out += "\n";
  117. }
  118. void CMakeOutputCallback(const char* m, size_t len, void* s)
  119. {
  120. std::string* out = static_cast<std::string*>(s);
  121. out->append(m, len);
  122. }
  123. class cmCTestBuildAndTestCaptureRAII
  124. {
  125. cmake& CM;
  126. public:
  127. cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s)
  128. : CM(cm)
  129. {
  130. cmSystemTools::SetMessageCallback(CMakeMessageCallback, &s);
  131. cmSystemTools::SetStdoutCallback(CMakeOutputCallback, &s);
  132. cmSystemTools::SetStderrCallback(CMakeOutputCallback, &s);
  133. this->CM.SetProgressCallback(CMakeProgressCallback, &s);
  134. }
  135. ~cmCTestBuildAndTestCaptureRAII()
  136. {
  137. this->CM.SetProgressCallback(nullptr, nullptr);
  138. cmSystemTools::SetStderrCallback(nullptr, nullptr);
  139. cmSystemTools::SetStdoutCallback(nullptr, nullptr);
  140. cmSystemTools::SetMessageCallback(nullptr, nullptr);
  141. }
  142. };
  143. int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
  144. {
  145. // if the generator and make program are not specified then it is an error
  146. if (this->BuildGenerator.empty()) {
  147. if (outstring) {
  148. *outstring = "--build-and-test requires that the generator "
  149. "be provided using the --build-generator "
  150. "command line option. ";
  151. }
  152. return 1;
  153. }
  154. cmake cm(cmake::RoleProject);
  155. cm.SetHomeDirectory("");
  156. cm.SetHomeOutputDirectory("");
  157. std::string cmakeOutString;
  158. cmCTestBuildAndTestCaptureRAII captureRAII(cm, cmakeOutString);
  159. static_cast<void>(captureRAII);
  160. std::ostringstream out;
  161. if (this->CTest->GetConfigType().empty() && !this->ConfigSample.empty()) {
  162. // use the config sample to set the ConfigType
  163. std::string fullPath;
  164. std::string resultingConfig;
  165. std::vector<std::string> extraPaths;
  166. std::vector<std::string> failed;
  167. fullPath = cmCTestTestHandler::FindExecutable(
  168. this->CTest, this->ConfigSample.c_str(), resultingConfig, extraPaths,
  169. failed);
  170. if (!fullPath.empty() && !resultingConfig.empty()) {
  171. this->CTest->SetConfigType(resultingConfig.c_str());
  172. }
  173. out << "Using config sample with results: " << fullPath << " and "
  174. << resultingConfig << std::endl;
  175. }
  176. // we need to honor the timeout specified, the timeout include cmake, build
  177. // and test time
  178. auto clock_start = std::chrono::steady_clock::now();
  179. // make sure the binary dir is there
  180. out << "Internal cmake changing into directory: " << this->BinaryDir
  181. << std::endl;
  182. if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) {
  183. cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
  184. }
  185. cmWorkingDirectory workdir(this->BinaryDir);
  186. if (this->BuildNoCMake) {
  187. // Make the generator available for the Build call below.
  188. cm.SetGlobalGenerator(cm.CreateGlobalGenerator(this->BuildGenerator));
  189. cm.SetGeneratorPlatform(this->BuildGeneratorPlatform);
  190. cm.SetGeneratorToolset(this->BuildGeneratorToolset);
  191. // Load the cache to make CMAKE_MAKE_PROGRAM available.
  192. cm.LoadCache(this->BinaryDir);
  193. } else {
  194. // do the cmake step, no timeout here since it is not a sub process
  195. if (this->RunCMake(outstring, out, cmakeOutString, &cm)) {
  196. return 1;
  197. }
  198. }
  199. // do the build
  200. if (this->BuildTargets.empty()) {
  201. this->BuildTargets.push_back("");
  202. }
  203. for (std::string const& tar : this->BuildTargets) {
  204. std::chrono::duration<double> remainingTime = std::chrono::seconds(0);
  205. if (this->Timeout > 0) {
  206. remainingTime = std::chrono::duration<double>(this->Timeout) -
  207. std::chrono::duration_cast<std::chrono::seconds>(
  208. 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.count());
  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. std::chrono::duration<double> remainingTime = std::chrono::seconds(0);
  300. if (this->Timeout > 0) {
  301. remainingTime = std::chrono::duration<double>(this->Timeout) -
  302. std::chrono::duration_cast<std::chrono::seconds>(
  303. std::chrono::steady_clock::now() - clock_start);
  304. if (remainingTime <= std::chrono::seconds(0)) {
  305. if (outstring) {
  306. *outstring = "--build-and-test timeout exceeded. ";
  307. }
  308. return 1;
  309. }
  310. }
  311. int runTestRes = this->CTest->RunTest(testCommand, &outs, &retval, nullptr,
  312. remainingTime, nullptr);
  313. if (runTestRes != cmsysProcess_State_Exited || retval != 0) {
  314. out << "Test command failed: " << testCommand[0] << "\n";
  315. retval = 1;
  316. }
  317. out << outs << "\n";
  318. if (outstring) {
  319. *outstring = out.str();
  320. } else {
  321. cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
  322. }
  323. return retval;
  324. }
  325. int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
  326. const std::string& currentArg, size_t& idx,
  327. const std::vector<std::string>& allArgs)
  328. {
  329. // --build-and-test options
  330. if (currentArg.find("--build-and-test", 0) == 0 &&
  331. idx < allArgs.size() - 1) {
  332. if (idx + 2 < allArgs.size()) {
  333. idx++;
  334. this->SourceDir = allArgs[idx];
  335. idx++;
  336. this->BinaryDir = allArgs[idx];
  337. // dir must exist before CollapseFullPath is called
  338. cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
  339. this->BinaryDir = cmSystemTools::CollapseFullPath(this->BinaryDir);
  340. this->SourceDir = cmSystemTools::CollapseFullPath(this->SourceDir);
  341. } else {
  342. cmCTestLog(this->CTest, ERROR_MESSAGE,
  343. "--build-and-test must have source and binary dir"
  344. << std::endl);
  345. return 0;
  346. }
  347. }
  348. if (currentArg.find("--build-target", 0) == 0 && idx < allArgs.size() - 1) {
  349. idx++;
  350. this->BuildTargets.push_back(allArgs[idx]);
  351. }
  352. if (currentArg.find("--build-nocmake", 0) == 0) {
  353. this->BuildNoCMake = true;
  354. }
  355. if (currentArg.find("--build-run-dir", 0) == 0 && idx < allArgs.size() - 1) {
  356. idx++;
  357. this->BuildRunDir = allArgs[idx];
  358. }
  359. if (currentArg.find("--build-two-config", 0) == 0) {
  360. this->BuildTwoConfig = true;
  361. }
  362. if (currentArg.find("--build-exe-dir", 0) == 0 && idx < allArgs.size() - 1) {
  363. idx++;
  364. this->ExecutableDirectory = allArgs[idx];
  365. }
  366. if (currentArg.find("--test-timeout", 0) == 0 && idx < allArgs.size() - 1) {
  367. idx++;
  368. this->Timeout = atof(allArgs[idx].c_str());
  369. }
  370. if (currentArg == "--build-generator" && idx < allArgs.size() - 1) {
  371. idx++;
  372. this->BuildGenerator = allArgs[idx];
  373. }
  374. if (currentArg == "--build-generator-platform" && idx < allArgs.size() - 1) {
  375. idx++;
  376. this->BuildGeneratorPlatform = allArgs[idx];
  377. }
  378. if (currentArg == "--build-generator-toolset" && idx < allArgs.size() - 1) {
  379. idx++;
  380. this->BuildGeneratorToolset = allArgs[idx];
  381. }
  382. if (currentArg.find("--build-project", 0) == 0 && idx < allArgs.size() - 1) {
  383. idx++;
  384. this->BuildProject = allArgs[idx];
  385. }
  386. if (currentArg.find("--build-makeprogram", 0) == 0 &&
  387. idx < allArgs.size() - 1) {
  388. idx++;
  389. this->BuildMakeProgram = allArgs[idx];
  390. }
  391. if (currentArg.find("--build-config-sample", 0) == 0 &&
  392. idx < allArgs.size() - 1) {
  393. idx++;
  394. this->ConfigSample = allArgs[idx];
  395. }
  396. if (currentArg.find("--build-noclean", 0) == 0) {
  397. this->BuildNoClean = true;
  398. }
  399. if (currentArg.find("--build-options", 0) == 0) {
  400. while (idx + 1 < allArgs.size() && allArgs[idx + 1] != "--build-target" &&
  401. allArgs[idx + 1] != "--test-command") {
  402. ++idx;
  403. this->BuildOptions.push_back(allArgs[idx]);
  404. }
  405. }
  406. if (currentArg.find("--test-command", 0) == 0 && idx < allArgs.size() - 1) {
  407. ++idx;
  408. this->TestCommand = allArgs[idx];
  409. while (idx + 1 < allArgs.size()) {
  410. ++idx;
  411. this->TestCommandArgs.push_back(allArgs[idx]);
  412. }
  413. }
  414. return 1;
  415. }