cmGhsMultiTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. fout << " :binDirRelative=\"" << outpath << "\"" << std::endl;
  156. fout << " -o \"" << this->TargetNameReal << "\"" << std::endl;
  157. }
  158. // set target object file destination
  159. outpath = this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  160. fout << " :outputDirRelative=\"" << outpath << "\"" << std::endl;
  161. }
  162. void cmGhsMultiTargetGenerator::SetCompilerFlags(std::string const& config,
  163. const std::string& language)
  164. {
  165. auto i = this->FlagsByLanguage.find(language);
  166. if (i == this->FlagsByLanguage.end()) {
  167. std::string flags;
  168. const char* lang = language.c_str();
  169. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget, lang,
  170. config);
  171. this->LocalGenerator->AddCMP0018Flags(flags, this->GeneratorTarget, lang,
  172. config);
  173. this->LocalGenerator->AddVisibilityPresetFlags(
  174. flags, this->GeneratorTarget, lang);
  175. // Append old-style preprocessor definition flags.
  176. if (this->Makefile->GetDefineFlags() != " ") {
  177. this->LocalGenerator->AppendFlags(flags,
  178. this->Makefile->GetDefineFlags());
  179. }
  180. // Add target-specific flags.
  181. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, lang,
  182. config);
  183. std::map<std::string, std::string>::value_type entry(language, flags);
  184. i = this->FlagsByLanguage.insert(entry).first;
  185. }
  186. }
  187. std::string cmGhsMultiTargetGenerator::GetDefines(const std::string& language,
  188. std::string const& config)
  189. {
  190. auto i = this->DefinesByLanguage.find(language);
  191. if (i == this->DefinesByLanguage.end()) {
  192. std::set<std::string> defines;
  193. const char* lang = language.c_str();
  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, lang);
  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 (auto& f : ghsCompFlags) {
  215. fout << " " << f << std::endl;
  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 << std::endl;
  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 << "\"" << std::endl;
  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 (auto& l : lopts) {
  262. fout << " " << l << std::endl;
  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 (auto& l : lpath) {
  268. fout << " -L\"" << l << "\"" << std::endl;
  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 (auto& l : llibs) {
  276. if (l.compare(0, 2, "-l") == 0) {
  277. fout << " \"" << l << "\"" << std::endl;
  278. } else {
  279. std::string rl = cmSystemTools::CollapseFullPath(l, cbd);
  280. fout << " -l\"" << rl << "\"" << std::endl;
  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 << "\"" << std::endl;
  317. } else {
  318. fout << fname << std::endl;
  319. fout << " :outputName=\"" << fname << ".rule\"" << std::endl;
  320. }
  321. for (auto& byp : ccg.GetByproducts()) {
  322. fout << " :extraOutputFile=\"" << byp << "\"" << std::endl;
  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 << std::endl;
  406. fout << check_error << std::endl;
  407. }
  408. }
  409. void cmGhsMultiTargetGenerator::WriteSourceProperty(
  410. std::ostream& fout, const cmSourceFile* sf, std::string const& propName,
  411. std::string const& propFlag)
  412. {
  413. const char* prop = sf->GetProperty(propName);
  414. if (prop) {
  415. std::vector<std::string> list = cmExpandedList(prop);
  416. for (auto& p : list) {
  417. fout << " " << propFlag << p << std::endl;
  418. }
  419. }
  420. }
  421. void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj)
  422. {
  423. /* vector of all sources for this target */
  424. std::vector<cmSourceFile*> sources;
  425. this->GeneratorTarget->GetSourceFiles(sources, this->ConfigName);
  426. /* vector of all groups defined for this target
  427. * -- but the vector is not expanded with sub groups or in any useful order
  428. */
  429. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  430. /* for each source file assign it to its group */
  431. std::map<std::string, std::vector<cmSourceFile*>> groupFiles;
  432. std::set<std::string> groupNames;
  433. for (auto& sf : sources) {
  434. cmSourceGroup* sourceGroup =
  435. this->Makefile->FindSourceGroup(sf->ResolveFullPath(), sourceGroups);
  436. std::string gn = sourceGroup->GetFullName();
  437. groupFiles[gn].push_back(sf);
  438. groupNames.insert(std::move(gn));
  439. }
  440. /* list of known groups and the order they are displayed in a project file */
  441. const std::vector<std::string> standardGroups = {
  442. "CMake Rules", "Header Files", "Source Files",
  443. "Object Files", "Object Libraries", "Resources"
  444. };
  445. /* list of groups in the order they are displayed in a project file*/
  446. std::vector<std::string> groupFilesList(groupFiles.size());
  447. /* put the groups in the order they should be listed
  448. * - standard groups first, and then everything else
  449. * in the order used by std::map.
  450. */
  451. int i = 0;
  452. for (const std::string& gn : standardGroups) {
  453. auto n = groupNames.find(gn);
  454. if (n != groupNames.end()) {
  455. groupFilesList[i] = *n;
  456. i += 1;
  457. groupNames.erase(gn);
  458. } else if (this->TagType == GhsMultiGpj::CUSTOM_TARGET &&
  459. gn == "CMake Rules") {
  460. /* make sure that rules folder always exists in case of custom targets
  461. * that have no custom commands except for pre or post build events.
  462. */
  463. groupFilesList.resize(groupFilesList.size() + 1);
  464. groupFilesList[i] = gn;
  465. i += 1;
  466. }
  467. }
  468. { /* catch-all group - is last item */
  469. std::string gn;
  470. auto n = groupNames.find(gn);
  471. if (n != groupNames.end()) {
  472. groupFilesList.back() = *n;
  473. groupNames.erase(gn);
  474. }
  475. }
  476. for (auto& n : groupNames) {
  477. groupFilesList[i] = n;
  478. i += 1;
  479. }
  480. /* sort the files within each group */
  481. for (auto& n : groupFilesList) {
  482. std::sort(groupFiles[n].begin(), groupFiles[n].end(),
  483. [](cmSourceFile* l, cmSourceFile* r) {
  484. return l->ResolveFullPath() < r->ResolveFullPath();
  485. });
  486. }
  487. /* list of open project files */
  488. std::vector<cmGeneratedFileStream*> gfiles;
  489. /* write files into the proper project file
  490. * -- groups go into main project file
  491. * unless NO_SOURCE_GROUP_FILE property or variable is set.
  492. */
  493. for (auto& sg : groupFilesList) {
  494. std::ostream* fout;
  495. bool useProjectFile =
  496. cmIsOn(this->GeneratorTarget->GetProperty("GHS_NO_SOURCE_GROUP_FILE")) ||
  497. cmIsOn(this->Makefile->GetDefinition("CMAKE_GHS_NO_SOURCE_GROUP_FILE"));
  498. if (useProjectFile || sg.empty()) {
  499. fout = &fout_proj;
  500. } else {
  501. // Open the filestream in copy-if-different mode.
  502. std::string gname = sg;
  503. cmsys::SystemTools::ReplaceString(gname, "\\", "_");
  504. std::string lpath = cmStrCat(
  505. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget), '/',
  506. gname, cmGlobalGhsMultiGenerator::FILE_EXTENSION);
  507. std::string fpath = cmStrCat(
  508. this->LocalGenerator->GetCurrentBinaryDirectory(), '/', lpath);
  509. cmGeneratedFileStream* f = new cmGeneratedFileStream(fpath);
  510. f->SetCopyIfDifferent(true);
  511. gfiles.push_back(f);
  512. fout = f;
  513. this->GetGlobalGenerator()->WriteFileHeader(*f);
  514. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::SUBPROJECT, *f);
  515. fout_proj << lpath << " ";
  516. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::SUBPROJECT, fout_proj);
  517. }
  518. if (useProjectFile) {
  519. if (sg.empty()) {
  520. *fout << "{comment} Others" << std::endl;
  521. } else {
  522. *fout << "{comment} " << sg << std::endl;
  523. }
  524. } else if (sg.empty()) {
  525. *fout << "{comment} Others" << std::endl;
  526. }
  527. if (sg != "CMake Rules") {
  528. /* output rule for each source file */
  529. for (const cmSourceFile* si : groupFiles[sg]) {
  530. bool compile = true;
  531. // Convert filename to native system
  532. // WORKAROUND: GHS MULTI 6.1.4 and 6.1.6 are known to need backslash on
  533. // windows when opening some files from the search window.
  534. std::string fname(si->GetFullPath());
  535. cmSystemTools::ConvertToOutputSlashes(fname);
  536. /* For custom targets list any associated sources,
  537. * comment out source code to prevent it from being
  538. * compiled when processing this target.
  539. * Otherwise, comment out any custom command (main) dependencies that
  540. * are listed as source files to prevent them from being considered
  541. * part of the build.
  542. */
  543. std::string comment;
  544. if ((this->TagType == GhsMultiGpj::CUSTOM_TARGET &&
  545. !si->GetLanguage().empty()) ||
  546. si->GetCustomCommand()) {
  547. comment = "{comment} ";
  548. compile = false;
  549. }
  550. *fout << comment << fname << std::endl;
  551. if (compile) {
  552. if ("ld" != si->GetExtension() && "int" != si->GetExtension() &&
  553. "bsp" != si->GetExtension()) {
  554. WriteObjectLangOverride(*fout, si);
  555. }
  556. this->WriteSourceProperty(*fout, si, "INCLUDE_DIRECTORIES", "-I");
  557. this->WriteSourceProperty(*fout, si, "COMPILE_DEFINITIONS", "-D");
  558. this->WriteSourceProperty(*fout, si, "COMPILE_OPTIONS", "");
  559. /* to avoid clutter in the GUI only print out the objectName if it
  560. * has been renamed */
  561. std::string objectName = this->GeneratorTarget->GetObjectName(si);
  562. if (!objectName.empty() &&
  563. this->GeneratorTarget->HasExplicitObjectName(si)) {
  564. *fout << " -o " << objectName << std::endl;
  565. }
  566. }
  567. }
  568. } else {
  569. std::vector<cmSourceFile const*> customCommands;
  570. if (ComputeCustomCommandOrder(customCommands)) {
  571. std::string message = "The custom commands for target [" +
  572. this->GeneratorTarget->GetName() + "] had a cycle.\n";
  573. cmSystemTools::Error(message);
  574. } else {
  575. /* Custom targets do not have a dependency on SOURCES files.
  576. * Therefore the dependency list may include SOURCES files after the
  577. * custom target. Because nothing can depend on the custom target just
  578. * move it to the last item.
  579. */
  580. for (auto sf = customCommands.begin(); sf != customCommands.end();
  581. ++sf) {
  582. if (((*sf)->GetLocation()).GetName() == this->Name + ".rule") {
  583. std::rotate(sf, sf + 1, customCommands.end());
  584. break;
  585. }
  586. }
  587. int cmdcount = 0;
  588. for (auto& sf : customCommands) {
  589. const cmCustomCommand* cc = sf->GetCustomCommand();
  590. cmCustomCommandGenerator ccg(*cc, this->ConfigName,
  591. this->LocalGenerator);
  592. // Open the filestream for this custom command
  593. std::string fname = cmStrCat(
  594. this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  595. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
  596. '/', this->Name, "_cc", cmdcount++, '_',
  597. (sf->GetLocation()).GetName(),
  598. this->CmdWindowsShell ? ".bat" : ".sh");
  599. cmGeneratedFileStream f(fname);
  600. f.SetCopyIfDifferent(true);
  601. this->WriteCustomCommandsHelper(f, ccg);
  602. f.Close();
  603. this->WriteCustomCommandLine(*fout, fname, ccg);
  604. }
  605. }
  606. if (this->TagType == GhsMultiGpj::CUSTOM_TARGET) {
  607. this->WriteBuildEvents(*fout);
  608. }
  609. }
  610. }
  611. for (cmGeneratedFileStream* f : gfiles) {
  612. f->Close();
  613. }
  614. }
  615. void cmGhsMultiTargetGenerator::WriteCustomCommandLine(
  616. std::ostream& fout, std::string& fname, cmCustomCommandGenerator const& ccg)
  617. {
  618. /* NOTE: Customization Files are not well documented. Testing showed
  619. * that ":outputName=file" can only be used once per script. The
  620. * script will only run if ":outputName=file" is missing or just run
  621. * once if ":outputName=file" is not specified. If there are
  622. * multiple outputs then the script needs to be listed multiple times
  623. * for each output. Otherwise it won't rerun the script if one of
  624. * the outputs is manually deleted.
  625. */
  626. bool specifyExtra = true;
  627. for (auto& out : ccg.GetOutputs()) {
  628. fout << fname << std::endl;
  629. fout << " :outputName=\"" << out << "\"" << std::endl;
  630. if (specifyExtra) {
  631. for (auto& byp : ccg.GetByproducts()) {
  632. fout << " :extraOutputFile=\"" << byp << "\"" << std::endl;
  633. }
  634. for (auto& dep : ccg.GetDepends()) {
  635. fout << " :depends=\"" << dep << "\"" << std::endl;
  636. }
  637. specifyExtra = false;
  638. }
  639. }
  640. }
  641. void cmGhsMultiTargetGenerator::WriteObjectLangOverride(
  642. std::ostream& fout, const cmSourceFile* sourceFile)
  643. {
  644. const char* rawLangProp = sourceFile->GetProperty("LANGUAGE");
  645. if (nullptr != rawLangProp) {
  646. std::string sourceLangProp(rawLangProp);
  647. std::string const& extension = sourceFile->GetExtension();
  648. if ("CXX" == sourceLangProp && ("c" == extension || "C" == extension)) {
  649. fout << " -dotciscxx" << std::endl;
  650. }
  651. }
  652. }
  653. bool cmGhsMultiTargetGenerator::DetermineIfIntegrityApp()
  654. {
  655. const char* p = this->GeneratorTarget->GetProperty("ghs_integrity_app");
  656. if (p) {
  657. return cmIsOn(this->GeneratorTarget->GetProperty("ghs_integrity_app"));
  658. }
  659. std::vector<cmSourceFile*> sources;
  660. this->GeneratorTarget->GetSourceFiles(sources, this->ConfigName);
  661. for (auto& sf : sources) {
  662. if ("int" == sf->GetExtension()) {
  663. return true;
  664. }
  665. }
  666. return false;
  667. }
  668. bool cmGhsMultiTargetGenerator::ComputeCustomCommandOrder(
  669. std::vector<cmSourceFile const*>& order)
  670. {
  671. std::set<cmSourceFile const*> temp;
  672. std::set<cmSourceFile const*> perm;
  673. // Collect all custom commands for this target
  674. std::vector<cmSourceFile const*> customCommands;
  675. this->GeneratorTarget->GetCustomCommands(customCommands, this->ConfigName);
  676. for (cmSourceFile const* si : customCommands) {
  677. bool r = VisitCustomCommand(temp, perm, order, si);
  678. if (r) {
  679. return r;
  680. }
  681. }
  682. return false;
  683. }
  684. bool cmGhsMultiTargetGenerator::VisitCustomCommand(
  685. std::set<cmSourceFile const*>& temp, std::set<cmSourceFile const*>& perm,
  686. std::vector<cmSourceFile const*>& order, cmSourceFile const* si)
  687. {
  688. /* check if permanent mark is set*/
  689. if (perm.find(si) == perm.end()) {
  690. /* set temporary mark; check if revisit*/
  691. if (temp.insert(si).second) {
  692. for (auto& di : si->GetCustomCommand()->GetDepends()) {
  693. cmSourceFile const* sf = this->GeneratorTarget->GetLocalGenerator()
  694. ->GetMakefile()
  695. ->GetSourceFileWithOutput(di);
  696. /* if sf exists then visit */
  697. if (sf && this->VisitCustomCommand(temp, perm, order, sf)) {
  698. return true;
  699. }
  700. }
  701. /* mark as complete; insert into beginning of list*/
  702. perm.insert(si);
  703. order.push_back(si);
  704. return false;
  705. }
  706. /* revisiting item - not a DAG */
  707. return true;
  708. }
  709. /* already complete */
  710. return false;
  711. }