cmGlobalGhsMultiGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 "cmDocumentationEntry.h"
  5. #include "cmGeneratedFileStream.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmGhsMultiGpj.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmLocalGhsMultiGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmState.h"
  12. #include "cmStateTypes.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. #include "cmVersion.h"
  16. #include "cmake.h"
  17. #include <algorithm>
  18. #include <map>
  19. #include <ostream>
  20. #include <string.h>
  21. #include <utility>
  22. const char* cmGlobalGhsMultiGenerator::FILE_EXTENSION = ".gpj";
  23. #ifdef __linux__
  24. const char* cmGlobalGhsMultiGenerator::DEFAULT_BUILD_PROGRAM = "gbuild";
  25. const char* cmGlobalGhsMultiGenerator::DEFAULT_TOOLSET_ROOT = "/usr/ghs";
  26. #elif defined(_WIN32)
  27. const char* cmGlobalGhsMultiGenerator::DEFAULT_BUILD_PROGRAM = "gbuild.exe";
  28. const char* cmGlobalGhsMultiGenerator::DEFAULT_TOOLSET_ROOT = "C:/ghs";
  29. #endif
  30. cmGlobalGhsMultiGenerator::cmGlobalGhsMultiGenerator(cmake* cm)
  31. : cmGlobalGenerator(cm)
  32. {
  33. cm->GetState()->SetGhsMultiIDE(true);
  34. }
  35. cmGlobalGhsMultiGenerator::~cmGlobalGhsMultiGenerator() = default;
  36. cmLocalGenerator* cmGlobalGhsMultiGenerator::CreateLocalGenerator(
  37. cmMakefile* mf)
  38. {
  39. return new cmLocalGhsMultiGenerator(this, mf);
  40. }
  41. void cmGlobalGhsMultiGenerator::GetDocumentation(cmDocumentationEntry& entry)
  42. {
  43. entry.Name = GetActualName();
  44. entry.Brief =
  45. "Generates Green Hills MULTI files (experimental, work-in-progress).";
  46. }
  47. void cmGlobalGhsMultiGenerator::ComputeTargetObjectDirectory(
  48. cmGeneratorTarget* gt) const
  49. {
  50. // Compute full path to object file directory for this target.
  51. std::string dir;
  52. dir += gt->LocalGenerator->GetCurrentBinaryDirectory();
  53. dir += "/";
  54. dir += gt->LocalGenerator->GetTargetDirectory(gt);
  55. dir += "/";
  56. gt->ObjectDirectory = dir;
  57. }
  58. bool cmGlobalGhsMultiGenerator::SetGeneratorToolset(std::string const& ts,
  59. cmMakefile* mf)
  60. {
  61. std::string tsp; /* toolset path */
  62. this->GetToolset(mf, tsp, ts);
  63. /* no toolset was found */
  64. if (tsp.empty()) {
  65. return false;
  66. }
  67. if (ts.empty()) {
  68. std::string message;
  69. message =
  70. "Green Hills MULTI: -T <toolset> not specified; defaulting to \"";
  71. message += tsp;
  72. message += "\"";
  73. cmSystemTools::Message(message);
  74. /* store the full toolset for later use
  75. * -- already done if -T<toolset> was specified
  76. */
  77. mf->AddCacheDefinition("CMAKE_GENERATOR_TOOLSET", tsp.c_str(),
  78. "Location of generator toolset.",
  79. cmStateEnums::INTERNAL);
  80. }
  81. /* set the build tool to use */
  82. std::string gbuild(tsp + ((tsp.back() == '/') ? "" : "/") +
  83. DEFAULT_BUILD_PROGRAM);
  84. const char* prevTool = mf->GetDefinition("CMAKE_MAKE_PROGRAM");
  85. /* check if the toolset changed from last generate */
  86. if (prevTool != nullptr && (gbuild != prevTool)) {
  87. std::string message = "toolset build tool: ";
  88. message += gbuild;
  89. message += "\nDoes not match the previously used build tool: ";
  90. message += prevTool;
  91. message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
  92. "directory or choose a different binary directory.";
  93. cmSystemTools::Error(message);
  94. return false;
  95. }
  96. /* store the toolset that is being used for this build */
  97. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", gbuild.c_str(),
  98. "build program to use", cmStateEnums::INTERNAL, true);
  99. mf->AddDefinition("CMAKE_SYSTEM_VERSION", tsp);
  100. return true;
  101. }
  102. bool cmGlobalGhsMultiGenerator::SetGeneratorPlatform(std::string const& p,
  103. cmMakefile* mf)
  104. {
  105. std::string arch;
  106. if (p.empty()) {
  107. cmSystemTools::Message(
  108. "Green Hills MULTI: -A <arch> not specified; defaulting to \"arm\"");
  109. arch = "arm";
  110. /* store the platform name for later use
  111. * -- already done if -A<arch> was specified
  112. */
  113. mf->AddCacheDefinition("CMAKE_GENERATOR_PLATFORM", arch.c_str(),
  114. "Name of generator platform.",
  115. cmStateEnums::INTERNAL);
  116. } else {
  117. arch = p;
  118. }
  119. /* check if OS location has been updated by platform scripts */
  120. std::string platform = mf->GetSafeDefinition("GHS_TARGET_PLATFORM");
  121. std::string osdir = mf->GetSafeDefinition("GHS_OS_DIR");
  122. if (cmIsOff(osdir) && platform.find("integrity") != std::string::npos) {
  123. if (!this->CMakeInstance->GetIsInTryCompile()) {
  124. /* required OS location is not found */
  125. std::string m =
  126. "Green Hills MULTI: GHS_OS_DIR not specified; No OS found in \"";
  127. m += mf->GetSafeDefinition("GHS_OS_ROOT");
  128. m += "\"";
  129. cmSystemTools::Message(m);
  130. }
  131. osdir = "GHS_OS_DIR-NOT-SPECIFIED";
  132. } else if (!this->CMakeInstance->GetIsInTryCompile() &&
  133. cmIsOff(this->OsDir) && !cmIsOff(osdir)) {
  134. /* OS location was updated by auto-selection */
  135. std::string m = "Green Hills MULTI: GHS_OS_DIR not specified; found \"";
  136. m += osdir;
  137. m += "\"";
  138. cmSystemTools::Message(m);
  139. }
  140. this->OsDir = osdir;
  141. // Determine GHS_BSP_NAME
  142. std::string bspName = mf->GetSafeDefinition("GHS_BSP_NAME");
  143. if (cmIsOff(bspName) && platform.find("integrity") != std::string::npos) {
  144. bspName = "sim" + arch;
  145. /* write back the calculate name for next time */
  146. mf->AddCacheDefinition("GHS_BSP_NAME", bspName.c_str(),
  147. "Name of GHS target platform.",
  148. cmStateEnums::STRING, true);
  149. std::string m =
  150. "Green Hills MULTI: GHS_BSP_NAME not specified; defaulting to \"";
  151. m += bspName;
  152. m += "\"";
  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 = root->GetCurrentBinaryDirectory();
  358. rootBinaryDir += "/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 = root->GetCurrentBinaryDirectory();
  429. rootBinaryDir += "/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 = root->GetCurrentBinaryDirectory();
  480. fname += "/";
  481. fname += root->GetProjectName();
  482. fname += ".top";
  483. fname += 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. auto p = std::find(files.begin(), files.end(), proj);
  522. if (p == files.end()) {
  523. proj = files.at(0);
  524. }
  525. }
  526. makeCommand.Add("-top", proj);
  527. if (!targetNames.empty()) {
  528. if (std::find(targetNames.begin(), targetNames.end(), "clean") !=
  529. targetNames.end()) {
  530. makeCommand.Add("-clean");
  531. } else {
  532. for (const auto& tname : targetNames) {
  533. if (!tname.empty()) {
  534. makeCommand.Add(tname + ".tgt.gpj");
  535. }
  536. }
  537. }
  538. } else {
  539. /* transform name to default build */;
  540. std::string all = proj;
  541. all.replace(all.end() - 7, all.end(),
  542. std::string(this->GetAllTargetName()) + ".tgt.gpj");
  543. makeCommand.Add(all);
  544. }
  545. return { makeCommand };
  546. }
  547. void cmGlobalGhsMultiGenerator::WriteMacros(std::ostream& fout,
  548. cmLocalGenerator* root)
  549. {
  550. fout << "macro PROJ_NAME=" << root->GetProjectName() << std::endl;
  551. char const* ghsGpjMacros =
  552. this->GetCMakeInstance()->GetCacheDefinition("GHS_GPJ_MACROS");
  553. if (nullptr != ghsGpjMacros) {
  554. std::vector<std::string> expandedList;
  555. cmExpandList(std::string(ghsGpjMacros), expandedList);
  556. for (std::string const& arg : expandedList) {
  557. fout << "macro " << arg << std::endl;
  558. }
  559. }
  560. }
  561. void cmGlobalGhsMultiGenerator::WriteHighLevelDirectives(
  562. cmLocalGenerator* root, std::ostream& fout)
  563. {
  564. /* set primary target */
  565. std::string tgt;
  566. const char* t =
  567. this->GetCMakeInstance()->GetCacheDefinition("GHS_PRIMARY_TARGET");
  568. if (t && *t != '\0') {
  569. tgt = t;
  570. this->GetCMakeInstance()->MarkCliAsUsed("GHS_PRIMARY_TARGET");
  571. } else {
  572. const char* a =
  573. this->GetCMakeInstance()->GetCacheDefinition("CMAKE_GENERATOR_PLATFORM");
  574. const char* p =
  575. this->GetCMakeInstance()->GetCacheDefinition("GHS_TARGET_PLATFORM");
  576. tgt = (a ? a : "");
  577. tgt += "_";
  578. tgt += (p ? p : "");
  579. tgt += ".tgt";
  580. }
  581. fout << "primaryTarget=" << tgt << std::endl;
  582. fout << "customization=" << root->GetBinaryDirectory()
  583. << "/CMakeFiles/custom_rule.bod" << std::endl;
  584. fout << "customization=" << root->GetBinaryDirectory()
  585. << "/CMakeFiles/custom_target.bod" << std::endl;
  586. char const* const customization =
  587. this->GetCMakeInstance()->GetCacheDefinition("GHS_CUSTOMIZATION");
  588. if (nullptr != customization && strlen(customization) > 0) {
  589. fout << "customization=" << this->TrimQuotes(customization) << std::endl;
  590. this->GetCMakeInstance()->MarkCliAsUsed("GHS_CUSTOMIZATION");
  591. }
  592. }
  593. std::string cmGlobalGhsMultiGenerator::TrimQuotes(std::string const& str)
  594. {
  595. std::string result;
  596. result.reserve(str.size());
  597. for (const char* ch = str.c_str(); *ch != '\0'; ++ch) {
  598. if (*ch != '"') {
  599. result += *ch;
  600. }
  601. }
  602. return result;
  603. }
  604. bool cmGlobalGhsMultiGenerator::TargetCompare::operator()(
  605. cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
  606. {
  607. // Make sure a given named target is ordered first,
  608. // e.g. to set ALL_BUILD as the default active project.
  609. // When the empty string is named this is a no-op.
  610. if (r->GetName() == this->First) {
  611. return false;
  612. }
  613. if (l->GetName() == this->First) {
  614. return true;
  615. }
  616. return l->GetName() < r->GetName();
  617. }
  618. cmGlobalGhsMultiGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  619. TargetDependSet const& targets, std::string const& first)
  620. : derived(TargetCompare(first))
  621. {
  622. this->insert(targets.begin(), targets.end());
  623. }
  624. bool cmGlobalGhsMultiGenerator::ComputeTargetBuildOrder(
  625. cmGeneratorTarget const* tgt, std::vector<cmGeneratorTarget const*>& build)
  626. {
  627. std::vector<cmGeneratorTarget const*> t{ tgt };
  628. return ComputeTargetBuildOrder(t, build);
  629. }
  630. bool cmGlobalGhsMultiGenerator::ComputeTargetBuildOrder(
  631. std::vector<cmGeneratorTarget const*>& tgt,
  632. std::vector<cmGeneratorTarget const*>& build)
  633. {
  634. std::set<cmGeneratorTarget const*> temp;
  635. std::set<cmGeneratorTarget const*> perm;
  636. for (auto ti : tgt) {
  637. bool r = VisitTarget(temp, perm, build, ti);
  638. if (r) {
  639. return r;
  640. }
  641. }
  642. return false;
  643. }
  644. bool cmGlobalGhsMultiGenerator::VisitTarget(
  645. std::set<cmGeneratorTarget const*>& temp,
  646. std::set<cmGeneratorTarget const*>& perm,
  647. std::vector<cmGeneratorTarget const*>& order, cmGeneratorTarget const* ti)
  648. {
  649. /* check if permanent mark is set*/
  650. if (perm.find(ti) == perm.end()) {
  651. /* set temporary mark; check if revisit*/
  652. if (temp.insert(ti).second) {
  653. /* sort targets lexicographically to ensure that nodes are always visited
  654. * in the same order */
  655. OrderedTargetDependSet sortedTargets(this->GetTargetDirectDepends(ti),
  656. "");
  657. for (auto& di : sortedTargets) {
  658. if (this->VisitTarget(temp, perm, order, di)) {
  659. return true;
  660. }
  661. }
  662. /* mark as complete; insert into beginning of list*/
  663. perm.insert(ti);
  664. order.push_back(ti);
  665. return false;
  666. }
  667. /* revisiting item - not a DAG */
  668. return true;
  669. }
  670. /* already complete */
  671. return false;
  672. }