cmGhsMultiTargetGenerator.cxx 27 KB

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