cmGlobalGhsMultiGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 "cmGlobalGhsMultiGenerator.h"
  4. #include <algorithm>
  5. #include <functional>
  6. #include <map>
  7. #include <sstream>
  8. #include <utility>
  9. #include <cm/memory>
  10. #include <cm/string>
  11. #include <cmext/algorithm>
  12. #include <cmext/memory>
  13. #include "cmCustomCommand.h"
  14. #include "cmCustomCommandLines.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmGhsMultiGpj.h"
  18. #include "cmList.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmLocalGhsMultiGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmMessageType.h"
  23. #include "cmSourceFile.h"
  24. #include "cmState.h"
  25. #include "cmStateTypes.h"
  26. #include "cmStringAlgorithms.h"
  27. #include "cmSystemTools.h"
  28. #include "cmTarget.h"
  29. #include "cmValue.h"
  30. #include "cmVersion.h"
  31. #include "cmake.h"
  32. const char* cmGlobalGhsMultiGenerator::FILE_EXTENSION = ".gpj";
  33. #ifdef __linux__
  34. const char* cmGlobalGhsMultiGenerator::DEFAULT_BUILD_PROGRAM = "gbuild";
  35. #elif defined(_WIN32)
  36. const char* cmGlobalGhsMultiGenerator::DEFAULT_BUILD_PROGRAM = "gbuild.exe";
  37. #endif
  38. const char* cmGlobalGhsMultiGenerator::CHECK_BUILD_SYSTEM_TARGET =
  39. "RERUN_CMAKE";
  40. cmGlobalGhsMultiGenerator::cmGlobalGhsMultiGenerator(cmake* cm)
  41. : cmGlobalGenerator(cm)
  42. {
  43. cm->GetState()->SetGhsMultiIDE(true);
  44. }
  45. cmGlobalGhsMultiGenerator::~cmGlobalGhsMultiGenerator() = default;
  46. std::unique_ptr<cmLocalGenerator>
  47. cmGlobalGhsMultiGenerator::CreateLocalGenerator(cmMakefile* mf)
  48. {
  49. return std::unique_ptr<cmLocalGenerator>(
  50. cm::make_unique<cmLocalGhsMultiGenerator>(this, mf));
  51. }
  52. cmDocumentationEntry cmGlobalGhsMultiGenerator::GetDocumentation()
  53. {
  54. return {
  55. GetActualName(),
  56. "Generates Green Hills MULTI files (experimental, work-in-progress)."
  57. };
  58. }
  59. void cmGlobalGhsMultiGenerator::ComputeTargetObjectDirectory(
  60. cmGeneratorTarget* gt) const
  61. {
  62. // Compute full path to object file directory for this target.
  63. std::string dir =
  64. cmStrCat(gt->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  65. gt->LocalGenerator->GetTargetDirectory(gt), '/');
  66. gt->ObjectDirectory = dir;
  67. }
  68. bool cmGlobalGhsMultiGenerator::SetGeneratorToolset(std::string const& ts,
  69. bool build, cmMakefile* mf)
  70. {
  71. /* In build mode nothing to be done.
  72. * Toolset already determined and build tool absolute path is cached.
  73. */
  74. if (build) {
  75. return true;
  76. }
  77. /* Determine the absolute directory for the toolset */
  78. std::string tsp;
  79. this->GetToolset(mf, tsp, ts);
  80. /* no toolset was found */
  81. if (tsp.empty()) {
  82. return false;
  83. }
  84. /* set the build tool to use */
  85. std::string gbuild(tsp + ((tsp.back() == '/') ? "" : "/") +
  86. DEFAULT_BUILD_PROGRAM);
  87. cmValue prevTool = mf->GetDefinition("CMAKE_MAKE_PROGRAM");
  88. /* check if the toolset changed from last generate */
  89. if (cmNonempty(prevTool) && !cmSystemTools::ComparePath(gbuild, *prevTool)) {
  90. std::string const& e = cmStrCat(
  91. "toolset build tool: ", gbuild, '\n',
  92. "Does not match the previously used build tool: ", *prevTool, '\n',
  93. "Either remove the CMakeCache.txt file and CMakeFiles "
  94. "directory or choose a different binary directory.");
  95. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  96. return false;
  97. }
  98. /* store the toolset that is being used for this build */
  99. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", gbuild, "build program to use",
  100. cmStateEnums::INTERNAL, true);
  101. mf->AddDefinition("CMAKE_SYSTEM_VERSION", tsp);
  102. return true;
  103. }
  104. bool cmGlobalGhsMultiGenerator::SetGeneratorPlatform(std::string const& p,
  105. cmMakefile* mf)
  106. {
  107. /* set primary target */
  108. cmValue t = mf->GetDefinition("GHS_PRIMARY_TARGET");
  109. if (cmIsOff(t)) {
  110. /* Use the value from `-A` or use `arm` */
  111. std::string arch = "arm";
  112. if (!cmIsOff(p)) {
  113. arch = p;
  114. }
  115. cmValue platform = mf->GetDefinition("GHS_TARGET_PLATFORM");
  116. std::string tgt = cmStrCat(arch, '_', platform, ".tgt");
  117. /* update the primary target name*/
  118. mf->AddDefinition("GHS_PRIMARY_TARGET", tgt);
  119. }
  120. return true;
  121. }
  122. void cmGlobalGhsMultiGenerator::EnableLanguage(
  123. std::vector<std::string> const& l, cmMakefile* mf, bool optional)
  124. {
  125. mf->AddDefinition("CMAKE_SYSTEM_NAME", "GHS-MULTI");
  126. mf->AddDefinition("GHSMULTI", "1"); // identifier for user CMake files
  127. this->cmGlobalGenerator::EnableLanguage(l, mf, optional);
  128. }
  129. bool cmGlobalGhsMultiGenerator::FindMakeProgram(cmMakefile* /*mf*/)
  130. {
  131. // The GHS generator only knows how to lookup its build tool
  132. // during generation of the project files, but this
  133. // can only be done after the toolset is specified.
  134. return true;
  135. }
  136. void cmGlobalGhsMultiGenerator::GetToolset(cmMakefile* mf, std::string& tsp,
  137. const std::string& ts)
  138. {
  139. /* Determine tsp - full path of the toolset from ts (toolset hint via -T) */
  140. std::string root = mf->GetSafeDefinition("GHS_TOOLSET_ROOT");
  141. // Check if `-T` was set by user
  142. if (ts.empty()) {
  143. // Enter toolset search mode
  144. std::vector<std::string> output;
  145. // Make sure root exists...
  146. if (!cmSystemTools::PathExists(root)) {
  147. std::string msg =
  148. "GHS_TOOLSET_ROOT directory \"" + root + "\" does not exist.";
  149. mf->IssueMessage(MessageType::FATAL_ERROR, msg);
  150. tsp = "";
  151. return;
  152. }
  153. // Add a directory separator
  154. if (root.back() != '/') {
  155. root += "/";
  156. }
  157. // Get all compiler directories in toolset root
  158. cmSystemTools::Glob(root, "comp_[^;]+", output);
  159. if (output.empty()) {
  160. // No compiler directories found
  161. std::string msg =
  162. "No GHS toolsets found in GHS_TOOLSET_ROOT \"" + root + "\".";
  163. mf->IssueMessage(MessageType::FATAL_ERROR, msg);
  164. tsp = "";
  165. } else {
  166. // Use latest? version
  167. tsp = root + output.back();
  168. }
  169. } else {
  170. // Toolset was provided by user
  171. std::string tryPath;
  172. // NOTE: CollapseFullPath() will determine if user toolset was full path or
  173. // or relative path.
  174. tryPath = cmSystemTools::CollapseFullPath(ts, root);
  175. if (!cmSystemTools::FileExists(tryPath)) {
  176. std::string msg = "GHS toolset \"" + tryPath + "\" does not exist.";
  177. mf->IssueMessage(MessageType::FATAL_ERROR, msg);
  178. tsp = "";
  179. } else {
  180. tsp = tryPath;
  181. }
  182. }
  183. }
  184. void cmGlobalGhsMultiGenerator::WriteFileHeader(std::ostream& fout)
  185. {
  186. /* clang-format off */
  187. fout << "#!gbuild\n"
  188. "#\n"
  189. "# CMAKE generated file: DO NOT EDIT!\n"
  190. "# Generated by \"" << GetActualName() << "\""
  191. " Generator, CMake Version " << cmVersion::GetMajorVersion() << '.'
  192. << cmVersion::GetMinorVersion() << "\n"
  193. "#\n\n";
  194. /* clang-format on */
  195. }
  196. void cmGlobalGhsMultiGenerator::WriteCustomRuleBOD(std::ostream& fout)
  197. {
  198. fout << "Commands {\n"
  199. " Custom_Rule_Command {\n"
  200. " name = \"Custom Rule Command\"\n"
  201. " exec = \""
  202. #ifdef _WIN32
  203. "cmd.exe"
  204. #else
  205. "/bin/sh"
  206. #endif
  207. "\"\n"
  208. " options = {\"SpecialOptions\"}\n"
  209. " }\n"
  210. "}\n"
  211. "\n\n"
  212. "FileTypes {\n"
  213. " CmakeRule {\n"
  214. " name = \"Custom Rule\"\n"
  215. " action = \"&Run\"\n"
  216. " extensions = {\""
  217. #ifdef _WIN32
  218. "bat"
  219. #else
  220. "sh"
  221. #endif
  222. "\"}\n"
  223. " grepable = false\n"
  224. " command = \"Custom Rule Command\"\n"
  225. " commandLine = \"$COMMAND "
  226. #ifdef _WIN32
  227. "/c"
  228. #endif
  229. " $INPUTFILE\"\n"
  230. " progress = \"Processing Custom Rule\"\n"
  231. " promoteToFirstPass = true\n"
  232. " outputType = \"None\"\n"
  233. " color = \"#800080\"\n"
  234. " }\n"
  235. "}\n";
  236. }
  237. void cmGlobalGhsMultiGenerator::WriteCustomTargetBOD(std::ostream& fout)
  238. {
  239. fout << "FileTypes {\n"
  240. " CmakeTarget {\n"
  241. " name = \"Custom Target\"\n"
  242. " action = \"&Execute\"\n"
  243. " grepable = false\n"
  244. " outputType = \"None\"\n"
  245. " color = \"#800080\"\n"
  246. " }\n"
  247. "}\n";
  248. }
  249. void cmGlobalGhsMultiGenerator::WriteTopLevelProject(std::ostream& fout,
  250. cmLocalGenerator* root)
  251. {
  252. this->WriteFileHeader(fout);
  253. this->WriteMacros(fout, root);
  254. this->WriteHighLevelDirectives(fout, root);
  255. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fout);
  256. fout << "# Top Level Project File\n";
  257. // Specify BSP option if supplied by user
  258. // -- not all platforms require this entry in the project file
  259. cmValue bspName = root->GetMakefile()->GetDefinition("GHS_BSP_NAME");
  260. if (!cmIsOff(bspName)) {
  261. fout << " -bsp " << *bspName << '\n';
  262. }
  263. // Specify OS DIR if supplied by user
  264. // -- not all platforms require this entry in the project file
  265. cmValue osDir = root->GetMakefile()->GetDefinition("GHS_OS_DIR");
  266. if (!cmIsOff(osDir)) {
  267. cmValue osDirOption =
  268. root->GetMakefile()->GetDefinition("GHS_OS_DIR_OPTION");
  269. fout << " ";
  270. if (cmIsOff(osDirOption)) {
  271. fout << "";
  272. } else {
  273. fout << *osDirOption;
  274. }
  275. fout << "\"" << osDir << "\"\n";
  276. }
  277. }
  278. void cmGlobalGhsMultiGenerator::WriteSubProjects(std::ostream& fout,
  279. bool filterPredefined)
  280. {
  281. std::set<std::string> predefinedTargets;
  282. predefinedTargets.insert(this->GetInstallTargetName());
  283. predefinedTargets.insert(this->GetAllTargetName());
  284. predefinedTargets.insert(std::string(CHECK_BUILD_SYSTEM_TARGET));
  285. // All known targets
  286. for (cmGeneratorTarget const* target : this->ProjectTargets) {
  287. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  288. target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  289. target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  290. (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
  291. target->GetName() != this->GetInstallTargetName())) {
  292. continue;
  293. }
  294. /* Check if the current target is a predefined CMake target */
  295. bool predefinedTarget =
  296. predefinedTargets.find(target->GetName()) != predefinedTargets.end();
  297. if ((filterPredefined && predefinedTarget) ||
  298. (!filterPredefined && !predefinedTarget)) {
  299. fout << target->GetName() + ".tgt" + FILE_EXTENSION << " [Project]\n";
  300. }
  301. }
  302. }
  303. void cmGlobalGhsMultiGenerator::WriteProjectLine(
  304. std::ostream& fout, cmGeneratorTarget const* target,
  305. std::string& rootBinaryDir)
  306. {
  307. cmValue projFile = target->GetProperty("GENERATOR_FILE_NAME");
  308. cmValue projType = target->GetProperty("GENERATOR_FILE_NAME_EXT");
  309. /* If either value is not valid then this particular target is an
  310. * unsupported target type and should be skipped.
  311. */
  312. if (projFile && projType) {
  313. std::string path = cmSystemTools::RelativePath(rootBinaryDir, *projFile);
  314. fout << path;
  315. fout << ' ' << *projType << '\n';
  316. }
  317. }
  318. void cmGlobalGhsMultiGenerator::WriteTargets(cmLocalGenerator* root)
  319. {
  320. std::string rootBinaryDir = root->GetCurrentBinaryDirectory();
  321. // All known targets
  322. for (cmGeneratorTarget const* target : this->ProjectTargets) {
  323. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  324. target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  325. target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  326. (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
  327. target->GetName() != this->GetInstallTargetName())) {
  328. continue;
  329. }
  330. // create target build file
  331. std::string name = cmStrCat(target->GetName(), ".tgt", FILE_EXTENSION);
  332. std::string fname = cmStrCat(rootBinaryDir, "/", name);
  333. cmGeneratedFileStream fbld(fname);
  334. fbld.SetCopyIfDifferent(true);
  335. this->WriteFileHeader(fbld);
  336. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fbld);
  337. std::vector<cmGeneratorTarget const*> build;
  338. if (this->ComputeTargetBuildOrder(target, build)) {
  339. cmSystemTools::Error(
  340. cmStrCat("The inter-target dependency graph for target [",
  341. target->GetName(), "] had a cycle.\n"));
  342. } else {
  343. for (auto& tgt : build) {
  344. this->WriteProjectLine(fbld, tgt, rootBinaryDir);
  345. }
  346. }
  347. fbld.Close();
  348. }
  349. }
  350. void cmGlobalGhsMultiGenerator::Generate()
  351. {
  352. std::string fname;
  353. // first do the superclass method
  354. this->cmGlobalGenerator::Generate();
  355. // output top-level projects
  356. for (auto& it : this->ProjectMap) {
  357. this->OutputTopLevelProject(it.second[0], it.second);
  358. }
  359. // create custom rule BOD file
  360. fname = this->GetCMakeInstance()->GetHomeOutputDirectory() +
  361. "/CMakeFiles/custom_rule.bod";
  362. cmGeneratedFileStream frule(fname);
  363. frule.SetCopyIfDifferent(true);
  364. this->WriteFileHeader(frule);
  365. this->WriteCustomRuleBOD(frule);
  366. frule.Close();
  367. // create custom target BOD file
  368. fname = this->GetCMakeInstance()->GetHomeOutputDirectory() +
  369. "/CMakeFiles/custom_target.bod";
  370. cmGeneratedFileStream ftarget(fname);
  371. ftarget.SetCopyIfDifferent(true);
  372. this->WriteFileHeader(ftarget);
  373. this->WriteCustomTargetBOD(ftarget);
  374. ftarget.Close();
  375. }
  376. void cmGlobalGhsMultiGenerator::OutputTopLevelProject(
  377. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  378. {
  379. std::string fname;
  380. if (generators.empty()) {
  381. return;
  382. }
  383. // Collect all targets under this root generator and the transitive
  384. // closure of their dependencies.
  385. TargetDependSet projectTargets;
  386. TargetDependSet originalTargets;
  387. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  388. OrderedTargetDependSet sortedProjectTargets(projectTargets, "");
  389. this->ProjectTargets.clear();
  390. for (cmGeneratorTarget const* t : sortedProjectTargets) {
  391. /* save list of all targets in sorted order */
  392. this->ProjectTargets.push_back(t);
  393. }
  394. /* Name top-level projects as filename.top.gpj to avoid name clashes
  395. * with target projects. This avoid the issue where the project has
  396. * the same name as the executable target.
  397. */
  398. fname = cmStrCat(root->GetCurrentBinaryDirectory(), '/',
  399. root->GetProjectName(), ".top", FILE_EXTENSION);
  400. cmGeneratedFileStream top(fname);
  401. top.SetCopyIfDifferent(true);
  402. this->WriteTopLevelProject(top, root);
  403. this->WriteTargets(root);
  404. this->WriteSubProjects(top, true);
  405. this->WriteSubProjects(top, false);
  406. top.Close();
  407. }
  408. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  409. cmGlobalGhsMultiGenerator::GenerateBuildCommand(
  410. const std::string& makeProgram, const std::string& projectName,
  411. const std::string& projectDir, std::vector<std::string> const& targetNames,
  412. const std::string& /*config*/, int jobs, bool verbose,
  413. const cmBuildOptions& /*buildOptions*/,
  414. std::vector<std::string> const& makeOptions)
  415. {
  416. GeneratedMakeCommand makeCommand;
  417. makeCommand.Add(this->SelectMakeProgram(makeProgram));
  418. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  419. if (jobs == cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  420. makeCommand.Add("-parallel");
  421. } else {
  422. makeCommand.Add(std::string("-parallel=") + std::to_string(jobs));
  423. }
  424. }
  425. /* determine the top-project file in the project directory */
  426. std::string proj = projectName + ".top" + FILE_EXTENSION;
  427. std::vector<std::string> files;
  428. cmSystemTools::Glob(projectDir, ".*\\.top\\.gpj", files);
  429. if (!files.empty()) {
  430. /* use the real top-level project in the directory */
  431. proj = files.at(0);
  432. }
  433. makeCommand.Add("-top", proj);
  434. /* determine targets to build */
  435. bool build_all = false;
  436. if (!targetNames.empty()) {
  437. for (const auto& tname : targetNames) {
  438. if (!tname.empty()) {
  439. if (tname == "clean") {
  440. makeCommand.Add("-clean");
  441. } else {
  442. makeCommand.Add(tname + ".tgt.gpj");
  443. }
  444. } else {
  445. build_all = true;
  446. }
  447. }
  448. } else {
  449. build_all = true;
  450. }
  451. if (build_all) {
  452. /* transform name to default build */;
  453. std::string all = std::string(this->GetAllTargetName()) + ".tgt.gpj";
  454. makeCommand.Add(all);
  455. }
  456. if (verbose) {
  457. makeCommand.Add("-commands");
  458. }
  459. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  460. return { std::move(makeCommand) };
  461. }
  462. void cmGlobalGhsMultiGenerator::WriteMacros(std::ostream& fout,
  463. cmLocalGenerator* root)
  464. {
  465. fout << "macro PROJ_NAME=" << root->GetProjectName() << '\n';
  466. cmValue ghsGpjMacros = root->GetMakefile()->GetDefinition("GHS_GPJ_MACROS");
  467. if (ghsGpjMacros) {
  468. cmList expandedList{ *ghsGpjMacros };
  469. for (std::string const& arg : expandedList) {
  470. fout << "macro " << arg << '\n';
  471. }
  472. }
  473. }
  474. void cmGlobalGhsMultiGenerator::WriteHighLevelDirectives(
  475. std::ostream& fout, cmLocalGenerator* root)
  476. {
  477. /* put primary target and customization files into project file */
  478. cmValue const tgt = root->GetMakefile()->GetDefinition("GHS_PRIMARY_TARGET");
  479. /* clang-format off */
  480. fout << "primaryTarget=" << tgt << "\n"
  481. "customization=" << root->GetBinaryDirectory()
  482. << "/CMakeFiles/custom_rule.bod\n"
  483. "customization=" << root->GetBinaryDirectory()
  484. << "/CMakeFiles/custom_target.bod" << '\n';
  485. /* clang-format on */
  486. cmValue const customization =
  487. root->GetMakefile()->GetDefinition("GHS_CUSTOMIZATION");
  488. if (cmNonempty(customization)) {
  489. fout << "customization="
  490. << cmGlobalGhsMultiGenerator::TrimQuotes(*customization) << '\n';
  491. this->GetCMakeInstance()->MarkCliAsUsed("GHS_CUSTOMIZATION");
  492. }
  493. }
  494. std::string cmGlobalGhsMultiGenerator::TrimQuotes(std::string str)
  495. {
  496. cm::erase(str, '"');
  497. return str;
  498. }
  499. bool cmGlobalGhsMultiGenerator::TargetCompare::operator()(
  500. cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
  501. {
  502. // Make sure a given named target is ordered first,
  503. // e.g. to set ALL_BUILD as the default active project.
  504. // When the empty string is named this is a no-op.
  505. if (r->GetName() == this->First) {
  506. return false;
  507. }
  508. if (l->GetName() == this->First) {
  509. return true;
  510. }
  511. return l->GetName() < r->GetName();
  512. }
  513. cmGlobalGhsMultiGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  514. TargetDependSet const& targets, std::string const& first)
  515. : derived(TargetCompare(first))
  516. {
  517. this->insert(targets.begin(), targets.end());
  518. }
  519. bool cmGlobalGhsMultiGenerator::ComputeTargetBuildOrder(
  520. cmGeneratorTarget const* tgt, std::vector<cmGeneratorTarget const*>& build)
  521. {
  522. std::vector<cmGeneratorTarget const*> t{ tgt };
  523. return this->ComputeTargetBuildOrder(t, build);
  524. }
  525. bool cmGlobalGhsMultiGenerator::ComputeTargetBuildOrder(
  526. std::vector<cmGeneratorTarget const*>& tgt,
  527. std::vector<cmGeneratorTarget const*>& build)
  528. {
  529. std::set<cmGeneratorTarget const*> temp;
  530. std::set<cmGeneratorTarget const*> perm;
  531. for (const auto* const ti : tgt) {
  532. bool r = this->VisitTarget(temp, perm, build, ti);
  533. if (r) {
  534. return r;
  535. }
  536. }
  537. return false;
  538. }
  539. bool cmGlobalGhsMultiGenerator::VisitTarget(
  540. std::set<cmGeneratorTarget const*>& temp,
  541. std::set<cmGeneratorTarget const*>& perm,
  542. std::vector<cmGeneratorTarget const*>& order, cmGeneratorTarget const* ti)
  543. {
  544. /* check if permanent mark is set*/
  545. if (perm.find(ti) == perm.end()) {
  546. /* set temporary mark; check if revisit*/
  547. if (temp.insert(ti).second) {
  548. /* sort targets lexicographically to ensure that nodes are always visited
  549. * in the same order */
  550. OrderedTargetDependSet sortedTargets(this->GetTargetDirectDepends(ti),
  551. "");
  552. for (auto const& di : sortedTargets) {
  553. if (this->VisitTarget(temp, perm, order, di)) {
  554. return true;
  555. }
  556. }
  557. /* mark as complete; insert into beginning of list*/
  558. perm.insert(ti);
  559. order.push_back(ti);
  560. return false;
  561. }
  562. /* revisiting item - not a DAG */
  563. return true;
  564. }
  565. /* already complete */
  566. return false;
  567. }
  568. bool cmGlobalGhsMultiGenerator::AddCheckTarget()
  569. {
  570. // Skip the target if no regeneration is to be done.
  571. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  572. return false;
  573. }
  574. // Get the generators.
  575. std::vector<std::unique_ptr<cmLocalGenerator>> const& generators =
  576. this->LocalGenerators;
  577. auto& lg =
  578. cm::static_reference_cast<cmLocalGhsMultiGenerator>(generators[0]);
  579. // The name of the output file for the custom command.
  580. this->StampFile = lg.GetBinaryDirectory() + std::string("/CMakeFiles/") +
  581. CHECK_BUILD_SYSTEM_TARGET;
  582. // Add a custom rule to re-run CMake if any input files changed.
  583. {
  584. // Collect the input files used to generate all targets in this
  585. // project.
  586. std::vector<std::string> listFiles;
  587. for (const auto& gen : generators) {
  588. cm::append(listFiles, gen->GetMakefile()->GetListFiles());
  589. }
  590. // Add the cache file.
  591. listFiles.emplace_back(cmStrCat(
  592. this->GetCMakeInstance()->GetHomeOutputDirectory(), "/CMakeCache.txt"));
  593. // Print not implemented warning.
  594. if (this->GetCMakeInstance()->DoWriteGlobVerifyTarget()) {
  595. std::ostringstream msg;
  596. msg << "Any pre-check scripts, such as those generated for file(GLOB "
  597. "CONFIGURE_DEPENDS), will not be run by gbuild.";
  598. this->GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_WARNING,
  599. msg.str());
  600. }
  601. // Sort the list of input files and remove duplicates.
  602. std::sort(listFiles.begin(), listFiles.end(), std::less<std::string>());
  603. auto newEnd = std::unique(listFiles.begin(), listFiles.end());
  604. listFiles.erase(newEnd, listFiles.end());
  605. // Create a rule to re-run CMake and create output file.
  606. cmCustomCommandLines commandLines;
  607. commandLines.emplace_back(
  608. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E", "rm", "-f",
  609. this->StampFile }));
  610. std::string argS = cmStrCat("-S", lg.GetSourceDirectory());
  611. std::string argB = cmStrCat("-B", lg.GetBinaryDirectory());
  612. commandLines.emplace_back(
  613. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), argS, argB }));
  614. commandLines.emplace_back(cmMakeCommandLine(
  615. { cmSystemTools::GetCMakeCommand(), "-E", "touch", this->StampFile }));
  616. /* Create the target(Exclude from ALL_BUILD).
  617. *
  618. * The build tool, currently, does not support rereading the project files
  619. * if they get updated. So do not run this target as part of ALL_BUILD.
  620. */
  621. auto cc = cm::make_unique<cmCustomCommand>();
  622. cmTarget* tgt =
  623. lg.AddUtilityCommand(CHECK_BUILD_SYSTEM_TARGET, true, std::move(cc));
  624. auto ptr = cm::make_unique<cmGeneratorTarget>(tgt, &lg);
  625. auto* gt = ptr.get();
  626. lg.AddGeneratorTarget(std::move(ptr));
  627. // Add the rule.
  628. cc = cm::make_unique<cmCustomCommand>();
  629. cc->SetOutputs(this->StampFile);
  630. cc->SetDepends(listFiles);
  631. cc->SetCommandLines(commandLines);
  632. cc->SetComment("Checking Build System");
  633. cc->SetEscapeOldStyle(false);
  634. cc->SetStdPipesUTF8(true);
  635. if (cmSourceFile* file =
  636. lg.AddCustomCommandToOutput(std::move(cc), true)) {
  637. gt->AddSource(file->ResolveFullPath());
  638. } else {
  639. cmSystemTools::Error("Error adding rule for " + this->StampFile);
  640. }
  641. // Organize in the "predefined targets" folder:
  642. if (this->UseFolderProperty()) {
  643. tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  644. }
  645. }
  646. return true;
  647. }
  648. void cmGlobalGhsMultiGenerator::AddAllTarget()
  649. {
  650. // Add a special target that depends on ALL projects for easy build
  651. // of one configuration only.
  652. for (auto const& it : this->ProjectMap) {
  653. std::vector<cmLocalGenerator*> const& gen = it.second;
  654. // add the ALL_BUILD to the first local generator of each project
  655. if (!gen.empty()) {
  656. // Use no actual command lines so that the target itself is not
  657. // considered always out of date.
  658. auto cc = cm::make_unique<cmCustomCommand>();
  659. cc->SetEscapeOldStyle(false);
  660. cc->SetComment("Build all projects");
  661. cmTarget* allBuild = gen[0]->AddUtilityCommand(this->GetAllTargetName(),
  662. true, std::move(cc));
  663. gen[0]->AddGeneratorTarget(
  664. cm::make_unique<cmGeneratorTarget>(allBuild, gen[0]));
  665. // Organize in the "predefined targets" folder:
  666. if (this->UseFolderProperty()) {
  667. allBuild->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  668. }
  669. // Now make all targets depend on the ALL_BUILD target
  670. for (cmLocalGenerator const* i : gen) {
  671. for (const auto& tgt : i->GetGeneratorTargets()) {
  672. // Skip global or imported targets
  673. if (tgt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  674. tgt->IsImported()) {
  675. continue;
  676. }
  677. // Skip Exclude From All Targets
  678. if (!this->IsExcluded(gen[0], tgt.get())) {
  679. allBuild->AddUtility(tgt->GetName(), false);
  680. }
  681. }
  682. }
  683. }
  684. }
  685. }
  686. void cmGlobalGhsMultiGenerator::AddExtraIDETargets()
  687. {
  688. // Add a special target that depends on ALL projects.
  689. this->AddAllTarget();
  690. /* Add Custom Target to check if CMake needs to be rerun.
  691. *
  692. * The build tool, currently, does not support rereading the project files
  693. * if they get updated. So do not make the other targets dependent on this
  694. * check.
  695. */
  696. this->AddCheckTarget();
  697. }