cmMakefileTargetGenerator.cxx 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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, l);
  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->NumberOfProgressActions++;
  551. if(!this->NoRuleMessages)
  552. {
  553. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  554. this->MakeEchoProgress(progress);
  555. std::string buildEcho = "Building ";
  556. buildEcho += lang;
  557. buildEcho += " object ";
  558. buildEcho += relativeObj;
  559. this->LocalGenerator->AppendEcho
  560. (commands, buildEcho.c_str(), cmLocalUnixMakefileGenerator3::EchoBuild,
  561. &progress);
  562. }
  563. std::string targetOutPathReal;
  564. std::string targetOutPathPDB;
  565. std::string targetOutPathCompilePDB;
  566. {
  567. std::string targetFullPathReal;
  568. std::string targetFullPathPDB;
  569. std::string targetFullPathCompilePDB;
  570. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  571. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  572. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  573. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  574. {
  575. targetFullPathReal =
  576. this->Target->GetFullPath(this->ConfigName, false, true);
  577. targetFullPathPDB = this->Target->GetPDBDirectory(this->ConfigName);
  578. targetFullPathPDB += "/";
  579. targetFullPathPDB += this->Target->GetPDBName(this->ConfigName);
  580. }
  581. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  582. {
  583. targetFullPathCompilePDB =
  584. this->Target->GetCompilePDBPath(this->ConfigName);
  585. if(targetFullPathCompilePDB.empty())
  586. {
  587. targetFullPathCompilePDB = this->Target->GetSupportDirectory() + "/";
  588. }
  589. }
  590. targetOutPathReal = this->Convert(targetFullPathReal,
  591. cmLocalGenerator::START_OUTPUT,
  592. cmLocalGenerator::SHELL);
  593. targetOutPathPDB =
  594. this->Convert(targetFullPathPDB,cmLocalGenerator::NONE,
  595. cmLocalGenerator::SHELL);
  596. targetOutPathCompilePDB =
  597. this->Convert(targetFullPathCompilePDB,
  598. cmLocalGenerator::START_OUTPUT,
  599. cmLocalGenerator::SHELL);
  600. }
  601. cmLocalGenerator::RuleVariables vars;
  602. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  603. vars.CMTarget = this->Target;
  604. vars.Language = lang.c_str();
  605. vars.Target = targetOutPathReal.c_str();
  606. vars.TargetPDB = targetOutPathPDB.c_str();
  607. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  608. vars.Source = sourceFile.c_str();
  609. std::string shellObj =
  610. this->Convert(obj,
  611. cmLocalGenerator::NONE,
  612. cmLocalGenerator::SHELL);
  613. vars.Object = shellObj.c_str();
  614. std::string objectDir = this->Target->GetSupportDirectory();
  615. objectDir = this->Convert(objectDir,
  616. cmLocalGenerator::START_OUTPUT,
  617. cmLocalGenerator::SHELL);
  618. vars.ObjectDir = objectDir.c_str();
  619. std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
  620. objectFileDir = this->Convert(objectFileDir,
  621. cmLocalGenerator::START_OUTPUT,
  622. cmLocalGenerator::SHELL);
  623. vars.ObjectFileDir = objectFileDir.c_str();
  624. vars.Flags = flags.c_str();
  625. std::string definesString = "$(";
  626. definesString += lang;
  627. definesString += "_DEFINES)";
  628. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  629. vars.Defines = definesString.c_str();
  630. // At the moment, it is assumed that C, C++, and Fortran have both
  631. // assembly and preprocessor capabilities. The same is true for the
  632. // ability to export compile commands
  633. bool lang_has_preprocessor = ((lang == "C") ||
  634. (lang == "CXX") ||
  635. (lang == "Fortran"));
  636. bool const lang_has_assembly = lang_has_preprocessor;
  637. bool const lang_can_export_cmds = lang_has_preprocessor;
  638. // Construct the compile rules.
  639. {
  640. std::string compileRuleVar = "CMAKE_";
  641. compileRuleVar += lang;
  642. compileRuleVar += "_COMPILE_OBJECT";
  643. std::string compileRule =
  644. this->Makefile->GetRequiredDefinition(compileRuleVar);
  645. std::vector<std::string> compileCommands;
  646. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  647. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  648. lang_can_export_cmds && compileCommands.size() == 1)
  649. {
  650. std::string compileCommand = compileCommands[0];
  651. this->LocalGenerator->ExpandRuleVariables(compileCommand, vars);
  652. std::string workingDirectory =
  653. this->LocalGenerator->Convert(
  654. this->Makefile->GetStartOutputDirectory(), cmLocalGenerator::FULL);
  655. compileCommand.replace(compileCommand.find(langFlags),
  656. langFlags.size(), this->GetFlags(lang));
  657. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  658. compileCommand.replace(compileCommand.find(langDefines),
  659. langDefines.size(), this->GetDefines(lang));
  660. this->GlobalGenerator->AddCXXCompileCommand(
  661. source.GetFullPath(), workingDirectory, compileCommand);
  662. }
  663. // Expand placeholders in the commands.
  664. for(std::vector<std::string>::iterator i = compileCommands.begin();
  665. i != compileCommands.end(); ++i)
  666. {
  667. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  668. }
  669. // Change the command working directory to the local build tree.
  670. this->LocalGenerator->CreateCDCommand
  671. (compileCommands,
  672. this->Makefile->GetStartOutputDirectory(),
  673. cmLocalGenerator::HOME_OUTPUT);
  674. commands.insert(commands.end(),
  675. compileCommands.begin(), compileCommands.end());
  676. }
  677. // Check for extra outputs created by the compilation.
  678. std::vector<std::string> outputs(1, relativeObj);
  679. if(const char* extra_outputs_str =
  680. source.GetProperty("OBJECT_OUTPUTS"))
  681. {
  682. // Register these as extra files to clean.
  683. cmSystemTools::ExpandListArgument(extra_outputs_str, outputs);
  684. this->CleanFiles.insert(this->CleanFiles.end(),
  685. outputs.begin() + 1, outputs.end());
  686. }
  687. // Write the rule.
  688. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  689. outputs, depends, commands, false);
  690. bool do_preprocess_rules = lang_has_preprocessor &&
  691. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  692. bool do_assembly_rules = lang_has_assembly &&
  693. this->LocalGenerator->GetCreateAssemblySourceRules();
  694. if(do_preprocess_rules || do_assembly_rules)
  695. {
  696. std::vector<std::string> force_depends;
  697. force_depends.push_back("cmake_force");
  698. std::string::size_type dot_pos = relativeObj.rfind(".");
  699. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  700. dot_pos = obj.rfind(".");
  701. std::string objBase = obj.substr(0, dot_pos);
  702. if(do_preprocess_rules)
  703. {
  704. commands.clear();
  705. std::string relativeObjI = relativeObjBase + ".i";
  706. std::string objI = objBase + ".i";
  707. std::string preprocessEcho = "Preprocessing ";
  708. preprocessEcho += lang;
  709. preprocessEcho += " source to ";
  710. preprocessEcho += objI;
  711. this->LocalGenerator->AppendEcho(
  712. commands, preprocessEcho.c_str(),
  713. cmLocalUnixMakefileGenerator3::EchoBuild
  714. );
  715. std::string preprocessRuleVar = "CMAKE_";
  716. preprocessRuleVar += lang;
  717. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  718. if(const char* preprocessRule =
  719. this->Makefile->GetDefinition(preprocessRuleVar))
  720. {
  721. std::vector<std::string> preprocessCommands;
  722. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  723. std::string shellObjI =
  724. this->Convert(objI,
  725. cmLocalGenerator::NONE,
  726. cmLocalGenerator::SHELL);
  727. vars.PreprocessedSource = shellObjI.c_str();
  728. // Expand placeholders in the commands.
  729. for(std::vector<std::string>::iterator i = preprocessCommands.begin();
  730. i != preprocessCommands.end(); ++i)
  731. {
  732. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  733. }
  734. this->LocalGenerator->CreateCDCommand
  735. (preprocessCommands,
  736. this->Makefile->GetStartOutputDirectory(),
  737. cmLocalGenerator::HOME_OUTPUT);
  738. commands.insert(commands.end(),
  739. preprocessCommands.begin(),
  740. preprocessCommands.end());
  741. }
  742. else
  743. {
  744. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  745. cmd += preprocessRuleVar;
  746. commands.push_back(cmd);
  747. }
  748. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  749. relativeObjI,
  750. force_depends, commands, false);
  751. }
  752. if(do_assembly_rules)
  753. {
  754. commands.clear();
  755. std::string relativeObjS = relativeObjBase + ".s";
  756. std::string objS = objBase + ".s";
  757. std::string assemblyEcho = "Compiling ";
  758. assemblyEcho += lang;
  759. assemblyEcho += " source to assembly ";
  760. assemblyEcho += objS;
  761. this->LocalGenerator->AppendEcho(
  762. commands, assemblyEcho.c_str(),
  763. cmLocalUnixMakefileGenerator3::EchoBuild
  764. );
  765. std::string assemblyRuleVar = "CMAKE_";
  766. assemblyRuleVar += lang;
  767. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  768. if(const char* assemblyRule =
  769. this->Makefile->GetDefinition(assemblyRuleVar))
  770. {
  771. std::vector<std::string> assemblyCommands;
  772. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  773. std::string shellObjS =
  774. this->Convert(objS,
  775. cmLocalGenerator::NONE,
  776. cmLocalGenerator::SHELL);
  777. vars.AssemblySource = shellObjS.c_str();
  778. // Expand placeholders in the commands.
  779. for(std::vector<std::string>::iterator i = assemblyCommands.begin();
  780. i != assemblyCommands.end(); ++i)
  781. {
  782. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  783. }
  784. this->LocalGenerator->CreateCDCommand
  785. (assemblyCommands,
  786. this->Makefile->GetStartOutputDirectory(),
  787. cmLocalGenerator::HOME_OUTPUT);
  788. commands.insert(commands.end(),
  789. assemblyCommands.begin(),
  790. assemblyCommands.end());
  791. }
  792. else
  793. {
  794. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  795. cmd += assemblyRuleVar;
  796. commands.push_back(cmd);
  797. }
  798. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  799. relativeObjS,
  800. force_depends, commands, false);
  801. }
  802. }
  803. // If the language needs provides-requires mode, create the
  804. // corresponding targets.
  805. std::string objectRequires = relativeObj;
  806. objectRequires += ".requires";
  807. std::vector<std::string> p_depends;
  808. // always provide an empty requires target
  809. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  810. objectRequires, p_depends,
  811. no_commands, true);
  812. // write a build rule to recursively build what this obj provides
  813. std::string objectProvides = relativeObj;
  814. objectProvides += ".provides";
  815. std::string temp = relativeObj;
  816. temp += ".provides.build";
  817. std::vector<std::string> r_commands;
  818. std::string tgtMakefileName =
  819. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  820. tgtMakefileName += "/build.make";
  821. r_commands.push_back
  822. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  823. temp));
  824. p_depends.clear();
  825. p_depends.push_back(objectRequires);
  826. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  827. objectProvides, p_depends,
  828. r_commands, true);
  829. // write the provides.build rule dependency on the obj file
  830. p_depends.clear();
  831. p_depends.push_back(relativeObj);
  832. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  833. temp, p_depends, no_commands,
  834. false);
  835. }
  836. //----------------------------------------------------------------------------
  837. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  838. {
  839. std::vector<std::string> depends;
  840. std::vector<std::string> no_commands;
  841. // Construct the name of the dependency generation target.
  842. std::string depTarget =
  843. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  844. depTarget += "/requires";
  845. // This target drives dependency generation for all object files.
  846. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  847. std::string objTarget;
  848. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  849. obj != this->Objects.end(); ++obj)
  850. {
  851. objTarget = relPath;
  852. objTarget += *obj;
  853. objTarget += ".requires";
  854. depends.push_back(objTarget);
  855. }
  856. // Write the rule.
  857. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  858. depTarget,
  859. depends, no_commands, true);
  860. }
  861. //----------------------------------------------------------------------------
  862. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  863. {
  864. std::vector<std::string> depends;
  865. std::vector<std::string> commands;
  866. // Construct the clean target name.
  867. std::string cleanTarget =
  868. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  869. cleanTarget += "/clean";
  870. // Construct the clean command.
  871. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  872. *this->Target);
  873. this->LocalGenerator->CreateCDCommand
  874. (commands,
  875. this->Makefile->GetStartOutputDirectory(),
  876. cmLocalGenerator::HOME_OUTPUT);
  877. // Write the rule.
  878. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  879. cleanTarget,
  880. depends, commands, true);
  881. }
  882. //----------------------------------------------------------------------------
  883. void cmMakefileTargetGenerator::WriteTargetDependRules()
  884. {
  885. // must write the targets depend info file
  886. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  887. this->InfoFileNameFull = dir;
  888. this->InfoFileNameFull += "/DependInfo.cmake";
  889. this->InfoFileNameFull =
  890. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  891. this->InfoFileStream =
  892. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  893. this->InfoFileStream->SetCopyIfDifferent(true);
  894. if(!*this->InfoFileStream)
  895. {
  896. return;
  897. }
  898. this->LocalGenerator->
  899. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  900. // Store list of targets linked directly or transitively.
  901. {
  902. *this->InfoFileStream
  903. << "\n"
  904. << "# Targets to which this target links.\n"
  905. << "set(CMAKE_TARGET_LINKED_INFO_FILES\n";
  906. std::set<cmTarget const*> emitted;
  907. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  908. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  909. {
  910. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  911. for(cmComputeLinkInformation::ItemVector::const_iterator
  912. i = items.begin(); i != items.end(); ++i)
  913. {
  914. cmTarget const* linkee = i->Target;
  915. if(linkee && !linkee->IsImported()
  916. // We can ignore the INTERFACE_LIBRARY items because
  917. // Target->GetLinkInformation already processed their
  918. // link interface and they don't have any output themselves.
  919. && linkee->GetType() != cmTarget::INTERFACE_LIBRARY
  920. && emitted.insert(linkee).second)
  921. {
  922. cmMakefile* mf = linkee->GetMakefile();
  923. cmLocalGenerator* lg = mf->GetLocalGenerator();
  924. std::string di = mf->GetStartOutputDirectory();
  925. di += "/";
  926. di += lg->GetTargetDirectory(*linkee);
  927. di += "/DependInfo.cmake";
  928. *this->InfoFileStream << " \"" << di << "\"\n";
  929. }
  930. }
  931. }
  932. *this->InfoFileStream
  933. << " )\n";
  934. }
  935. // Check for a target-specific module output directory.
  936. if(const char* mdir = this->GetFortranModuleDirectory())
  937. {
  938. *this->InfoFileStream
  939. << "\n"
  940. << "# Fortran module output directory.\n"
  941. << "set(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  942. }
  943. // and now write the rule to use it
  944. std::vector<std::string> depends;
  945. std::vector<std::string> commands;
  946. // Construct the name of the dependency generation target.
  947. std::string depTarget =
  948. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  949. depTarget += "/depend";
  950. // Add a command to call CMake to scan dependencies. CMake will
  951. // touch the corresponding depends file after scanning dependencies.
  952. std::ostringstream depCmd;
  953. // TODO: Account for source file properties and directory-level
  954. // definitions when scanning for dependencies.
  955. #if !defined(_WIN32) || defined(__CYGWIN__)
  956. // This platform supports symlinks, so cmSystemTools will translate
  957. // paths. Make sure PWD is set to the original name of the home
  958. // output directory to help cmSystemTools to create the same
  959. // translation table for the dependency scanning process.
  960. depCmd << "cd "
  961. << (this->LocalGenerator->Convert(
  962. this->Makefile->GetHomeOutputDirectory(),
  963. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  964. << " && ";
  965. #endif
  966. // Generate a call this signature:
  967. //
  968. // cmake -E cmake_depends <generator>
  969. // <home-src-dir> <start-src-dir>
  970. // <home-out-dir> <start-out-dir>
  971. // <dep-info> --color=$(COLOR)
  972. //
  973. // This gives the dependency scanner enough information to recreate
  974. // the state of our local generator sufficiently for its needs.
  975. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  976. << this->GlobalGenerator->GetName() << "\" "
  977. << this->Convert(this->Makefile->GetHomeDirectory(),
  978. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  979. << " "
  980. << this->Convert(this->Makefile->GetStartDirectory(),
  981. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  982. << " "
  983. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  984. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  985. << " "
  986. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  987. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  988. << " "
  989. << this->Convert(this->InfoFileNameFull,
  990. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  991. if(this->LocalGenerator->GetColorMakefile())
  992. {
  993. depCmd << " --color=$(COLOR)";
  994. }
  995. commands.push_back(depCmd.str());
  996. // Make sure all custom command outputs in this target are built.
  997. if(this->CustomCommandDriver == OnDepends)
  998. {
  999. this->DriveCustomCommands(depends);
  1000. }
  1001. // Write the rule.
  1002. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1003. depTarget,
  1004. depends, commands, true);
  1005. }
  1006. //----------------------------------------------------------------------------
  1007. void
  1008. cmMakefileTargetGenerator
  1009. ::DriveCustomCommands(std::vector<std::string>& depends)
  1010. {
  1011. // Depend on all custom command outputs.
  1012. std::vector<cmSourceFile*> sources;
  1013. this->Target->GetSourceFiles(sources,
  1014. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1015. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1016. source != sources.end(); ++source)
  1017. {
  1018. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  1019. {
  1020. cmCustomCommandGenerator ccg(*cc, this->ConfigName, this->Makefile);
  1021. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1022. depends.insert(depends.end(), outputs.begin(), outputs.end());
  1023. }
  1024. }
  1025. }
  1026. //----------------------------------------------------------------------------
  1027. void cmMakefileTargetGenerator
  1028. ::WriteObjectDependRules(cmSourceFile const& source,
  1029. std::vector<std::string>& depends)
  1030. {
  1031. // Create the list of dependencies known at cmake time. These are
  1032. // shared between the object file and dependency scanning rule.
  1033. depends.push_back(source.GetFullPath());
  1034. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  1035. {
  1036. cmSystemTools::ExpandListArgument(objectDeps, depends);
  1037. }
  1038. }
  1039. //----------------------------------------------------------------------------
  1040. void cmMakefileTargetGenerator
  1041. ::GenerateCustomRuleFile(cmCustomCommandGenerator const& ccg)
  1042. {
  1043. // Collect the commands.
  1044. std::vector<std::string> commands;
  1045. std::string comment = this->LocalGenerator->ConstructComment(ccg);
  1046. if(!comment.empty())
  1047. {
  1048. // add in a progress call if needed
  1049. this->NumberOfProgressActions++;
  1050. if(!this->NoRuleMessages)
  1051. {
  1052. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  1053. this->MakeEchoProgress(progress);
  1054. this->LocalGenerator
  1055. ->AppendEcho(commands, comment.c_str(),
  1056. cmLocalUnixMakefileGenerator3::EchoGenerate,
  1057. &progress);
  1058. }
  1059. }
  1060. // Now append the actual user-specified commands.
  1061. std::ostringstream content;
  1062. this->LocalGenerator->AppendCustomCommand(commands, ccg, this->Target, false,
  1063. cmLocalGenerator::HOME_OUTPUT,
  1064. &content);
  1065. // Collect the dependencies.
  1066. std::vector<std::string> depends;
  1067. this->LocalGenerator->AppendCustomDepend(depends, ccg);
  1068. // Check whether we need to bother checking for a symbolic output.
  1069. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  1070. // Write the rule.
  1071. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1072. std::vector<std::string>::const_iterator o = outputs.begin();
  1073. {
  1074. bool symbolic = false;
  1075. if(need_symbolic)
  1076. {
  1077. if(cmSourceFile* sf = this->Makefile->GetSource(*o))
  1078. {
  1079. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1080. }
  1081. }
  1082. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1083. outputs, depends, commands,
  1084. symbolic);
  1085. // If the rule has changed make sure the output is rebuilt.
  1086. if(!symbolic)
  1087. {
  1088. this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
  1089. }
  1090. }
  1091. // Setup implicit dependency scanning.
  1092. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1093. idi = ccg.GetCC().GetImplicitDepends().begin();
  1094. idi != ccg.GetCC().GetImplicitDepends().end(); ++idi)
  1095. {
  1096. std::string objFullPath =
  1097. this->Convert(outputs[0], cmLocalGenerator::FULL);
  1098. std::string srcFullPath =
  1099. this->Convert(idi->second, cmLocalGenerator::FULL);
  1100. this->LocalGenerator->
  1101. AddImplicitDepends(*this->Target, idi->first,
  1102. objFullPath.c_str(),
  1103. srcFullPath.c_str());
  1104. }
  1105. }
  1106. //----------------------------------------------------------------------------
  1107. void
  1108. cmMakefileTargetGenerator
  1109. ::MakeEchoProgress(cmLocalUnixMakefileGenerator3::EchoProgress& progress) const
  1110. {
  1111. progress.Dir = this->Makefile->GetHomeOutputDirectory();
  1112. progress.Dir += cmake::GetCMakeFilesDirectory();
  1113. std::ostringstream progressArg;
  1114. progressArg << "$(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1115. progress.Arg = progressArg.str();
  1116. }
  1117. //----------------------------------------------------------------------------
  1118. void
  1119. cmMakefileTargetGenerator
  1120. ::WriteObjectsVariable(std::string& variableName,
  1121. std::string& variableNameExternal,
  1122. bool useWatcomQuote)
  1123. {
  1124. // Write a make variable assignment that lists all objects for the
  1125. // target.
  1126. variableName =
  1127. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1128. "_OBJECTS");
  1129. *this->BuildFileStream
  1130. << "# Object files for target " << this->Target->GetName() << "\n"
  1131. << variableName << " =";
  1132. std::string object;
  1133. const char* lineContinue =
  1134. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1135. if(!lineContinue)
  1136. {
  1137. lineContinue = "\\";
  1138. }
  1139. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1140. i != this->Objects.end(); ++i)
  1141. {
  1142. *this->BuildFileStream << " " << lineContinue << "\n";
  1143. *this->BuildFileStream <<
  1144. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1145. useWatcomQuote);
  1146. }
  1147. *this->BuildFileStream << "\n";
  1148. // Write a make variable assignment that lists all external objects
  1149. // for the target.
  1150. variableNameExternal =
  1151. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1152. "_EXTERNAL_OBJECTS");
  1153. *this->BuildFileStream
  1154. << "\n"
  1155. << "# External object files for target "
  1156. << this->Target->GetName() << "\n"
  1157. << variableNameExternal << " =";
  1158. for(std::vector<std::string>::const_iterator i =
  1159. this->ExternalObjects.begin();
  1160. i != this->ExternalObjects.end(); ++i)
  1161. {
  1162. object = this->Convert(*i,cmLocalGenerator::START_OUTPUT);
  1163. *this->BuildFileStream
  1164. << " " << lineContinue << "\n"
  1165. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1166. *this->BuildFileStream <<
  1167. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1168. useWatcomQuote);
  1169. }
  1170. *this->BuildFileStream << "\n" << "\n";
  1171. }
  1172. //----------------------------------------------------------------------------
  1173. void
  1174. cmMakefileTargetGenerator
  1175. ::WriteObjectsString(std::string& buildObjs)
  1176. {
  1177. std::vector<std::string> objStrings;
  1178. this->WriteObjectsStrings(objStrings);
  1179. buildObjs = objStrings[0];
  1180. }
  1181. //----------------------------------------------------------------------------
  1182. class cmMakefileTargetGeneratorObjectStrings
  1183. {
  1184. public:
  1185. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1186. cmLocalUnixMakefileGenerator3* lg,
  1187. std::string::size_type limit):
  1188. Strings(strings), LocalGenerator(lg), LengthLimit(limit)
  1189. {
  1190. this->Space = "";
  1191. }
  1192. void Feed(std::string const& obj)
  1193. {
  1194. // Construct the name of the next object.
  1195. this->NextObject =
  1196. this->LocalGenerator->Convert(obj,
  1197. cmLocalGenerator::START_OUTPUT,
  1198. cmLocalGenerator::RESPONSE);
  1199. // Roll over to next string if the limit will be exceeded.
  1200. if(this->LengthLimit != std::string::npos &&
  1201. (this->CurrentString.length() + 1 + this->NextObject.length()
  1202. > this->LengthLimit))
  1203. {
  1204. this->Strings.push_back(this->CurrentString);
  1205. this->CurrentString = "";
  1206. this->Space = "";
  1207. }
  1208. // Separate from previous object.
  1209. this->CurrentString += this->Space;
  1210. this->Space = " ";
  1211. // Append this object.
  1212. this->CurrentString += this->NextObject;
  1213. }
  1214. void Done()
  1215. {
  1216. this->Strings.push_back(this->CurrentString);
  1217. }
  1218. private:
  1219. std::vector<std::string>& Strings;
  1220. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1221. std::string::size_type LengthLimit;
  1222. std::string CurrentString;
  1223. std::string NextObject;
  1224. const char* Space;
  1225. };
  1226. //----------------------------------------------------------------------------
  1227. void
  1228. cmMakefileTargetGenerator
  1229. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1230. std::string::size_type limit)
  1231. {
  1232. cmMakefileTargetGeneratorObjectStrings
  1233. helper(objStrings, this->LocalGenerator, limit);
  1234. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1235. i != this->Objects.end(); ++i)
  1236. {
  1237. helper.Feed(*i);
  1238. }
  1239. for(std::vector<std::string>::const_iterator i =
  1240. this->ExternalObjects.begin();
  1241. i != this->ExternalObjects.end(); ++i)
  1242. {
  1243. helper.Feed(*i);
  1244. }
  1245. helper.Done();
  1246. }
  1247. //----------------------------------------------------------------------------
  1248. void cmMakefileTargetGenerator::WriteTargetDriverRule(
  1249. const std::string& main_output,
  1250. bool relink)
  1251. {
  1252. // Compute the name of the driver target.
  1253. std::string dir =
  1254. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1255. std::string buildTargetRuleName = dir;
  1256. buildTargetRuleName += relink?"/preinstall":"/build";
  1257. buildTargetRuleName = this->Convert(buildTargetRuleName,
  1258. cmLocalGenerator::HOME_OUTPUT,
  1259. cmLocalGenerator::UNCHANGED);
  1260. // Build the list of target outputs to drive.
  1261. std::vector<std::string> depends;
  1262. depends.push_back(main_output);
  1263. const char* comment = 0;
  1264. if(relink)
  1265. {
  1266. // Setup the comment for the preinstall driver.
  1267. comment = "Rule to relink during preinstall.";
  1268. }
  1269. else
  1270. {
  1271. // Setup the comment for the main build driver.
  1272. comment = "Rule to build all files generated by this target.";
  1273. // Make sure all custom command outputs in this target are built.
  1274. if(this->CustomCommandDriver == OnBuild)
  1275. {
  1276. this->DriveCustomCommands(depends);
  1277. }
  1278. // Make sure the extra files are built.
  1279. depends.insert(depends.end(),
  1280. this->ExtraFiles.begin(), this->ExtraFiles.end());
  1281. }
  1282. // Write the driver rule.
  1283. std::vector<std::string> no_commands;
  1284. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1285. buildTargetRuleName,
  1286. depends, no_commands, true);
  1287. }
  1288. //----------------------------------------------------------------------------
  1289. std::string cmMakefileTargetGenerator::GetFrameworkFlags(std::string const& l)
  1290. {
  1291. if(!this->Makefile->IsOn("APPLE"))
  1292. {
  1293. return std::string();
  1294. }
  1295. std::string fwSearchFlagVar = "CMAKE_" + l + "_FRAMEWORK_SEARCH_FLAG";
  1296. const char* fwSearchFlag =
  1297. this->Makefile->GetDefinition(fwSearchFlagVar);
  1298. if(!(fwSearchFlag && *fwSearchFlag))
  1299. {
  1300. return std::string();
  1301. }
  1302. std::set<std::string> emitted;
  1303. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1304. emitted.insert("/System/Library/Frameworks");
  1305. #endif
  1306. std::vector<std::string> includes;
  1307. const std::string& config =
  1308. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1309. this->LocalGenerator->GetIncludeDirectories(includes,
  1310. this->GeneratorTarget,
  1311. "C", config);
  1312. // check all include directories for frameworks as this
  1313. // will already have added a -F for the framework
  1314. for(std::vector<std::string>::iterator i = includes.begin();
  1315. i != includes.end(); ++i)
  1316. {
  1317. if(this->Target->NameResolvesToFramework(*i))
  1318. {
  1319. std::string frameworkDir = *i;
  1320. frameworkDir += "/../";
  1321. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1322. emitted.insert(frameworkDir);
  1323. }
  1324. }
  1325. std::string flags;
  1326. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1327. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1328. {
  1329. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1330. for(std::vector<std::string>::const_iterator i = frameworks.begin();
  1331. i != frameworks.end(); ++i)
  1332. {
  1333. if(emitted.insert(*i).second)
  1334. {
  1335. flags += fwSearchFlag;
  1336. flags += this->Convert(*i,
  1337. cmLocalGenerator::START_OUTPUT,
  1338. cmLocalGenerator::SHELL, true);
  1339. flags += " ";
  1340. }
  1341. }
  1342. }
  1343. return flags;
  1344. }
  1345. //----------------------------------------------------------------------------
  1346. void cmMakefileTargetGenerator
  1347. ::AppendTargetDepends(std::vector<std::string>& depends)
  1348. {
  1349. // Static libraries never depend on anything for linking.
  1350. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1351. {
  1352. return;
  1353. }
  1354. // Loop over all library dependencies.
  1355. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1356. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1357. {
  1358. std::vector<std::string> const& libDeps = cli->GetDepends();
  1359. depends.insert(depends.end(), libDeps.begin(), libDeps.end());
  1360. }
  1361. }
  1362. //----------------------------------------------------------------------------
  1363. void cmMakefileTargetGenerator
  1364. ::AppendObjectDepends(std::vector<std::string>& depends)
  1365. {
  1366. // Add dependencies on the compiled object files.
  1367. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1368. std::string objTarget;
  1369. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1370. obj != this->Objects.end(); ++obj)
  1371. {
  1372. objTarget = relPath;
  1373. objTarget += *obj;
  1374. depends.push_back(objTarget);
  1375. }
  1376. // Add dependencies on the external object files.
  1377. depends.insert(depends.end(),
  1378. this->ExternalObjects.begin(), this->ExternalObjects.end());
  1379. // Add a dependency on the rule file itself.
  1380. this->LocalGenerator->AppendRuleDepend(depends,
  1381. this->BuildFileNameFull.c_str());
  1382. }
  1383. //----------------------------------------------------------------------------
  1384. void cmMakefileTargetGenerator
  1385. ::AppendLinkDepends(std::vector<std::string>& depends)
  1386. {
  1387. this->AppendObjectDepends(depends);
  1388. // Add dependencies on targets that must be built first.
  1389. this->AppendTargetDepends(depends);
  1390. // Add a dependency on the link definitions file, if any.
  1391. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1392. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1393. if(!def.empty())
  1394. {
  1395. depends.push_back(def);
  1396. }
  1397. // Add user-specified dependencies.
  1398. if(const char* linkDepends =
  1399. this->Target->GetProperty("LINK_DEPENDS"))
  1400. {
  1401. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1402. }
  1403. }
  1404. //----------------------------------------------------------------------------
  1405. std::string cmMakefileTargetGenerator::GetLinkRule(
  1406. const std::string& linkRuleVar)
  1407. {
  1408. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1409. if(this->Target->HasImplibGNUtoMS())
  1410. {
  1411. std::string ruleVar = "CMAKE_";
  1412. ruleVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1413. ruleVar += "_GNUtoMS_RULE";
  1414. if(const char* rule = this->Makefile->GetDefinition(ruleVar))
  1415. {
  1416. linkRule += rule;
  1417. }
  1418. }
  1419. return linkRule;
  1420. }
  1421. //----------------------------------------------------------------------------
  1422. void cmMakefileTargetGenerator
  1423. ::CloseFileStreams()
  1424. {
  1425. delete this->BuildFileStream;
  1426. delete this->InfoFileStream;
  1427. delete this->FlagFileStream;
  1428. }
  1429. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1430. const std::string& linkLang,
  1431. std::string& linkFlags)
  1432. {
  1433. // check for language flags that are not allowed at link time, and
  1434. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1435. // which fails, there may be more]
  1436. std::string removeFlags = "CMAKE_";
  1437. removeFlags += linkLang;
  1438. removeFlags += flagVar;
  1439. std::string removeflags =
  1440. this->Makefile->GetSafeDefinition(removeFlags);
  1441. std::vector<std::string> removeList;
  1442. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1443. for(std::vector<std::string>::iterator i = removeList.begin();
  1444. i != removeList.end(); ++i)
  1445. {
  1446. std::string tmp;
  1447. std::string::size_type lastPosition = 0;
  1448. for(;;)
  1449. {
  1450. std::string::size_type position = linkFlags.find(*i, lastPosition);
  1451. if(position == std::string::npos)
  1452. {
  1453. tmp += linkFlags.substr(lastPosition);
  1454. break;
  1455. }
  1456. else
  1457. {
  1458. std::string::size_type prefixLength = position - lastPosition;
  1459. tmp += linkFlags.substr(lastPosition, prefixLength);
  1460. lastPosition = position + i->length();
  1461. bool validFlagStart = position == 0 ||
  1462. isspace(linkFlags[position - 1]);
  1463. bool validFlagEnd = lastPosition == linkFlags.size() ||
  1464. isspace(linkFlags[lastPosition]);
  1465. if(!validFlagStart || !validFlagEnd)
  1466. {
  1467. tmp += *i;
  1468. }
  1469. }
  1470. }
  1471. linkFlags = tmp;
  1472. }
  1473. }
  1474. //----------------------------------------------------------------------------
  1475. void
  1476. cmMakefileTargetGenerator
  1477. ::CreateLinkScript(const char* name,
  1478. std::vector<std::string> const& link_commands,
  1479. std::vector<std::string>& makefile_commands,
  1480. std::vector<std::string>& makefile_depends)
  1481. {
  1482. // Create the link script file.
  1483. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1484. linkScriptName += "/";
  1485. linkScriptName += name;
  1486. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1487. linkScriptStream.SetCopyIfDifferent(true);
  1488. for(std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1489. cmd != link_commands.end(); ++cmd)
  1490. {
  1491. // Do not write out empty commands or commands beginning in the
  1492. // shell no-op ":".
  1493. if(!cmd->empty() && (*cmd)[0] != ':')
  1494. {
  1495. linkScriptStream << *cmd << "\n";
  1496. }
  1497. }
  1498. // Create the makefile command to invoke the link script.
  1499. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1500. link_command += this->Convert(linkScriptName,
  1501. cmLocalGenerator::START_OUTPUT,
  1502. cmLocalGenerator::SHELL);
  1503. link_command += " --verbose=$(VERBOSE)";
  1504. makefile_commands.push_back(link_command);
  1505. makefile_depends.push_back(linkScriptName);
  1506. }
  1507. //----------------------------------------------------------------------------
  1508. std::string
  1509. cmMakefileTargetGenerator
  1510. ::CreateResponseFile(const char* name, std::string const& options,
  1511. std::vector<std::string>& makefile_depends)
  1512. {
  1513. // Create the response file.
  1514. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1515. responseFileNameFull += "/";
  1516. responseFileNameFull += name;
  1517. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1518. responseStream.SetCopyIfDifferent(true);
  1519. responseStream << options << "\n";
  1520. // Add a dependency so the target will rebuild when the set of
  1521. // objects changes.
  1522. makefile_depends.push_back(responseFileNameFull);
  1523. // Construct the name to be used on the command line.
  1524. std::string responseFileName = this->TargetBuildDirectory;
  1525. responseFileName += "/";
  1526. responseFileName += name;
  1527. return responseFileName;
  1528. }
  1529. //----------------------------------------------------------------------------
  1530. void
  1531. cmMakefileTargetGenerator
  1532. ::CreateLinkLibs(std::string& linkLibs, bool relink,
  1533. bool useResponseFile,
  1534. std::vector<std::string>& makefile_depends,
  1535. bool useWatcomQuote)
  1536. {
  1537. std::string frameworkPath;
  1538. std::string linkPath;
  1539. this->LocalGenerator
  1540. ->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1541. *this->GeneratorTarget, relink,
  1542. useResponseFile,
  1543. useWatcomQuote);
  1544. linkLibs = frameworkPath + linkPath + linkLibs;
  1545. if(useResponseFile && linkLibs.find_first_not_of(" ") != linkLibs.npos)
  1546. {
  1547. // Lookup the response file reference flag.
  1548. std::string responseFlagVar = "CMAKE_";
  1549. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1550. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1551. const char* responseFlag =
  1552. this->Makefile->GetDefinition(responseFlagVar);
  1553. if(!responseFlag)
  1554. {
  1555. responseFlag = "@";
  1556. }
  1557. // Create this response file.
  1558. std::string link_rsp =
  1559. this->CreateResponseFile("linklibs.rsp", linkLibs, makefile_depends);
  1560. // Reference the response file.
  1561. linkLibs = responseFlag;
  1562. linkLibs += this->Convert(link_rsp,
  1563. cmLocalGenerator::NONE,
  1564. cmLocalGenerator::SHELL);
  1565. }
  1566. }
  1567. //----------------------------------------------------------------------------
  1568. void
  1569. cmMakefileTargetGenerator
  1570. ::CreateObjectLists(bool useLinkScript, bool useArchiveRules,
  1571. bool useResponseFile, std::string& buildObjs,
  1572. std::vector<std::string>& makefile_depends,
  1573. bool useWatcomQuote)
  1574. {
  1575. std::string variableName;
  1576. std::string variableNameExternal;
  1577. this->WriteObjectsVariable(variableName, variableNameExternal,
  1578. useWatcomQuote);
  1579. if(useResponseFile)
  1580. {
  1581. // MSVC response files cannot exceed 128K.
  1582. std::string::size_type const responseFileLimit = 131000;
  1583. // Construct the individual object list strings.
  1584. std::vector<std::string> object_strings;
  1585. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1586. // Lookup the response file reference flag.
  1587. std::string responseFlagVar = "CMAKE_";
  1588. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1589. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1590. const char* responseFlag =
  1591. this->Makefile->GetDefinition(responseFlagVar);
  1592. if(!responseFlag)
  1593. {
  1594. responseFlag = "@";
  1595. }
  1596. // Write a response file for each string.
  1597. const char* sep = "";
  1598. for(unsigned int i = 0; i < object_strings.size(); ++i)
  1599. {
  1600. // Number the response files.
  1601. char rsp[32];
  1602. sprintf(rsp, "objects%u.rsp", i+1);
  1603. // Create this response file.
  1604. std::string objects_rsp =
  1605. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1606. // Separate from previous response file references.
  1607. buildObjs += sep;
  1608. sep = " ";
  1609. // Reference the response file.
  1610. buildObjs += responseFlag;
  1611. buildObjs += this->Convert(objects_rsp,
  1612. cmLocalGenerator::NONE,
  1613. cmLocalGenerator::SHELL);
  1614. }
  1615. }
  1616. else if(useLinkScript)
  1617. {
  1618. if(!useArchiveRules)
  1619. {
  1620. this->WriteObjectsString(buildObjs);
  1621. }
  1622. }
  1623. else
  1624. {
  1625. buildObjs = "$(";
  1626. buildObjs += variableName;
  1627. buildObjs += ") $(";
  1628. buildObjs += variableNameExternal;
  1629. buildObjs += ")";
  1630. }
  1631. }
  1632. //----------------------------------------------------------------------------
  1633. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1634. const std::string& lang)
  1635. {
  1636. std::string responseVar = "CMAKE_";
  1637. responseVar += lang;
  1638. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1639. bool useResponseFile = this->Makefile->IsOn(responseVar);
  1640. std::vector<std::string> includes;
  1641. const std::string& config =
  1642. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1643. this->LocalGenerator->GetIncludeDirectories(includes,
  1644. this->GeneratorTarget,
  1645. lang, config);
  1646. std::string includeFlags =
  1647. this->LocalGenerator->GetIncludeFlags(includes, this->GeneratorTarget,
  1648. lang, false, useResponseFile,
  1649. config);
  1650. if(includeFlags.empty())
  1651. {
  1652. return;
  1653. }
  1654. if(useResponseFile)
  1655. {
  1656. std::string name = "includes_";
  1657. name += lang;
  1658. name += ".rsp";
  1659. std::string arg = "@" +
  1660. this->CreateResponseFile(name.c_str(), includeFlags,
  1661. this->FlagFileDepends[lang]);
  1662. this->LocalGenerator->AppendFlags(flags, arg);
  1663. }
  1664. else
  1665. {
  1666. this->LocalGenerator->AppendFlags(flags, includeFlags);
  1667. }
  1668. }
  1669. //----------------------------------------------------------------------------
  1670. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1671. {
  1672. // Compute the module directory.
  1673. if(!this->FortranModuleDirectoryComputed)
  1674. {
  1675. const char* target_mod_dir =
  1676. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1677. const char* moddir_flag =
  1678. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1679. if(target_mod_dir && moddir_flag)
  1680. {
  1681. // Compute the full path to the module directory.
  1682. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1683. {
  1684. // Already a full path.
  1685. this->FortranModuleDirectory = target_mod_dir;
  1686. }
  1687. else
  1688. {
  1689. // Interpret relative to the current output directory.
  1690. this->FortranModuleDirectory =
  1691. this->Makefile->GetCurrentOutputDirectory();
  1692. this->FortranModuleDirectory += "/";
  1693. this->FortranModuleDirectory += target_mod_dir;
  1694. }
  1695. // Make sure the module output directory exists.
  1696. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1697. }
  1698. this->FortranModuleDirectoryComputed = true;
  1699. }
  1700. // Return the computed directory.
  1701. if(this->FortranModuleDirectory.empty())
  1702. {
  1703. return 0;
  1704. }
  1705. else
  1706. {
  1707. return this->FortranModuleDirectory.c_str();
  1708. }
  1709. }
  1710. //----------------------------------------------------------------------------
  1711. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1712. {
  1713. // Enable module output if necessary.
  1714. if(const char* modout_flag =
  1715. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  1716. {
  1717. this->LocalGenerator->AppendFlags(flags, modout_flag);
  1718. }
  1719. // Add a module output directory flag if necessary.
  1720. const char* mod_dir = this->GetFortranModuleDirectory();
  1721. if(!mod_dir)
  1722. {
  1723. mod_dir = this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  1724. }
  1725. if(mod_dir)
  1726. {
  1727. const char* moddir_flag =
  1728. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1729. std::string modflag = moddir_flag;
  1730. modflag += this->Convert(mod_dir,
  1731. cmLocalGenerator::START_OUTPUT,
  1732. cmLocalGenerator::SHELL);
  1733. this->LocalGenerator->AppendFlags(flags, modflag);
  1734. }
  1735. // If there is a separate module path flag then duplicate the
  1736. // include path with it. This compiler does not search the include
  1737. // path for modules.
  1738. if(const char* modpath_flag =
  1739. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1740. {
  1741. std::vector<std::string> includes;
  1742. const std::string& config =
  1743. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1744. this->LocalGenerator->GetIncludeDirectories(includes,
  1745. this->GeneratorTarget,
  1746. "C", config);
  1747. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1748. idi != includes.end(); ++idi)
  1749. {
  1750. std::string flg = modpath_flag;
  1751. flg += this->Convert(*idi,
  1752. cmLocalGenerator::NONE,
  1753. cmLocalGenerator::SHELL);
  1754. this->LocalGenerator->AppendFlags(flags, flg);
  1755. }
  1756. }
  1757. }
  1758. //----------------------------------------------------------------------------
  1759. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1760. {
  1761. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1762. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1763. if(def.empty())
  1764. {
  1765. return;
  1766. }
  1767. // TODO: Create a per-language flag variable.
  1768. const char* defFileFlag =
  1769. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1770. if(!defFileFlag)
  1771. {
  1772. return;
  1773. }
  1774. // Append the flag and value. Use ConvertToLinkReference to help
  1775. // vs6's "cl -link" pass it to the linker.
  1776. std::string flag = defFileFlag;
  1777. flag += (this->LocalGenerator->ConvertToLinkReference(def));
  1778. this->LocalGenerator->AppendFlags(flags, flag);
  1779. }
  1780. //----------------------------------------------------------------------------
  1781. const char* cmMakefileTargetGenerator::GetFeature(const std::string& feature)
  1782. {
  1783. return this->Target->GetFeature(feature, this->ConfigName);
  1784. }
  1785. //----------------------------------------------------------------------------
  1786. bool cmMakefileTargetGenerator::GetFeatureAsBool(const std::string& feature)
  1787. {
  1788. return this->Target->GetFeatureAsBool(feature, this->ConfigName);
  1789. }
  1790. //----------------------------------------------------------------------------
  1791. void cmMakefileTargetGenerator::AddFeatureFlags(
  1792. std::string& flags, const std::string& lang
  1793. )
  1794. {
  1795. // Add language-specific flags.
  1796. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1797. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1798. {
  1799. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1800. }
  1801. }