cmGlobalGhsMultiGenerator.cxx 23 KB

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