cmGhsMultiTargetGenerator.cxx 27 KB

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