cmGlobalGhsMultiGenerator.cxx 23 KB

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