cmGlobalGhsMultiGenerator.cxx 25 KB

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