cmGhsMultiTargetGenerator.cxx 28 KB

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