cmCTestBuildAndTestHandler.cxx 14 KB

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