cmCTestBuildAndTestHandler.cxx 14 KB

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