cmGhsMultiTargetGenerator.cxx 27 KB

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