cmGlobalGhsMultiGenerator.cxx 23 KB

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