cmGhsMultiTargetGenerator.cxx 27 KB

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