cmCTestBuildAndTestHandler.cxx 14 KB

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