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