cmGlobalGhsMultiGenerator.cxx 24 KB

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