cmCommonTargetGenerator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2015 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCommonTargetGenerator.h"
  11. #include "cmComputeLinkInformation.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalCommonGenerator.h"
  14. #include "cmLocalCommonGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSourceFile.h"
  17. #include "cmSystemTools.h"
  18. cmCommonTargetGenerator::cmCommonTargetGenerator(
  19. cmOutputConverter::RelativeRoot wd,
  20. cmGeneratorTarget* gt
  21. )
  22. : WorkingDirectory(wd)
  23. , GeneratorTarget(gt)
  24. , Makefile(gt->Makefile)
  25. , LocalGenerator(static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
  26. , GlobalGenerator(static_cast<cmGlobalCommonGenerator*>(
  27. gt->LocalGenerator->GetGlobalGenerator()))
  28. , ConfigName(LocalGenerator->GetConfigName())
  29. , ModuleDefinitionFile(GeneratorTarget->GetModuleDefinitionFile(ConfigName))
  30. , FortranModuleDirectoryComputed(false)
  31. {
  32. }
  33. cmCommonTargetGenerator::~cmCommonTargetGenerator()
  34. {
  35. }
  36. std::string const& cmCommonTargetGenerator::GetConfigName() const
  37. {
  38. return this->ConfigName;
  39. }
  40. std::string cmCommonTargetGenerator::Convert(
  41. std::string const& source,
  42. cmLocalGenerator::RelativeRoot relative,
  43. cmLocalGenerator::OutputFormat output)
  44. {
  45. return this->LocalGenerator->Convert(source, relative, output);
  46. }
  47. //----------------------------------------------------------------------------
  48. const char* cmCommonTargetGenerator::GetFeature(const std::string& feature)
  49. {
  50. return this->GeneratorTarget->GetFeature(feature, this->ConfigName);
  51. }
  52. //----------------------------------------------------------------------------
  53. bool cmCommonTargetGenerator::GetFeatureAsBool(const std::string& feature)
  54. {
  55. return this->GeneratorTarget->GetFeatureAsBool(feature, this->ConfigName);
  56. }
  57. //----------------------------------------------------------------------------
  58. void cmCommonTargetGenerator::AddFeatureFlags(
  59. std::string& flags, const std::string& lang
  60. )
  61. {
  62. // Add language-specific flags.
  63. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  64. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  65. {
  66. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  67. }
  68. }
  69. //----------------------------------------------------------------------------
  70. void cmCommonTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  71. {
  72. if(this->ModuleDefinitionFile.empty())
  73. {
  74. return;
  75. }
  76. // TODO: Create a per-language flag variable.
  77. const char* defFileFlag =
  78. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  79. if(!defFileFlag)
  80. {
  81. return;
  82. }
  83. // Append the flag and value. Use ConvertToLinkReference to help
  84. // vs6's "cl -link" pass it to the linker.
  85. std::string flag = defFileFlag;
  86. flag += (this->LocalGenerator->ConvertToLinkReference(
  87. this->ModuleDefinitionFile));
  88. this->LocalGenerator->AppendFlags(flags, flag);
  89. }
  90. //----------------------------------------------------------------------------
  91. std::string cmCommonTargetGenerator::ComputeFortranModuleDirectory() const
  92. {
  93. std::string mod_dir;
  94. const char* target_mod_dir =
  95. this->GeneratorTarget->GetProperty("Fortran_MODULE_DIRECTORY");
  96. const char* moddir_flag =
  97. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  98. if(target_mod_dir && moddir_flag)
  99. {
  100. // Compute the full path to the module directory.
  101. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  102. {
  103. // Already a full path.
  104. mod_dir = target_mod_dir;
  105. }
  106. else
  107. {
  108. // Interpret relative to the current output directory.
  109. mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  110. mod_dir += "/";
  111. mod_dir += target_mod_dir;
  112. }
  113. // Make sure the module output directory exists.
  114. cmSystemTools::MakeDirectory(mod_dir);
  115. }
  116. return mod_dir;
  117. }
  118. //----------------------------------------------------------------------------
  119. std::string cmCommonTargetGenerator::GetFortranModuleDirectory()
  120. {
  121. // Compute the module directory.
  122. if(!this->FortranModuleDirectoryComputed)
  123. {
  124. this->FortranModuleDirectoryComputed = true;
  125. this->FortranModuleDirectory = this->ComputeFortranModuleDirectory();
  126. }
  127. // Return the computed directory.
  128. return this->FortranModuleDirectory;
  129. }
  130. //----------------------------------------------------------------------------
  131. void cmCommonTargetGenerator::AddFortranFlags(std::string& flags)
  132. {
  133. // Enable module output if necessary.
  134. if(const char* modout_flag =
  135. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  136. {
  137. this->LocalGenerator->AppendFlags(flags, modout_flag);
  138. }
  139. // Add a module output directory flag if necessary.
  140. std::string mod_dir = this->GetFortranModuleDirectory();
  141. if (!mod_dir.empty())
  142. {
  143. mod_dir = this->Convert(mod_dir,
  144. this->WorkingDirectory,
  145. cmLocalGenerator::SHELL);
  146. }
  147. else
  148. {
  149. mod_dir =
  150. this->Makefile->GetSafeDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  151. }
  152. if (!mod_dir.empty())
  153. {
  154. const char* moddir_flag =
  155. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  156. std::string modflag = moddir_flag;
  157. modflag += mod_dir;
  158. this->LocalGenerator->AppendFlags(flags, modflag);
  159. }
  160. // If there is a separate module path flag then duplicate the
  161. // include path with it. This compiler does not search the include
  162. // path for modules.
  163. if(const char* modpath_flag =
  164. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  165. {
  166. std::vector<std::string> includes;
  167. const std::string& config =
  168. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  169. this->LocalGenerator->GetIncludeDirectories(includes,
  170. this->GeneratorTarget,
  171. "C", config);
  172. for(std::vector<std::string>::const_iterator idi = includes.begin();
  173. idi != includes.end(); ++idi)
  174. {
  175. std::string flg = modpath_flag;
  176. flg += this->Convert(*idi,
  177. cmLocalGenerator::NONE,
  178. cmLocalGenerator::SHELL);
  179. this->LocalGenerator->AppendFlags(flags, flg);
  180. }
  181. }
  182. }
  183. //----------------------------------------------------------------------------
  184. void
  185. cmCommonTargetGenerator
  186. ::AppendFortranFormatFlags(std::string& flags, cmSourceFile const& source)
  187. {
  188. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  189. cmLocalGenerator::FortranFormat format =
  190. this->LocalGenerator->GetFortranFormat(srcfmt);
  191. if(format == cmLocalGenerator::FortranFormatNone)
  192. {
  193. const char* tgtfmt = this->GeneratorTarget->GetProperty("Fortran_FORMAT");
  194. format = this->LocalGenerator->GetFortranFormat(tgtfmt);
  195. }
  196. const char* var = 0;
  197. switch (format)
  198. {
  199. case cmLocalGenerator::FortranFormatFixed:
  200. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG"; break;
  201. case cmLocalGenerator::FortranFormatFree:
  202. var = "CMAKE_Fortran_FORMAT_FREE_FLAG"; break;
  203. default: break;
  204. }
  205. if(var)
  206. {
  207. this->LocalGenerator->AppendFlags(
  208. flags, this->Makefile->GetDefinition(var));
  209. }
  210. }
  211. //----------------------------------------------------------------------------
  212. std::string cmCommonTargetGenerator::GetFrameworkFlags(std::string const& l)
  213. {
  214. if(!this->Makefile->IsOn("APPLE"))
  215. {
  216. return std::string();
  217. }
  218. std::string fwSearchFlagVar = "CMAKE_" + l + "_FRAMEWORK_SEARCH_FLAG";
  219. const char* fwSearchFlag =
  220. this->Makefile->GetDefinition(fwSearchFlagVar);
  221. if(!(fwSearchFlag && *fwSearchFlag))
  222. {
  223. return std::string();
  224. }
  225. std::set<std::string> emitted;
  226. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  227. emitted.insert("/System/Library/Frameworks");
  228. #endif
  229. std::vector<std::string> includes;
  230. const std::string& config =
  231. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  232. this->LocalGenerator->GetIncludeDirectories(includes,
  233. this->GeneratorTarget,
  234. "C", config);
  235. // check all include directories for frameworks as this
  236. // will already have added a -F for the framework
  237. for(std::vector<std::string>::iterator i = includes.begin();
  238. i != includes.end(); ++i)
  239. {
  240. if(this->GlobalGenerator->NameResolvesToFramework(*i))
  241. {
  242. std::string frameworkDir = *i;
  243. frameworkDir += "/../";
  244. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  245. emitted.insert(frameworkDir);
  246. }
  247. }
  248. std::string flags;
  249. const char* cfg = this->LocalGenerator->GetConfigName().c_str();
  250. if(cmComputeLinkInformation* cli =
  251. this->GeneratorTarget->GetLinkInformation(cfg))
  252. {
  253. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  254. for(std::vector<std::string>::const_iterator i = frameworks.begin();
  255. i != frameworks.end(); ++i)
  256. {
  257. if(emitted.insert(*i).second)
  258. {
  259. flags += fwSearchFlag;
  260. flags += this->LocalGenerator
  261. ->ConvertToOutputFormat(*i, cmLocalGenerator::SHELL);
  262. flags += " ";
  263. }
  264. }
  265. }
  266. return flags;
  267. }
  268. //----------------------------------------------------------------------------
  269. std::string cmCommonTargetGenerator::GetFlags(const std::string &l)
  270. {
  271. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  272. if (i == this->FlagsByLanguage.end())
  273. {
  274. std::string flags;
  275. const char *lang = l.c_str();
  276. // Add language feature flags.
  277. this->AddFeatureFlags(flags, lang);
  278. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  279. lang, this->ConfigName);
  280. // Fortran-specific flags computed for this target.
  281. if(l == "Fortran")
  282. {
  283. this->AddFortranFlags(flags);
  284. }
  285. this->LocalGenerator->AddCMP0018Flags(flags, this->GeneratorTarget,
  286. lang, this->ConfigName);
  287. this->LocalGenerator->AddVisibilityPresetFlags(flags,
  288. this->GeneratorTarget,
  289. lang);
  290. // Append old-style preprocessor definition flags.
  291. this->LocalGenerator->
  292. AppendFlags(flags, this->Makefile->GetDefineFlags());
  293. // Add framework directory flags.
  294. this->LocalGenerator->
  295. AppendFlags(flags,this->GetFrameworkFlags(l));
  296. // Add target-specific flags.
  297. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  298. lang, this->ConfigName);
  299. ByLanguageMap::value_type entry(l, flags);
  300. i = this->FlagsByLanguage.insert(entry).first;
  301. }
  302. return i->second;
  303. }
  304. std::string cmCommonTargetGenerator::GetDefines(const std::string &l)
  305. {
  306. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  307. if (i == this->DefinesByLanguage.end())
  308. {
  309. std::set<std::string> defines;
  310. const char *lang = l.c_str();
  311. // Add the export symbol definition for shared library objects.
  312. if(const char* exportMacro =
  313. this->GeneratorTarget->GetExportMacro())
  314. {
  315. this->LocalGenerator->AppendDefines(defines, exportMacro);
  316. }
  317. // Add preprocessor definitions for this target and configuration.
  318. this->LocalGenerator->AddCompileDefinitions(defines, this->GeneratorTarget,
  319. this->LocalGenerator->GetConfigName(), l);
  320. std::string definesString;
  321. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  322. ByLanguageMap::value_type entry(l, definesString);
  323. i = this->DefinesByLanguage.insert(entry).first;
  324. }
  325. return i->second;
  326. }
  327. std::string cmCommonTargetGenerator::GetIncludes(std::string const& l)
  328. {
  329. ByLanguageMap::iterator i = this->IncludesByLanguage.find(l);
  330. if (i == this->IncludesByLanguage.end())
  331. {
  332. std::string includes;
  333. this->AddIncludeFlags(includes, l);
  334. ByLanguageMap::value_type entry(l, includes);
  335. i = this->IncludesByLanguage.insert(entry).first;
  336. }
  337. return i->second;
  338. }
  339. std::vector<std::string>
  340. cmCommonTargetGenerator::GetLinkedTargetDirectories() const
  341. {
  342. std::vector<std::string> dirs;
  343. std::set<cmGeneratorTarget const*> emitted;
  344. if (cmComputeLinkInformation* cli =
  345. this->GeneratorTarget->GetLinkInformation(this->ConfigName))
  346. {
  347. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  348. for(cmComputeLinkInformation::ItemVector::const_iterator
  349. i = items.begin(); i != items.end(); ++i)
  350. {
  351. cmGeneratorTarget const* linkee = i->Target;
  352. if(linkee && !linkee->IsImported()
  353. // We can ignore the INTERFACE_LIBRARY items because
  354. // Target->GetLinkInformation already processed their
  355. // link interface and they don't have any output themselves.
  356. && linkee->GetType() != cmState::INTERFACE_LIBRARY
  357. && emitted.insert(linkee).second)
  358. {
  359. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  360. std::string di = lg->GetCurrentBinaryDirectory();
  361. di += "/";
  362. di += lg->GetTargetDirectory(linkee);
  363. dirs.push_back(di);
  364. }
  365. }
  366. }
  367. return dirs;
  368. }
  369. std::string cmCommonTargetGenerator::GetManifests()
  370. {
  371. std::vector<cmSourceFile const*> manifest_srcs;
  372. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  373. std::vector<std::string> manifests;
  374. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  375. mi != manifest_srcs.end(); ++mi)
  376. {
  377. manifests.push_back(this->Convert((*mi)->GetFullPath(),
  378. this->WorkingDirectory,
  379. cmOutputConverter::SHELL));
  380. }
  381. return cmJoin(manifests, " ");
  382. }