cmGhsMultiTargetGenerator.cxx 27 KB

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