cmMakefileTargetGenerator.cxx 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 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 "cmMakefileTargetGenerator.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmGlobalUnixMakefileGenerator3.h"
  15. #include "cmLocalUnixMakefileGenerator3.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. #include "cmTarget.h"
  19. #include "cmake.h"
  20. #include "cmComputeLinkInformation.h"
  21. #include "cmCustomCommandGenerator.h"
  22. #include "cmGeneratorExpression.h"
  23. #include "cmMakefileExecutableTargetGenerator.h"
  24. #include "cmMakefileLibraryTargetGenerator.h"
  25. #include "cmMakefileUtilityTargetGenerator.h"
  26. #include <ctype.h>
  27. cmMakefileTargetGenerator::cmMakefileTargetGenerator(cmTarget* target)
  28. : OSXBundleGenerator(0)
  29. , MacOSXContentGenerator(0)
  30. {
  31. this->BuildFileStream = 0;
  32. this->InfoFileStream = 0;
  33. this->FlagFileStream = 0;
  34. this->CustomCommandDriver = OnBuild;
  35. this->FortranModuleDirectoryComputed = false;
  36. this->Target = target;
  37. this->Makefile = this->Target->GetMakefile();
  38. this->LocalGenerator =
  39. static_cast<cmLocalUnixMakefileGenerator3*>(
  40. this->Makefile->GetLocalGenerator());
  41. this->ConfigName = this->LocalGenerator->ConfigurationName.c_str();
  42. this->GlobalGenerator =
  43. static_cast<cmGlobalUnixMakefileGenerator3*>(
  44. this->LocalGenerator->GetGlobalGenerator());
  45. this->GeneratorTarget = this->GlobalGenerator->GetGeneratorTarget(target);
  46. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  47. this->NoRuleMessages = false;
  48. if(const char* ruleStatus = cm->GetProperty("RULE_MESSAGES"))
  49. {
  50. this->NoRuleMessages = cmSystemTools::IsOff(ruleStatus);
  51. }
  52. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  53. }
  54. cmMakefileTargetGenerator::~cmMakefileTargetGenerator()
  55. {
  56. delete MacOSXContentGenerator;
  57. }
  58. cmMakefileTargetGenerator *
  59. cmMakefileTargetGenerator::New(cmGeneratorTarget *tgt)
  60. {
  61. cmMakefileTargetGenerator *result = 0;
  62. switch (tgt->GetType())
  63. {
  64. case cmTarget::EXECUTABLE:
  65. result = new cmMakefileExecutableTargetGenerator(tgt);
  66. break;
  67. case cmTarget::STATIC_LIBRARY:
  68. case cmTarget::SHARED_LIBRARY:
  69. case cmTarget::MODULE_LIBRARY:
  70. case cmTarget::OBJECT_LIBRARY:
  71. result = new cmMakefileLibraryTargetGenerator(tgt);
  72. break;
  73. case cmTarget::UTILITY:
  74. result = new cmMakefileUtilityTargetGenerator(tgt);
  75. break;
  76. default:
  77. return result;
  78. // break; /* unreachable */
  79. }
  80. return result;
  81. }
  82. //----------------------------------------------------------------------------
  83. void cmMakefileTargetGenerator::CreateRuleFile()
  84. {
  85. // Create a directory for this target.
  86. this->TargetBuildDirectory =
  87. this->LocalGenerator->GetTargetDirectory(*this->Target);
  88. this->TargetBuildDirectoryFull =
  89. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  90. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  91. // Construct the rule file name.
  92. this->BuildFileName = this->TargetBuildDirectory;
  93. this->BuildFileName += "/build.make";
  94. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  95. this->BuildFileNameFull += "/build.make";
  96. // Construct the rule file name.
  97. this->ProgressFileNameFull = this->TargetBuildDirectoryFull;
  98. this->ProgressFileNameFull += "/progress.make";
  99. // reset the progress count
  100. this->NumberOfProgressActions = 0;
  101. // Open the rule file. This should be copy-if-different because the
  102. // rules may depend on this file itself.
  103. this->BuildFileStream =
  104. new cmGeneratedFileStream(this->BuildFileNameFull.c_str());
  105. this->BuildFileStream->SetCopyIfDifferent(true);
  106. if(!this->BuildFileStream)
  107. {
  108. return;
  109. }
  110. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  111. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  112. }
  113. //----------------------------------------------------------------------------
  114. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  115. {
  116. const std::string& config =
  117. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  118. // write the custom commands for this target
  119. // Look for files registered for cleaning in this directory.
  120. if(const char* additional_clean_files =
  121. this->Makefile->GetProperty
  122. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  123. {
  124. cmGeneratorExpression ge;
  125. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge =
  126. ge.Parse(additional_clean_files);
  127. cmSystemTools::ExpandListArgument(cge->Evaluate(this->Makefile, config,
  128. false, this->Target, 0, 0),
  129. this->CleanFiles);
  130. }
  131. // add custom commands to the clean rules?
  132. const char* clean_no_custom =
  133. this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  134. bool clean = cmSystemTools::IsOff(clean_no_custom);
  135. // First generate the object rule files. Save a list of all object
  136. // files for this target.
  137. std::vector<cmSourceFile const*> customCommands;
  138. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  139. for(std::vector<cmSourceFile const*>::const_iterator
  140. si = customCommands.begin();
  141. si != customCommands.end(); ++si)
  142. {
  143. cmCustomCommandGenerator ccg(*(*si)->GetCustomCommand(),
  144. this->ConfigName,
  145. this->Makefile);
  146. this->GenerateCustomRuleFile(ccg);
  147. if (clean)
  148. {
  149. const std::vector<std::string>& outputs = ccg.GetOutputs();
  150. for(std::vector<std::string>::const_iterator o = outputs.begin();
  151. o != outputs.end(); ++o)
  152. {
  153. this->CleanFiles.push_back
  154. (this->Convert(*o,
  155. cmLocalGenerator::START_OUTPUT,
  156. cmLocalGenerator::UNCHANGED));
  157. }
  158. }
  159. }
  160. std::vector<cmSourceFile const*> headerSources;
  161. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  162. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  163. headerSources,
  164. this->MacOSXContentGenerator);
  165. std::vector<cmSourceFile const*> extraSources;
  166. this->GeneratorTarget->GetExtraSources(extraSources, config);
  167. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  168. extraSources,
  169. this->MacOSXContentGenerator);
  170. std::vector<cmSourceFile const*> externalObjects;
  171. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  172. for(std::vector<cmSourceFile const*>::const_iterator
  173. si = externalObjects.begin();
  174. si != externalObjects.end(); ++si)
  175. {
  176. this->ExternalObjects.push_back((*si)->GetFullPath());
  177. }
  178. std::vector<cmSourceFile const*> objectSources;
  179. this->GeneratorTarget->GetObjectSources(objectSources, config);
  180. for(std::vector<cmSourceFile const*>::const_iterator
  181. si = objectSources.begin(); si != objectSources.end(); ++si)
  182. {
  183. // Generate this object file's rule file.
  184. this->WriteObjectRuleFiles(**si);
  185. }
  186. }
  187. //----------------------------------------------------------------------------
  188. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  189. {
  190. const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")?
  191. "$(CMAKE_BINARY_DIR)/" : "");
  192. // Include the dependencies for the target.
  193. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  194. dependFileNameFull += "/depend.make";
  195. *this->BuildFileStream
  196. << "# Include any dependencies generated for this target.\n"
  197. << this->LocalGenerator->IncludeDirective << " " << root
  198. << this->Convert(dependFileNameFull,
  199. cmLocalGenerator::HOME_OUTPUT,
  200. cmLocalGenerator::MAKERULE)
  201. << "\n\n";
  202. if(!this->NoRuleMessages)
  203. {
  204. // Include the progress variables for the target.
  205. *this->BuildFileStream
  206. << "# Include the progress variables for this target.\n"
  207. << this->LocalGenerator->IncludeDirective << " " << root
  208. << this->Convert(this->ProgressFileNameFull,
  209. cmLocalGenerator::HOME_OUTPUT,
  210. cmLocalGenerator::MAKERULE)
  211. << "\n\n";
  212. }
  213. // make sure the depend file exists
  214. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  215. {
  216. // Write an empty dependency file.
  217. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  218. depFileStream
  219. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  220. << "# This may be replaced when dependencies are built." << std::endl;
  221. }
  222. // Open the flags file. This should be copy-if-different because the
  223. // rules may depend on this file itself.
  224. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  225. this->FlagFileNameFull += "/flags.make";
  226. this->FlagFileStream =
  227. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  228. this->FlagFileStream->SetCopyIfDifferent(true);
  229. if(!this->FlagFileStream)
  230. {
  231. return;
  232. }
  233. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  234. // Include the flags for the target.
  235. *this->BuildFileStream
  236. << "# Include the compile flags for this target's objects.\n"
  237. << this->LocalGenerator->IncludeDirective << " " << root
  238. << this->Convert(this->FlagFileNameFull,
  239. cmLocalGenerator::HOME_OUTPUT,
  240. cmLocalGenerator::MAKERULE)
  241. << "\n\n";
  242. }
  243. //----------------------------------------------------------------------------
  244. std::string cmMakefileTargetGenerator::GetFlags(const std::string &l)
  245. {
  246. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  247. if (i == this->FlagsByLanguage.end())
  248. {
  249. std::string flags;
  250. const char *lang = l.c_str();
  251. // Add language feature flags.
  252. this->AddFeatureFlags(flags, lang);
  253. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  254. lang, this->ConfigName);
  255. // Fortran-specific flags computed for this target.
  256. if(l == "Fortran")
  257. {
  258. this->AddFortranFlags(flags);
  259. }
  260. this->LocalGenerator->AddCMP0018Flags(flags, this->Target,
  261. lang, this->ConfigName);
  262. this->LocalGenerator->AddVisibilityPresetFlags(flags, this->Target,
  263. lang);
  264. // Add include directory flags.
  265. this->AddIncludeFlags(flags, lang);
  266. // Append old-style preprocessor definition flags.
  267. this->LocalGenerator->
  268. AppendFlags(flags, this->Makefile->GetDefineFlags());
  269. // Add include directory flags.
  270. this->LocalGenerator->
  271. AppendFlags(flags,this->GetFrameworkFlags(l));
  272. // Add target-specific flags.
  273. this->LocalGenerator->AddCompileOptions(flags, this->Target,
  274. lang, this->ConfigName);
  275. ByLanguageMap::value_type entry(l, flags);
  276. i = this->FlagsByLanguage.insert(entry).first;
  277. }
  278. return i->second;
  279. }
  280. std::string cmMakefileTargetGenerator::GetDefines(const std::string &l)
  281. {
  282. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  283. if (i == this->DefinesByLanguage.end())
  284. {
  285. std::set<std::string> defines;
  286. const char *lang = l.c_str();
  287. // Add the export symbol definition for shared library objects.
  288. if(const char* exportMacro = this->Target->GetExportMacro())
  289. {
  290. this->LocalGenerator->AppendDefines(defines, exportMacro);
  291. }
  292. // Add preprocessor definitions for this target and configuration.
  293. this->LocalGenerator->AddCompileDefinitions(defines, this->Target,
  294. this->LocalGenerator->ConfigurationName);
  295. std::string definesString;
  296. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  297. ByLanguageMap::value_type entry(l, definesString);
  298. i = this->DefinesByLanguage.insert(entry).first;
  299. }
  300. return i->second;
  301. }
  302. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  303. {
  304. // write language flags for target
  305. std::set<std::string> languages;
  306. this->Target->GetLanguages(languages,
  307. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  308. // put the compiler in the rules.make file so that if it changes
  309. // things rebuild
  310. for(std::set<std::string>::const_iterator l = languages.begin();
  311. l != languages.end(); ++l)
  312. {
  313. std::string compiler = "CMAKE_";
  314. compiler += *l;
  315. compiler += "_COMPILER";
  316. *this->FlagFileStream << "# compile " << *l << " with " <<
  317. this->Makefile->GetSafeDefinition(compiler) << "\n";
  318. }
  319. for(std::set<std::string>::const_iterator l = languages.begin();
  320. l != languages.end(); ++l)
  321. {
  322. std::string flags = this->GetFlags(*l);
  323. std::string defines = this->GetDefines(*l);
  324. // Escape comment characters so they do not terminate assignment.
  325. cmSystemTools::ReplaceString(flags, "#", "\\#");
  326. cmSystemTools::ReplaceString(defines, "#", "\\#");
  327. *this->FlagFileStream << *l << "_FLAGS = " << flags << "\n\n";
  328. *this->FlagFileStream << *l << "_DEFINES = " << defines << "\n\n";
  329. }
  330. }
  331. //----------------------------------------------------------------------------
  332. void
  333. cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()
  334. (cmSourceFile const& source, const char* pkgloc)
  335. {
  336. // Skip OS X content when not building a Framework or Bundle.
  337. if(!this->Generator->GetTarget()->IsBundleOnApple())
  338. {
  339. return;
  340. }
  341. std::string macdir =
  342. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  343. // Get the input file location.
  344. std::string input = source.GetFullPath();
  345. // Get the output file location.
  346. std::string output = macdir;
  347. output += "/";
  348. output += cmSystemTools::GetFilenameName(input);
  349. this->Generator->CleanFiles.push_back(
  350. this->Generator->Convert(output,
  351. cmLocalGenerator::START_OUTPUT));
  352. output = this->Generator->Convert(output,
  353. cmLocalGenerator::HOME_OUTPUT);
  354. // Create a rule to copy the content into the bundle.
  355. std::vector<std::string> depends;
  356. std::vector<std::string> commands;
  357. depends.push_back(input);
  358. std::string copyEcho = "Copying OS X content ";
  359. copyEcho += output;
  360. this->Generator->LocalGenerator->AppendEcho(
  361. commands, copyEcho.c_str(),
  362. cmLocalUnixMakefileGenerator3::EchoBuild);
  363. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  364. copyCommand += this->Generator->Convert(input,
  365. cmLocalGenerator::NONE,
  366. cmLocalGenerator::SHELL);
  367. copyCommand += " ";
  368. copyCommand += this->Generator->Convert(output,
  369. cmLocalGenerator::NONE,
  370. cmLocalGenerator::SHELL);
  371. commands.push_back(copyCommand);
  372. this->Generator->LocalGenerator->WriteMakeRule(
  373. *this->Generator->BuildFileStream, 0,
  374. output,
  375. depends, commands, false);
  376. this->Generator->ExtraFiles.insert(output);
  377. }
  378. //----------------------------------------------------------------------------
  379. void cmMakefileTargetGenerator
  380. ::WriteObjectRuleFiles(cmSourceFile const& source)
  381. {
  382. // Identify the language of the source file.
  383. const std::string& lang =
  384. this->LocalGenerator->GetSourceFileLanguage(source);
  385. if(lang.empty())
  386. {
  387. // don't know anything about this file so skip it
  388. return;
  389. }
  390. // Get the full path name of the object file.
  391. std::string const& objectName = this->GeneratorTarget
  392. ->GetObjectName(&source);
  393. std::string obj = this->LocalGenerator->GetTargetDirectory(*this->Target);
  394. obj += "/";
  395. obj += objectName;
  396. // Avoid generating duplicate rules.
  397. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  398. {
  399. this->ObjectFiles.insert(obj);
  400. }
  401. else
  402. {
  403. std::ostringstream err;
  404. err << "Warning: Source file \""
  405. << source.GetFullPath()
  406. << "\" is listed multiple times for target \""
  407. << this->Target->GetName()
  408. << "\".";
  409. cmSystemTools::Message(err.str().c_str(), "Warning");
  410. return;
  411. }
  412. // Create the directory containing the object file. This may be a
  413. // subdirectory under the target's directory.
  414. std::string dir = cmSystemTools::GetFilenamePath(obj);
  415. cmSystemTools::MakeDirectory
  416. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  417. // Save this in the target's list of object files.
  418. this->Objects.push_back(obj);
  419. this->CleanFiles.push_back(obj);
  420. // TODO: Remove
  421. //std::string relativeObj
  422. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  423. //relativeObj += obj;
  424. // we compute some depends when writing the depend.make that we will also
  425. // use in the build.make, same with depMakeFile
  426. std::vector<std::string> depends;
  427. std::string depMakeFile;
  428. // generate the build rule file
  429. this->WriteObjectBuildFile(obj, lang, source, depends);
  430. // The object file should be checked for dependency integrity.
  431. std::string objFullPath = this->Makefile->GetCurrentOutputDirectory();
  432. objFullPath += "/";
  433. objFullPath += obj;
  434. objFullPath =
  435. this->Convert(objFullPath, cmLocalGenerator::FULL);
  436. std::string srcFullPath =
  437. this->Convert(source.GetFullPath(), cmLocalGenerator::FULL);
  438. this->LocalGenerator->
  439. AddImplicitDepends(*this->Target, lang,
  440. objFullPath.c_str(),
  441. srcFullPath.c_str());
  442. }
  443. //----------------------------------------------------------------------------
  444. void
  445. cmMakefileTargetGenerator
  446. ::AppendFortranFormatFlags(std::string& flags, cmSourceFile const& source)
  447. {
  448. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  449. cmLocalGenerator::FortranFormat format =
  450. this->LocalGenerator->GetFortranFormat(srcfmt);
  451. if(format == cmLocalGenerator::FortranFormatNone)
  452. {
  453. const char* tgtfmt = this->Target->GetProperty("Fortran_FORMAT");
  454. format = this->LocalGenerator->GetFortranFormat(tgtfmt);
  455. }
  456. const char* var = 0;
  457. switch (format)
  458. {
  459. case cmLocalGenerator::FortranFormatFixed:
  460. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG"; break;
  461. case cmLocalGenerator::FortranFormatFree:
  462. var = "CMAKE_Fortran_FORMAT_FREE_FLAG"; break;
  463. default: break;
  464. }
  465. if(var)
  466. {
  467. this->LocalGenerator->AppendFlags(
  468. flags, this->Makefile->GetDefinition(var));
  469. }
  470. }
  471. //----------------------------------------------------------------------------
  472. void
  473. cmMakefileTargetGenerator
  474. ::WriteObjectBuildFile(std::string &obj,
  475. const std::string& lang,
  476. cmSourceFile const& source,
  477. std::vector<std::string>& depends)
  478. {
  479. this->LocalGenerator->AppendRuleDepend(depends,
  480. this->FlagFileNameFull.c_str());
  481. this->LocalGenerator->AppendRuleDepends(depends,
  482. this->FlagFileDepends[lang]);
  483. // generate the depend scanning rule
  484. this->WriteObjectDependRules(source, depends);
  485. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  486. relativeObj += obj;
  487. // Write the build rule.
  488. // Build the set of compiler flags.
  489. std::string flags;
  490. // Add language-specific flags.
  491. std::string langFlags = "$(";
  492. langFlags += lang;
  493. langFlags += "_FLAGS)";
  494. this->LocalGenerator->AppendFlags(flags, langFlags);
  495. std::string configUpper =
  496. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  497. // Add Fortran format flags.
  498. if(lang == "Fortran")
  499. {
  500. this->AppendFortranFormatFlags(flags, source);
  501. }
  502. // Add flags from source file properties.
  503. if (source.GetProperty("COMPILE_FLAGS"))
  504. {
  505. this->LocalGenerator->AppendFlags
  506. (flags, source.GetProperty("COMPILE_FLAGS"));
  507. *this->FlagFileStream << "# Custom flags: "
  508. << relativeObj << "_FLAGS = "
  509. << source.GetProperty("COMPILE_FLAGS")
  510. << "\n"
  511. << "\n";
  512. }
  513. // Add language-specific defines.
  514. std::set<std::string> defines;
  515. // Add source-sepcific preprocessor definitions.
  516. if(const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS"))
  517. {
  518. this->LocalGenerator->AppendDefines(defines, compile_defs);
  519. *this->FlagFileStream << "# Custom defines: "
  520. << relativeObj << "_DEFINES = "
  521. << compile_defs << "\n"
  522. << "\n";
  523. }
  524. std::string defPropName = "COMPILE_DEFINITIONS_";
  525. defPropName += configUpper;
  526. if(const char* config_compile_defs =
  527. source.GetProperty(defPropName))
  528. {
  529. this->LocalGenerator->AppendDefines(defines, config_compile_defs);
  530. *this->FlagFileStream
  531. << "# Custom defines: "
  532. << relativeObj << "_DEFINES_" << configUpper
  533. << " = " << config_compile_defs << "\n"
  534. << "\n";
  535. }
  536. // Get the output paths for source and object files.
  537. std::string sourceFile = source.GetFullPath();
  538. if(this->LocalGenerator->UseRelativePaths)
  539. {
  540. sourceFile = this->Convert(sourceFile,
  541. cmLocalGenerator::START_OUTPUT);
  542. }
  543. sourceFile = this->Convert(sourceFile,
  544. cmLocalGenerator::NONE,
  545. cmLocalGenerator::SHELL);
  546. // Construct the build message.
  547. std::vector<std::string> no_commands;
  548. std::vector<std::string> commands;
  549. // add in a progress call if needed
  550. this->AppendProgress(commands);
  551. if(!this->NoRuleMessages)
  552. {
  553. std::string buildEcho = "Building ";
  554. buildEcho += lang;
  555. buildEcho += " object ";
  556. buildEcho += relativeObj;
  557. this->LocalGenerator->AppendEcho
  558. (commands, buildEcho.c_str(), cmLocalUnixMakefileGenerator3::EchoBuild);
  559. }
  560. std::string targetOutPathReal;
  561. std::string targetOutPathPDB;
  562. std::string targetOutPathCompilePDB;
  563. {
  564. std::string targetFullPathReal;
  565. std::string targetFullPathPDB;
  566. std::string targetFullPathCompilePDB;
  567. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  568. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  569. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  570. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  571. {
  572. targetFullPathReal =
  573. this->Target->GetFullPath(this->ConfigName, false, true);
  574. targetFullPathPDB = this->Target->GetPDBDirectory(this->ConfigName);
  575. targetFullPathPDB += "/";
  576. targetFullPathPDB += this->Target->GetPDBName(this->ConfigName);
  577. }
  578. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  579. {
  580. targetFullPathCompilePDB =
  581. this->Target->GetCompilePDBPath(this->ConfigName);
  582. if(targetFullPathCompilePDB.empty())
  583. {
  584. targetFullPathCompilePDB = this->Target->GetSupportDirectory() + "/";
  585. }
  586. }
  587. targetOutPathReal = this->Convert(targetFullPathReal,
  588. cmLocalGenerator::START_OUTPUT,
  589. cmLocalGenerator::SHELL);
  590. targetOutPathPDB =
  591. this->Convert(targetFullPathPDB,cmLocalGenerator::NONE,
  592. cmLocalGenerator::SHELL);
  593. targetOutPathCompilePDB =
  594. this->Convert(targetFullPathCompilePDB,
  595. cmLocalGenerator::START_OUTPUT,
  596. cmLocalGenerator::SHELL);
  597. }
  598. cmLocalGenerator::RuleVariables vars;
  599. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  600. vars.CMTarget = this->Target;
  601. vars.Language = lang.c_str();
  602. vars.Target = targetOutPathReal.c_str();
  603. vars.TargetPDB = targetOutPathPDB.c_str();
  604. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  605. vars.Source = sourceFile.c_str();
  606. std::string shellObj =
  607. this->Convert(obj,
  608. cmLocalGenerator::NONE,
  609. cmLocalGenerator::SHELL);
  610. vars.Object = shellObj.c_str();
  611. std::string objectDir = this->Target->GetSupportDirectory();
  612. objectDir = this->Convert(objectDir,
  613. cmLocalGenerator::START_OUTPUT,
  614. cmLocalGenerator::SHELL);
  615. vars.ObjectDir = objectDir.c_str();
  616. std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
  617. objectFileDir = this->Convert(objectFileDir,
  618. cmLocalGenerator::START_OUTPUT,
  619. cmLocalGenerator::SHELL);
  620. vars.ObjectFileDir = objectFileDir.c_str();
  621. vars.Flags = flags.c_str();
  622. std::string definesString = "$(";
  623. definesString += lang;
  624. definesString += "_DEFINES)";
  625. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  626. vars.Defines = definesString.c_str();
  627. // At the moment, it is assumed that C, C++, and Fortran have both
  628. // assembly and preprocessor capabilities. The same is true for the
  629. // ability to export compile commands
  630. bool lang_has_preprocessor = ((lang == "C") ||
  631. (lang == "CXX") ||
  632. (lang == "Fortran"));
  633. bool const lang_has_assembly = lang_has_preprocessor;
  634. bool const lang_can_export_cmds = lang_has_preprocessor;
  635. // Construct the compile rules.
  636. {
  637. std::string compileRuleVar = "CMAKE_";
  638. compileRuleVar += lang;
  639. compileRuleVar += "_COMPILE_OBJECT";
  640. std::string compileRule =
  641. this->Makefile->GetRequiredDefinition(compileRuleVar);
  642. std::vector<std::string> compileCommands;
  643. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  644. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  645. lang_can_export_cmds && compileCommands.size() == 1)
  646. {
  647. std::string compileCommand = compileCommands[0];
  648. this->LocalGenerator->ExpandRuleVariables(compileCommand, vars);
  649. std::string workingDirectory =
  650. this->LocalGenerator->Convert(
  651. this->Makefile->GetStartOutputDirectory(), cmLocalGenerator::FULL);
  652. compileCommand.replace(compileCommand.find(langFlags),
  653. langFlags.size(), this->GetFlags(lang));
  654. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  655. compileCommand.replace(compileCommand.find(langDefines),
  656. langDefines.size(), this->GetDefines(lang));
  657. this->GlobalGenerator->AddCXXCompileCommand(
  658. source.GetFullPath(), workingDirectory, compileCommand);
  659. }
  660. // Expand placeholders in the commands.
  661. for(std::vector<std::string>::iterator i = compileCommands.begin();
  662. i != compileCommands.end(); ++i)
  663. {
  664. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  665. }
  666. // Change the command working directory to the local build tree.
  667. this->LocalGenerator->CreateCDCommand
  668. (compileCommands,
  669. this->Makefile->GetStartOutputDirectory(),
  670. cmLocalGenerator::HOME_OUTPUT);
  671. commands.insert(commands.end(),
  672. compileCommands.begin(), compileCommands.end());
  673. }
  674. // Check for extra outputs created by the compilation.
  675. std::vector<std::string> outputs(1, relativeObj);
  676. if(const char* extra_outputs_str =
  677. source.GetProperty("OBJECT_OUTPUTS"))
  678. {
  679. // Register these as extra files to clean.
  680. cmSystemTools::ExpandListArgument(extra_outputs_str, outputs);
  681. this->CleanFiles.insert(this->CleanFiles.end(),
  682. outputs.begin() + 1, outputs.end());
  683. }
  684. // Write the rule.
  685. this->WriteMakeRule(*this->BuildFileStream, 0, outputs,
  686. depends, commands, false);
  687. bool do_preprocess_rules = lang_has_preprocessor &&
  688. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  689. bool do_assembly_rules = lang_has_assembly &&
  690. this->LocalGenerator->GetCreateAssemblySourceRules();
  691. if(do_preprocess_rules || do_assembly_rules)
  692. {
  693. std::vector<std::string> force_depends;
  694. force_depends.push_back("cmake_force");
  695. std::string::size_type dot_pos = relativeObj.rfind(".");
  696. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  697. dot_pos = obj.rfind(".");
  698. std::string objBase = obj.substr(0, dot_pos);
  699. if(do_preprocess_rules)
  700. {
  701. commands.clear();
  702. std::string relativeObjI = relativeObjBase + ".i";
  703. std::string objI = objBase + ".i";
  704. std::string preprocessEcho = "Preprocessing ";
  705. preprocessEcho += lang;
  706. preprocessEcho += " source to ";
  707. preprocessEcho += objI;
  708. this->LocalGenerator->AppendEcho(
  709. commands, preprocessEcho.c_str(),
  710. cmLocalUnixMakefileGenerator3::EchoBuild
  711. );
  712. std::string preprocessRuleVar = "CMAKE_";
  713. preprocessRuleVar += lang;
  714. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  715. if(const char* preprocessRule =
  716. this->Makefile->GetDefinition(preprocessRuleVar))
  717. {
  718. std::vector<std::string> preprocessCommands;
  719. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  720. std::string shellObjI =
  721. this->Convert(objI,
  722. cmLocalGenerator::NONE,
  723. cmLocalGenerator::SHELL);
  724. vars.PreprocessedSource = shellObjI.c_str();
  725. // Expand placeholders in the commands.
  726. for(std::vector<std::string>::iterator i = preprocessCommands.begin();
  727. i != preprocessCommands.end(); ++i)
  728. {
  729. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  730. }
  731. this->LocalGenerator->CreateCDCommand
  732. (preprocessCommands,
  733. this->Makefile->GetStartOutputDirectory(),
  734. cmLocalGenerator::HOME_OUTPUT);
  735. commands.insert(commands.end(),
  736. preprocessCommands.begin(),
  737. preprocessCommands.end());
  738. }
  739. else
  740. {
  741. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  742. cmd += preprocessRuleVar;
  743. commands.push_back(cmd);
  744. }
  745. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  746. relativeObjI,
  747. force_depends, commands, false);
  748. }
  749. if(do_assembly_rules)
  750. {
  751. commands.clear();
  752. std::string relativeObjS = relativeObjBase + ".s";
  753. std::string objS = objBase + ".s";
  754. std::string assemblyEcho = "Compiling ";
  755. assemblyEcho += lang;
  756. assemblyEcho += " source to assembly ";
  757. assemblyEcho += objS;
  758. this->LocalGenerator->AppendEcho(
  759. commands, assemblyEcho.c_str(),
  760. cmLocalUnixMakefileGenerator3::EchoBuild
  761. );
  762. std::string assemblyRuleVar = "CMAKE_";
  763. assemblyRuleVar += lang;
  764. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  765. if(const char* assemblyRule =
  766. this->Makefile->GetDefinition(assemblyRuleVar))
  767. {
  768. std::vector<std::string> assemblyCommands;
  769. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  770. std::string shellObjS =
  771. this->Convert(objS,
  772. cmLocalGenerator::NONE,
  773. cmLocalGenerator::SHELL);
  774. vars.AssemblySource = shellObjS.c_str();
  775. // Expand placeholders in the commands.
  776. for(std::vector<std::string>::iterator i = assemblyCommands.begin();
  777. i != assemblyCommands.end(); ++i)
  778. {
  779. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  780. }
  781. this->LocalGenerator->CreateCDCommand
  782. (assemblyCommands,
  783. this->Makefile->GetStartOutputDirectory(),
  784. cmLocalGenerator::HOME_OUTPUT);
  785. commands.insert(commands.end(),
  786. assemblyCommands.begin(),
  787. assemblyCommands.end());
  788. }
  789. else
  790. {
  791. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  792. cmd += assemblyRuleVar;
  793. commands.push_back(cmd);
  794. }
  795. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  796. relativeObjS,
  797. force_depends, commands, false);
  798. }
  799. }
  800. // If the language needs provides-requires mode, create the
  801. // corresponding targets.
  802. std::string objectRequires = relativeObj;
  803. objectRequires += ".requires";
  804. std::vector<std::string> p_depends;
  805. // always provide an empty requires target
  806. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  807. objectRequires, p_depends,
  808. no_commands, true);
  809. // write a build rule to recursively build what this obj provides
  810. std::string objectProvides = relativeObj;
  811. objectProvides += ".provides";
  812. std::string temp = relativeObj;
  813. temp += ".provides.build";
  814. std::vector<std::string> r_commands;
  815. std::string tgtMakefileName =
  816. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  817. tgtMakefileName += "/build.make";
  818. r_commands.push_back
  819. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  820. temp));
  821. p_depends.clear();
  822. p_depends.push_back(objectRequires);
  823. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  824. objectProvides, p_depends,
  825. r_commands, true);
  826. // write the provides.build rule dependency on the obj file
  827. p_depends.clear();
  828. p_depends.push_back(relativeObj);
  829. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  830. temp, p_depends, no_commands,
  831. false);
  832. }
  833. //----------------------------------------------------------------------------
  834. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  835. {
  836. std::vector<std::string> depends;
  837. std::vector<std::string> no_commands;
  838. // Construct the name of the dependency generation target.
  839. std::string depTarget =
  840. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  841. depTarget += "/requires";
  842. // This target drives dependency generation for all object files.
  843. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  844. std::string objTarget;
  845. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  846. obj != this->Objects.end(); ++obj)
  847. {
  848. objTarget = relPath;
  849. objTarget += *obj;
  850. objTarget += ".requires";
  851. depends.push_back(objTarget);
  852. }
  853. // Write the rule.
  854. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  855. depTarget,
  856. depends, no_commands, true);
  857. }
  858. //----------------------------------------------------------------------------
  859. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  860. {
  861. std::vector<std::string> depends;
  862. std::vector<std::string> commands;
  863. // Construct the clean target name.
  864. std::string cleanTarget =
  865. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  866. cleanTarget += "/clean";
  867. // Construct the clean command.
  868. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  869. *this->Target);
  870. this->LocalGenerator->CreateCDCommand
  871. (commands,
  872. this->Makefile->GetStartOutputDirectory(),
  873. cmLocalGenerator::HOME_OUTPUT);
  874. // Write the rule.
  875. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  876. cleanTarget,
  877. depends, commands, true);
  878. }
  879. //----------------------------------------------------------------------------
  880. void cmMakefileTargetGenerator::WriteMakeRule(
  881. std::ostream& os,
  882. const char* comment,
  883. const std::vector<std::string>& outputs,
  884. const std::vector<std::string>& depends,
  885. const std::vector<std::string>& commands,
  886. bool symbolic,
  887. bool in_help)
  888. {
  889. if (outputs.size() == 0)
  890. {
  891. return;
  892. }
  893. // We always attach the actual commands to the first output.
  894. this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
  895. commands, symbolic, in_help);
  896. // For single outputs, we are done.
  897. if (outputs.size() == 1)
  898. {
  899. return;
  900. }
  901. // For multiple outputs, make the extra ones depend on the first one.
  902. std::vector<std::string> const output_depends(1, outputs[0]);
  903. for (std::vector<std::string>::const_iterator o = outputs.begin()+1;
  904. o != outputs.end(); ++o)
  905. {
  906. // Touch the extra output so "make" knows that it was updated,
  907. // but only if the output was acually created.
  908. std::string const out = this->Convert(*o, cmLocalGenerator::HOME_OUTPUT,
  909. cmLocalGenerator::SHELL);
  910. std::vector<std::string> output_commands;
  911. if (!symbolic)
  912. {
  913. output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
  914. }
  915. this->LocalGenerator->WriteMakeRule(os, 0, *o, output_depends,
  916. output_commands, symbolic, in_help);
  917. if (!symbolic)
  918. {
  919. // At build time, remove the first output if this one does not exist
  920. // so that "make" will rerun the real commands that create this one.
  921. MultipleOutputPairsType::value_type p(*o, outputs[0]);
  922. this->MultipleOutputPairs.insert(p);
  923. }
  924. }
  925. }
  926. //----------------------------------------------------------------------------
  927. void cmMakefileTargetGenerator::WriteTargetDependRules()
  928. {
  929. // must write the targets depend info file
  930. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  931. this->InfoFileNameFull = dir;
  932. this->InfoFileNameFull += "/DependInfo.cmake";
  933. this->InfoFileNameFull =
  934. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  935. this->InfoFileStream =
  936. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  937. this->InfoFileStream->SetCopyIfDifferent(true);
  938. if(!*this->InfoFileStream)
  939. {
  940. return;
  941. }
  942. this->LocalGenerator->
  943. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  944. // Store multiple output pairs in the depend info file.
  945. if(!this->MultipleOutputPairs.empty())
  946. {
  947. *this->InfoFileStream
  948. << "\n"
  949. << "# Pairs of files generated by the same build rule.\n"
  950. << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  951. for(MultipleOutputPairsType::const_iterator pi =
  952. this->MultipleOutputPairs.begin();
  953. pi != this->MultipleOutputPairs.end(); ++pi)
  954. {
  955. *this->InfoFileStream
  956. << " " << this->LocalGenerator->EscapeForCMake(pi->first)
  957. << " " << this->LocalGenerator->EscapeForCMake(pi->second)
  958. << "\n";
  959. }
  960. *this->InfoFileStream << " )\n\n";
  961. }
  962. // Store list of targets linked directly or transitively.
  963. {
  964. *this->InfoFileStream
  965. << "\n"
  966. << "# Targets to which this target links.\n"
  967. << "set(CMAKE_TARGET_LINKED_INFO_FILES\n";
  968. std::set<cmTarget const*> emitted;
  969. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  970. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  971. {
  972. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  973. for(cmComputeLinkInformation::ItemVector::const_iterator
  974. i = items.begin(); i != items.end(); ++i)
  975. {
  976. cmTarget const* linkee = i->Target;
  977. if(linkee && !linkee->IsImported()
  978. // We can ignore the INTERFACE_LIBRARY items because
  979. // Target->GetLinkInformation already processed their
  980. // link interface and they don't have any output themselves.
  981. && linkee->GetType() != cmTarget::INTERFACE_LIBRARY
  982. && emitted.insert(linkee).second)
  983. {
  984. cmMakefile* mf = linkee->GetMakefile();
  985. cmLocalGenerator* lg = mf->GetLocalGenerator();
  986. std::string di = mf->GetStartOutputDirectory();
  987. di += "/";
  988. di += lg->GetTargetDirectory(*linkee);
  989. di += "/DependInfo.cmake";
  990. *this->InfoFileStream << " \"" << di << "\"\n";
  991. }
  992. }
  993. }
  994. *this->InfoFileStream
  995. << " )\n";
  996. }
  997. // Check for a target-specific module output directory.
  998. if(const char* mdir = this->GetFortranModuleDirectory())
  999. {
  1000. *this->InfoFileStream
  1001. << "\n"
  1002. << "# Fortran module output directory.\n"
  1003. << "set(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  1004. }
  1005. // Target-specific include directories:
  1006. *this->InfoFileStream
  1007. << "\n"
  1008. << "# The include file search paths:\n";
  1009. *this->InfoFileStream
  1010. << "set(CMAKE_C_TARGET_INCLUDE_PATH\n";
  1011. std::vector<std::string> includes;
  1012. const std::string& config =
  1013. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1014. this->LocalGenerator->GetIncludeDirectories(includes,
  1015. this->GeneratorTarget,
  1016. "C", config);
  1017. for(std::vector<std::string>::iterator i = includes.begin();
  1018. i != includes.end(); ++i)
  1019. {
  1020. *this->InfoFileStream
  1021. << " \""
  1022. << this->LocalGenerator->Convert(*i,
  1023. cmLocalGenerator::HOME_OUTPUT)
  1024. << "\"\n";
  1025. }
  1026. *this->InfoFileStream
  1027. << " )\n";
  1028. *this->InfoFileStream
  1029. << "set(CMAKE_CXX_TARGET_INCLUDE_PATH "
  1030. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  1031. *this->InfoFileStream
  1032. << "set(CMAKE_Fortran_TARGET_INCLUDE_PATH "
  1033. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  1034. *this->InfoFileStream
  1035. << "set(CMAKE_ASM_TARGET_INCLUDE_PATH "
  1036. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  1037. // and now write the rule to use it
  1038. std::vector<std::string> depends;
  1039. std::vector<std::string> commands;
  1040. // Construct the name of the dependency generation target.
  1041. std::string depTarget =
  1042. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1043. depTarget += "/depend";
  1044. // Add a command to call CMake to scan dependencies. CMake will
  1045. // touch the corresponding depends file after scanning dependencies.
  1046. std::ostringstream depCmd;
  1047. // TODO: Account for source file properties and directory-level
  1048. // definitions when scanning for dependencies.
  1049. #if !defined(_WIN32) || defined(__CYGWIN__)
  1050. // This platform supports symlinks, so cmSystemTools will translate
  1051. // paths. Make sure PWD is set to the original name of the home
  1052. // output directory to help cmSystemTools to create the same
  1053. // translation table for the dependency scanning process.
  1054. depCmd << "cd "
  1055. << (this->LocalGenerator->Convert(
  1056. this->Makefile->GetHomeOutputDirectory(),
  1057. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  1058. << " && ";
  1059. #endif
  1060. // Generate a call this signature:
  1061. //
  1062. // cmake -E cmake_depends <generator>
  1063. // <home-src-dir> <start-src-dir>
  1064. // <home-out-dir> <start-out-dir>
  1065. // <dep-info> --color=$(COLOR)
  1066. //
  1067. // This gives the dependency scanner enough information to recreate
  1068. // the state of our local generator sufficiently for its needs.
  1069. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  1070. << this->GlobalGenerator->GetName() << "\" "
  1071. << this->Convert(this->Makefile->GetHomeDirectory(),
  1072. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1073. << " "
  1074. << this->Convert(this->Makefile->GetStartDirectory(),
  1075. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1076. << " "
  1077. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  1078. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1079. << " "
  1080. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  1081. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1082. << " "
  1083. << this->Convert(this->InfoFileNameFull,
  1084. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  1085. if(this->LocalGenerator->GetColorMakefile())
  1086. {
  1087. depCmd << " --color=$(COLOR)";
  1088. }
  1089. commands.push_back(depCmd.str());
  1090. // Make sure all custom command outputs in this target are built.
  1091. if(this->CustomCommandDriver == OnDepends)
  1092. {
  1093. this->DriveCustomCommands(depends);
  1094. }
  1095. // Write the rule.
  1096. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1097. depTarget,
  1098. depends, commands, true);
  1099. }
  1100. //----------------------------------------------------------------------------
  1101. void
  1102. cmMakefileTargetGenerator
  1103. ::DriveCustomCommands(std::vector<std::string>& depends)
  1104. {
  1105. // Depend on all custom command outputs.
  1106. std::vector<cmSourceFile*> sources;
  1107. this->Target->GetSourceFiles(sources,
  1108. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1109. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1110. source != sources.end(); ++source)
  1111. {
  1112. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  1113. {
  1114. cmCustomCommandGenerator ccg(*cc, this->ConfigName, this->Makefile);
  1115. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1116. depends.insert(depends.end(), outputs.begin(), outputs.end());
  1117. }
  1118. }
  1119. }
  1120. //----------------------------------------------------------------------------
  1121. void cmMakefileTargetGenerator
  1122. ::WriteObjectDependRules(cmSourceFile const& source,
  1123. std::vector<std::string>& depends)
  1124. {
  1125. // Create the list of dependencies known at cmake time. These are
  1126. // shared between the object file and dependency scanning rule.
  1127. depends.push_back(source.GetFullPath());
  1128. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  1129. {
  1130. cmSystemTools::ExpandListArgument(objectDeps, depends);
  1131. }
  1132. }
  1133. //----------------------------------------------------------------------------
  1134. void cmMakefileTargetGenerator
  1135. ::GenerateCustomRuleFile(cmCustomCommandGenerator const& ccg)
  1136. {
  1137. // Collect the commands.
  1138. std::vector<std::string> commands;
  1139. std::string comment = this->LocalGenerator->ConstructComment(ccg);
  1140. if(!comment.empty())
  1141. {
  1142. // add in a progress call if needed
  1143. this->AppendProgress(commands);
  1144. if(!this->NoRuleMessages)
  1145. {
  1146. this->LocalGenerator
  1147. ->AppendEcho(commands, comment.c_str(),
  1148. cmLocalUnixMakefileGenerator3::EchoGenerate);
  1149. }
  1150. }
  1151. // Now append the actual user-specified commands.
  1152. std::ostringstream content;
  1153. this->LocalGenerator->AppendCustomCommand(commands, ccg, this->Target, false,
  1154. cmLocalGenerator::HOME_OUTPUT,
  1155. &content);
  1156. // Collect the dependencies.
  1157. std::vector<std::string> depends;
  1158. this->LocalGenerator->AppendCustomDepend(depends, ccg);
  1159. // Check whether we need to bother checking for a symbolic output.
  1160. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  1161. // Write the rule.
  1162. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1163. std::vector<std::string>::const_iterator o = outputs.begin();
  1164. {
  1165. bool symbolic = false;
  1166. if(need_symbolic)
  1167. {
  1168. if(cmSourceFile* sf = this->Makefile->GetSource(*o))
  1169. {
  1170. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1171. }
  1172. }
  1173. this->WriteMakeRule(*this->BuildFileStream, 0, outputs,
  1174. depends, commands, symbolic);
  1175. // If the rule has changed make sure the output is rebuilt.
  1176. if(!symbolic)
  1177. {
  1178. this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
  1179. }
  1180. }
  1181. // Setup implicit dependency scanning.
  1182. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1183. idi = ccg.GetCC().GetImplicitDepends().begin();
  1184. idi != ccg.GetCC().GetImplicitDepends().end(); ++idi)
  1185. {
  1186. std::string objFullPath =
  1187. this->Convert(outputs[0], cmLocalGenerator::FULL);
  1188. std::string srcFullPath =
  1189. this->Convert(idi->second, cmLocalGenerator::FULL);
  1190. this->LocalGenerator->
  1191. AddImplicitDepends(*this->Target, idi->first,
  1192. objFullPath.c_str(),
  1193. srcFullPath.c_str());
  1194. }
  1195. }
  1196. //----------------------------------------------------------------------------
  1197. void
  1198. cmMakefileTargetGenerator::AppendProgress(std::vector<std::string>& commands)
  1199. {
  1200. this->NumberOfProgressActions++;
  1201. if(this->NoRuleMessages)
  1202. {
  1203. return;
  1204. }
  1205. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1206. progressDir += cmake::GetCMakeFilesDirectory();
  1207. std::ostringstream progCmd;
  1208. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  1209. progCmd << this->LocalGenerator->Convert(progressDir,
  1210. cmLocalGenerator::FULL,
  1211. cmLocalGenerator::SHELL);
  1212. progCmd << " $(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1213. commands.push_back(progCmd.str());
  1214. }
  1215. //----------------------------------------------------------------------------
  1216. void
  1217. cmMakefileTargetGenerator
  1218. ::WriteObjectsVariable(std::string& variableName,
  1219. std::string& variableNameExternal,
  1220. bool useWatcomQuote)
  1221. {
  1222. // Write a make variable assignment that lists all objects for the
  1223. // target.
  1224. variableName =
  1225. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1226. "_OBJECTS");
  1227. *this->BuildFileStream
  1228. << "# Object files for target " << this->Target->GetName() << "\n"
  1229. << variableName << " =";
  1230. std::string object;
  1231. const char* lineContinue =
  1232. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1233. if(!lineContinue)
  1234. {
  1235. lineContinue = "\\";
  1236. }
  1237. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1238. i != this->Objects.end(); ++i)
  1239. {
  1240. *this->BuildFileStream << " " << lineContinue << "\n";
  1241. *this->BuildFileStream <<
  1242. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1243. useWatcomQuote);
  1244. }
  1245. *this->BuildFileStream << "\n";
  1246. // Write a make variable assignment that lists all external objects
  1247. // for the target.
  1248. variableNameExternal =
  1249. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1250. "_EXTERNAL_OBJECTS");
  1251. *this->BuildFileStream
  1252. << "\n"
  1253. << "# External object files for target "
  1254. << this->Target->GetName() << "\n"
  1255. << variableNameExternal << " =";
  1256. for(std::vector<std::string>::const_iterator i =
  1257. this->ExternalObjects.begin();
  1258. i != this->ExternalObjects.end(); ++i)
  1259. {
  1260. object = this->Convert(*i,cmLocalGenerator::START_OUTPUT);
  1261. *this->BuildFileStream
  1262. << " " << lineContinue << "\n"
  1263. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1264. *this->BuildFileStream <<
  1265. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1266. useWatcomQuote);
  1267. }
  1268. *this->BuildFileStream << "\n" << "\n";
  1269. }
  1270. //----------------------------------------------------------------------------
  1271. void
  1272. cmMakefileTargetGenerator
  1273. ::WriteObjectsString(std::string& buildObjs)
  1274. {
  1275. std::vector<std::string> objStrings;
  1276. this->WriteObjectsStrings(objStrings);
  1277. buildObjs = objStrings[0];
  1278. }
  1279. //----------------------------------------------------------------------------
  1280. class cmMakefileTargetGeneratorObjectStrings
  1281. {
  1282. public:
  1283. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1284. cmLocalUnixMakefileGenerator3* lg,
  1285. std::string::size_type limit):
  1286. Strings(strings), LocalGenerator(lg), LengthLimit(limit)
  1287. {
  1288. this->Space = "";
  1289. }
  1290. void Feed(std::string const& obj)
  1291. {
  1292. // Construct the name of the next object.
  1293. this->NextObject =
  1294. this->LocalGenerator->Convert(obj,
  1295. cmLocalGenerator::START_OUTPUT,
  1296. cmLocalGenerator::RESPONSE);
  1297. // Roll over to next string if the limit will be exceeded.
  1298. if(this->LengthLimit != std::string::npos &&
  1299. (this->CurrentString.length() + 1 + this->NextObject.length()
  1300. > this->LengthLimit))
  1301. {
  1302. this->Strings.push_back(this->CurrentString);
  1303. this->CurrentString = "";
  1304. this->Space = "";
  1305. }
  1306. // Separate from previous object.
  1307. this->CurrentString += this->Space;
  1308. this->Space = " ";
  1309. // Append this object.
  1310. this->CurrentString += this->NextObject;
  1311. }
  1312. void Done()
  1313. {
  1314. this->Strings.push_back(this->CurrentString);
  1315. }
  1316. private:
  1317. std::vector<std::string>& Strings;
  1318. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1319. std::string::size_type LengthLimit;
  1320. std::string CurrentString;
  1321. std::string NextObject;
  1322. const char* Space;
  1323. };
  1324. //----------------------------------------------------------------------------
  1325. void
  1326. cmMakefileTargetGenerator
  1327. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1328. std::string::size_type limit)
  1329. {
  1330. cmMakefileTargetGeneratorObjectStrings
  1331. helper(objStrings, this->LocalGenerator, limit);
  1332. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1333. i != this->Objects.end(); ++i)
  1334. {
  1335. helper.Feed(*i);
  1336. }
  1337. for(std::vector<std::string>::const_iterator i =
  1338. this->ExternalObjects.begin();
  1339. i != this->ExternalObjects.end(); ++i)
  1340. {
  1341. helper.Feed(*i);
  1342. }
  1343. helper.Done();
  1344. }
  1345. //----------------------------------------------------------------------------
  1346. void cmMakefileTargetGenerator::WriteTargetDriverRule(
  1347. const std::string& main_output,
  1348. bool relink)
  1349. {
  1350. // Compute the name of the driver target.
  1351. std::string dir =
  1352. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1353. std::string buildTargetRuleName = dir;
  1354. buildTargetRuleName += relink?"/preinstall":"/build";
  1355. buildTargetRuleName = this->Convert(buildTargetRuleName,
  1356. cmLocalGenerator::HOME_OUTPUT,
  1357. cmLocalGenerator::UNCHANGED);
  1358. // Build the list of target outputs to drive.
  1359. std::vector<std::string> depends;
  1360. depends.push_back(main_output);
  1361. const char* comment = 0;
  1362. if(relink)
  1363. {
  1364. // Setup the comment for the preinstall driver.
  1365. comment = "Rule to relink during preinstall.";
  1366. }
  1367. else
  1368. {
  1369. // Setup the comment for the main build driver.
  1370. comment = "Rule to build all files generated by this target.";
  1371. // Make sure all custom command outputs in this target are built.
  1372. if(this->CustomCommandDriver == OnBuild)
  1373. {
  1374. this->DriveCustomCommands(depends);
  1375. }
  1376. // Make sure the extra files are built.
  1377. depends.insert(depends.end(),
  1378. this->ExtraFiles.begin(), this->ExtraFiles.end());
  1379. }
  1380. // Write the driver rule.
  1381. std::vector<std::string> no_commands;
  1382. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1383. buildTargetRuleName,
  1384. depends, no_commands, true);
  1385. }
  1386. //----------------------------------------------------------------------------
  1387. std::string cmMakefileTargetGenerator::GetFrameworkFlags(std::string const& l)
  1388. {
  1389. if(!this->Makefile->IsOn("APPLE"))
  1390. {
  1391. return std::string();
  1392. }
  1393. std::string fwSearchFlagVar = "CMAKE_" + l + "_FRAMEWORK_SEARCH_FLAG";
  1394. const char* fwSearchFlag =
  1395. this->Makefile->GetDefinition(fwSearchFlagVar);
  1396. if(!(fwSearchFlag && *fwSearchFlag))
  1397. {
  1398. return std::string();
  1399. }
  1400. std::set<std::string> emitted;
  1401. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1402. emitted.insert("/System/Library/Frameworks");
  1403. #endif
  1404. std::vector<std::string> includes;
  1405. const std::string& config =
  1406. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1407. this->LocalGenerator->GetIncludeDirectories(includes,
  1408. this->GeneratorTarget,
  1409. "C", config);
  1410. // check all include directories for frameworks as this
  1411. // will already have added a -F for the framework
  1412. for(std::vector<std::string>::iterator i = includes.begin();
  1413. i != includes.end(); ++i)
  1414. {
  1415. if(this->Target->NameResolvesToFramework(*i))
  1416. {
  1417. std::string frameworkDir = *i;
  1418. frameworkDir += "/../";
  1419. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1420. emitted.insert(frameworkDir);
  1421. }
  1422. }
  1423. std::string flags;
  1424. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1425. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1426. {
  1427. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1428. for(std::vector<std::string>::const_iterator i = frameworks.begin();
  1429. i != frameworks.end(); ++i)
  1430. {
  1431. if(emitted.insert(*i).second)
  1432. {
  1433. flags += fwSearchFlag;
  1434. flags += this->Convert(*i,
  1435. cmLocalGenerator::START_OUTPUT,
  1436. cmLocalGenerator::SHELL, true);
  1437. flags += " ";
  1438. }
  1439. }
  1440. }
  1441. return flags;
  1442. }
  1443. //----------------------------------------------------------------------------
  1444. void cmMakefileTargetGenerator
  1445. ::AppendTargetDepends(std::vector<std::string>& depends)
  1446. {
  1447. // Static libraries never depend on anything for linking.
  1448. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1449. {
  1450. return;
  1451. }
  1452. // Loop over all library dependencies.
  1453. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1454. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1455. {
  1456. std::vector<std::string> const& libDeps = cli->GetDepends();
  1457. depends.insert(depends.end(), libDeps.begin(), libDeps.end());
  1458. }
  1459. }
  1460. //----------------------------------------------------------------------------
  1461. void cmMakefileTargetGenerator
  1462. ::AppendObjectDepends(std::vector<std::string>& depends)
  1463. {
  1464. // Add dependencies on the compiled object files.
  1465. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1466. std::string objTarget;
  1467. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1468. obj != this->Objects.end(); ++obj)
  1469. {
  1470. objTarget = relPath;
  1471. objTarget += *obj;
  1472. depends.push_back(objTarget);
  1473. }
  1474. // Add dependencies on the external object files.
  1475. depends.insert(depends.end(),
  1476. this->ExternalObjects.begin(), this->ExternalObjects.end());
  1477. // Add a dependency on the rule file itself.
  1478. this->LocalGenerator->AppendRuleDepend(depends,
  1479. this->BuildFileNameFull.c_str());
  1480. }
  1481. //----------------------------------------------------------------------------
  1482. void cmMakefileTargetGenerator
  1483. ::AppendLinkDepends(std::vector<std::string>& depends)
  1484. {
  1485. this->AppendObjectDepends(depends);
  1486. // Add dependencies on targets that must be built first.
  1487. this->AppendTargetDepends(depends);
  1488. // Add a dependency on the link definitions file, if any.
  1489. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1490. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1491. if(!def.empty())
  1492. {
  1493. depends.push_back(def);
  1494. }
  1495. // Add user-specified dependencies.
  1496. if(const char* linkDepends =
  1497. this->Target->GetProperty("LINK_DEPENDS"))
  1498. {
  1499. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1500. }
  1501. }
  1502. //----------------------------------------------------------------------------
  1503. std::string cmMakefileTargetGenerator::GetLinkRule(
  1504. const std::string& linkRuleVar)
  1505. {
  1506. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1507. if(this->Target->HasImplibGNUtoMS())
  1508. {
  1509. std::string ruleVar = "CMAKE_";
  1510. ruleVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1511. ruleVar += "_GNUtoMS_RULE";
  1512. if(const char* rule = this->Makefile->GetDefinition(ruleVar))
  1513. {
  1514. linkRule += rule;
  1515. }
  1516. }
  1517. return linkRule;
  1518. }
  1519. //----------------------------------------------------------------------------
  1520. void cmMakefileTargetGenerator
  1521. ::CloseFileStreams()
  1522. {
  1523. delete this->BuildFileStream;
  1524. delete this->InfoFileStream;
  1525. delete this->FlagFileStream;
  1526. }
  1527. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1528. const std::string& linkLang,
  1529. std::string& linkFlags)
  1530. {
  1531. // check for language flags that are not allowed at link time, and
  1532. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1533. // which fails, there may be more]
  1534. std::string removeFlags = "CMAKE_";
  1535. removeFlags += linkLang;
  1536. removeFlags += flagVar;
  1537. std::string removeflags =
  1538. this->Makefile->GetSafeDefinition(removeFlags);
  1539. std::vector<std::string> removeList;
  1540. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1541. for(std::vector<std::string>::iterator i = removeList.begin();
  1542. i != removeList.end(); ++i)
  1543. {
  1544. std::string tmp;
  1545. std::string::size_type lastPosition = 0;
  1546. for(;;)
  1547. {
  1548. std::string::size_type position = linkFlags.find(*i, lastPosition);
  1549. if(position == std::string::npos)
  1550. {
  1551. tmp += linkFlags.substr(lastPosition);
  1552. break;
  1553. }
  1554. else
  1555. {
  1556. std::string::size_type prefixLength = position - lastPosition;
  1557. tmp += linkFlags.substr(lastPosition, prefixLength);
  1558. lastPosition = position + i->length();
  1559. bool validFlagStart = position == 0 ||
  1560. isspace(linkFlags[position - 1]);
  1561. bool validFlagEnd = lastPosition == linkFlags.size() ||
  1562. isspace(linkFlags[lastPosition]);
  1563. if(!validFlagStart || !validFlagEnd)
  1564. {
  1565. tmp += *i;
  1566. }
  1567. }
  1568. }
  1569. linkFlags = tmp;
  1570. }
  1571. }
  1572. //----------------------------------------------------------------------------
  1573. void
  1574. cmMakefileTargetGenerator
  1575. ::CreateLinkScript(const char* name,
  1576. std::vector<std::string> const& link_commands,
  1577. std::vector<std::string>& makefile_commands,
  1578. std::vector<std::string>& makefile_depends)
  1579. {
  1580. // Create the link script file.
  1581. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1582. linkScriptName += "/";
  1583. linkScriptName += name;
  1584. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1585. linkScriptStream.SetCopyIfDifferent(true);
  1586. for(std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1587. cmd != link_commands.end(); ++cmd)
  1588. {
  1589. // Do not write out empty commands or commands beginning in the
  1590. // shell no-op ":".
  1591. if(!cmd->empty() && (*cmd)[0] != ':')
  1592. {
  1593. linkScriptStream << *cmd << "\n";
  1594. }
  1595. }
  1596. // Create the makefile command to invoke the link script.
  1597. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1598. link_command += this->Convert(linkScriptName,
  1599. cmLocalGenerator::START_OUTPUT,
  1600. cmLocalGenerator::SHELL);
  1601. link_command += " --verbose=$(VERBOSE)";
  1602. makefile_commands.push_back(link_command);
  1603. makefile_depends.push_back(linkScriptName);
  1604. }
  1605. //----------------------------------------------------------------------------
  1606. std::string
  1607. cmMakefileTargetGenerator
  1608. ::CreateResponseFile(const char* name, std::string const& options,
  1609. std::vector<std::string>& makefile_depends)
  1610. {
  1611. // Create the response file.
  1612. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1613. responseFileNameFull += "/";
  1614. responseFileNameFull += name;
  1615. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1616. responseStream.SetCopyIfDifferent(true);
  1617. responseStream << options << "\n";
  1618. // Add a dependency so the target will rebuild when the set of
  1619. // objects changes.
  1620. makefile_depends.push_back(responseFileNameFull);
  1621. // Construct the name to be used on the command line.
  1622. std::string responseFileName = this->TargetBuildDirectory;
  1623. responseFileName += "/";
  1624. responseFileName += name;
  1625. return responseFileName;
  1626. }
  1627. //----------------------------------------------------------------------------
  1628. void
  1629. cmMakefileTargetGenerator
  1630. ::CreateLinkLibs(std::string& linkLibs, bool relink,
  1631. bool useResponseFile,
  1632. std::vector<std::string>& makefile_depends,
  1633. bool useWatcomQuote)
  1634. {
  1635. std::string frameworkPath;
  1636. std::string linkPath;
  1637. this->LocalGenerator
  1638. ->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1639. *this->GeneratorTarget, relink,
  1640. useResponseFile,
  1641. useWatcomQuote);
  1642. linkLibs = frameworkPath + linkPath + linkLibs;
  1643. if(useResponseFile && linkLibs.find_first_not_of(" ") != linkLibs.npos)
  1644. {
  1645. // Lookup the response file reference flag.
  1646. std::string responseFlagVar = "CMAKE_";
  1647. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1648. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1649. const char* responseFlag =
  1650. this->Makefile->GetDefinition(responseFlagVar);
  1651. if(!responseFlag)
  1652. {
  1653. responseFlag = "@";
  1654. }
  1655. // Create this response file.
  1656. std::string link_rsp =
  1657. this->CreateResponseFile("linklibs.rsp", linkLibs, makefile_depends);
  1658. // Reference the response file.
  1659. linkLibs = responseFlag;
  1660. linkLibs += this->Convert(link_rsp,
  1661. cmLocalGenerator::NONE,
  1662. cmLocalGenerator::SHELL);
  1663. }
  1664. }
  1665. //----------------------------------------------------------------------------
  1666. void
  1667. cmMakefileTargetGenerator
  1668. ::CreateObjectLists(bool useLinkScript, bool useArchiveRules,
  1669. bool useResponseFile, std::string& buildObjs,
  1670. std::vector<std::string>& makefile_depends,
  1671. bool useWatcomQuote)
  1672. {
  1673. std::string variableName;
  1674. std::string variableNameExternal;
  1675. this->WriteObjectsVariable(variableName, variableNameExternal,
  1676. useWatcomQuote);
  1677. if(useResponseFile)
  1678. {
  1679. // MSVC response files cannot exceed 128K.
  1680. std::string::size_type const responseFileLimit = 131000;
  1681. // Construct the individual object list strings.
  1682. std::vector<std::string> object_strings;
  1683. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1684. // Lookup the response file reference flag.
  1685. std::string responseFlagVar = "CMAKE_";
  1686. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1687. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1688. const char* responseFlag =
  1689. this->Makefile->GetDefinition(responseFlagVar);
  1690. if(!responseFlag)
  1691. {
  1692. responseFlag = "@";
  1693. }
  1694. // Write a response file for each string.
  1695. const char* sep = "";
  1696. for(unsigned int i = 0; i < object_strings.size(); ++i)
  1697. {
  1698. // Number the response files.
  1699. char rsp[32];
  1700. sprintf(rsp, "objects%u.rsp", i+1);
  1701. // Create this response file.
  1702. std::string objects_rsp =
  1703. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1704. // Separate from previous response file references.
  1705. buildObjs += sep;
  1706. sep = " ";
  1707. // Reference the response file.
  1708. buildObjs += responseFlag;
  1709. buildObjs += this->Convert(objects_rsp,
  1710. cmLocalGenerator::NONE,
  1711. cmLocalGenerator::SHELL);
  1712. }
  1713. }
  1714. else if(useLinkScript)
  1715. {
  1716. if(!useArchiveRules)
  1717. {
  1718. this->WriteObjectsString(buildObjs);
  1719. }
  1720. }
  1721. else
  1722. {
  1723. buildObjs = "$(";
  1724. buildObjs += variableName;
  1725. buildObjs += ") $(";
  1726. buildObjs += variableNameExternal;
  1727. buildObjs += ")";
  1728. }
  1729. }
  1730. //----------------------------------------------------------------------------
  1731. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1732. const std::string& lang)
  1733. {
  1734. std::string responseVar = "CMAKE_";
  1735. responseVar += lang;
  1736. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1737. bool useResponseFile = this->Makefile->IsOn(responseVar);
  1738. std::vector<std::string> includes;
  1739. const std::string& config =
  1740. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1741. this->LocalGenerator->GetIncludeDirectories(includes,
  1742. this->GeneratorTarget,
  1743. lang, config);
  1744. std::string includeFlags =
  1745. this->LocalGenerator->GetIncludeFlags(includes, this->GeneratorTarget,
  1746. lang, false, useResponseFile,
  1747. config);
  1748. if(includeFlags.empty())
  1749. {
  1750. return;
  1751. }
  1752. if(useResponseFile)
  1753. {
  1754. std::string name = "includes_";
  1755. name += lang;
  1756. name += ".rsp";
  1757. std::string arg = "@" +
  1758. this->CreateResponseFile(name.c_str(), includeFlags,
  1759. this->FlagFileDepends[lang]);
  1760. this->LocalGenerator->AppendFlags(flags, arg);
  1761. }
  1762. else
  1763. {
  1764. this->LocalGenerator->AppendFlags(flags, includeFlags);
  1765. }
  1766. }
  1767. //----------------------------------------------------------------------------
  1768. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1769. {
  1770. // Compute the module directory.
  1771. if(!this->FortranModuleDirectoryComputed)
  1772. {
  1773. const char* target_mod_dir =
  1774. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1775. const char* moddir_flag =
  1776. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1777. if(target_mod_dir && moddir_flag)
  1778. {
  1779. // Compute the full path to the module directory.
  1780. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1781. {
  1782. // Already a full path.
  1783. this->FortranModuleDirectory = target_mod_dir;
  1784. }
  1785. else
  1786. {
  1787. // Interpret relative to the current output directory.
  1788. this->FortranModuleDirectory =
  1789. this->Makefile->GetCurrentOutputDirectory();
  1790. this->FortranModuleDirectory += "/";
  1791. this->FortranModuleDirectory += target_mod_dir;
  1792. }
  1793. // Make sure the module output directory exists.
  1794. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1795. }
  1796. this->FortranModuleDirectoryComputed = true;
  1797. }
  1798. // Return the computed directory.
  1799. if(this->FortranModuleDirectory.empty())
  1800. {
  1801. return 0;
  1802. }
  1803. else
  1804. {
  1805. return this->FortranModuleDirectory.c_str();
  1806. }
  1807. }
  1808. //----------------------------------------------------------------------------
  1809. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1810. {
  1811. // Enable module output if necessary.
  1812. if(const char* modout_flag =
  1813. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  1814. {
  1815. this->LocalGenerator->AppendFlags(flags, modout_flag);
  1816. }
  1817. // Add a module output directory flag if necessary.
  1818. const char* mod_dir = this->GetFortranModuleDirectory();
  1819. if(!mod_dir)
  1820. {
  1821. mod_dir = this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  1822. }
  1823. if(mod_dir)
  1824. {
  1825. const char* moddir_flag =
  1826. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1827. std::string modflag = moddir_flag;
  1828. modflag += this->Convert(mod_dir,
  1829. cmLocalGenerator::START_OUTPUT,
  1830. cmLocalGenerator::SHELL);
  1831. this->LocalGenerator->AppendFlags(flags, modflag);
  1832. }
  1833. // If there is a separate module path flag then duplicate the
  1834. // include path with it. This compiler does not search the include
  1835. // path for modules.
  1836. if(const char* modpath_flag =
  1837. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1838. {
  1839. std::vector<std::string> includes;
  1840. const std::string& config =
  1841. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1842. this->LocalGenerator->GetIncludeDirectories(includes,
  1843. this->GeneratorTarget,
  1844. "C", config);
  1845. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1846. idi != includes.end(); ++idi)
  1847. {
  1848. std::string flg = modpath_flag;
  1849. flg += this->Convert(*idi,
  1850. cmLocalGenerator::NONE,
  1851. cmLocalGenerator::SHELL);
  1852. this->LocalGenerator->AppendFlags(flags, flg);
  1853. }
  1854. }
  1855. }
  1856. //----------------------------------------------------------------------------
  1857. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1858. {
  1859. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1860. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1861. if(def.empty())
  1862. {
  1863. return;
  1864. }
  1865. // TODO: Create a per-language flag variable.
  1866. const char* defFileFlag =
  1867. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1868. if(!defFileFlag)
  1869. {
  1870. return;
  1871. }
  1872. // Append the flag and value. Use ConvertToLinkReference to help
  1873. // vs6's "cl -link" pass it to the linker.
  1874. std::string flag = defFileFlag;
  1875. flag += (this->LocalGenerator->ConvertToLinkReference(def));
  1876. this->LocalGenerator->AppendFlags(flags, flag);
  1877. }
  1878. //----------------------------------------------------------------------------
  1879. const char* cmMakefileTargetGenerator::GetFeature(const std::string& feature)
  1880. {
  1881. return this->Target->GetFeature(feature, this->ConfigName);
  1882. }
  1883. //----------------------------------------------------------------------------
  1884. bool cmMakefileTargetGenerator::GetFeatureAsBool(const std::string& feature)
  1885. {
  1886. return this->Target->GetFeatureAsBool(feature, this->ConfigName);
  1887. }
  1888. //----------------------------------------------------------------------------
  1889. void cmMakefileTargetGenerator::AddFeatureFlags(
  1890. std::string& flags, const std::string& lang
  1891. )
  1892. {
  1893. // Add language-specific flags.
  1894. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1895. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1896. {
  1897. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1898. }
  1899. }