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 "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 "cmSystemTools.h"
  14. #include "cmVersion.h"
  15. #include "cmake.h"
  16. #include <algorithm>
  17. #include <map>
  18. #include <ostream>
  19. #include <string.h>
  20. #include <utility>
  21. const char* cmGlobalGhsMultiGenerator::FILE_EXTENSION = ".gpj";
  22. #ifdef __linux__
  23. const char* cmGlobalGhsMultiGenerator::DEFAULT_BUILD_PROGRAM = "gbuild";
  24. const char* cmGlobalGhsMultiGenerator::DEFAULT_TOOLSET_ROOT = "/usr/ghs";
  25. #elif defined(_WIN32)
  26. const char* cmGlobalGhsMultiGenerator::DEFAULT_BUILD_PROGRAM = "gbuild.exe";
  27. const char* cmGlobalGhsMultiGenerator::DEFAULT_TOOLSET_ROOT = "C:/ghs";
  28. #endif
  29. cmGlobalGhsMultiGenerator::cmGlobalGhsMultiGenerator(cmake* cm)
  30. : cmGlobalGenerator(cm)
  31. {
  32. cm->GetState()->SetGhsMultiIDE(true);
  33. }
  34. cmGlobalGhsMultiGenerator::~cmGlobalGhsMultiGenerator() = default;
  35. cmLocalGenerator* cmGlobalGhsMultiGenerator::CreateLocalGenerator(
  36. cmMakefile* mf)
  37. {
  38. return new cmLocalGhsMultiGenerator(this, mf);
  39. }
  40. void cmGlobalGhsMultiGenerator::GetDocumentation(cmDocumentationEntry& entry)
  41. {
  42. entry.Name = GetActualName();
  43. entry.Brief =
  44. "Generates Green Hills MULTI files (experimental, work-in-progress).";
  45. }
  46. void cmGlobalGhsMultiGenerator::ComputeTargetObjectDirectory(
  47. cmGeneratorTarget* gt) const
  48. {
  49. // Compute full path to object file directory for this target.
  50. std::string dir;
  51. dir += gt->LocalGenerator->GetCurrentBinaryDirectory();
  52. dir += "/";
  53. dir += gt->LocalGenerator->GetTargetDirectory(gt);
  54. dir += "/";
  55. gt->ObjectDirectory = dir;
  56. }
  57. bool cmGlobalGhsMultiGenerator::SetGeneratorToolset(std::string const& ts,
  58. cmMakefile* mf)
  59. {
  60. std::string tsp; /* toolset path */
  61. this->GetToolset(mf, tsp, ts);
  62. /* no toolset was found */
  63. if (tsp.empty()) {
  64. return false;
  65. }
  66. if (ts.empty()) {
  67. std::string message;
  68. message =
  69. "Green Hills MULTI: -T <toolset> not specified; defaulting to \"";
  70. message += tsp;
  71. message += "\"";
  72. cmSystemTools::Message(message);
  73. /* store the full toolset for later use
  74. * -- already done if -T<toolset> was specified
  75. */
  76. mf->AddCacheDefinition("CMAKE_GENERATOR_TOOLSET", tsp.c_str(),
  77. "Location of generator toolset.",
  78. cmStateEnums::INTERNAL);
  79. }
  80. /* set the build tool to use */
  81. std::string gbuild(tsp + ((tsp.back() == '/') ? "" : "/") +
  82. DEFAULT_BUILD_PROGRAM);
  83. const char* prevTool = mf->GetDefinition("CMAKE_MAKE_PROGRAM");
  84. /* check if the toolset changed from last generate */
  85. if (prevTool != nullptr && (gbuild != prevTool)) {
  86. std::string message = "toolset build tool: ";
  87. message += gbuild;
  88. message += "\nDoes not match the previously used build tool: ";
  89. message += prevTool;
  90. message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
  91. "directory or choose a different binary directory.";
  92. cmSystemTools::Error(message);
  93. return false;
  94. }
  95. /* store the toolset that is being used for this build */
  96. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", gbuild.c_str(),
  97. "build program to use", cmStateEnums::INTERNAL, true);
  98. mf->AddDefinition("CMAKE_SYSTEM_VERSION", tsp);
  99. return true;
  100. }
  101. bool cmGlobalGhsMultiGenerator::SetGeneratorPlatform(std::string const& p,
  102. cmMakefile* mf)
  103. {
  104. std::string arch;
  105. if (p.empty()) {
  106. cmSystemTools::Message(
  107. "Green Hills MULTI: -A <arch> not specified; defaulting to \"arm\"");
  108. arch = "arm";
  109. /* store the platform name for later use
  110. * -- already done if -A<arch> was specified
  111. */
  112. mf->AddCacheDefinition("CMAKE_GENERATOR_PLATFORM", arch.c_str(),
  113. "Name of generator platform.",
  114. cmStateEnums::INTERNAL);
  115. } else {
  116. arch = p;
  117. }
  118. /* check if OS location has been updated by platform scripts */
  119. std::string platform = mf->GetSafeDefinition("GHS_TARGET_PLATFORM");
  120. std::string osdir = mf->GetSafeDefinition("GHS_OS_DIR");
  121. if (cmSystemTools::IsOff(osdir.c_str()) &&
  122. 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. cmSystemTools::IsOff(this->OsDir) &&
  134. !cmSystemTools::IsOff(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 (cmSystemTools::IsOff(bspName.c_str()) &&
  145. platform.find("integrity") != std::string::npos) {
  146. bspName = "sim" + arch;
  147. /* write back the calculate name for next time */
  148. mf->AddCacheDefinition("GHS_BSP_NAME", bspName.c_str(),
  149. "Name of GHS target platform.",
  150. cmStateEnums::STRING, true);
  151. std::string m =
  152. "Green Hills MULTI: GHS_BSP_NAME not specified; defaulting to \"";
  153. m += bspName;
  154. m += "\"";
  155. cmSystemTools::Message(m);
  156. }
  157. return true;
  158. }
  159. void cmGlobalGhsMultiGenerator::EnableLanguage(
  160. std::vector<std::string> const& l, cmMakefile* mf, bool optional)
  161. {
  162. mf->AddDefinition("CMAKE_SYSTEM_NAME", "GHS-MULTI");
  163. mf->AddDefinition("GHSMULTI", "1"); // identifier for user CMake files
  164. const char* tgtPlatform = mf->GetDefinition("GHS_TARGET_PLATFORM");
  165. if (!tgtPlatform) {
  166. cmSystemTools::Message("Green Hills MULTI: GHS_TARGET_PLATFORM not "
  167. "specified; defaulting to \"integrity\"");
  168. tgtPlatform = "integrity";
  169. }
  170. /* store the platform name for later use */
  171. mf->AddCacheDefinition("GHS_TARGET_PLATFORM", tgtPlatform,
  172. "Name of GHS target platform.", cmStateEnums::STRING);
  173. /* store original OS location */
  174. this->OsDir = mf->GetSafeDefinition("GHS_OS_DIR");
  175. this->cmGlobalGenerator::EnableLanguage(l, mf, optional);
  176. }
  177. bool cmGlobalGhsMultiGenerator::FindMakeProgram(cmMakefile* /*mf*/)
  178. {
  179. // The GHS generator only knows how to lookup its build tool
  180. // during generation of the project files, but this
  181. // can only be done after the toolset is specified.
  182. return true;
  183. }
  184. void cmGlobalGhsMultiGenerator::GetToolset(cmMakefile* mf, std::string& tsd,
  185. const std::string& ts)
  186. {
  187. const char* ghsRoot = mf->GetDefinition("GHS_TOOLSET_ROOT");
  188. if (!ghsRoot || ghsRoot[0] == '\0') {
  189. ghsRoot = DEFAULT_TOOLSET_ROOT;
  190. }
  191. tsd = ghsRoot;
  192. if (ts.empty()) {
  193. std::vector<std::string> output;
  194. // Use latest? version
  195. if (tsd.back() != '/') {
  196. tsd += "/";
  197. }
  198. cmSystemTools::Glob(tsd, "comp_[^;]+", output);
  199. if (output.empty()) {
  200. std::string msg =
  201. "No GHS toolsets found in GHS_TOOLSET_ROOT \"" + tsd + "\".";
  202. cmSystemTools::Error(msg);
  203. tsd = "";
  204. } else {
  205. tsd += output.back();
  206. }
  207. } else {
  208. std::string tryPath;
  209. tryPath = cmSystemTools::CollapseFullPath(ts, tsd);
  210. if (!cmSystemTools::FileExists(tryPath)) {
  211. std::string msg = "GHS toolset \"" + tryPath + "\" not found.";
  212. cmSystemTools::Error(msg);
  213. tsd = "";
  214. } else {
  215. tsd = tryPath;
  216. }
  217. }
  218. }
  219. void cmGlobalGhsMultiGenerator::WriteFileHeader(std::ostream& fout)
  220. {
  221. fout << "#!gbuild" << std::endl;
  222. fout << "#" << std::endl
  223. << "# CMAKE generated file: DO NOT EDIT!" << std::endl
  224. << "# Generated by \"" << GetActualName() << "\""
  225. << " Generator, CMake Version " << cmVersion::GetMajorVersion() << "."
  226. << cmVersion::GetMinorVersion() << std::endl
  227. << "#" << std::endl
  228. << std::endl;
  229. }
  230. void cmGlobalGhsMultiGenerator::WriteCustomRuleBOD(std::ostream& fout)
  231. {
  232. fout << "Commands {\n"
  233. " Custom_Rule_Command {\n"
  234. " name = \"Custom Rule Command\"\n"
  235. " exec = \"";
  236. #ifdef _WIN32
  237. fout << "cmd.exe";
  238. #else
  239. fout << "/bin/sh";
  240. #endif
  241. fout << "\"\n"
  242. " options = {\"SpecialOptions\"}\n"
  243. " }\n"
  244. "}\n";
  245. fout << "\n\n";
  246. fout << "FileTypes {\n"
  247. " CmakeRule {\n"
  248. " name = \"Custom Rule\"\n"
  249. " action = \"&Run\"\n"
  250. " extensions = {\"";
  251. #ifdef _WIN32
  252. fout << "bat";
  253. #else
  254. fout << "sh";
  255. #endif
  256. fout << "\"}\n"
  257. " grepable = false\n"
  258. " command = \"Custom Rule Command\"\n"
  259. " commandLine = \"$COMMAND ";
  260. #ifdef _WIN32
  261. fout << "/c";
  262. #endif
  263. fout << " $INPUTFILE\"\n"
  264. " progress = \"Processing Custom Rule\"\n"
  265. " promoteToFirstPass = true\n"
  266. " outputType = \"None\"\n"
  267. " color = \"#800080\"\n"
  268. " }\n"
  269. "}\n";
  270. }
  271. void cmGlobalGhsMultiGenerator::WriteCustomTargetBOD(std::ostream& fout)
  272. {
  273. fout << "FileTypes {\n"
  274. " CmakeTarget {\n"
  275. " name = \"Custom Target\"\n"
  276. " action = \"&Execute\"\n"
  277. " grepable = false\n"
  278. " outputType = \"None\"\n"
  279. " color = \"#800080\"\n"
  280. " }\n"
  281. "}\n";
  282. }
  283. void cmGlobalGhsMultiGenerator::WriteTopLevelProject(std::ostream& fout,
  284. cmLocalGenerator* root)
  285. {
  286. this->WriteFileHeader(fout);
  287. this->WriteMacros(fout, root);
  288. this->WriteHighLevelDirectives(root, fout);
  289. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fout);
  290. fout << "# Top Level Project File" << std::endl;
  291. // Specify BSP option if supplied by user
  292. const char* bspName =
  293. this->GetCMakeInstance()->GetCacheDefinition("GHS_BSP_NAME");
  294. if (!cmSystemTools::IsOff(bspName)) {
  295. fout << " -bsp " << bspName << std::endl;
  296. }
  297. // Specify OS DIR if supplied by user
  298. // -- not all platforms require this entry in the project file
  299. if (!cmSystemTools::IsOff(this->OsDir.c_str())) {
  300. const char* osDirOption =
  301. this->GetCMakeInstance()->GetCacheDefinition("GHS_OS_DIR_OPTION");
  302. std::replace(this->OsDir.begin(), this->OsDir.end(), '\\', '/');
  303. fout << " ";
  304. if (cmSystemTools::IsOff(osDirOption)) {
  305. fout << "";
  306. } else {
  307. fout << osDirOption;
  308. }
  309. fout << "\"" << this->OsDir << "\"" << std::endl;
  310. }
  311. }
  312. void cmGlobalGhsMultiGenerator::WriteSubProjects(std::ostream& fout,
  313. std::string& all_target)
  314. {
  315. fout << "CMakeFiles/" << all_target << " [Project]" << std::endl;
  316. // All known targets
  317. for (cmGeneratorTarget const* target : this->ProjectTargets) {
  318. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  319. target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  320. target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  321. (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
  322. target->GetName() != GetInstallTargetName())) {
  323. continue;
  324. }
  325. fout << "CMakeFiles/" << target->GetName() + ".tgt" + FILE_EXTENSION
  326. << " [Project]" << std::endl;
  327. }
  328. }
  329. void cmGlobalGhsMultiGenerator::WriteProjectLine(
  330. std::ostream& fout, cmGeneratorTarget const* target, cmLocalGenerator* root,
  331. std::string& rootBinaryDir)
  332. {
  333. const char* projName = target->GetProperty("GENERATOR_FILE_NAME");
  334. const char* projType = target->GetProperty("GENERATOR_FILE_NAME_EXT");
  335. if (projName && projType) {
  336. cmLocalGenerator* lg = target->GetLocalGenerator();
  337. std::string dir = lg->GetCurrentBinaryDirectory();
  338. dir = root->MaybeConvertToRelativePath(rootBinaryDir, dir);
  339. if (dir == ".") {
  340. dir.clear();
  341. } else {
  342. if (dir.back() != '/') {
  343. dir += "/";
  344. }
  345. }
  346. std::string projFile = dir + projName + FILE_EXTENSION;
  347. fout << projFile;
  348. fout << " " << projType << std::endl;
  349. } else {
  350. /* Should never happen */
  351. std::string message =
  352. "The project file for target [" + target->GetName() + "] is missing.\n";
  353. cmSystemTools::Error(message);
  354. fout << "{comment} " << target->GetName() << " [missing project file]\n";
  355. }
  356. }
  357. void cmGlobalGhsMultiGenerator::WriteTargets(cmLocalGenerator* root)
  358. {
  359. std::string rootBinaryDir = root->GetCurrentBinaryDirectory();
  360. rootBinaryDir += "/CMakeFiles";
  361. // All known targets
  362. for (cmGeneratorTarget const* target : this->ProjectTargets) {
  363. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  364. target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  365. target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  366. (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
  367. target->GetName() != GetInstallTargetName())) {
  368. continue;
  369. }
  370. // create target build file
  371. std::string name = target->GetName() + ".tgt" + FILE_EXTENSION;
  372. std::string fname = rootBinaryDir + "/" + name;
  373. cmGeneratedFileStream fbld(fname);
  374. fbld.SetCopyIfDifferent(true);
  375. this->WriteFileHeader(fbld);
  376. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fbld);
  377. std::vector<cmGeneratorTarget const*> build;
  378. if (ComputeTargetBuildOrder(target, build)) {
  379. std::string message = "The inter-target dependency graph for target [" +
  380. target->GetName() + "] had a cycle.\n";
  381. cmSystemTools::Error(message);
  382. } else {
  383. for (auto& tgt : build) {
  384. WriteProjectLine(fbld, tgt, root, rootBinaryDir);
  385. }
  386. }
  387. fbld.Close();
  388. }
  389. }
  390. void cmGlobalGhsMultiGenerator::WriteAllTarget(
  391. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators,
  392. std::string& all_target)
  393. {
  394. this->ProjectTargets.clear();
  395. // create target build file
  396. all_target = root->GetProjectName() + "." + this->GetAllTargetName() +
  397. ".tgt" + FILE_EXTENSION;
  398. std::string fname =
  399. root->GetCurrentBinaryDirectory() + "/CMakeFiles/" + all_target;
  400. cmGeneratedFileStream fbld(fname);
  401. fbld.SetCopyIfDifferent(true);
  402. this->WriteFileHeader(fbld);
  403. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::PROJECT, fbld);
  404. // Collect all targets under this root generator and the transitive
  405. // closure of their dependencies.
  406. TargetDependSet projectTargets;
  407. TargetDependSet originalTargets;
  408. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  409. OrderedTargetDependSet sortedProjectTargets(projectTargets, "");
  410. std::vector<cmGeneratorTarget const*> defaultTargets;
  411. for (cmGeneratorTarget const* t : sortedProjectTargets) {
  412. /* save list of all targets in sorted order */
  413. this->ProjectTargets.push_back(t);
  414. }
  415. for (cmGeneratorTarget const* t : sortedProjectTargets) {
  416. if (t->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  417. continue;
  418. }
  419. if (!cmSystemTools::IsOn(t->GetProperty("EXCLUDE_FROM_ALL"))) {
  420. defaultTargets.push_back(t);
  421. }
  422. }
  423. std::vector<cmGeneratorTarget const*> build;
  424. if (ComputeTargetBuildOrder(defaultTargets, build)) {
  425. std::string message = "The inter-target dependency graph for project [" +
  426. root->GetProjectName() + "] had a cycle.\n";
  427. cmSystemTools::Error(message);
  428. } else {
  429. // determine the targets for ALL target
  430. std::string rootBinaryDir = root->GetCurrentBinaryDirectory();
  431. rootBinaryDir += "/CMakeFiles";
  432. for (cmGeneratorTarget const* target : build) {
  433. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  434. target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  435. target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  436. continue;
  437. }
  438. this->WriteProjectLine(fbld, target, root, rootBinaryDir);
  439. }
  440. }
  441. fbld.Close();
  442. }
  443. void cmGlobalGhsMultiGenerator::Generate()
  444. {
  445. std::string fname;
  446. // first do the superclass method
  447. this->cmGlobalGenerator::Generate();
  448. // output top-level projects
  449. for (auto& it : this->ProjectMap) {
  450. this->OutputTopLevelProject(it.second[0], it.second);
  451. }
  452. // create custom rule BOD file
  453. fname = this->GetCMakeInstance()->GetHomeOutputDirectory() +
  454. "/CMakeFiles/custom_rule.bod";
  455. cmGeneratedFileStream frule(fname);
  456. frule.SetCopyIfDifferent(true);
  457. this->WriteFileHeader(frule);
  458. this->WriteCustomRuleBOD(frule);
  459. frule.Close();
  460. // create custom target BOD file
  461. fname = this->GetCMakeInstance()->GetHomeOutputDirectory() +
  462. "/CMakeFiles/custom_target.bod";
  463. cmGeneratedFileStream ftarget(fname);
  464. ftarget.SetCopyIfDifferent(true);
  465. this->WriteFileHeader(ftarget);
  466. this->WriteCustomTargetBOD(ftarget);
  467. ftarget.Close();
  468. }
  469. void cmGlobalGhsMultiGenerator::OutputTopLevelProject(
  470. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  471. {
  472. std::string fname;
  473. std::string all_target;
  474. if (generators.empty()) {
  475. return;
  476. }
  477. /* Name top-level projects as filename.top.gpj to avoid name clashes
  478. * with target projects. This avoid the issue where the project has
  479. * the same name as the executable target.
  480. */
  481. fname = root->GetCurrentBinaryDirectory();
  482. fname += "/";
  483. fname += root->GetProjectName();
  484. fname += ".top";
  485. fname += FILE_EXTENSION;
  486. cmGeneratedFileStream top(fname);
  487. top.SetCopyIfDifferent(true);
  488. this->WriteTopLevelProject(top, root);
  489. this->WriteAllTarget(root, generators, all_target);
  490. this->WriteTargets(root);
  491. this->WriteSubProjects(top, all_target);
  492. top.Close();
  493. }
  494. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  495. cmGlobalGhsMultiGenerator::GenerateBuildCommand(
  496. const std::string& makeProgram, const std::string& projectName,
  497. const std::string& projectDir, std::vector<std::string> const& targetNames,
  498. const std::string& /*config*/, bool /*fast*/, int jobs, bool /*verbose*/,
  499. std::vector<std::string> const& makeOptions)
  500. {
  501. GeneratedMakeCommand makeCommand = {};
  502. std::string gbuild;
  503. if (const char* gbuildCached =
  504. this->CMakeInstance->GetCacheDefinition("CMAKE_MAKE_PROGRAM")) {
  505. gbuild = gbuildCached;
  506. }
  507. makeCommand.Add(this->SelectMakeProgram(makeProgram, gbuild));
  508. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  509. makeCommand.Add("-parallel");
  510. if (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  511. makeCommand.Add(std::to_string(jobs));
  512. }
  513. }
  514. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  515. /* determine which top-project file to use */
  516. std::string proj = projectName + ".top" + FILE_EXTENSION;
  517. std::vector<std::string> files;
  518. cmSystemTools::Glob(projectDir, ".*\\.top\\.gpj", files);
  519. if (!files.empty()) {
  520. /* if multiple top-projects are found in build directory
  521. * then prefer projectName top-project.
  522. */
  523. auto p = std::find(files.begin(), files.end(), proj);
  524. if (p == files.end()) {
  525. proj = files.at(0);
  526. }
  527. }
  528. makeCommand.Add("-top", proj);
  529. if (!targetNames.empty()) {
  530. if (std::find(targetNames.begin(), targetNames.end(), "clean") !=
  531. targetNames.end()) {
  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. cmSystemTools::ExpandListArgument(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. }