cmGhsMultiTargetGenerator.cxx 27 KB

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