cmGhsMultiTargetGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 "cmGhsMultiTargetGenerator.h"
  4. #include <algorithm>
  5. #include <memory>
  6. #include <ostream>
  7. #include <set>
  8. #include <type_traits>
  9. #include <utility>
  10. #include <vector>
  11. #include <cm/optional>
  12. #include "cmCustomCommand.h"
  13. #include "cmCustomCommandGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmGlobalGhsMultiGenerator.h"
  17. #include "cmLinkLineComputer.h" // IWYU pragma: keep
  18. #include "cmList.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmLocalGhsMultiGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmOutputConverter.h"
  23. #include "cmSourceFile.h"
  24. #include "cmSourceFileLocation.h"
  25. #include "cmSourceGroup.h"
  26. #include "cmStateDirectory.h"
  27. #include "cmStateSnapshot.h"
  28. #include "cmStateTypes.h"
  29. #include "cmStringAlgorithms.h"
  30. #include "cmSystemTools.h"
  31. #include "cmTarget.h"
  32. #include "cmValue.h"
  33. cmGhsMultiTargetGenerator::cmGhsMultiTargetGenerator(cmGeneratorTarget* target)
  34. : GeneratorTarget(target)
  35. , LocalGenerator(
  36. static_cast<cmLocalGhsMultiGenerator*>(target->GetLocalGenerator()))
  37. , Makefile(target->Target->GetMakefile())
  38. , Name(target->GetName())
  39. {
  40. // Store the configuration name that is being used
  41. if (cmValue config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE")) {
  42. // Use the build type given by the user.
  43. this->ConfigName = *config;
  44. } else {
  45. // No configuration type given.
  46. this->ConfigName.clear();
  47. }
  48. }
  49. cmGhsMultiTargetGenerator::~cmGhsMultiTargetGenerator() = default;
  50. void cmGhsMultiTargetGenerator::Generate()
  51. {
  52. // Determine type of target for this project
  53. switch (this->GeneratorTarget->GetType()) {
  54. case cmStateEnums::EXECUTABLE: {
  55. // Get the name of the executable to generate.
  56. this->TargetNameReal =
  57. this->GeneratorTarget->GetExecutableNames(this->ConfigName).Real;
  58. if (this->cmGhsMultiTargetGenerator::DetermineIfIntegrityApp()) {
  59. this->TagType = GhsMultiGpj::INTERGRITY_APPLICATION;
  60. } else {
  61. this->TagType = GhsMultiGpj::PROGRAM;
  62. }
  63. break;
  64. }
  65. case cmStateEnums::STATIC_LIBRARY: {
  66. this->TargetNameReal =
  67. this->GeneratorTarget->GetLibraryNames(this->ConfigName).Real;
  68. this->TagType = GhsMultiGpj::LIBRARY;
  69. break;
  70. }
  71. case cmStateEnums::SHARED_LIBRARY: {
  72. std::string msg =
  73. cmStrCat("add_library(<name> SHARED ...) not supported: ", this->Name);
  74. cmSystemTools::Message(msg);
  75. return;
  76. }
  77. case cmStateEnums::OBJECT_LIBRARY: {
  78. this->TargetNameReal =
  79. this->GeneratorTarget->GetLibraryNames(this->ConfigName).Real;
  80. this->TagType = GhsMultiGpj::SUBPROJECT;
  81. break;
  82. }
  83. case cmStateEnums::MODULE_LIBRARY: {
  84. std::string msg =
  85. cmStrCat("add_library(<name> MODULE ...) not supported: ", this->Name);
  86. cmSystemTools::Message(msg);
  87. return;
  88. }
  89. case cmStateEnums::UTILITY: {
  90. this->TargetNameReal = this->GeneratorTarget->GetName();
  91. this->TagType = GhsMultiGpj::CUSTOM_TARGET;
  92. break;
  93. }
  94. case cmStateEnums::GLOBAL_TARGET: {
  95. this->TargetNameReal = this->GeneratorTarget->GetName();
  96. if (this->TargetNameReal ==
  97. this->GetGlobalGenerator()->GetInstallTargetName()) {
  98. this->TagType = GhsMultiGpj::CUSTOM_TARGET;
  99. } else {
  100. return;
  101. }
  102. break;
  103. }
  104. default:
  105. return;
  106. }
  107. this->GenerateTarget();
  108. }
  109. void cmGhsMultiTargetGenerator::GenerateTarget()
  110. {
  111. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE &&
  112. !this->GeneratorTarget
  113. ->GetLinkerTypeProperty(
  114. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName),
  115. this->ConfigName)
  116. .empty()) {
  117. // Green Hill MULTI does not support this feature.
  118. cmSystemTools::Message(
  119. cmStrCat("'LINKER_TYPE' property, specified on target '",
  120. this->GeneratorTarget->GetName(),
  121. "', is not supported by this generator."));
  122. }
  123. // Open the target file in copy-if-different mode.
  124. std::string fproj =
  125. cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  126. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
  127. '/', this->Name, cmGlobalGhsMultiGenerator::FILE_EXTENSION);
  128. // Tell the global generator the name of the project file
  129. this->GeneratorTarget->Target->SetProperty("GENERATOR_FILE_NAME", fproj);
  130. this->GeneratorTarget->Target->SetProperty(
  131. "GENERATOR_FILE_NAME_EXT", GhsMultiGpj::GetGpjTag(this->TagType));
  132. cmGeneratedFileStream fout(fproj);
  133. fout.SetCopyIfDifferent(true);
  134. this->GetGlobalGenerator()->WriteFileHeader(fout);
  135. GhsMultiGpj::WriteGpjTag(this->TagType, fout);
  136. if (this->TagType != GhsMultiGpj::CUSTOM_TARGET) {
  137. const std::string language(
  138. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName));
  139. this->WriteTargetSpecifics(fout, this->ConfigName);
  140. this->SetCompilerFlags(this->ConfigName, language);
  141. this->WriteCompilerFlags(fout, this->ConfigName, language);
  142. this->WriteCompilerDefinitions(fout, this->ConfigName, language);
  143. this->WriteIncludes(fout, this->ConfigName, language);
  144. this->WriteTargetLinkLine(fout, this->ConfigName);
  145. this->WriteBuildEvents(fout);
  146. }
  147. this->WriteSources(fout);
  148. fout.Close();
  149. }
  150. cmGlobalGhsMultiGenerator* cmGhsMultiTargetGenerator::GetGlobalGenerator()
  151. const
  152. {
  153. return static_cast<cmGlobalGhsMultiGenerator*>(
  154. this->LocalGenerator->GetGlobalGenerator());
  155. }
  156. void cmGhsMultiTargetGenerator::WriteTargetSpecifics(std::ostream& fout,
  157. const std::string& config)
  158. {
  159. std::string outpath;
  160. /* Determine paths from the target project file to where the output artifacts
  161. * need to be located.
  162. */
  163. if (this->TagType != GhsMultiGpj::SUBPROJECT) {
  164. // set target binary file destination
  165. std::string binpath = cmStrCat(
  166. this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  167. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget));
  168. outpath = cmSystemTools::RelativePath(
  169. binpath, this->GeneratorTarget->GetDirectory(config));
  170. /* clang-format off */
  171. fout << " :binDirRelative=\"" << outpath << "\"\n"
  172. " -o \"" << this->TargetNameReal << "\"\n";
  173. /* clang-format on */
  174. }
  175. // set target object file destination
  176. outpath = ".";
  177. fout << " :outputDirRelative=\"" << outpath << "\"\n";
  178. }
  179. void cmGhsMultiTargetGenerator::SetCompilerFlags(std::string const& config,
  180. const std::string& language)
  181. {
  182. auto i = this->FlagsByLanguage.find(language);
  183. if (i == this->FlagsByLanguage.end()) {
  184. std::string flags;
  185. this->LocalGenerator->AddLanguageFlags(
  186. flags, this->GeneratorTarget, cmBuildStep::Compile, language, config);
  187. this->LocalGenerator->AddCMP0018Flags(flags, this->GeneratorTarget,
  188. language, config);
  189. this->LocalGenerator->AddVisibilityPresetFlags(
  190. flags, this->GeneratorTarget, language);
  191. this->LocalGenerator->AddColorDiagnosticsFlags(flags, language);
  192. // Append old-style preprocessor definition flags.
  193. if (this->Makefile->GetDefineFlags() != " ") {
  194. this->LocalGenerator->AppendFlags(flags,
  195. this->Makefile->GetDefineFlags());
  196. }
  197. // Add target-specific flags.
  198. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  199. language, config);
  200. std::map<std::string, std::string>::value_type entry(language, flags);
  201. i = this->FlagsByLanguage.insert(entry).first;
  202. }
  203. }
  204. std::string cmGhsMultiTargetGenerator::GetDefines(const std::string& language,
  205. std::string const& config)
  206. {
  207. auto i = this->DefinesByLanguage.find(language);
  208. if (i == this->DefinesByLanguage.end()) {
  209. std::set<std::string> defines;
  210. // Add preprocessor definitions for this target and configuration.
  211. this->LocalGenerator->GetTargetDefines(this->GeneratorTarget, config,
  212. language, defines);
  213. std::string definesString;
  214. this->LocalGenerator->JoinDefines(defines, definesString, language);
  215. std::map<std::string, std::string>::value_type entry(language,
  216. definesString);
  217. i = this->DefinesByLanguage.insert(entry).first;
  218. }
  219. return i->second;
  220. }
  221. void cmGhsMultiTargetGenerator::WriteCompilerFlags(std::ostream& fout,
  222. std::string const&,
  223. const std::string& language)
  224. {
  225. auto flagsByLangI = this->FlagsByLanguage.find(language);
  226. if (flagsByLangI != this->FlagsByLanguage.end()) {
  227. if (!flagsByLangI->second.empty()) {
  228. std::vector<std::string> ghsCompFlags =
  229. cmSystemTools::ParseArguments(flagsByLangI->second);
  230. for (const std::string& f : ghsCompFlags) {
  231. fout << " " << f << '\n';
  232. }
  233. }
  234. }
  235. }
  236. void cmGhsMultiTargetGenerator::WriteCompilerDefinitions(
  237. std::ostream& fout, const std::string& config, const std::string& language)
  238. {
  239. std::vector<std::string> compileDefinitions;
  240. this->GeneratorTarget->GetCompileDefinitions(compileDefinitions, config,
  241. language);
  242. for (std::string const& compileDefinition : compileDefinitions) {
  243. fout << " -D" << compileDefinition << '\n';
  244. }
  245. }
  246. void cmGhsMultiTargetGenerator::WriteIncludes(std::ostream& fout,
  247. const std::string& config,
  248. const std::string& language)
  249. {
  250. std::vector<std::string> includes;
  251. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  252. language, config);
  253. for (std::string const& include : includes) {
  254. fout << " -I\"" << include << "\"\n";
  255. }
  256. }
  257. void cmGhsMultiTargetGenerator::WriteTargetLinkLine(std::ostream& fout,
  258. std::string const& config)
  259. {
  260. if (this->TagType == GhsMultiGpj::INTERGRITY_APPLICATION) {
  261. return;
  262. }
  263. std::string linkLibraries;
  264. std::string flags;
  265. std::string linkFlags;
  266. std::string frameworkPath;
  267. std::string linkPath;
  268. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  269. this->GetGlobalGenerator()->CreateLinkLineComputer(
  270. this->LocalGenerator,
  271. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  272. this->LocalGenerator->GetTargetFlags(
  273. linkLineComputer.get(), config, linkLibraries, flags, linkFlags,
  274. frameworkPath, linkPath, this->GeneratorTarget);
  275. // write out link options
  276. std::vector<std::string> lopts = cmSystemTools::ParseArguments(linkFlags);
  277. for (const std::string& l : lopts) {
  278. fout << " " << l << '\n';
  279. }
  280. // write out link search paths
  281. // must be quoted for paths that contain spaces
  282. std::vector<std::string> lpath = cmSystemTools::ParseArguments(linkPath);
  283. for (const std::string& l : lpath) {
  284. fout << " -L\"" << l << "\"\n";
  285. }
  286. // write out link libs
  287. // must be quoted for filepaths that contains spaces
  288. std::string cbd = this->LocalGenerator->GetCurrentBinaryDirectory();
  289. std::vector<std::string> llibs =
  290. cmSystemTools::ParseArguments(linkLibraries);
  291. for (const std::string& l : llibs) {
  292. if (l.compare(0, 2, "-l") == 0) {
  293. fout << " \"" << l << "\"\n";
  294. } else {
  295. std::string rl = cmSystemTools::CollapseFullPath(l, cbd);
  296. fout << " -l\"" << rl << "\"\n";
  297. }
  298. }
  299. }
  300. void cmGhsMultiTargetGenerator::WriteBuildEvents(std::ostream& fout)
  301. {
  302. this->WriteBuildEventsHelper(fout,
  303. this->GeneratorTarget->GetPreBuildCommands(),
  304. std::string("prebuild"),
  305. #ifdef _WIN32
  306. std::string("preexecShell")
  307. #else
  308. std::string("preexec")
  309. #endif
  310. );
  311. if (this->TagType != GhsMultiGpj::CUSTOM_TARGET) {
  312. this->WriteBuildEventsHelper(fout,
  313. this->GeneratorTarget->GetPreLinkCommands(),
  314. std::string("prelink"),
  315. #ifdef _WIN32
  316. std::string("preexecShell")
  317. #else
  318. std::string("preexec")
  319. #endif
  320. );
  321. }
  322. this->WriteBuildEventsHelper(fout,
  323. this->GeneratorTarget->GetPostBuildCommands(),
  324. std::string("postbuild"),
  325. #ifdef _WIN32
  326. std::string("postexecShell")
  327. #else
  328. std::string("postexec")
  329. #endif
  330. );
  331. }
  332. void cmGhsMultiTargetGenerator::WriteBuildEventsHelper(
  333. std::ostream& fout, const std::vector<cmCustomCommand>& ccv,
  334. std::string const& name, std::string const& cmd)
  335. {
  336. int cmdcount = 0;
  337. #ifdef _WIN32
  338. std::string fext = ".bat";
  339. std::string shell;
  340. #else
  341. std::string fext = ".sh";
  342. std::string shell = "/bin/sh ";
  343. #endif
  344. for (cmCustomCommand const& cc : ccv) {
  345. cmCustomCommandGenerator ccg(cc, this->ConfigName, this->LocalGenerator);
  346. // Open the filestream for this custom command
  347. std::string fname =
  348. cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  349. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
  350. '/', this->Name, '_', name, cmdcount++, fext);
  351. cmGeneratedFileStream f(fname);
  352. f.SetCopyIfDifferent(true);
  353. this->WriteCustomCommandsHelper(f, ccg);
  354. f.Close();
  355. if (this->TagType != GhsMultiGpj::CUSTOM_TARGET) {
  356. fout << " :" << cmd << "=\"" << shell << fname << "\"\n";
  357. } else {
  358. fout << fname << "\n :outputName=\"" << fname << ".rule\"\n";
  359. }
  360. for (const auto& byp : ccg.GetByproducts()) {
  361. fout << " :extraOutputFile=\"" << byp << "\"\n";
  362. }
  363. }
  364. }
  365. void cmGhsMultiTargetGenerator::WriteCustomCommandsHelper(
  366. std::ostream& fout, cmCustomCommandGenerator const& ccg)
  367. {
  368. std::vector<std::string> cmdLines;
  369. // if the command specified a working directory use it.
  370. std::string dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  371. std::string workingDir = ccg.GetWorkingDirectory();
  372. if (!workingDir.empty()) {
  373. dir = workingDir;
  374. }
  375. // Line to check for error between commands.
  376. #ifdef _WIN32
  377. std::string check_error = "if %errorlevel% neq 0 exit /b %errorlevel%";
  378. #else
  379. std::string check_error = "if [ $? -ne 0 ]; then exit 1; fi";
  380. #endif
  381. #ifdef _WIN32
  382. cmdLines.push_back("@echo off");
  383. #endif
  384. // Echo the custom command's comment text.
  385. if (cm::optional<std::string> comment = ccg.GetComment()) {
  386. std::string escapedComment = this->LocalGenerator->EscapeForShell(
  387. *comment, ccg.GetCC().GetEscapeAllowMakeVars());
  388. std::string echocmd = cmStrCat("echo ", escapedComment);
  389. cmdLines.push_back(std::move(echocmd));
  390. }
  391. // Switch to working directory
  392. std::string cdCmd;
  393. #ifdef _WIN32
  394. std::string cdStr = "cd /D ";
  395. #else
  396. std::string cdStr = "cd ";
  397. #endif
  398. cdCmd = cdStr +
  399. this->LocalGenerator->ConvertToOutputFormat(dir, cmOutputConverter::SHELL);
  400. cmdLines.push_back(std::move(cdCmd));
  401. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  402. // Build the command line in a single string.
  403. std::string cmd = ccg.GetCommand(c);
  404. if (!cmd.empty()) {
  405. // Use "call " before any invocations of .bat or .cmd files
  406. // invoked as custom commands in the WindowsShell.
  407. //
  408. bool useCall = false;
  409. #ifdef _WIN32
  410. std::string suffix;
  411. if (cmd.size() > 4) {
  412. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
  413. if (suffix == ".bat" || suffix == ".cmd") {
  414. useCall = true;
  415. }
  416. }
  417. #endif
  418. cmSystemTools::ReplaceString(cmd, "/./", "/");
  419. // Convert the command to a relative path only if the current
  420. // working directory will be the start-output directory.
  421. bool had_slash = cmd.find('/') != std::string::npos;
  422. if (workingDir.empty()) {
  423. cmd = this->LocalGenerator->MaybeRelativeToCurBinDir(cmd);
  424. }
  425. bool has_slash = cmd.find('/') != std::string::npos;
  426. if (had_slash && !has_slash) {
  427. // This command was specified as a path to a file in the
  428. // current directory. Add a leading "./" so it can run
  429. // without the current directory being in the search path.
  430. cmd = cmStrCat("./", cmd);
  431. }
  432. cmd = this->LocalGenerator->ConvertToOutputFormat(
  433. cmd, cmOutputConverter::SHELL);
  434. if (useCall) {
  435. cmd = cmStrCat("call ", cmd);
  436. }
  437. ccg.AppendArguments(c, cmd);
  438. cmdLines.push_back(std::move(cmd));
  439. }
  440. }
  441. // push back the custom commands
  442. for (auto const& c : cmdLines) {
  443. fout << c << '\n' << check_error << '\n';
  444. }
  445. }
  446. void cmGhsMultiTargetGenerator::WriteSourceProperty(
  447. std::ostream& fout, const cmSourceFile* sf, std::string const& propName,
  448. std::string const& propFlag)
  449. {
  450. cmValue prop = sf->GetProperty(propName);
  451. if (prop) {
  452. cmList list{ *prop };
  453. for (const std::string& p : list) {
  454. fout << " " << propFlag << p << '\n';
  455. }
  456. }
  457. }
  458. void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj)
  459. {
  460. /* vector of all sources for this target */
  461. std::vector<cmSourceFile*> sources;
  462. this->GeneratorTarget->GetSourceFiles(sources, this->ConfigName);
  463. /* vector of all groups defined for this target
  464. * -- but the vector is not expanded with sub groups or in any useful order
  465. */
  466. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  467. /* for each source file assign it to its group */
  468. std::map<std::string, std::vector<cmSourceFile*>> groupFiles;
  469. std::set<std::string> groupNames;
  470. for (cmSourceFile* sf : sources) {
  471. cmSourceGroup* sourceGroup =
  472. this->Makefile->FindSourceGroup(sf->ResolveFullPath(), sourceGroups);
  473. std::string gn = sourceGroup->GetFullName();
  474. groupFiles[gn].push_back(sf);
  475. groupNames.insert(std::move(gn));
  476. }
  477. /* list of known groups and the order they are displayed in a project file */
  478. const std::vector<std::string> standardGroups = {
  479. "CMake Rules", "Header Files", "Source Files",
  480. "Object Files", "Object Libraries", "Resources"
  481. };
  482. /* list of groups in the order they are displayed in a project file*/
  483. std::vector<std::string> groupFilesList(groupFiles.size());
  484. /* put the groups in the order they should be listed
  485. * - standard groups first, and then everything else
  486. * in the order used by std::map.
  487. */
  488. int i = 0;
  489. for (const std::string& gn : standardGroups) {
  490. auto n = groupNames.find(gn);
  491. if (n != groupNames.end()) {
  492. groupFilesList[i] = *n;
  493. i += 1;
  494. groupNames.erase(gn);
  495. } else if (this->TagType == GhsMultiGpj::CUSTOM_TARGET &&
  496. gn == "CMake Rules") {
  497. /* make sure that rules folder always exists in case of custom targets
  498. * that have no custom commands except for pre or post build events.
  499. */
  500. groupFilesList.resize(groupFilesList.size() + 1);
  501. groupFilesList[i] = gn;
  502. i += 1;
  503. }
  504. }
  505. { /* catch-all group - is last item */
  506. std::string gn;
  507. auto n = groupNames.find(gn);
  508. if (n != groupNames.end()) {
  509. groupFilesList.back() = *n;
  510. groupNames.erase(gn);
  511. }
  512. }
  513. for (const auto& n : groupNames) {
  514. groupFilesList[i] = n;
  515. i += 1;
  516. }
  517. /* sort the files within each group */
  518. for (auto& n : groupFilesList) {
  519. std::sort(groupFiles[n].begin(), groupFiles[n].end(),
  520. [](cmSourceFile* l, cmSourceFile* r) {
  521. return l->ResolveFullPath() < r->ResolveFullPath();
  522. });
  523. }
  524. /* list of open project files */
  525. std::vector<cmGeneratedFileStream*> gfiles;
  526. /* write files into the proper project file
  527. * -- groups go into main project file
  528. * unless NO_SOURCE_GROUP_FILE property or variable is set.
  529. */
  530. for (auto& sg : groupFilesList) {
  531. std::ostream* fout;
  532. bool useProjectFile =
  533. cmIsOn(this->GeneratorTarget->GetProperty("GHS_NO_SOURCE_GROUP_FILE")) ||
  534. this->Makefile->IsOn("CMAKE_GHS_NO_SOURCE_GROUP_FILE");
  535. if (useProjectFile || sg.empty()) {
  536. fout = &fout_proj;
  537. } else {
  538. // Open the filestream in copy-if-different mode.
  539. std::string gname = sg;
  540. cmsys::SystemTools::ReplaceString(gname, "\\", "_");
  541. std::string lpath =
  542. cmStrCat(gname, cmGlobalGhsMultiGenerator::FILE_EXTENSION);
  543. std::string fpath = cmStrCat(
  544. this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  545. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget), '/',
  546. lpath);
  547. cmGeneratedFileStream* f = new cmGeneratedFileStream(fpath);
  548. f->SetCopyIfDifferent(true);
  549. gfiles.push_back(f);
  550. fout = f;
  551. this->GetGlobalGenerator()->WriteFileHeader(*f);
  552. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::SUBPROJECT, *f);
  553. fout_proj << lpath << " ";
  554. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::SUBPROJECT, fout_proj);
  555. }
  556. if (useProjectFile) {
  557. if (sg.empty()) {
  558. *fout << "{comment} Others" << '\n';
  559. } else {
  560. *fout << "{comment} " << sg << '\n';
  561. }
  562. } else if (sg.empty()) {
  563. *fout << "{comment} Others\n";
  564. }
  565. if (sg != "CMake Rules") {
  566. /* output rule for each source file */
  567. for (const cmSourceFile* si : groupFiles[sg]) {
  568. bool compile = true;
  569. // Convert filename to native system
  570. // WORKAROUND: GHS MULTI 6.1.4 and 6.1.6 are known to need backslash on
  571. // windows when opening some files from the search window.
  572. std::string fname(si->GetFullPath());
  573. cmSystemTools::ConvertToOutputSlashes(fname);
  574. /* For custom targets list any associated sources,
  575. * comment out source code to prevent it from being
  576. * compiled when processing this target.
  577. * Otherwise, comment out any custom command (main) dependencies that
  578. * are listed as source files to prevent them from being considered
  579. * part of the build.
  580. */
  581. std::string comment;
  582. if ((this->TagType == GhsMultiGpj::CUSTOM_TARGET &&
  583. !si->GetLanguage().empty()) ||
  584. si->GetCustomCommand()) {
  585. comment = "{comment} ";
  586. compile = false;
  587. }
  588. *fout << comment << fname << WriteObjectLangOverride(si) << '\n';
  589. if (compile) {
  590. this->WriteSourceProperty(*fout, si, "INCLUDE_DIRECTORIES", "-I");
  591. this->WriteSourceProperty(*fout, si, "COMPILE_DEFINITIONS", "-D");
  592. this->WriteSourceProperty(*fout, si, "COMPILE_OPTIONS", "");
  593. /* to avoid clutter in the GUI only print out the objectName if it
  594. * has been renamed */
  595. std::string objectName = this->GeneratorTarget->GetObjectName(si);
  596. if (!objectName.empty() &&
  597. this->GeneratorTarget->HasExplicitObjectName(si)) {
  598. *fout << " -o " << objectName << '\n';
  599. }
  600. }
  601. }
  602. } else {
  603. std::vector<cmSourceFile const*> customCommands;
  604. if (this->ComputeCustomCommandOrder(customCommands)) {
  605. std::string message = "The custom commands for target [" +
  606. this->GeneratorTarget->GetName() + "] had a cycle.\n";
  607. cmSystemTools::Error(message);
  608. } else {
  609. /* Custom targets do not have a dependency on SOURCES files.
  610. * Therefore the dependency list may include SOURCES files after the
  611. * custom target. Because nothing can depend on the custom target just
  612. * move it to the last item.
  613. */
  614. for (auto sf = customCommands.begin(); sf != customCommands.end();
  615. ++sf) {
  616. if (((*sf)->GetLocation()).GetName() == this->Name + ".rule") {
  617. std::rotate(sf, sf + 1, customCommands.end());
  618. break;
  619. }
  620. }
  621. int cmdcount = 0;
  622. #ifdef _WIN32
  623. std::string fext = ".bat";
  624. #else
  625. std::string fext = ".sh";
  626. #endif
  627. for (auto& sf : customCommands) {
  628. const cmCustomCommand* cc = sf->GetCustomCommand();
  629. cmCustomCommandGenerator ccg(*cc, this->ConfigName,
  630. this->LocalGenerator);
  631. // Open the filestream for this custom command
  632. std::string fname = cmStrCat(
  633. this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  634. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
  635. '/', this->Name, "_cc", cmdcount++, '_',
  636. (sf->GetLocation()).GetName(), fext);
  637. cmGeneratedFileStream f(fname);
  638. f.SetCopyIfDifferent(true);
  639. this->WriteCustomCommandsHelper(f, ccg);
  640. f.Close();
  641. this->WriteCustomCommandLine(*fout, fname, ccg);
  642. }
  643. }
  644. if (this->TagType == GhsMultiGpj::CUSTOM_TARGET) {
  645. this->WriteBuildEvents(*fout);
  646. }
  647. }
  648. }
  649. for (cmGeneratedFileStream* f : gfiles) {
  650. f->Close();
  651. }
  652. }
  653. void cmGhsMultiTargetGenerator::WriteCustomCommandLine(
  654. std::ostream& fout, std::string& fname, cmCustomCommandGenerator const& ccg)
  655. {
  656. /* NOTE: Customization Files are not well documented. Testing showed
  657. * that ":outputName=file" can only be used once per script. The
  658. * script will only run if ":outputName=file" is missing or just run
  659. * once if ":outputName=file" is not specified. If there are
  660. * multiple outputs then the script needs to be listed multiple times
  661. * for each output. Otherwise it won't rerun the script if one of
  662. * the outputs is manually deleted.
  663. */
  664. bool specifyExtra = true;
  665. for (const auto& out : ccg.GetOutputs()) {
  666. fout << fname << '\n';
  667. fout << " :outputName=\"" << out << "\"\n";
  668. if (specifyExtra) {
  669. for (const auto& byp : ccg.GetByproducts()) {
  670. fout << " :extraOutputFile=\"" << byp << "\"\n";
  671. }
  672. for (const auto& dep : ccg.GetDepends()) {
  673. fout << " :depends=\"" << dep << "\"\n";
  674. }
  675. specifyExtra = false;
  676. }
  677. }
  678. }
  679. std::string cmGhsMultiTargetGenerator::WriteObjectLangOverride(
  680. const cmSourceFile* sourceFile)
  681. {
  682. std::string ret;
  683. cmValue rawLangProp = sourceFile->GetProperty("LANGUAGE");
  684. if (rawLangProp) {
  685. ret = cmStrCat(" [", *rawLangProp, "]");
  686. }
  687. return ret;
  688. }
  689. bool cmGhsMultiTargetGenerator::DetermineIfIntegrityApp()
  690. {
  691. if (cmValue p = this->GeneratorTarget->GetProperty("ghs_integrity_app")) {
  692. return cmIsOn(*p);
  693. }
  694. std::vector<cmSourceFile*> sources;
  695. this->GeneratorTarget->GetSourceFiles(sources, this->ConfigName);
  696. return std::any_of(sources.begin(), sources.end(),
  697. [](cmSourceFile const* sf) -> bool {
  698. return "int" == sf->GetExtension();
  699. });
  700. }
  701. bool cmGhsMultiTargetGenerator::ComputeCustomCommandOrder(
  702. std::vector<cmSourceFile const*>& order)
  703. {
  704. std::set<cmSourceFile const*> temp;
  705. std::set<cmSourceFile const*> perm;
  706. // Collect all custom commands for this target
  707. std::vector<cmSourceFile const*> customCommands;
  708. this->GeneratorTarget->GetCustomCommands(customCommands, this->ConfigName);
  709. for (cmSourceFile const* si : customCommands) {
  710. bool r = this->VisitCustomCommand(temp, perm, order, si);
  711. if (r) {
  712. return r;
  713. }
  714. }
  715. return false;
  716. }
  717. bool cmGhsMultiTargetGenerator::VisitCustomCommand(
  718. std::set<cmSourceFile const*>& temp, std::set<cmSourceFile const*>& perm,
  719. std::vector<cmSourceFile const*>& order, cmSourceFile const* si)
  720. {
  721. /* check if permanent mark is set*/
  722. if (perm.find(si) == perm.end()) {
  723. /* set temporary mark; check if revisit*/
  724. if (temp.insert(si).second) {
  725. for (const auto& di : si->GetCustomCommand()->GetDepends()) {
  726. cmSourceFile const* sf =
  727. this->GeneratorTarget->GetLocalGenerator()->GetSourceFileWithOutput(
  728. di);
  729. /* if sf exists then visit */
  730. if (sf && this->VisitCustomCommand(temp, perm, order, sf)) {
  731. return true;
  732. }
  733. }
  734. /* mark as complete; insert into beginning of list*/
  735. perm.insert(si);
  736. order.push_back(si);
  737. return false;
  738. }
  739. /* revisiting item - not a DAG */
  740. return true;
  741. }
  742. /* already complete */
  743. return false;
  744. }