cmGhsMultiTargetGenerator.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 "cmComputeLinkInformation.h"
  5. #include "cmGeneratedFileStream.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmGlobalGhsMultiGenerator.h"
  8. #include "cmLinkLineComputer.h"
  9. #include "cmLocalGhsMultiGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmSourceFile.h"
  12. #include "cmSourceGroup.h"
  13. #include "cmTarget.h"
  14. cmGhsMultiTargetGenerator::cmGhsMultiTargetGenerator(cmGeneratorTarget* target)
  15. : GeneratorTarget(target)
  16. , LocalGenerator(
  17. static_cast<cmLocalGhsMultiGenerator*>(target->GetLocalGenerator()))
  18. , Makefile(target->Target->GetMakefile())
  19. , Name(target->GetName())
  20. {
  21. // Store the configuration name that is being used
  22. if (const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE")) {
  23. // Use the build type given by the user.
  24. this->ConfigName = config;
  25. } else {
  26. // No configuration type given.
  27. this->ConfigName.clear();
  28. }
  29. }
  30. cmGhsMultiTargetGenerator::~cmGhsMultiTargetGenerator()
  31. {
  32. }
  33. void cmGhsMultiTargetGenerator::Generate()
  34. {
  35. // Determine type of target for this project
  36. switch (this->GeneratorTarget->GetType()) {
  37. case cmStateEnums::EXECUTABLE: {
  38. // Get the name of the executable to generate.
  39. std::string targetName;
  40. std::string targetNameImport;
  41. std::string targetNamePDB;
  42. this->GeneratorTarget->GetExecutableNames(
  43. targetName, this->TargetNameReal, targetNameImport, targetNamePDB,
  44. this->ConfigName);
  45. if (cmGhsMultiTargetGenerator::DetermineIfIntegrityApp()) {
  46. this->TagType = GhsMultiGpj::INTERGRITY_APPLICATION;
  47. } else {
  48. this->TagType = GhsMultiGpj::PROGRAM;
  49. }
  50. break;
  51. }
  52. case cmStateEnums::STATIC_LIBRARY: {
  53. std::string targetName;
  54. std::string targetNameSO;
  55. std::string targetNameImport;
  56. std::string targetNamePDB;
  57. this->GeneratorTarget->GetLibraryNames(
  58. targetName, targetNameSO, this->TargetNameReal, targetNameImport,
  59. targetNamePDB, this->ConfigName);
  60. this->TagType = GhsMultiGpj::LIBRARY;
  61. break;
  62. }
  63. case cmStateEnums::SHARED_LIBRARY: {
  64. std::string msg = "add_library(<name> SHARED ...) not supported: ";
  65. msg += this->Name;
  66. cmSystemTools::Message(msg);
  67. return;
  68. }
  69. case cmStateEnums::OBJECT_LIBRARY: {
  70. std::string targetName;
  71. std::string targetNameSO;
  72. std::string targetNameImport;
  73. std::string targetNamePDB;
  74. this->GeneratorTarget->GetLibraryNames(
  75. targetName, targetNameSO, this->TargetNameReal, targetNameImport,
  76. targetNamePDB, this->ConfigName);
  77. this->TagType = GhsMultiGpj::SUBPROJECT;
  78. break;
  79. }
  80. case cmStateEnums::MODULE_LIBRARY: {
  81. std::string msg = "add_library(<name> MODULE ...) not supported: ";
  82. msg += this->Name;
  83. cmSystemTools::Message(msg);
  84. return;
  85. }
  86. case cmStateEnums::UTILITY: {
  87. std::string msg = "add_custom_target(<name> ...) not supported: ";
  88. msg += this->Name;
  89. cmSystemTools::Message(msg);
  90. return;
  91. }
  92. default:
  93. return;
  94. }
  95. // Tell the global generator the name of the project file
  96. this->GeneratorTarget->Target->SetProperty("GENERATOR_FILE_NAME",
  97. this->Name.c_str());
  98. this->GeneratorTarget->Target->SetProperty(
  99. "GENERATOR_FILE_NAME_EXT", GhsMultiGpj::GetGpjTag(this->TagType));
  100. this->GenerateTarget();
  101. }
  102. void cmGhsMultiTargetGenerator::GenerateTarget()
  103. {
  104. // Open the filestream in copy-if-different mode.
  105. std::string fname = this->LocalGenerator->GetCurrentBinaryDirectory();
  106. fname += "/";
  107. fname += this->Name;
  108. fname += cmGlobalGhsMultiGenerator::FILE_EXTENSION;
  109. cmGeneratedFileStream fout(fname.c_str());
  110. fout.SetCopyIfDifferent(true);
  111. this->GetGlobalGenerator()->WriteFileHeader(fout);
  112. GhsMultiGpj::WriteGpjTag(this->TagType, fout);
  113. const std::string language(
  114. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName));
  115. this->WriteTargetSpecifics(fout, this->ConfigName);
  116. this->SetCompilerFlags(this->ConfigName, language);
  117. this->WriteCompilerFlags(fout, this->ConfigName, language);
  118. this->WriteCompilerDefinitions(fout, this->ConfigName, language);
  119. this->WriteIncludes(fout, this->ConfigName, language);
  120. this->WriteTargetLinkLine(fout, this->ConfigName);
  121. this->WriteCustomCommands(fout);
  122. this->WriteSources(fout);
  123. this->WriteReferences(fout);
  124. fout.Close();
  125. }
  126. cmGlobalGhsMultiGenerator* cmGhsMultiTargetGenerator::GetGlobalGenerator()
  127. const
  128. {
  129. return static_cast<cmGlobalGhsMultiGenerator*>(
  130. this->LocalGenerator->GetGlobalGenerator());
  131. }
  132. void cmGhsMultiTargetGenerator::WriteTargetSpecifics(std::ostream& fout,
  133. const std::string& config)
  134. {
  135. std::string outpath;
  136. std::string rootpath = this->LocalGenerator->GetCurrentBinaryDirectory();
  137. if (this->TagType != GhsMultiGpj::SUBPROJECT) {
  138. // set target binary file destination
  139. outpath = this->GeneratorTarget->GetDirectory(config);
  140. outpath =
  141. this->LocalGenerator->MaybeConvertToRelativePath(rootpath, outpath);
  142. fout << " :binDirRelative=\"" << outpath << "\"" << std::endl;
  143. fout << " -o \"" << this->TargetNameReal << "\"" << std::endl;
  144. }
  145. // set target object file destination
  146. outpath = this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  147. fout << " :outputDirRelative=\"" << outpath << "\"" << std::endl;
  148. }
  149. void cmGhsMultiTargetGenerator::SetCompilerFlags(std::string const& config,
  150. const std::string& language)
  151. {
  152. std::map<std::string, std::string>::iterator i =
  153. this->FlagsByLanguage.find(language);
  154. if (i == this->FlagsByLanguage.end()) {
  155. std::string flags;
  156. const char* lang = language.c_str();
  157. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget, lang,
  158. config);
  159. this->LocalGenerator->AddCMP0018Flags(flags, this->GeneratorTarget, lang,
  160. config);
  161. this->LocalGenerator->AddVisibilityPresetFlags(
  162. flags, this->GeneratorTarget, lang);
  163. // Append old-style preprocessor definition flags.
  164. if (this->Makefile->GetDefineFlags() != " ") {
  165. this->LocalGenerator->AppendFlags(flags,
  166. this->Makefile->GetDefineFlags());
  167. }
  168. // Add target-specific flags.
  169. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, lang,
  170. config);
  171. std::map<std::string, std::string>::value_type entry(language, flags);
  172. i = this->FlagsByLanguage.insert(entry).first;
  173. }
  174. }
  175. std::string cmGhsMultiTargetGenerator::GetDefines(const std::string& language,
  176. std::string const& config)
  177. {
  178. std::map<std::string, std::string>::iterator i =
  179. this->DefinesByLanguage.find(language);
  180. if (i == this->DefinesByLanguage.end()) {
  181. std::set<std::string> defines;
  182. const char* lang = language.c_str();
  183. // Add preprocessor definitions for this target and configuration.
  184. this->LocalGenerator->GetTargetDefines(this->GeneratorTarget, config,
  185. language, defines);
  186. std::string definesString;
  187. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  188. std::map<std::string, std::string>::value_type entry(language,
  189. definesString);
  190. i = this->DefinesByLanguage.insert(entry).first;
  191. }
  192. return i->second;
  193. }
  194. void cmGhsMultiTargetGenerator::WriteCompilerFlags(std::ostream& fout,
  195. std::string const&,
  196. const std::string& language)
  197. {
  198. std::map<std::string, std::string>::iterator flagsByLangI =
  199. this->FlagsByLanguage.find(language);
  200. if (flagsByLangI != this->FlagsByLanguage.end()) {
  201. if (!flagsByLangI->second.empty()) {
  202. std::vector<std::string> ghsCompFlags =
  203. cmSystemTools::ParseArguments(flagsByLangI->second.c_str());
  204. for (auto& f : ghsCompFlags) {
  205. fout << " " << f << std::endl;
  206. }
  207. }
  208. }
  209. }
  210. void cmGhsMultiTargetGenerator::WriteCompilerDefinitions(
  211. std::ostream& fout, const std::string& config, const std::string& language)
  212. {
  213. std::vector<std::string> compileDefinitions;
  214. this->GeneratorTarget->GetCompileDefinitions(compileDefinitions, config,
  215. language);
  216. for (std::string const& compileDefinition : compileDefinitions) {
  217. fout << " -D" << compileDefinition << std::endl;
  218. }
  219. }
  220. void cmGhsMultiTargetGenerator::WriteIncludes(std::ostream& fout,
  221. const std::string& config,
  222. const std::string& language)
  223. {
  224. std::vector<std::string> includes;
  225. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  226. language, config);
  227. for (std::string const& include : includes) {
  228. fout << " -I\"" << include << "\"" << std::endl;
  229. }
  230. }
  231. void cmGhsMultiTargetGenerator::WriteTargetLinkLine(std::ostream& fout,
  232. std::string const& config)
  233. {
  234. if (this->TagType == GhsMultiGpj::INTERGRITY_APPLICATION) {
  235. return;
  236. }
  237. std::string linkLibraries;
  238. std::string flags;
  239. std::string linkFlags;
  240. std::string frameworkPath;
  241. std::string linkPath;
  242. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  243. this->GetGlobalGenerator()->CreateLinkLineComputer(
  244. this->LocalGenerator,
  245. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  246. this->LocalGenerator->GetTargetFlags(
  247. linkLineComputer.get(), config, linkLibraries, flags, linkFlags,
  248. frameworkPath, linkPath, this->GeneratorTarget);
  249. // write out link options
  250. std::vector<std::string> lopts =
  251. cmSystemTools::ParseArguments(linkFlags.c_str());
  252. for (auto& l : lopts) {
  253. fout << " " << l << std::endl;
  254. }
  255. // write out link search paths
  256. // must be quoted for paths that contain spaces
  257. std::vector<std::string> lpath =
  258. cmSystemTools::ParseArguments(linkPath.c_str());
  259. for (auto& l : lpath) {
  260. fout << " -L\"" << l << "\"" << std::endl;
  261. }
  262. // write out link libs
  263. // must be quoted for filepaths that contains spaces
  264. std::string cbd = this->LocalGenerator->GetCurrentBinaryDirectory();
  265. std::vector<std::string> llibs =
  266. cmSystemTools::ParseArguments(linkLibraries.c_str());
  267. for (auto& l : llibs) {
  268. if (l.compare(0, 2, "-l") == 0) {
  269. fout << " \"" << l << "\"" << std::endl;
  270. } else {
  271. std::string rl = cmSystemTools::CollapseCombinedPath(cbd, l);
  272. fout << " -l\"" << rl << "\"" << std::endl;
  273. }
  274. }
  275. }
  276. void cmGhsMultiTargetGenerator::WriteCustomCommands(std::ostream& fout)
  277. {
  278. WriteCustomCommandsHelper(fout, this->GeneratorTarget->GetPreBuildCommands(),
  279. cmTarget::PRE_BUILD);
  280. WriteCustomCommandsHelper(
  281. fout, this->GeneratorTarget->GetPostBuildCommands(), cmTarget::POST_BUILD);
  282. }
  283. void cmGhsMultiTargetGenerator::WriteCustomCommandsHelper(
  284. std::ostream& fout, std::vector<cmCustomCommand> const& commandsSet,
  285. cmTarget::CustomCommandType const commandType)
  286. {
  287. for (cmCustomCommand const& customCommand : commandsSet) {
  288. cmCustomCommandLines const& commandLines = customCommand.GetCommandLines();
  289. for (cmCustomCommandLine const& command : commandLines) {
  290. switch (commandType) {
  291. case cmTarget::PRE_BUILD:
  292. fout << " :preexecShellSafe=";
  293. break;
  294. case cmTarget::POST_BUILD:
  295. fout << " :postexecShellSafe=";
  296. break;
  297. default:
  298. assert("Only pre and post are supported");
  299. }
  300. bool firstIteration = true;
  301. for (std::string const& commandLine : command) {
  302. std::string subCommandE =
  303. this->LocalGenerator->EscapeForShell(commandLine, true);
  304. fout << (firstIteration ? "'" : " ");
  305. // Need to double escape backslashes
  306. cmSystemTools::ReplaceString(subCommandE, "\\", "\\\\");
  307. fout << subCommandE;
  308. firstIteration = false;
  309. }
  310. if (!command.empty()) {
  311. fout << "'" << std::endl;
  312. }
  313. }
  314. }
  315. }
  316. void cmGhsMultiTargetGenerator::WriteSourceProperty(std::ostream& fout,
  317. const cmSourceFile* sf,
  318. std::string propName,
  319. std::string propFlag)
  320. {
  321. const char* prop = sf->GetProperty(propName);
  322. if (prop) {
  323. std::vector<std::string> list;
  324. cmSystemTools::ExpandListArgument(prop, list);
  325. for (auto& p : list) {
  326. fout << " " << propFlag << p << std::endl;
  327. }
  328. }
  329. }
  330. void cmGhsMultiTargetGenerator::WriteSources(std::ostream& fout_proj)
  331. {
  332. /* vector of all sources for this target */
  333. std::vector<cmSourceFile*> sources;
  334. this->GeneratorTarget->GetSourceFiles(sources, this->ConfigName);
  335. /* vector of all groups defined for this target
  336. * -- but the vector is not expanded with sub groups or in any useful order
  337. */
  338. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  339. /* for each source file assign it to its group */
  340. std::map<std::string, std::vector<cmSourceFile*>> groupFiles;
  341. std::set<std::string> groupNames;
  342. for (auto& sf : sources) {
  343. cmSourceGroup* sourceGroup =
  344. this->Makefile->FindSourceGroup(sf->GetFullPath(), sourceGroups);
  345. std::string gn = sourceGroup->GetFullName();
  346. groupFiles[gn].push_back(sf);
  347. groupNames.insert(gn);
  348. }
  349. /* list of known groups and the order they are displayed in a project file */
  350. const std::vector<std::string> standardGroups = {
  351. "Header Files", "Source Files", "CMake Rules",
  352. "Object Files", "Object Libraries", "Resources"
  353. };
  354. /* list of groups in the order they are displayed in a project file*/
  355. std::vector<std::string> groupFilesList(groupFiles.size());
  356. /* put the groups in the order they should be listed
  357. * - standard groups first, and then everything else
  358. * in the order used by std::map.
  359. */
  360. int i = 0;
  361. for (const std::string& gn : standardGroups) {
  362. auto n = groupNames.find(gn);
  363. if (n != groupNames.end()) {
  364. groupFilesList[i] = *n;
  365. i += 1;
  366. groupNames.erase(gn);
  367. }
  368. }
  369. { /* catch-all group - is last item */
  370. std::string gn = "";
  371. auto n = groupNames.find(gn);
  372. if (n != groupNames.end()) {
  373. groupFilesList.back() = *n;
  374. groupNames.erase(gn);
  375. }
  376. }
  377. for (auto& n : groupNames) {
  378. groupFilesList[i] = n;
  379. i += 1;
  380. }
  381. /* sort the files within each group */
  382. for (auto& n : groupFilesList) {
  383. std::sort(groupFiles[n].begin(), groupFiles[n].end(),
  384. [](cmSourceFile* l, cmSourceFile* r) {
  385. return l->GetFullPath() < r->GetFullPath();
  386. });
  387. }
  388. /* list of open project files */
  389. std::vector<cmGeneratedFileStream*> gfiles;
  390. /* write files into the proper project file
  391. * -- groups go into main project file
  392. * unless FOLDER property or variable is set.
  393. */
  394. for (auto& sg : groupFilesList) {
  395. std::ostream* fout;
  396. bool useProjectFile =
  397. cmSystemTools::IsOn(
  398. this->GeneratorTarget->GetProperty("GHS_NO_SOURCE_GROUP_FILE")) ||
  399. cmSystemTools::IsOn(
  400. this->Makefile->GetDefinition("GHS_NO_SOURCE_GROUP_FILE"));
  401. if (useProjectFile || sg.empty()) {
  402. fout = &fout_proj;
  403. } else {
  404. // Open the filestream in copy-if-different mode.
  405. std::string gname = sg;
  406. cmsys::SystemTools::ReplaceString(gname, "\\", "_");
  407. std::string lpath =
  408. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  409. lpath += "/";
  410. lpath += gname;
  411. lpath += cmGlobalGhsMultiGenerator::FILE_EXTENSION;
  412. std::string fpath = this->LocalGenerator->GetCurrentBinaryDirectory();
  413. fpath += "/";
  414. fpath += lpath;
  415. cmGeneratedFileStream* f = new cmGeneratedFileStream(fpath.c_str());
  416. f->SetCopyIfDifferent(true);
  417. gfiles.push_back(f);
  418. fout = f;
  419. this->GetGlobalGenerator()->WriteFileHeader(*f);
  420. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::SUBPROJECT, *f);
  421. fout_proj << lpath << " ";
  422. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::SUBPROJECT, fout_proj);
  423. }
  424. if (useProjectFile) {
  425. if (sg.empty()) {
  426. *fout << "{comment} Others" << std::endl;
  427. } else {
  428. *fout << "{comment} " << sg << std::endl;
  429. }
  430. }
  431. /* output rule for each source file */
  432. for (const cmSourceFile* si : groupFiles[sg]) {
  433. // Convert filename to native system
  434. // WORKAROUND: GHS MULTI 6.1.4 and 6.1.6 are known to need backslash on
  435. // windows when opening some files from the search window.
  436. std::string fname(si->GetFullPath());
  437. cmSystemTools::ConvertToOutputSlashes(fname);
  438. *fout << fname << std::endl;
  439. if ("ld" != si->GetExtension() && "int" != si->GetExtension() &&
  440. "bsp" != si->GetExtension()) {
  441. this->WriteObjectLangOverride(*fout, si);
  442. }
  443. this->WriteSourceProperty(*fout, si, "INCLUDE_DIRECTORIES", "-I");
  444. this->WriteSourceProperty(*fout, si, "COMPILE_DEFINITIONS", "-D");
  445. this->WriteSourceProperty(*fout, si, "COMPILE_OPTIONS", "");
  446. /* to avoid clutter in the gui only print out the objectName if it has
  447. * been renamed */
  448. std::string objectName = this->GeneratorTarget->GetObjectName(si);
  449. if (!objectName.empty() &&
  450. this->GeneratorTarget->HasExplicitObjectName(si)) {
  451. *fout << " -o " << objectName << std::endl;
  452. }
  453. }
  454. }
  455. for (cmGeneratedFileStream* f : gfiles) {
  456. f->Close();
  457. }
  458. }
  459. void cmGhsMultiTargetGenerator::WriteObjectLangOverride(
  460. std::ostream& fout, const cmSourceFile* sourceFile)
  461. {
  462. const char* rawLangProp = sourceFile->GetProperty("LANGUAGE");
  463. if (NULL != rawLangProp) {
  464. std::string sourceLangProp(rawLangProp);
  465. std::string extension(sourceFile->GetExtension());
  466. if ("CXX" == sourceLangProp && ("c" == extension || "C" == extension)) {
  467. fout << " -dotciscxx" << std::endl;
  468. }
  469. }
  470. }
  471. void cmGhsMultiTargetGenerator::WriteReferences(std::ostream& fout)
  472. {
  473. // This only applies to INTEGRITY Applications
  474. if (this->TagType != GhsMultiGpj::INTERGRITY_APPLICATION) {
  475. return;
  476. }
  477. // Get the targets that this one depends upon
  478. cmTargetDependSet unordered =
  479. this->GetGlobalGenerator()->GetTargetDirectDepends(this->GeneratorTarget);
  480. cmGlobalGhsMultiGenerator::OrderedTargetDependSet ordered(unordered,
  481. this->Name);
  482. for (auto& t : ordered) {
  483. std::string tname = t->GetName();
  484. std::string tpath = t->LocalGenerator->GetCurrentBinaryDirectory();
  485. std::string rootpath = this->LocalGenerator->GetCurrentBinaryDirectory();
  486. std::string outpath =
  487. this->LocalGenerator->MaybeConvertToRelativePath(rootpath, tpath) + "/" +
  488. tname + "REF" + cmGlobalGhsMultiGenerator::FILE_EXTENSION;
  489. fout << outpath;
  490. fout << " ";
  491. GhsMultiGpj::WriteGpjTag(GhsMultiGpj::REFERENCE, fout);
  492. // Tell the global generator that a refernce project needs to be created
  493. t->Target->SetProperty("GHS_REFERENCE_PROJECT", "ON");
  494. }
  495. }
  496. bool cmGhsMultiTargetGenerator::DetermineIfIntegrityApp(void)
  497. {
  498. const char* p = this->GeneratorTarget->GetProperty("ghs_integrity_app");
  499. if (p) {
  500. return cmSystemTools::IsOn(
  501. this->GeneratorTarget->GetProperty("ghs_integrity_app"));
  502. } else {
  503. std::vector<cmSourceFile*> sources;
  504. this->GeneratorTarget->GetSourceFiles(sources, this->ConfigName);
  505. for (auto& sf : sources) {
  506. if ("int" == sf->GetExtension()) {
  507. return true;
  508. }
  509. }
  510. return false;
  511. }
  512. }