cmCTestBuildAndTestHandler.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 <stdlib.h>
  12. cmCTestBuildAndTestHandler::cmCTestBuildAndTestHandler()
  13. {
  14. this->BuildTwoConfig = false;
  15. this->BuildNoClean = false;
  16. this->BuildNoCMake = false;
  17. this->Timeout = 0;
  18. }
  19. void cmCTestBuildAndTestHandler::Initialize()
  20. {
  21. this->BuildTargets.clear();
  22. this->Superclass::Initialize();
  23. }
  24. const char* cmCTestBuildAndTestHandler::GetOutput()
  25. {
  26. return this->Output.c_str();
  27. }
  28. int cmCTestBuildAndTestHandler::ProcessHandler()
  29. {
  30. this->Output = "";
  31. std::string output;
  32. cmSystemTools::ResetErrorOccuredFlag();
  33. int retv = this->RunCMakeAndTest(&this->Output);
  34. cmSystemTools::ResetErrorOccuredFlag();
  35. return retv;
  36. }
  37. int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
  38. std::ostringstream& out,
  39. std::string& cmakeOutString,
  40. cmake* cm)
  41. {
  42. unsigned int k;
  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 = CM_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 (k = 0; k < this->BuildOptions.size(); ++k) {
  75. args.push_back(this->BuildOptions[k]);
  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 = (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 = (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 = (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(CM_NULLPTR, CM_NULLPTR);
  138. cmSystemTools::SetStderrCallback(CM_NULLPTR, CM_NULLPTR);
  139. cmSystemTools::SetStdoutCallback(CM_NULLPTR, CM_NULLPTR);
  140. cmSystemTools::SetMessageCallback(CM_NULLPTR, CM_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;
  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. double clock_start = cmSystemTools::GetTime();
  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. std::vector<std::string>::iterator tarIt;
  201. if (this->BuildTargets.empty()) {
  202. this->BuildTargets.push_back("");
  203. }
  204. for (tarIt = this->BuildTargets.begin(); tarIt != this->BuildTargets.end();
  205. ++tarIt) {
  206. double remainingTime = 0;
  207. if (this->Timeout > 0) {
  208. remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
  209. if (remainingTime <= 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 = CM_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, *tarIt, 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 (unsigned int i = 0; i < failed.size(); ++i) {
  271. out << failed[i] << "\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 (size_t k = 0; k < this->TestCommandArgs.size(); ++k) {
  283. testCommand.push_back(this->TestCommandArgs[k].c_str());
  284. }
  285. testCommand.push_back(CM_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 (size_t k = 0; k < this->TestCommandArgs.size(); ++k) {
  295. out << " \"" << this->TestCommandArgs[k] << "\"";
  296. }
  297. out << "\n";
  298. // how much time is remaining
  299. double remainingTime = 0;
  300. if (this->Timeout > 0) {
  301. remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
  302. if (remainingTime <= 0) {
  303. if (outstring) {
  304. *outstring = "--build-and-test timeout exceeded. ";
  305. }
  306. return 1;
  307. }
  308. }
  309. int runTestRes = this->CTest->RunTest(testCommand, &outs, &retval,
  310. CM_NULLPTR, remainingTime, CM_NULLPTR);
  311. if (runTestRes != cmsysProcess_State_Exited || retval != 0) {
  312. out << "Test command failed: " << testCommand[0] << "\n";
  313. retval = 1;
  314. }
  315. out << outs << "\n";
  316. if (outstring) {
  317. *outstring = out.str();
  318. } else {
  319. cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
  320. }
  321. return retval;
  322. }
  323. int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
  324. const std::string& currentArg, size_t& idx,
  325. const std::vector<std::string>& allArgs)
  326. {
  327. // --build-and-test options
  328. if (currentArg.find("--build-and-test", 0) == 0 &&
  329. idx < allArgs.size() - 1) {
  330. if (idx + 2 < allArgs.size()) {
  331. idx++;
  332. this->SourceDir = allArgs[idx];
  333. idx++;
  334. this->BinaryDir = allArgs[idx];
  335. // dir must exist before CollapseFullPath is called
  336. cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
  337. this->BinaryDir = cmSystemTools::CollapseFullPath(this->BinaryDir);
  338. this->SourceDir = cmSystemTools::CollapseFullPath(this->SourceDir);
  339. } else {
  340. cmCTestLog(this->CTest, ERROR_MESSAGE,
  341. "--build-and-test must have source and binary dir"
  342. << std::endl);
  343. return 0;
  344. }
  345. }
  346. if (currentArg.find("--build-target", 0) == 0 && idx < allArgs.size() - 1) {
  347. idx++;
  348. this->BuildTargets.push_back(allArgs[idx]);
  349. }
  350. if (currentArg.find("--build-nocmake", 0) == 0) {
  351. this->BuildNoCMake = true;
  352. }
  353. if (currentArg.find("--build-run-dir", 0) == 0 && idx < allArgs.size() - 1) {
  354. idx++;
  355. this->BuildRunDir = allArgs[idx];
  356. }
  357. if (currentArg.find("--build-two-config", 0) == 0) {
  358. this->BuildTwoConfig = true;
  359. }
  360. if (currentArg.find("--build-exe-dir", 0) == 0 && idx < allArgs.size() - 1) {
  361. idx++;
  362. this->ExecutableDirectory = allArgs[idx];
  363. }
  364. if (currentArg.find("--test-timeout", 0) == 0 && idx < allArgs.size() - 1) {
  365. idx++;
  366. this->Timeout = atof(allArgs[idx].c_str());
  367. }
  368. if (currentArg == "--build-generator" && idx < allArgs.size() - 1) {
  369. idx++;
  370. this->BuildGenerator = allArgs[idx];
  371. }
  372. if (currentArg == "--build-generator-platform" && idx < allArgs.size() - 1) {
  373. idx++;
  374. this->BuildGeneratorPlatform = allArgs[idx];
  375. }
  376. if (currentArg == "--build-generator-toolset" && idx < allArgs.size() - 1) {
  377. idx++;
  378. this->BuildGeneratorToolset = allArgs[idx];
  379. }
  380. if (currentArg.find("--build-project", 0) == 0 && idx < allArgs.size() - 1) {
  381. idx++;
  382. this->BuildProject = allArgs[idx];
  383. }
  384. if (currentArg.find("--build-makeprogram", 0) == 0 &&
  385. idx < allArgs.size() - 1) {
  386. idx++;
  387. this->BuildMakeProgram = allArgs[idx];
  388. }
  389. if (currentArg.find("--build-config-sample", 0) == 0 &&
  390. idx < allArgs.size() - 1) {
  391. idx++;
  392. this->ConfigSample = allArgs[idx];
  393. }
  394. if (currentArg.find("--build-noclean", 0) == 0) {
  395. this->BuildNoClean = true;
  396. }
  397. if (currentArg.find("--build-options", 0) == 0) {
  398. while (idx + 1 < allArgs.size() && allArgs[idx + 1] != "--build-target" &&
  399. allArgs[idx + 1] != "--test-command") {
  400. ++idx;
  401. this->BuildOptions.push_back(allArgs[idx]);
  402. }
  403. }
  404. if (currentArg.find("--test-command", 0) == 0 && idx < allArgs.size() - 1) {
  405. ++idx;
  406. this->TestCommand = allArgs[idx];
  407. while (idx + 1 < allArgs.size()) {
  408. ++idx;
  409. this->TestCommandArgs.push_back(allArgs[idx]);
  410. }
  411. }
  412. return 1;
  413. }