cmGlobalGhsMultiGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 (cmNonempty(ghsRoot)) {
  188. tsd = ghsRoot;
  189. } else {
  190. tsd = DEFAULT_TOOLSET_ROOT;
  191. }
  192. if (ts.empty()) {
  193. std::vector<std::string> output;
  194. // Use latest? version
  195. if (tsd.back() != '/') {
  196. tsd += "/";
  197. }
  198. cmSystemTools::Glob(tsd, "comp_[^;]+", output);
  199. if (output.empty()) {
  200. std::string msg =
  201. "No GHS toolsets found in GHS_TOOLSET_ROOT \"" + tsd + "\".";
  202. cmSystemTools::Error(msg);
  203. tsd = "";
  204. } else {
  205. tsd += output.back();
  206. }
  207. } else {
  208. std::string tryPath;
  209. tryPath = cmSystemTools::CollapseFullPath(ts, tsd);
  210. if (!cmSystemTools::FileExists(tryPath)) {
  211. std::string msg = "GHS toolset \"" + tryPath + "\" not found.";
  212. cmSystemTools::Error(msg);
  213. tsd = "";
  214. } else {
  215. tsd = tryPath;
  216. }
  217. }
  218. }
  219. void cmGlobalGhsMultiGenerator::WriteFileHeader(std::ostream& fout)
  220. {
  221. /* clang-format off */
  222. fout << "#!gbuild\n"
  223. "#\n"
  224. "# CMAKE generated file: DO NOT EDIT!\n"
  225. "# Generated by \"" << GetActualName() << "\""
  226. " Generator, CMake Version " << cmVersion::GetMajorVersion() << '.'
  227. << cmVersion::GetMinorVersion() << "\n"
  228. "#\n\n";
  229. /* clang-format on */
  230. }
  231. void cmGlobalGhsMultiGenerator::WriteCustomRuleBOD(std::ostream& fout)
  232. {
  233. fout << "Commands {\n"
  234. " Custom_Rule_Command {\n"
  235. " name = \"Custom Rule Command\"\n"
  236. " exec = \""
  237. #ifdef _WIN32
  238. "cmd.exe"
  239. #else
  240. "/bin/sh"
  241. #endif
  242. "\"\n"
  243. " options = {\"SpecialOptions\"}\n"
  244. " }\n"
  245. "}\n"
  246. "\n\n"
  247. "FileTypes {\n"
  248. " CmakeRule {\n"
  249. " name = \"Custom Rule\"\n"
  250. " action = \"&Run\"\n"
  251. " extensions = {\""
  252. #ifdef _WIN32
  253. "bat"
  254. #else
  255. "sh"
  256. #endif
  257. "\"}\n"
  258. " grepable = false\n"
  259. " command = \"Custom Rule Command\"\n"
  260. " commandLine = \"$COMMAND "
  261. #ifdef _WIN32
  262. "/c"
  263. #endif
  264. " $INPUTFILE\"\n"
  265. " progress = \"Processing Custom Rule\"\n"
  266. " promoteToFirstPass = true\n"
  267. " outputType = \"None\"\n"
  268. " color = \"#800080\"\n"
  269. " }\n"
  270. "}\n";
  271. }
  272. void cmGlobalGhsMultiGenerator::WriteCustomTargetBOD(std::ostream& fout)
  273. {
  274. fout << "FileTypes {\n"
  275. " CmakeTarget {\n"
  276. " name = \"Custom Target\"\n"
  277. " action = \"&Execute\"\n"
  278. " grepable = false\n"
  279. " outputType = \"None\"\n"
  280. " color = \"#800080\"\n"
  281. " }\n"
  282. "}\n";
  283. }
  284. void cmGlobalGhsMultiGenerator::WriteTopLevelProject(std::ostream& fout,
  285. cmLocalGenerator* root)
  286. {
  287. this->WriteFileHeader(fout);
  288. this->WriteMacros(fout, root);
  289. this->WriteHighLevelDirectives(root, fout);
  290. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fout);
  291. fout << "# Top Level Project File\n";
  292. // Specify BSP option if supplied by user
  293. const char* bspName =
  294. this->GetCMakeInstance()->GetCacheDefinition("GHS_BSP_NAME");
  295. if (!cmIsOff(bspName)) {
  296. fout << " -bsp " << bspName << '\n';
  297. }
  298. // Specify OS DIR if supplied by user
  299. // -- not all platforms require this entry in the project file
  300. if (!cmIsOff(this->OsDir)) {
  301. const char* osDirOption =
  302. this->GetCMakeInstance()->GetCacheDefinition("GHS_OS_DIR_OPTION");
  303. std::replace(this->OsDir.begin(), this->OsDir.end(), '\\', '/');
  304. fout << " ";
  305. if (cmIsOff(osDirOption)) {
  306. fout << "";
  307. } else {
  308. fout << osDirOption;
  309. }
  310. fout << "\"" << this->OsDir << "\"\n";
  311. }
  312. }
  313. void cmGlobalGhsMultiGenerator::WriteSubProjects(std::ostream& fout,
  314. std::string& all_target)
  315. {
  316. fout << "CMakeFiles/" << all_target << " [Project]\n";
  317. // All known targets
  318. for (cmGeneratorTarget const* target : this->ProjectTargets) {
  319. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  320. target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  321. target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  322. (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
  323. target->GetName() != GetInstallTargetName())) {
  324. continue;
  325. }
  326. fout << "CMakeFiles/" << target->GetName() + ".tgt" + FILE_EXTENSION
  327. << " [Project]\n";
  328. }
  329. }
  330. void cmGlobalGhsMultiGenerator::WriteProjectLine(
  331. std::ostream& fout, cmGeneratorTarget const* target, cmLocalGenerator* root,
  332. std::string& rootBinaryDir)
  333. {
  334. cmProp projName = target->GetProperty("GENERATOR_FILE_NAME");
  335. cmProp projType = target->GetProperty("GENERATOR_FILE_NAME_EXT");
  336. if (projName && projType) {
  337. cmLocalGenerator* lg = target->GetLocalGenerator();
  338. std::string dir = lg->GetCurrentBinaryDirectory();
  339. dir = root->MaybeConvertToRelativePath(rootBinaryDir, dir);
  340. if (dir == ".") {
  341. dir.clear();
  342. } else {
  343. if (dir.back() != '/') {
  344. dir += "/";
  345. }
  346. }
  347. std::string projFile = dir + *projName + FILE_EXTENSION;
  348. fout << projFile;
  349. fout << ' ' << *projType << '\n';
  350. } else {
  351. /* Should never happen */
  352. std::string message =
  353. "The project file for target [" + target->GetName() + "] is missing.\n";
  354. cmSystemTools::Error(message);
  355. fout << "{comment} " << target->GetName() << " [missing project file]\n";
  356. }
  357. }
  358. void cmGlobalGhsMultiGenerator::WriteTargets(cmLocalGenerator* root)
  359. {
  360. std::string rootBinaryDir =
  361. cmStrCat(root->GetCurrentBinaryDirectory(), "/CMakeFiles");
  362. // All known targets
  363. for (cmGeneratorTarget const* target : this->ProjectTargets) {
  364. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  365. target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  366. target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  367. (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
  368. target->GetName() != GetInstallTargetName())) {
  369. continue;
  370. }
  371. // create target build file
  372. std::string name = cmStrCat(target->GetName(), ".tgt", FILE_EXTENSION);
  373. std::string fname = cmStrCat(rootBinaryDir, "/", name);
  374. cmGeneratedFileStream fbld(fname);
  375. fbld.SetCopyIfDifferent(true);
  376. this->WriteFileHeader(fbld);
  377. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fbld);
  378. std::vector<cmGeneratorTarget const*> build;
  379. if (ComputeTargetBuildOrder(target, build)) {
  380. cmSystemTools::Error(
  381. cmStrCat("The inter-target dependency graph for target [",
  382. target->GetName(), "] had a cycle.\n"));
  383. } else {
  384. for (auto& tgt : build) {
  385. WriteProjectLine(fbld, tgt, root, rootBinaryDir);
  386. }
  387. }
  388. fbld.Close();
  389. }
  390. }
  391. void cmGlobalGhsMultiGenerator::WriteAllTarget(
  392. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators,
  393. std::string& all_target)
  394. {
  395. this->ProjectTargets.clear();
  396. // create target build file
  397. all_target = root->GetProjectName() + "." + this->GetAllTargetName() +
  398. ".tgt" + FILE_EXTENSION;
  399. std::string fname =
  400. root->GetCurrentBinaryDirectory() + "/CMakeFiles/" + all_target;
  401. cmGeneratedFileStream fbld(fname);
  402. fbld.SetCopyIfDifferent(true);
  403. this->WriteFileHeader(fbld);
  404. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fbld);
  405. // Collect all targets under this root generator and the transitive
  406. // closure of their dependencies.
  407. TargetDependSet projectTargets;
  408. TargetDependSet originalTargets;
  409. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  410. OrderedTargetDependSet sortedProjectTargets(projectTargets, "");
  411. std::vector<cmGeneratorTarget const*> defaultTargets;
  412. for (cmGeneratorTarget const* t : sortedProjectTargets) {
  413. /* save list of all targets in sorted order */
  414. this->ProjectTargets.push_back(t);
  415. }
  416. for (cmGeneratorTarget const* t : sortedProjectTargets) {
  417. if (!t->IsInBuildSystem()) {
  418. continue;
  419. }
  420. if (!IsExcluded(t->GetLocalGenerator(), t)) {
  421. defaultTargets.push_back(t);
  422. }
  423. }
  424. std::vector<cmGeneratorTarget const*> build;
  425. if (ComputeTargetBuildOrder(defaultTargets, build)) {
  426. std::string message = "The inter-target dependency graph for project [" +
  427. root->GetProjectName() + "] had a cycle.\n";
  428. cmSystemTools::Error(message);
  429. } else {
  430. // determine the targets for ALL target
  431. std::string rootBinaryDir =
  432. cmStrCat(root->GetCurrentBinaryDirectory(), "/CMakeFiles");
  433. for (cmGeneratorTarget const* target : build) {
  434. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  435. target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  436. target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  437. continue;
  438. }
  439. this->WriteProjectLine(fbld, target, root, rootBinaryDir);
  440. }
  441. }
  442. fbld.Close();
  443. }
  444. void cmGlobalGhsMultiGenerator::Generate()
  445. {
  446. std::string fname;
  447. // first do the superclass method
  448. this->cmGlobalGenerator::Generate();
  449. // output top-level projects
  450. for (auto& it : this->ProjectMap) {
  451. this->OutputTopLevelProject(it.second[0], it.second);
  452. }
  453. // create custom rule BOD file
  454. fname = this->GetCMakeInstance()->GetHomeOutputDirectory() +
  455. "/CMakeFiles/custom_rule.bod";
  456. cmGeneratedFileStream frule(fname);
  457. frule.SetCopyIfDifferent(true);
  458. this->WriteFileHeader(frule);
  459. this->WriteCustomRuleBOD(frule);
  460. frule.Close();
  461. // create custom target BOD file
  462. fname = this->GetCMakeInstance()->GetHomeOutputDirectory() +
  463. "/CMakeFiles/custom_target.bod";
  464. cmGeneratedFileStream ftarget(fname);
  465. ftarget.SetCopyIfDifferent(true);
  466. this->WriteFileHeader(ftarget);
  467. this->WriteCustomTargetBOD(ftarget);
  468. ftarget.Close();
  469. }
  470. void cmGlobalGhsMultiGenerator::OutputTopLevelProject(
  471. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  472. {
  473. std::string fname;
  474. std::string all_target;
  475. if (generators.empty()) {
  476. return;
  477. }
  478. /* Name top-level projects as filename.top.gpj to avoid name clashes
  479. * with target projects. This avoid the issue where the project has
  480. * the same name as the executable target.
  481. */
  482. fname = cmStrCat(root->GetCurrentBinaryDirectory(), '/',
  483. root->GetProjectName(), ".top", FILE_EXTENSION);
  484. cmGeneratedFileStream top(fname);
  485. top.SetCopyIfDifferent(true);
  486. this->WriteTopLevelProject(top, root);
  487. this->WriteAllTarget(root, generators, all_target);
  488. this->WriteTargets(root);
  489. this->WriteSubProjects(top, all_target);
  490. top.Close();
  491. }
  492. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  493. cmGlobalGhsMultiGenerator::GenerateBuildCommand(
  494. const std::string& makeProgram, const std::string& projectName,
  495. const std::string& projectDir, std::vector<std::string> const& targetNames,
  496. const std::string& /*config*/, bool /*fast*/, int jobs, bool /*verbose*/,
  497. std::vector<std::string> const& makeOptions)
  498. {
  499. GeneratedMakeCommand makeCommand = {};
  500. std::string gbuild;
  501. if (const char* gbuildCached =
  502. this->CMakeInstance->GetCacheDefinition("CMAKE_MAKE_PROGRAM")) {
  503. gbuild = gbuildCached;
  504. }
  505. makeCommand.Add(this->SelectMakeProgram(makeProgram, gbuild));
  506. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  507. makeCommand.Add("-parallel");
  508. if (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  509. makeCommand.Add(std::to_string(jobs));
  510. }
  511. }
  512. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  513. /* determine which top-project file to use */
  514. std::string proj = projectName + ".top" + FILE_EXTENSION;
  515. std::vector<std::string> files;
  516. cmSystemTools::Glob(projectDir, ".*\\.top\\.gpj", files);
  517. if (!files.empty()) {
  518. /* if multiple top-projects are found in build directory
  519. * then prefer projectName top-project.
  520. */
  521. if (!cm::contains(files, proj)) {
  522. proj = files.at(0);
  523. }
  524. }
  525. makeCommand.Add("-top", proj);
  526. if (!targetNames.empty()) {
  527. if (cm::contains(targetNames, "clean")) {
  528. makeCommand.Add("-clean");
  529. } else {
  530. for (const auto& tname : targetNames) {
  531. if (!tname.empty()) {
  532. makeCommand.Add(tname + ".tgt.gpj");
  533. }
  534. }
  535. }
  536. } else {
  537. /* transform name to default build */;
  538. std::string all = proj;
  539. all.replace(all.end() - 7, all.end(),
  540. std::string(this->GetAllTargetName()) + ".tgt.gpj");
  541. makeCommand.Add(all);
  542. }
  543. return { makeCommand };
  544. }
  545. void cmGlobalGhsMultiGenerator::WriteMacros(std::ostream& fout,
  546. cmLocalGenerator* root)
  547. {
  548. fout << "macro PROJ_NAME=" << root->GetProjectName() << '\n';
  549. char const* ghsGpjMacros =
  550. this->GetCMakeInstance()->GetCacheDefinition("GHS_GPJ_MACROS");
  551. if (nullptr != ghsGpjMacros) {
  552. std::vector<std::string> expandedList =
  553. cmExpandedList(std::string(ghsGpjMacros));
  554. for (std::string const& arg : expandedList) {
  555. fout << "macro " << arg << '\n';
  556. }
  557. }
  558. }
  559. void cmGlobalGhsMultiGenerator::WriteHighLevelDirectives(
  560. cmLocalGenerator* root, std::ostream& fout)
  561. {
  562. /* set primary target */
  563. std::string tgt;
  564. const char* t =
  565. this->GetCMakeInstance()->GetCacheDefinition("GHS_PRIMARY_TARGET");
  566. if (cmNonempty(t)) {
  567. tgt = t;
  568. this->GetCMakeInstance()->MarkCliAsUsed("GHS_PRIMARY_TARGET");
  569. } else {
  570. const char* a =
  571. this->GetCMakeInstance()->GetCacheDefinition("CMAKE_GENERATOR_PLATFORM");
  572. const char* p =
  573. this->GetCMakeInstance()->GetCacheDefinition("GHS_TARGET_PLATFORM");
  574. tgt = cmStrCat((a ? a : ""), '_', (p ? p : ""), ".tgt");
  575. }
  576. /* clang-format off */
  577. fout << "primaryTarget=" << tgt << "\n"
  578. "customization=" << root->GetBinaryDirectory()
  579. << "/CMakeFiles/custom_rule.bod\n"
  580. "customization=" << root->GetBinaryDirectory()
  581. << "/CMakeFiles/custom_target.bod" << '\n';
  582. /* clang-format on */
  583. char const* const customization =
  584. this->GetCMakeInstance()->GetCacheDefinition("GHS_CUSTOMIZATION");
  585. if (nullptr != customization && strlen(customization) > 0) {
  586. fout << "customization="
  587. << cmGlobalGhsMultiGenerator::TrimQuotes(customization) << '\n';
  588. this->GetCMakeInstance()->MarkCliAsUsed("GHS_CUSTOMIZATION");
  589. }
  590. }
  591. std::string cmGlobalGhsMultiGenerator::TrimQuotes(std::string str)
  592. {
  593. cm::erase(str, '"');
  594. return str;
  595. }
  596. bool cmGlobalGhsMultiGenerator::TargetCompare::operator()(
  597. cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
  598. {
  599. // Make sure a given named target is ordered first,
  600. // e.g. to set ALL_BUILD as the default active project.
  601. // When the empty string is named this is a no-op.
  602. if (r->GetName() == this->First) {
  603. return false;
  604. }
  605. if (l->GetName() == this->First) {
  606. return true;
  607. }
  608. return l->GetName() < r->GetName();
  609. }
  610. cmGlobalGhsMultiGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  611. TargetDependSet const& targets, std::string const& first)
  612. : derived(TargetCompare(first))
  613. {
  614. this->insert(targets.begin(), targets.end());
  615. }
  616. bool cmGlobalGhsMultiGenerator::ComputeTargetBuildOrder(
  617. cmGeneratorTarget const* tgt, std::vector<cmGeneratorTarget const*>& build)
  618. {
  619. std::vector<cmGeneratorTarget const*> t{ tgt };
  620. return ComputeTargetBuildOrder(t, build);
  621. }
  622. bool cmGlobalGhsMultiGenerator::ComputeTargetBuildOrder(
  623. std::vector<cmGeneratorTarget const*>& tgt,
  624. std::vector<cmGeneratorTarget const*>& build)
  625. {
  626. std::set<cmGeneratorTarget const*> temp;
  627. std::set<cmGeneratorTarget const*> perm;
  628. for (auto ti : tgt) {
  629. bool r = VisitTarget(temp, perm, build, ti);
  630. if (r) {
  631. return r;
  632. }
  633. }
  634. return false;
  635. }
  636. bool cmGlobalGhsMultiGenerator::VisitTarget(
  637. std::set<cmGeneratorTarget const*>& temp,
  638. std::set<cmGeneratorTarget const*>& perm,
  639. std::vector<cmGeneratorTarget const*>& order, cmGeneratorTarget const* ti)
  640. {
  641. /* check if permanent mark is set*/
  642. if (perm.find(ti) == perm.end()) {
  643. /* set temporary mark; check if revisit*/
  644. if (temp.insert(ti).second) {
  645. /* sort targets lexicographically to ensure that nodes are always visited
  646. * in the same order */
  647. OrderedTargetDependSet sortedTargets(this->GetTargetDirectDepends(ti),
  648. "");
  649. for (auto& di : sortedTargets) {
  650. if (this->VisitTarget(temp, perm, order, di)) {
  651. return true;
  652. }
  653. }
  654. /* mark as complete; insert into beginning of list*/
  655. perm.insert(ti);
  656. order.push_back(ti);
  657. return false;
  658. }
  659. /* revisiting item - not a DAG */
  660. return true;
  661. }
  662. /* already complete */
  663. return false;
  664. }