cmCTestBuildAndTestHandler.cxx 13 KB

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