cmGhsMultiTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 "cmSystemTools.h"
  21. #include "cmTarget.h"
  22. #include <algorithm>
  23. #include <ostream>
  24. #include <set>
  25. #include <utility>
  26. cmGhsMultiTargetGenerator::cmGhsMultiTargetGenerator(cmGeneratorTarget* target)
  27. : GeneratorTarget(target)
  28. , LocalGenerator(
  29. static_cast<cmLocalGhsMultiGenerator*>(target->GetLocalGenerator()))
  30. , Makefile(target->Target->GetMakefile())
  31. , Name(target->GetName())
  32. #ifdef _WIN32
  33. , CmdWindowsShell(true)
  34. #else
  35. , CmdWindowsShell(false)
  36. #endif
  37. {
  38. // Store the configuration name that is being used
  39. if (const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE")) {
  40. // Use the build type given by the user.
  41. this->ConfigName = config;
  42. } else {
  43. // No configuration type given.
  44. this->ConfigName.clear();
  45. }
  46. }
  47. cmGhsMultiTargetGenerator::~cmGhsMultiTargetGenerator() = default;
  48. void cmGhsMultiTargetGenerator::Generate()
  49. {
  50. // Determine type of target for this project
  51. switch (this->GeneratorTarget->GetType()) {
  52. case cmStateEnums::EXECUTABLE: {
  53. // Get the name of the executable to generate.
  54. this->TargetNameReal =
  55. this->GeneratorTarget->GetExecutableNames(this->ConfigName).Real;
  56. if (cmGhsMultiTargetGenerator::DetermineIfIntegrityApp()) {
  57. this->TagType = GhsMultiGpj::INTERGRITY_APPLICATION;
  58. } else {
  59. this->TagType = GhsMultiGpj::PROGRAM;
  60. }
  61. break;
  62. }
  63. case cmStateEnums::STATIC_LIBRARY: {
  64. this->TargetNameReal =
  65. this->GeneratorTarget->GetLibraryNames(this->ConfigName).Real;
  66. this->TagType = GhsMultiGpj::LIBRARY;
  67. break;
  68. }
  69. case cmStateEnums::SHARED_LIBRARY: {
  70. std::string msg = "add_library(<name> SHARED ...) not supported: ";
  71. msg += this->Name;
  72. cmSystemTools::Message(msg);
  73. return;
  74. }
  75. case cmStateEnums::OBJECT_LIBRARY: {
  76. this->TargetNameReal =
  77. this->GeneratorTarget->GetLibraryNames(this->ConfigName).Real;
  78. this->TagType = GhsMultiGpj::SUBPROJECT;
  79. break;
  80. }
  81. case cmStateEnums::MODULE_LIBRARY: {
  82. std::string msg = "add_library(<name> MODULE ...) not supported: ";
  83. msg += this->Name;
  84. cmSystemTools::Message(msg);
  85. return;
  86. }
  87. case cmStateEnums::UTILITY: {
  88. this->TargetNameReal = this->GeneratorTarget->GetName();
  89. this->TagType = GhsMultiGpj::CUSTOM_TARGET;
  90. break;
  91. }
  92. case cmStateEnums::GLOBAL_TARGET: {
  93. this->TargetNameReal = this->GeneratorTarget->GetName();
  94. if (this->TargetNameReal ==
  95. this->GetGlobalGenerator()->GetInstallTargetName()) {
  96. this->TagType = GhsMultiGpj::CUSTOM_TARGET;
  97. } else {
  98. return;
  99. }
  100. break;
  101. }
  102. default:
  103. return;
  104. }
  105. // Tell the global generator the name of the project file
  106. this->GeneratorTarget->Target->SetProperty("GENERATOR_FILE_NAME",
  107. this->Name.c_str());
  108. this->GeneratorTarget->Target->SetProperty(
  109. "GENERATOR_FILE_NAME_EXT", GhsMultiGpj::GetGpjTag(this->TagType));
  110. this->GenerateTarget();
  111. }
  112. void cmGhsMultiTargetGenerator::GenerateTarget()
  113. {
  114. // Open the target file in copy-if-different mode.
  115. std::string fproj = this->LocalGenerator->GetCurrentBinaryDirectory();
  116. fproj += "/";
  117. fproj += this->Name;
  118. fproj += cmGlobalGhsMultiGenerator::FILE_EXTENSION;
  119. cmGeneratedFileStream fout(fproj);
  120. fout.SetCopyIfDifferent(true);
  121. this->GetGlobalGenerator()->WriteFileHeader(fout);
  122. GhsMultiGpj::WriteGpjTag(this->TagType, fout);
  123. if (this->TagType != GhsMultiGpj::CUSTOM_TARGET) {
  124. const std::string language(
  125. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName));
  126. this->WriteTargetSpecifics(fout, this->ConfigName);
  127. this->SetCompilerFlags(this->ConfigName, language);
  128. this->WriteCompilerFlags(fout, this->ConfigName, language);
  129. this->WriteCompilerDefinitions(fout, this->ConfigName, language);
  130. this->WriteIncludes(fout, this->ConfigName, language);
  131. this->WriteTargetLinkLine(fout, this->ConfigName);
  132. this->WriteBuildEvents(fout);
  133. }
  134. this->WriteSources(fout);
  135. fout.Close();
  136. }
  137. cmGlobalGhsMultiGenerator* cmGhsMultiTargetGenerator::GetGlobalGenerator()
  138. const
  139. {
  140. return static_cast<cmGlobalGhsMultiGenerator*>(
  141. this->LocalGenerator->GetGlobalGenerator());
  142. }
  143. void cmGhsMultiTargetGenerator::WriteTargetSpecifics(std::ostream& fout,
  144. const std::string& config)
  145. {
  146. std::string outpath;
  147. std::string rootpath = this->LocalGenerator->GetCurrentBinaryDirectory();
  148. if (this->TagType != GhsMultiGpj::SUBPROJECT) {
  149. // set target binary file destination
  150. outpath = this->GeneratorTarget->GetDirectory(config);
  151. outpath =
  152. this->LocalGenerator->MaybeConvertToRelativePath(rootpath, outpath);
  153. fout << " :binDirRelative=\"" << outpath << "\"" << std::endl;
  154. fout << " -o \"" << this->TargetNameReal << "\"" << std::endl;
  155. }
  156. // set target object file destination
  157. outpath = this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  158. fout << " :outputDirRelative=\"" << outpath << "\"" << std::endl;
  159. }
  160. void cmGhsMultiTargetGenerator::SetCompilerFlags(std::string const& config,
  161. const std::string& language)
  162. {
  163. std::map<std::string, std::string>::iterator i =
  164. this->FlagsByLanguage.find(language);
  165. if (i == this->FlagsByLanguage.end()) {
  166. std::string flags;
  167. const char* lang = language.c_str();
  168. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget, lang,
  169. config);
  170. this->LocalGenerator->AddCMP0018Flags(flags, this->GeneratorTarget, lang,
  171. config);
  172. this->LocalGenerator->AddVisibilityPresetFlags(
  173. flags, this->GeneratorTarget, lang);
  174. // Append old-style preprocessor definition flags.
  175. if (this->Makefile->GetDefineFlags() != " ") {
  176. this->LocalGenerator->AppendFlags(flags,
  177. this->Makefile->GetDefineFlags());
  178. }
  179. // Add target-specific flags.
  180. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, lang,
  181. config);
  182. std::map<std::string, std::string>::value_type entry(language, flags);
  183. i = this->FlagsByLanguage.insert(entry).first;
  184. }
  185. }
  186. std::string cmGhsMultiTargetGenerator::GetDefines(const std::string& language,
  187. std::string const& config)
  188. {
  189. std::map<std::string, std::string>::iterator i =
  190. 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. std::map<std::string, std::string>::iterator flagsByLangI =
  210. this->FlagsByLanguage.find(language);
  211. if (flagsByLangI != this->FlagsByLanguage.end()) {
  212. if (!flagsByLangI->second.empty()) {
  213. std::vector<std::string> ghsCompFlags =
  214. cmSystemTools::ParseArguments(flagsByLangI->second);
  215. for (auto& f : ghsCompFlags) {
  216. fout << " " << f << std::endl;
  217. }
  218. }
  219. }
  220. }
  221. void cmGhsMultiTargetGenerator::WriteCompilerDefinitions(
  222. std::ostream& fout, const std::string& config, const std::string& language)
  223. {
  224. std::vector<std::string> compileDefinitions;
  225. this->GeneratorTarget->GetCompileDefinitions(compileDefinitions, config,
  226. language);
  227. for (std::string const& compileDefinition : compileDefinitions) {
  228. fout << " -D" << compileDefinition << std::endl;
  229. }
  230. }
  231. void cmGhsMultiTargetGenerator::WriteIncludes(std::ostream& fout,
  232. const std::string& config,
  233. const std::string& language)
  234. {
  235. std::vector<std::string> includes;
  236. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  237. language, config);
  238. for (std::string const& include : includes) {
  239. fout << " -I\"" << include << "\"" << std::endl;
  240. }
  241. }
  242. void cmGhsMultiTargetGenerator::WriteTargetLinkLine(std::ostream& fout,
  243. std::string const& config)
  244. {
  245. if (this->TagType == GhsMultiGpj::INTERGRITY_APPLICATION) {
  246. return;
  247. }
  248. std::string linkLibraries;
  249. std::string flags;
  250. std::string linkFlags;
  251. std::string frameworkPath;
  252. std::string linkPath;
  253. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  254. this->GetGlobalGenerator()->CreateLinkLineComputer(
  255. this->LocalGenerator,
  256. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  257. this->LocalGenerator->GetTargetFlags(
  258. linkLineComputer.get(), config, linkLibraries, flags, linkFlags,
  259. frameworkPath, linkPath, this->GeneratorTarget);
  260. // write out link options
  261. std::vector<std::string> lopts = cmSystemTools::ParseArguments(linkFlags);
  262. for (auto& l : lopts) {
  263. fout << " " << l << std::endl;
  264. }
  265. // write out link search paths
  266. // must be quoted for paths that contain spaces
  267. std::vector<std::string> lpath = cmSystemTools::ParseArguments(linkPath);
  268. for (auto& l : lpath) {
  269. fout << " -L\"" << l << "\"" << std::endl;
  270. }
  271. // write out link libs
  272. // must be quoted for filepaths that contains spaces
  273. std::string cbd = this->LocalGenerator->GetCurrentBinaryDirectory();
  274. std::vector<std::string> llibs =
  275. cmSystemTools::ParseArguments(linkLibraries);
  276. for (auto& l : llibs) {
  277. if (l.compare(0, 2, "-l") == 0) {
  278. fout << " \"" << l << "\"" << std::endl;
  279. } else {
  280. std::string rl = cmSystemTools::CollapseFullPath(l, cbd);
  281. fout << " -l\"" << rl << "\"" << std::endl;
  282. }
  283. }
  284. }
  285. void cmGhsMultiTargetGenerator::WriteBuildEvents(std::ostream& fout)
  286. {
  287. this->WriteBuildEventsHelper(
  288. fout, this->GeneratorTarget->GetPreBuildCommands(),
  289. std::string("prebuild"), std::string("preexecShell"));
  290. if (this->TagType != GhsMultiGpj::CUSTOM_TARGET) {
  291. this->WriteBuildEventsHelper(
  292. fout, this->GeneratorTarget->GetPreLinkCommands(),
  293. std::string("prelink"), std::string("preexecShell"));
  294. }
  295. this->WriteBuildEventsHelper(
  296. fout, this->GeneratorTarget->GetPostBuildCommands(),
  297. std::string("postbuild"), std::string("postexecShell"));
  298. }
  299. void cmGhsMultiTargetGenerator::WriteBuildEventsHelper(
  300. std::ostream& fout, const std::vector<cmCustomCommand>& ccv,
  301. std::string const& name, std::string const& cmd)
  302. {
  303. int cmdcount = 0;
  304. for (cmCustomCommand const& cc : ccv) {
  305. cmCustomCommandGenerator ccg(cc, this->ConfigName, this->LocalGenerator);
  306. // Open the filestream for this custom command
  307. std::string fname = this->LocalGenerator->GetCurrentBinaryDirectory();
  308. fname +=
  309. "/" + this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  310. fname += "/" + this->Name + "_" + name;
  311. fname += std::to_string(cmdcount++);
  312. fname += this->CmdWindowsShell ? ".bat" : ".sh";
  313. cmGeneratedFileStream f(fname);
  314. f.SetCopyIfDifferent(true);
  315. this->WriteCustomCommandsHelper(f, ccg);
  316. f.Close();
  317. if (this->TagType != GhsMultiGpj::CUSTOM_TARGET) {
  318. fout << " :" << cmd << "=\"" << fname << "\"" << std::endl;
  319. } else {
  320. fout << fname << std::endl;
  321. fout << " :outputName=\"" << fname << ".rule\"" << std::endl;
  322. }
  323. for (auto& byp : ccg.GetByproducts()) {
  324. fout << " :extraOutputFile=\"" << byp << "\"" << std::endl;
  325. }
  326. }
  327. }
  328. void cmGhsMultiTargetGenerator::WriteCustomCommandsHelper(
  329. std::ostream& fout, cmCustomCommandGenerator const& ccg)
  330. {
  331. std::vector<std::string> cmdLines;
  332. // if the command specified a working directory use it.
  333. std::string dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  334. std::string currentBinDir = dir;
  335. std::string workingDir = ccg.GetWorkingDirectory();
  336. if (!workingDir.empty()) {
  337. dir = workingDir;
  338. }
  339. // Line to check for error between commands.
  340. #ifdef _WIN32
  341. std::string check_error = "if %errorlevel% neq 0 exit /b %errorlevel%";
  342. #else
  343. std::string check_error = "if [[ $? -ne 0 ]]; then exit 1; fi";
  344. #endif
  345. #ifdef _WIN32
  346. cmdLines.push_back("@echo off");
  347. #endif
  348. // Echo the custom command's comment text.
  349. const char* comment = ccg.GetComment();
  350. if (comment && *comment) {
  351. std::string echocmd = "echo ";
  352. echocmd += 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 = "./" + cmd;
  396. }
  397. cmd = this->LocalGenerator->ConvertToOutputFormat(
  398. cmd, cmOutputConverter::SHELL);
  399. if (useCall) {
  400. cmd = "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. cmSystemTools::ExpandListArgument(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. cmSystemTools::IsOn(
  501. this->GeneratorTarget->GetProperty("GHS_NO_SOURCE_GROUP_FILE")) ||
  502. cmSystemTools::IsOn(
  503. this->Makefile->GetDefinition("CMAKE_GHS_NO_SOURCE_GROUP_FILE"));
  504. if (useProjectFile || sg.empty()) {
  505. fout = &fout_proj;
  506. } else {
  507. // Open the filestream in copy-if-different mode.
  508. std::string gname = sg;
  509. cmsys::SystemTools::ReplaceString(gname, "\\", "_");
  510. std::string lpath =
  511. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  512. lpath += "/";
  513. lpath += gname;
  514. lpath += cmGlobalGhsMultiGenerator::FILE_EXTENSION;
  515. std::string fpath = this->LocalGenerator->GetCurrentBinaryDirectory();
  516. fpath += "/";
  517. fpath += lpath;
  518. cmGeneratedFileStream* f = new cmGeneratedFileStream(fpath);
  519. f->SetCopyIfDifferent(true);
  520. gfiles.push_back(f);
  521. fout = f;
  522. this->GetGlobalGenerator()->WriteFileHeader(*f);
  523. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::SUBPROJECT, *f);
  524. fout_proj << lpath << " ";
  525. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::SUBPROJECT, fout_proj);
  526. }
  527. if (useProjectFile) {
  528. if (sg.empty()) {
  529. *fout << "{comment} Others" << std::endl;
  530. } else {
  531. *fout << "{comment} " << sg << std::endl;
  532. }
  533. } else if (sg.empty()) {
  534. *fout << "{comment} Others" << std::endl;
  535. }
  536. if (sg != "CMake Rules") {
  537. /* output rule for each source file */
  538. for (const cmSourceFile* si : groupFiles[sg]) {
  539. bool compile = true;
  540. // Convert filename to native system
  541. // WORKAROUND: GHS MULTI 6.1.4 and 6.1.6 are known to need backslash on
  542. // windows when opening some files from the search window.
  543. std::string fname(si->GetFullPath());
  544. cmSystemTools::ConvertToOutputSlashes(fname);
  545. /* For custom targets list any associated sources,
  546. * comment out source code to prevent it from being
  547. * compiled when processing this target.
  548. * Otherwise, comment out any custom command (main) dependencies that
  549. * are listed as source files to prevent them from being considered
  550. * part of the build.
  551. */
  552. std::string comment;
  553. if ((this->TagType == GhsMultiGpj::CUSTOM_TARGET &&
  554. !si->GetLanguage().empty()) ||
  555. si->GetCustomCommand()) {
  556. comment = "{comment} ";
  557. compile = false;
  558. }
  559. *fout << comment << fname << std::endl;
  560. if (compile) {
  561. if ("ld" != si->GetExtension() && "int" != si->GetExtension() &&
  562. "bsp" != si->GetExtension()) {
  563. WriteObjectLangOverride(*fout, si);
  564. }
  565. this->WriteSourceProperty(*fout, si, "INCLUDE_DIRECTORIES", "-I");
  566. this->WriteSourceProperty(*fout, si, "COMPILE_DEFINITIONS", "-D");
  567. this->WriteSourceProperty(*fout, si, "COMPILE_OPTIONS", "");
  568. /* to avoid clutter in the GUI only print out the objectName if it
  569. * has been renamed */
  570. std::string objectName = this->GeneratorTarget->GetObjectName(si);
  571. if (!objectName.empty() &&
  572. this->GeneratorTarget->HasExplicitObjectName(si)) {
  573. *fout << " -o " << objectName << std::endl;
  574. }
  575. }
  576. }
  577. } else {
  578. std::vector<cmSourceFile const*> customCommands;
  579. if (ComputeCustomCommandOrder(customCommands)) {
  580. std::string message = "The custom commands for target [" +
  581. this->GeneratorTarget->GetName() + "] had a cycle.\n";
  582. cmSystemTools::Error(message);
  583. } else {
  584. /* Custom targets do not have a dependency on SOURCES files.
  585. * Therefore the dependency list may include SOURCES files after the
  586. * custom target. Because nothing can depend on the custom target just
  587. * move it to the last item.
  588. */
  589. for (auto sf = customCommands.begin(); sf != customCommands.end();
  590. ++sf) {
  591. if (((*sf)->GetLocation()).GetName() == this->Name + ".rule") {
  592. std::rotate(sf, sf + 1, customCommands.end());
  593. break;
  594. }
  595. }
  596. int cmdcount = 0;
  597. for (auto& sf : customCommands) {
  598. const cmCustomCommand* cc = sf->GetCustomCommand();
  599. cmCustomCommandGenerator ccg(*cc, this->ConfigName,
  600. this->LocalGenerator);
  601. // Open the filestream for this custom command
  602. std::string fname =
  603. this->LocalGenerator->GetCurrentBinaryDirectory();
  604. fname += "/" +
  605. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  606. fname += "/" + this->Name + "_cc";
  607. fname += std::to_string(cmdcount++) + "_";
  608. fname += (sf->GetLocation()).GetName();
  609. fname += this->CmdWindowsShell ? ".bat" : ".sh";
  610. cmGeneratedFileStream f(fname);
  611. f.SetCopyIfDifferent(true);
  612. this->WriteCustomCommandsHelper(f, ccg);
  613. f.Close();
  614. this->WriteCustomCommandLine(*fout, fname, ccg);
  615. }
  616. }
  617. if (this->TagType == GhsMultiGpj::CUSTOM_TARGET) {
  618. this->WriteBuildEvents(*fout);
  619. }
  620. }
  621. }
  622. for (cmGeneratedFileStream* f : gfiles) {
  623. f->Close();
  624. }
  625. }
  626. void cmGhsMultiTargetGenerator::WriteCustomCommandLine(
  627. std::ostream& fout, std::string& fname, cmCustomCommandGenerator const& ccg)
  628. {
  629. /* NOTE: Customization Files are not well documented. Testing showed
  630. * that ":outputName=file" can only be used once per script. The
  631. * script will only run if ":outputName=file" is missing or just run
  632. * once if ":outputName=file" is not specified. If there are
  633. * multiple outputs then the script needs to be listed multiple times
  634. * for each output. Otherwise it won't rerun the script if one of
  635. * the outputs is manually deleted.
  636. */
  637. bool specifyExtra = true;
  638. for (auto& out : ccg.GetOutputs()) {
  639. fout << fname << std::endl;
  640. fout << " :outputName=\"" << out << "\"" << std::endl;
  641. if (specifyExtra) {
  642. for (auto& byp : ccg.GetByproducts()) {
  643. fout << " :extraOutputFile=\"" << byp << "\"" << std::endl;
  644. }
  645. for (auto& dep : ccg.GetDepends()) {
  646. fout << " :depends=\"" << dep << "\"" << std::endl;
  647. }
  648. specifyExtra = false;
  649. }
  650. }
  651. }
  652. void cmGhsMultiTargetGenerator::WriteObjectLangOverride(
  653. std::ostream& fout, const cmSourceFile* sourceFile)
  654. {
  655. const char* rawLangProp = sourceFile->GetProperty("LANGUAGE");
  656. if (nullptr != rawLangProp) {
  657. std::string sourceLangProp(rawLangProp);
  658. std::string const& extension = sourceFile->GetExtension();
  659. if ("CXX" == sourceLangProp && ("c" == extension || "C" == extension)) {
  660. fout << " -dotciscxx" << std::endl;
  661. }
  662. }
  663. }
  664. bool cmGhsMultiTargetGenerator::DetermineIfIntegrityApp()
  665. {
  666. const char* p = this->GeneratorTarget->GetProperty("ghs_integrity_app");
  667. if (p) {
  668. return cmSystemTools::IsOn(
  669. this->GeneratorTarget->GetProperty("ghs_integrity_app"));
  670. }
  671. std::vector<cmSourceFile*> sources;
  672. this->GeneratorTarget->GetSourceFiles(sources, this->ConfigName);
  673. for (auto& sf : sources) {
  674. if ("int" == sf->GetExtension()) {
  675. return true;
  676. }
  677. }
  678. return false;
  679. }
  680. bool cmGhsMultiTargetGenerator::ComputeCustomCommandOrder(
  681. std::vector<cmSourceFile const*>& order)
  682. {
  683. std::set<cmSourceFile const*> temp;
  684. std::set<cmSourceFile const*> perm;
  685. // Collect all custom commands for this target
  686. std::vector<cmSourceFile const*> customCommands;
  687. this->GeneratorTarget->GetCustomCommands(customCommands, this->ConfigName);
  688. for (cmSourceFile const* si : customCommands) {
  689. bool r = VisitCustomCommand(temp, perm, order, si);
  690. if (r) {
  691. return r;
  692. }
  693. }
  694. return false;
  695. }
  696. bool cmGhsMultiTargetGenerator::VisitCustomCommand(
  697. std::set<cmSourceFile const*>& temp, std::set<cmSourceFile const*>& perm,
  698. std::vector<cmSourceFile const*>& order, cmSourceFile const* si)
  699. {
  700. /* check if permanent mark is set*/
  701. if (perm.find(si) == perm.end()) {
  702. /* set temporary mark; check if revisit*/
  703. if (temp.insert(si).second) {
  704. for (auto& di : si->GetCustomCommand()->GetDepends()) {
  705. cmSourceFile const* sf = this->GeneratorTarget->GetLocalGenerator()
  706. ->GetMakefile()
  707. ->GetSourceFileWithOutput(di);
  708. /* if sf exists then visit */
  709. if (sf && this->VisitCustomCommand(temp, perm, order, sf)) {
  710. return true;
  711. }
  712. }
  713. /* mark as complete; insert into beginning of list*/
  714. perm.insert(si);
  715. order.push_back(si);
  716. return false;
  717. }
  718. /* revisiting item - not a DAG */
  719. return true;
  720. }
  721. /* already complete */
  722. return false;
  723. }