cmGlobalGhsMultiGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 <cstring>
  6. #include <map>
  7. #include <ostream>
  8. #include <utility>
  9. #include <cm/memory>
  10. #include <cm/string>
  11. #include <cmext/algorithm>
  12. #include "cmDocumentationEntry.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmGeneratorTarget.h"
  15. #include "cmGhsMultiGpj.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmLocalGhsMultiGenerator.h"
  18. #include "cmMakefile.h"
  19. #include "cmProperty.h"
  20. #include "cmState.h"
  21. #include "cmStateTypes.h"
  22. #include "cmStringAlgorithms.h"
  23. #include "cmSystemTools.h"
  24. #include "cmVersion.h"
  25. #include "cmake.h"
  26. const char* cmGlobalGhsMultiGenerator::FILE_EXTENSION = ".gpj";
  27. #ifdef __linux__
  28. const char* cmGlobalGhsMultiGenerator::DEFAULT_BUILD_PROGRAM = "gbuild";
  29. const char* cmGlobalGhsMultiGenerator::DEFAULT_TOOLSET_ROOT = "/usr/ghs";
  30. #elif defined(_WIN32)
  31. const char* cmGlobalGhsMultiGenerator::DEFAULT_BUILD_PROGRAM = "gbuild.exe";
  32. const char* cmGlobalGhsMultiGenerator::DEFAULT_TOOLSET_ROOT = "C:/ghs";
  33. #endif
  34. cmGlobalGhsMultiGenerator::cmGlobalGhsMultiGenerator(cmake* cm)
  35. : cmGlobalGenerator(cm)
  36. {
  37. cm->GetState()->SetGhsMultiIDE(true);
  38. }
  39. cmGlobalGhsMultiGenerator::~cmGlobalGhsMultiGenerator() = default;
  40. std::unique_ptr<cmLocalGenerator>
  41. cmGlobalGhsMultiGenerator::CreateLocalGenerator(cmMakefile* mf)
  42. {
  43. return std::unique_ptr<cmLocalGenerator>(
  44. cm::make_unique<cmLocalGhsMultiGenerator>(this, mf));
  45. }
  46. void cmGlobalGhsMultiGenerator::GetDocumentation(cmDocumentationEntry& entry)
  47. {
  48. entry.Name = GetActualName();
  49. entry.Brief =
  50. "Generates Green Hills MULTI files (experimental, work-in-progress).";
  51. }
  52. void cmGlobalGhsMultiGenerator::ComputeTargetObjectDirectory(
  53. cmGeneratorTarget* gt) const
  54. {
  55. // Compute full path to object file directory for this target.
  56. std::string dir =
  57. cmStrCat(gt->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  58. gt->LocalGenerator->GetTargetDirectory(gt), '/');
  59. gt->ObjectDirectory = dir;
  60. }
  61. bool cmGlobalGhsMultiGenerator::SetGeneratorToolset(std::string const& ts,
  62. bool build, cmMakefile* mf)
  63. {
  64. if (build) {
  65. return true;
  66. }
  67. std::string tsp; /* toolset path */
  68. this->GetToolset(mf, tsp, ts);
  69. /* no toolset was found */
  70. if (tsp.empty()) {
  71. return false;
  72. }
  73. if (ts.empty()) {
  74. std::string message;
  75. message = cmStrCat(
  76. "Green Hills MULTI: -T <toolset> not specified; defaulting to \"", tsp,
  77. '"');
  78. cmSystemTools::Message(message);
  79. /* store the full toolset for later use
  80. * -- already done if -T<toolset> was specified
  81. */
  82. mf->AddCacheDefinition("CMAKE_GENERATOR_TOOLSET", tsp,
  83. "Location of generator toolset.",
  84. cmStateEnums::INTERNAL);
  85. }
  86. /* set the build tool to use */
  87. std::string gbuild(tsp + ((tsp.back() == '/') ? "" : "/") +
  88. DEFAULT_BUILD_PROGRAM);
  89. const char* prevTool = mf->GetDefinition("CMAKE_MAKE_PROGRAM");
  90. /* check if the toolset changed from last generate */
  91. if (prevTool != nullptr && (gbuild != prevTool)) {
  92. std::string message =
  93. cmStrCat("toolset build tool: ", gbuild,
  94. "\nDoes not match the previously used build tool: ", prevTool,
  95. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  96. "directory or choose a different binary directory.");
  97. cmSystemTools::Error(message);
  98. return false;
  99. }
  100. /* store the toolset that is being used for this build */
  101. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", gbuild, "build program to use",
  102. cmStateEnums::INTERNAL, true);
  103. mf->AddDefinition("CMAKE_SYSTEM_VERSION", tsp);
  104. return true;
  105. }
  106. bool cmGlobalGhsMultiGenerator::SetGeneratorPlatform(std::string const& p,
  107. cmMakefile* mf)
  108. {
  109. std::string arch;
  110. if (p.empty()) {
  111. cmSystemTools::Message(
  112. "Green Hills MULTI: -A <arch> not specified; defaulting to \"arm\"");
  113. arch = "arm";
  114. /* store the platform name for later use
  115. * -- already done if -A<arch> was specified
  116. */
  117. mf->AddCacheDefinition("CMAKE_GENERATOR_PLATFORM", arch,
  118. "Name of generator platform.",
  119. cmStateEnums::INTERNAL);
  120. } else {
  121. arch = p;
  122. }
  123. /* check if OS location has been updated by platform scripts */
  124. std::string platform = mf->GetSafeDefinition("GHS_TARGET_PLATFORM");
  125. std::string osdir = mf->GetSafeDefinition("GHS_OS_DIR");
  126. if (cmIsOff(osdir) && platform.find("integrity") != std::string::npos) {
  127. if (!this->CMakeInstance->GetIsInTryCompile()) {
  128. /* required OS location is not found */
  129. std::string m = cmStrCat(
  130. "Green Hills MULTI: GHS_OS_DIR not specified; No OS found in \"",
  131. mf->GetSafeDefinition("GHS_OS_ROOT"), '"');
  132. cmSystemTools::Message(m);
  133. }
  134. osdir = "GHS_OS_DIR-NOT-SPECIFIED";
  135. } else if (!this->CMakeInstance->GetIsInTryCompile() &&
  136. cmIsOff(this->OsDir) && !cmIsOff(osdir)) {
  137. /* OS location was updated by auto-selection */
  138. std::string m = cmStrCat(
  139. "Green Hills MULTI: GHS_OS_DIR not specified; found \"", osdir, '"');
  140. cmSystemTools::Message(m);
  141. }
  142. this->OsDir = osdir;
  143. // Determine GHS_BSP_NAME
  144. std::string bspName = mf->GetSafeDefinition("GHS_BSP_NAME");
  145. if (cmIsOff(bspName) && platform.find("integrity") != std::string::npos) {
  146. bspName = "sim" + arch;
  147. /* write back the calculate name for next time */
  148. mf->AddCacheDefinition("GHS_BSP_NAME", bspName,
  149. "Name of GHS target platform.",
  150. cmStateEnums::STRING, true);
  151. std::string m = cmStrCat(
  152. "Green Hills MULTI: GHS_BSP_NAME not specified; defaulting to \"",
  153. bspName, '"');
  154. cmSystemTools::Message(m);
  155. }
  156. return true;
  157. }
  158. void cmGlobalGhsMultiGenerator::EnableLanguage(
  159. std::vector<std::string> const& l, cmMakefile* mf, bool optional)
  160. {
  161. mf->AddDefinition("CMAKE_SYSTEM_NAME", "GHS-MULTI");
  162. mf->AddDefinition("GHSMULTI", "1"); // identifier for user CMake files
  163. const char* tgtPlatform = mf->GetDefinition("GHS_TARGET_PLATFORM");
  164. if (!tgtPlatform) {
  165. cmSystemTools::Message("Green Hills MULTI: GHS_TARGET_PLATFORM not "
  166. "specified; defaulting to \"integrity\"");
  167. tgtPlatform = "integrity";
  168. }
  169. /* store the platform name for later use */
  170. mf->AddCacheDefinition("GHS_TARGET_PLATFORM", tgtPlatform,
  171. "Name of GHS target platform.", cmStateEnums::STRING);
  172. /* store original OS location */
  173. this->OsDir = mf->GetSafeDefinition("GHS_OS_DIR");
  174. this->cmGlobalGenerator::EnableLanguage(l, mf, optional);
  175. }
  176. bool cmGlobalGhsMultiGenerator::FindMakeProgram(cmMakefile* /*mf*/)
  177. {
  178. // The GHS generator only knows how to lookup its build tool
  179. // during generation of the project files, but this
  180. // can only be done after the toolset is specified.
  181. return true;
  182. }
  183. void cmGlobalGhsMultiGenerator::GetToolset(cmMakefile* mf, std::string& tsd,
  184. const std::string& ts)
  185. {
  186. const char* ghsRoot = mf->GetDefinition("GHS_TOOLSET_ROOT");
  187. if (!ghsRoot || ghsRoot[0] == '\0') {
  188. ghsRoot = DEFAULT_TOOLSET_ROOT;
  189. }
  190. tsd = ghsRoot;
  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. const char* 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. const char* 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() != 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, cmLocalGenerator* root,
  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 = root->MaybeConvertToRelativePath(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() != 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 (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. WriteProjectLine(fbld, tgt, root, 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 (!IsExcluded(t->GetLocalGenerator(), t)) {
  420. defaultTargets.push_back(t);
  421. }
  422. }
  423. std::vector<cmGeneratorTarget const*> build;
  424. if (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, root, 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 (const char* 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. char const* ghsGpjMacros =
  549. this->GetCMakeInstance()->GetCacheDefinition("GHS_GPJ_MACROS");
  550. if (nullptr != ghsGpjMacros) {
  551. std::vector<std::string> expandedList =
  552. cmExpandedList(std::string(ghsGpjMacros));
  553. for (std::string const& arg : expandedList) {
  554. fout << "macro " << arg << '\n';
  555. }
  556. }
  557. }
  558. void cmGlobalGhsMultiGenerator::WriteHighLevelDirectives(
  559. cmLocalGenerator* root, std::ostream& fout)
  560. {
  561. /* set primary target */
  562. std::string tgt;
  563. const char* t =
  564. this->GetCMakeInstance()->GetCacheDefinition("GHS_PRIMARY_TARGET");
  565. if (cmNonempty(t)) {
  566. tgt = t;
  567. this->GetCMakeInstance()->MarkCliAsUsed("GHS_PRIMARY_TARGET");
  568. } else {
  569. const char* a =
  570. this->GetCMakeInstance()->GetCacheDefinition("CMAKE_GENERATOR_PLATFORM");
  571. const char* p =
  572. this->GetCMakeInstance()->GetCacheDefinition("GHS_TARGET_PLATFORM");
  573. tgt = cmStrCat((a ? a : ""), '_', (p ? p : ""), ".tgt");
  574. }
  575. /* clang-format off */
  576. fout << "primaryTarget=" << tgt << "\n"
  577. "customization=" << root->GetBinaryDirectory()
  578. << "/CMakeFiles/custom_rule.bod\n"
  579. "customization=" << root->GetBinaryDirectory()
  580. << "/CMakeFiles/custom_target.bod" << '\n';
  581. /* clang-format on */
  582. char const* const customization =
  583. this->GetCMakeInstance()->GetCacheDefinition("GHS_CUSTOMIZATION");
  584. if (nullptr != customization && strlen(customization) > 0) {
  585. fout << "customization="
  586. << cmGlobalGhsMultiGenerator::TrimQuotes(customization) << '\n';
  587. this->GetCMakeInstance()->MarkCliAsUsed("GHS_CUSTOMIZATION");
  588. }
  589. }
  590. std::string cmGlobalGhsMultiGenerator::TrimQuotes(std::string str)
  591. {
  592. cm::erase(str, '"');
  593. return str;
  594. }
  595. bool cmGlobalGhsMultiGenerator::TargetCompare::operator()(
  596. cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
  597. {
  598. // Make sure a given named target is ordered first,
  599. // e.g. to set ALL_BUILD as the default active project.
  600. // When the empty string is named this is a no-op.
  601. if (r->GetName() == this->First) {
  602. return false;
  603. }
  604. if (l->GetName() == this->First) {
  605. return true;
  606. }
  607. return l->GetName() < r->GetName();
  608. }
  609. cmGlobalGhsMultiGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  610. TargetDependSet const& targets, std::string const& first)
  611. : derived(TargetCompare(first))
  612. {
  613. this->insert(targets.begin(), targets.end());
  614. }
  615. bool cmGlobalGhsMultiGenerator::ComputeTargetBuildOrder(
  616. cmGeneratorTarget const* tgt, std::vector<cmGeneratorTarget const*>& build)
  617. {
  618. std::vector<cmGeneratorTarget const*> t{ tgt };
  619. return ComputeTargetBuildOrder(t, build);
  620. }
  621. bool cmGlobalGhsMultiGenerator::ComputeTargetBuildOrder(
  622. std::vector<cmGeneratorTarget const*>& tgt,
  623. std::vector<cmGeneratorTarget const*>& build)
  624. {
  625. std::set<cmGeneratorTarget const*> temp;
  626. std::set<cmGeneratorTarget const*> perm;
  627. for (auto ti : tgt) {
  628. bool r = VisitTarget(temp, perm, build, ti);
  629. if (r) {
  630. return r;
  631. }
  632. }
  633. return false;
  634. }
  635. bool cmGlobalGhsMultiGenerator::VisitTarget(
  636. std::set<cmGeneratorTarget const*>& temp,
  637. std::set<cmGeneratorTarget const*>& perm,
  638. std::vector<cmGeneratorTarget const*>& order, cmGeneratorTarget const* ti)
  639. {
  640. /* check if permanent mark is set*/
  641. if (perm.find(ti) == perm.end()) {
  642. /* set temporary mark; check if revisit*/
  643. if (temp.insert(ti).second) {
  644. /* sort targets lexicographically to ensure that nodes are always visited
  645. * in the same order */
  646. OrderedTargetDependSet sortedTargets(this->GetTargetDirectDepends(ti),
  647. "");
  648. for (auto& di : sortedTargets) {
  649. if (this->VisitTarget(temp, perm, order, di)) {
  650. return true;
  651. }
  652. }
  653. /* mark as complete; insert into beginning of list*/
  654. perm.insert(ti);
  655. order.push_back(ti);
  656. return false;
  657. }
  658. /* revisiting item - not a DAG */
  659. return true;
  660. }
  661. /* already complete */
  662. return false;
  663. }