cmMakefileTargetGenerator.cxx 72 KB

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