1
0

cmCTestBuildAndTestHandler.cxx 13 KB

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