cmGlobalGhsMultiGenerator.cxx 23 KB

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