cmMakefileTargetGenerator.cxx 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  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->WriteMakeRule(*this->BuildFileStream, 0, outputs,
  689. 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::WriteMakeRule(
  884. std::ostream& os,
  885. const char* comment,
  886. const std::vector<std::string>& outputs,
  887. const std::vector<std::string>& depends,
  888. const std::vector<std::string>& commands,
  889. bool symbolic,
  890. bool in_help)
  891. {
  892. if (outputs.size() == 0)
  893. {
  894. return;
  895. }
  896. // We always attach the actual commands to the first output.
  897. this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
  898. commands, symbolic, in_help);
  899. // For single outputs, we are done.
  900. if (outputs.size() == 1)
  901. {
  902. return;
  903. }
  904. // For multiple outputs, make the extra ones depend on the first one.
  905. std::vector<std::string> const output_depends(1, outputs[0]);
  906. for (std::vector<std::string>::const_iterator o = outputs.begin()+1;
  907. o != outputs.end(); ++o)
  908. {
  909. // Touch the extra output so "make" knows that it was updated,
  910. // but only if the output was acually created.
  911. std::string const out = this->Convert(*o, cmLocalGenerator::HOME_OUTPUT,
  912. cmLocalGenerator::SHELL);
  913. std::vector<std::string> output_commands;
  914. if (!symbolic)
  915. {
  916. output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
  917. }
  918. this->LocalGenerator->WriteMakeRule(os, 0, *o, output_depends,
  919. output_commands, symbolic, in_help);
  920. if (!symbolic)
  921. {
  922. // At build time, remove the first output if this one does not exist
  923. // so that "make" will rerun the real commands that create this one.
  924. MultipleOutputPairsType::value_type p(*o, outputs[0]);
  925. this->MultipleOutputPairs.insert(p);
  926. }
  927. }
  928. }
  929. //----------------------------------------------------------------------------
  930. void cmMakefileTargetGenerator::WriteTargetDependRules()
  931. {
  932. // must write the targets depend info file
  933. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  934. this->InfoFileNameFull = dir;
  935. this->InfoFileNameFull += "/DependInfo.cmake";
  936. this->InfoFileNameFull =
  937. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  938. this->InfoFileStream =
  939. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  940. this->InfoFileStream->SetCopyIfDifferent(true);
  941. if(!*this->InfoFileStream)
  942. {
  943. return;
  944. }
  945. this->LocalGenerator->
  946. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  947. // Store multiple output pairs in the depend info file.
  948. if(!this->MultipleOutputPairs.empty())
  949. {
  950. *this->InfoFileStream
  951. << "\n"
  952. << "# Pairs of files generated by the same build rule.\n"
  953. << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  954. for(MultipleOutputPairsType::const_iterator pi =
  955. this->MultipleOutputPairs.begin();
  956. pi != this->MultipleOutputPairs.end(); ++pi)
  957. {
  958. *this->InfoFileStream
  959. << " " << this->LocalGenerator->EscapeForCMake(pi->first)
  960. << " " << this->LocalGenerator->EscapeForCMake(pi->second)
  961. << "\n";
  962. }
  963. *this->InfoFileStream << " )\n\n";
  964. }
  965. // Store list of targets linked directly or transitively.
  966. {
  967. *this->InfoFileStream
  968. << "\n"
  969. << "# Targets to which this target links.\n"
  970. << "set(CMAKE_TARGET_LINKED_INFO_FILES\n";
  971. std::set<cmTarget const*> emitted;
  972. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  973. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  974. {
  975. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  976. for(cmComputeLinkInformation::ItemVector::const_iterator
  977. i = items.begin(); i != items.end(); ++i)
  978. {
  979. cmTarget const* linkee = i->Target;
  980. if(linkee && !linkee->IsImported()
  981. // We can ignore the INTERFACE_LIBRARY items because
  982. // Target->GetLinkInformation already processed their
  983. // link interface and they don't have any output themselves.
  984. && linkee->GetType() != cmTarget::INTERFACE_LIBRARY
  985. && emitted.insert(linkee).second)
  986. {
  987. cmMakefile* mf = linkee->GetMakefile();
  988. cmLocalGenerator* lg = mf->GetLocalGenerator();
  989. std::string di = mf->GetStartOutputDirectory();
  990. di += "/";
  991. di += lg->GetTargetDirectory(*linkee);
  992. di += "/DependInfo.cmake";
  993. *this->InfoFileStream << " \"" << di << "\"\n";
  994. }
  995. }
  996. }
  997. *this->InfoFileStream
  998. << " )\n";
  999. }
  1000. // Check for a target-specific module output directory.
  1001. if(const char* mdir = this->GetFortranModuleDirectory())
  1002. {
  1003. *this->InfoFileStream
  1004. << "\n"
  1005. << "# Fortran module output directory.\n"
  1006. << "set(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  1007. }
  1008. // and now write the rule to use it
  1009. std::vector<std::string> depends;
  1010. std::vector<std::string> commands;
  1011. // Construct the name of the dependency generation target.
  1012. std::string depTarget =
  1013. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1014. depTarget += "/depend";
  1015. // Add a command to call CMake to scan dependencies. CMake will
  1016. // touch the corresponding depends file after scanning dependencies.
  1017. std::ostringstream depCmd;
  1018. // TODO: Account for source file properties and directory-level
  1019. // definitions when scanning for dependencies.
  1020. #if !defined(_WIN32) || defined(__CYGWIN__)
  1021. // This platform supports symlinks, so cmSystemTools will translate
  1022. // paths. Make sure PWD is set to the original name of the home
  1023. // output directory to help cmSystemTools to create the same
  1024. // translation table for the dependency scanning process.
  1025. depCmd << "cd "
  1026. << (this->LocalGenerator->Convert(
  1027. this->Makefile->GetHomeOutputDirectory(),
  1028. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  1029. << " && ";
  1030. #endif
  1031. // Generate a call this signature:
  1032. //
  1033. // cmake -E cmake_depends <generator>
  1034. // <home-src-dir> <start-src-dir>
  1035. // <home-out-dir> <start-out-dir>
  1036. // <dep-info> --color=$(COLOR)
  1037. //
  1038. // This gives the dependency scanner enough information to recreate
  1039. // the state of our local generator sufficiently for its needs.
  1040. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  1041. << this->GlobalGenerator->GetName() << "\" "
  1042. << this->Convert(this->Makefile->GetHomeDirectory(),
  1043. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1044. << " "
  1045. << this->Convert(this->Makefile->GetStartDirectory(),
  1046. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1047. << " "
  1048. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  1049. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1050. << " "
  1051. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  1052. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1053. << " "
  1054. << this->Convert(this->InfoFileNameFull,
  1055. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  1056. if(this->LocalGenerator->GetColorMakefile())
  1057. {
  1058. depCmd << " --color=$(COLOR)";
  1059. }
  1060. commands.push_back(depCmd.str());
  1061. // Make sure all custom command outputs in this target are built.
  1062. if(this->CustomCommandDriver == OnDepends)
  1063. {
  1064. this->DriveCustomCommands(depends);
  1065. }
  1066. // Write the rule.
  1067. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1068. depTarget,
  1069. depends, commands, true);
  1070. }
  1071. //----------------------------------------------------------------------------
  1072. void
  1073. cmMakefileTargetGenerator
  1074. ::DriveCustomCommands(std::vector<std::string>& depends)
  1075. {
  1076. // Depend on all custom command outputs.
  1077. std::vector<cmSourceFile*> sources;
  1078. this->Target->GetSourceFiles(sources,
  1079. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1080. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1081. source != sources.end(); ++source)
  1082. {
  1083. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  1084. {
  1085. cmCustomCommandGenerator ccg(*cc, this->ConfigName, this->Makefile);
  1086. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1087. depends.insert(depends.end(), outputs.begin(), outputs.end());
  1088. }
  1089. }
  1090. }
  1091. //----------------------------------------------------------------------------
  1092. void cmMakefileTargetGenerator
  1093. ::WriteObjectDependRules(cmSourceFile const& source,
  1094. std::vector<std::string>& depends)
  1095. {
  1096. // Create the list of dependencies known at cmake time. These are
  1097. // shared between the object file and dependency scanning rule.
  1098. depends.push_back(source.GetFullPath());
  1099. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  1100. {
  1101. cmSystemTools::ExpandListArgument(objectDeps, depends);
  1102. }
  1103. }
  1104. //----------------------------------------------------------------------------
  1105. void cmMakefileTargetGenerator
  1106. ::GenerateCustomRuleFile(cmCustomCommandGenerator const& ccg)
  1107. {
  1108. // Collect the commands.
  1109. std::vector<std::string> commands;
  1110. std::string comment = this->LocalGenerator->ConstructComment(ccg);
  1111. if(!comment.empty())
  1112. {
  1113. // add in a progress call if needed
  1114. this->NumberOfProgressActions++;
  1115. if(!this->NoRuleMessages)
  1116. {
  1117. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  1118. this->MakeEchoProgress(progress);
  1119. this->LocalGenerator
  1120. ->AppendEcho(commands, comment.c_str(),
  1121. cmLocalUnixMakefileGenerator3::EchoGenerate,
  1122. &progress);
  1123. }
  1124. }
  1125. // Now append the actual user-specified commands.
  1126. std::ostringstream content;
  1127. this->LocalGenerator->AppendCustomCommand(commands, ccg, this->Target, false,
  1128. cmLocalGenerator::HOME_OUTPUT,
  1129. &content);
  1130. // Collect the dependencies.
  1131. std::vector<std::string> depends;
  1132. this->LocalGenerator->AppendCustomDepend(depends, ccg);
  1133. // Check whether we need to bother checking for a symbolic output.
  1134. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  1135. // Write the rule.
  1136. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1137. std::vector<std::string>::const_iterator o = outputs.begin();
  1138. {
  1139. bool symbolic = false;
  1140. if(need_symbolic)
  1141. {
  1142. if(cmSourceFile* sf = this->Makefile->GetSource(*o))
  1143. {
  1144. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1145. }
  1146. }
  1147. this->WriteMakeRule(*this->BuildFileStream, 0, outputs,
  1148. depends, commands, symbolic);
  1149. // If the rule has changed make sure the output is rebuilt.
  1150. if(!symbolic)
  1151. {
  1152. this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
  1153. }
  1154. }
  1155. // Setup implicit dependency scanning.
  1156. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1157. idi = ccg.GetCC().GetImplicitDepends().begin();
  1158. idi != ccg.GetCC().GetImplicitDepends().end(); ++idi)
  1159. {
  1160. std::string objFullPath =
  1161. this->Convert(outputs[0], cmLocalGenerator::FULL);
  1162. std::string srcFullPath =
  1163. this->Convert(idi->second, cmLocalGenerator::FULL);
  1164. this->LocalGenerator->
  1165. AddImplicitDepends(*this->Target, idi->first,
  1166. objFullPath.c_str(),
  1167. srcFullPath.c_str());
  1168. }
  1169. }
  1170. //----------------------------------------------------------------------------
  1171. void
  1172. cmMakefileTargetGenerator
  1173. ::MakeEchoProgress(cmLocalUnixMakefileGenerator3::EchoProgress& progress) const
  1174. {
  1175. progress.Dir = this->Makefile->GetHomeOutputDirectory();
  1176. progress.Dir += cmake::GetCMakeFilesDirectory();
  1177. std::ostringstream progressArg;
  1178. progressArg << "$(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1179. progress.Arg = progressArg.str();
  1180. }
  1181. //----------------------------------------------------------------------------
  1182. void
  1183. cmMakefileTargetGenerator
  1184. ::WriteObjectsVariable(std::string& variableName,
  1185. std::string& variableNameExternal,
  1186. bool useWatcomQuote)
  1187. {
  1188. // Write a make variable assignment that lists all objects for the
  1189. // target.
  1190. variableName =
  1191. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1192. "_OBJECTS");
  1193. *this->BuildFileStream
  1194. << "# Object files for target " << this->Target->GetName() << "\n"
  1195. << variableName << " =";
  1196. std::string object;
  1197. const char* lineContinue =
  1198. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1199. if(!lineContinue)
  1200. {
  1201. lineContinue = "\\";
  1202. }
  1203. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1204. i != this->Objects.end(); ++i)
  1205. {
  1206. *this->BuildFileStream << " " << lineContinue << "\n";
  1207. *this->BuildFileStream <<
  1208. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1209. useWatcomQuote);
  1210. }
  1211. *this->BuildFileStream << "\n";
  1212. // Write a make variable assignment that lists all external objects
  1213. // for the target.
  1214. variableNameExternal =
  1215. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1216. "_EXTERNAL_OBJECTS");
  1217. *this->BuildFileStream
  1218. << "\n"
  1219. << "# External object files for target "
  1220. << this->Target->GetName() << "\n"
  1221. << variableNameExternal << " =";
  1222. for(std::vector<std::string>::const_iterator i =
  1223. this->ExternalObjects.begin();
  1224. i != this->ExternalObjects.end(); ++i)
  1225. {
  1226. object = this->Convert(*i,cmLocalGenerator::START_OUTPUT);
  1227. *this->BuildFileStream
  1228. << " " << lineContinue << "\n"
  1229. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1230. *this->BuildFileStream <<
  1231. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1232. useWatcomQuote);
  1233. }
  1234. *this->BuildFileStream << "\n" << "\n";
  1235. }
  1236. //----------------------------------------------------------------------------
  1237. void
  1238. cmMakefileTargetGenerator
  1239. ::WriteObjectsString(std::string& buildObjs)
  1240. {
  1241. std::vector<std::string> objStrings;
  1242. this->WriteObjectsStrings(objStrings);
  1243. buildObjs = objStrings[0];
  1244. }
  1245. //----------------------------------------------------------------------------
  1246. class cmMakefileTargetGeneratorObjectStrings
  1247. {
  1248. public:
  1249. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1250. cmLocalUnixMakefileGenerator3* lg,
  1251. std::string::size_type limit):
  1252. Strings(strings), LocalGenerator(lg), LengthLimit(limit)
  1253. {
  1254. this->Space = "";
  1255. }
  1256. void Feed(std::string const& obj)
  1257. {
  1258. // Construct the name of the next object.
  1259. this->NextObject =
  1260. this->LocalGenerator->Convert(obj,
  1261. cmLocalGenerator::START_OUTPUT,
  1262. cmLocalGenerator::RESPONSE);
  1263. // Roll over to next string if the limit will be exceeded.
  1264. if(this->LengthLimit != std::string::npos &&
  1265. (this->CurrentString.length() + 1 + this->NextObject.length()
  1266. > this->LengthLimit))
  1267. {
  1268. this->Strings.push_back(this->CurrentString);
  1269. this->CurrentString = "";
  1270. this->Space = "";
  1271. }
  1272. // Separate from previous object.
  1273. this->CurrentString += this->Space;
  1274. this->Space = " ";
  1275. // Append this object.
  1276. this->CurrentString += this->NextObject;
  1277. }
  1278. void Done()
  1279. {
  1280. this->Strings.push_back(this->CurrentString);
  1281. }
  1282. private:
  1283. std::vector<std::string>& Strings;
  1284. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1285. std::string::size_type LengthLimit;
  1286. std::string CurrentString;
  1287. std::string NextObject;
  1288. const char* Space;
  1289. };
  1290. //----------------------------------------------------------------------------
  1291. void
  1292. cmMakefileTargetGenerator
  1293. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1294. std::string::size_type limit)
  1295. {
  1296. cmMakefileTargetGeneratorObjectStrings
  1297. helper(objStrings, this->LocalGenerator, limit);
  1298. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1299. i != this->Objects.end(); ++i)
  1300. {
  1301. helper.Feed(*i);
  1302. }
  1303. for(std::vector<std::string>::const_iterator i =
  1304. this->ExternalObjects.begin();
  1305. i != this->ExternalObjects.end(); ++i)
  1306. {
  1307. helper.Feed(*i);
  1308. }
  1309. helper.Done();
  1310. }
  1311. //----------------------------------------------------------------------------
  1312. void cmMakefileTargetGenerator::WriteTargetDriverRule(
  1313. const std::string& main_output,
  1314. bool relink)
  1315. {
  1316. // Compute the name of the driver target.
  1317. std::string dir =
  1318. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1319. std::string buildTargetRuleName = dir;
  1320. buildTargetRuleName += relink?"/preinstall":"/build";
  1321. buildTargetRuleName = this->Convert(buildTargetRuleName,
  1322. cmLocalGenerator::HOME_OUTPUT,
  1323. cmLocalGenerator::UNCHANGED);
  1324. // Build the list of target outputs to drive.
  1325. std::vector<std::string> depends;
  1326. depends.push_back(main_output);
  1327. const char* comment = 0;
  1328. if(relink)
  1329. {
  1330. // Setup the comment for the preinstall driver.
  1331. comment = "Rule to relink during preinstall.";
  1332. }
  1333. else
  1334. {
  1335. // Setup the comment for the main build driver.
  1336. comment = "Rule to build all files generated by this target.";
  1337. // Make sure all custom command outputs in this target are built.
  1338. if(this->CustomCommandDriver == OnBuild)
  1339. {
  1340. this->DriveCustomCommands(depends);
  1341. }
  1342. // Make sure the extra files are built.
  1343. depends.insert(depends.end(),
  1344. this->ExtraFiles.begin(), this->ExtraFiles.end());
  1345. }
  1346. // Write the driver rule.
  1347. std::vector<std::string> no_commands;
  1348. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1349. buildTargetRuleName,
  1350. depends, no_commands, true);
  1351. }
  1352. //----------------------------------------------------------------------------
  1353. std::string cmMakefileTargetGenerator::GetFrameworkFlags(std::string const& l)
  1354. {
  1355. if(!this->Makefile->IsOn("APPLE"))
  1356. {
  1357. return std::string();
  1358. }
  1359. std::string fwSearchFlagVar = "CMAKE_" + l + "_FRAMEWORK_SEARCH_FLAG";
  1360. const char* fwSearchFlag =
  1361. this->Makefile->GetDefinition(fwSearchFlagVar);
  1362. if(!(fwSearchFlag && *fwSearchFlag))
  1363. {
  1364. return std::string();
  1365. }
  1366. std::set<std::string> emitted;
  1367. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1368. emitted.insert("/System/Library/Frameworks");
  1369. #endif
  1370. std::vector<std::string> includes;
  1371. const std::string& config =
  1372. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1373. this->LocalGenerator->GetIncludeDirectories(includes,
  1374. this->GeneratorTarget,
  1375. "C", config);
  1376. // check all include directories for frameworks as this
  1377. // will already have added a -F for the framework
  1378. for(std::vector<std::string>::iterator i = includes.begin();
  1379. i != includes.end(); ++i)
  1380. {
  1381. if(this->Target->NameResolvesToFramework(*i))
  1382. {
  1383. std::string frameworkDir = *i;
  1384. frameworkDir += "/../";
  1385. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1386. emitted.insert(frameworkDir);
  1387. }
  1388. }
  1389. std::string flags;
  1390. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1391. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1392. {
  1393. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1394. for(std::vector<std::string>::const_iterator i = frameworks.begin();
  1395. i != frameworks.end(); ++i)
  1396. {
  1397. if(emitted.insert(*i).second)
  1398. {
  1399. flags += fwSearchFlag;
  1400. flags += this->Convert(*i,
  1401. cmLocalGenerator::START_OUTPUT,
  1402. cmLocalGenerator::SHELL, true);
  1403. flags += " ";
  1404. }
  1405. }
  1406. }
  1407. return flags;
  1408. }
  1409. //----------------------------------------------------------------------------
  1410. void cmMakefileTargetGenerator
  1411. ::AppendTargetDepends(std::vector<std::string>& depends)
  1412. {
  1413. // Static libraries never depend on anything for linking.
  1414. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1415. {
  1416. return;
  1417. }
  1418. // Loop over all library dependencies.
  1419. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1420. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1421. {
  1422. std::vector<std::string> const& libDeps = cli->GetDepends();
  1423. depends.insert(depends.end(), libDeps.begin(), libDeps.end());
  1424. }
  1425. }
  1426. //----------------------------------------------------------------------------
  1427. void cmMakefileTargetGenerator
  1428. ::AppendObjectDepends(std::vector<std::string>& depends)
  1429. {
  1430. // Add dependencies on the compiled object files.
  1431. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1432. std::string objTarget;
  1433. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1434. obj != this->Objects.end(); ++obj)
  1435. {
  1436. objTarget = relPath;
  1437. objTarget += *obj;
  1438. depends.push_back(objTarget);
  1439. }
  1440. // Add dependencies on the external object files.
  1441. depends.insert(depends.end(),
  1442. this->ExternalObjects.begin(), this->ExternalObjects.end());
  1443. // Add a dependency on the rule file itself.
  1444. this->LocalGenerator->AppendRuleDepend(depends,
  1445. this->BuildFileNameFull.c_str());
  1446. }
  1447. //----------------------------------------------------------------------------
  1448. void cmMakefileTargetGenerator
  1449. ::AppendLinkDepends(std::vector<std::string>& depends)
  1450. {
  1451. this->AppendObjectDepends(depends);
  1452. // Add dependencies on targets that must be built first.
  1453. this->AppendTargetDepends(depends);
  1454. // Add a dependency on the link definitions file, if any.
  1455. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1456. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1457. if(!def.empty())
  1458. {
  1459. depends.push_back(def);
  1460. }
  1461. // Add user-specified dependencies.
  1462. if(const char* linkDepends =
  1463. this->Target->GetProperty("LINK_DEPENDS"))
  1464. {
  1465. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1466. }
  1467. }
  1468. //----------------------------------------------------------------------------
  1469. std::string cmMakefileTargetGenerator::GetLinkRule(
  1470. const std::string& linkRuleVar)
  1471. {
  1472. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1473. if(this->Target->HasImplibGNUtoMS())
  1474. {
  1475. std::string ruleVar = "CMAKE_";
  1476. ruleVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1477. ruleVar += "_GNUtoMS_RULE";
  1478. if(const char* rule = this->Makefile->GetDefinition(ruleVar))
  1479. {
  1480. linkRule += rule;
  1481. }
  1482. }
  1483. return linkRule;
  1484. }
  1485. //----------------------------------------------------------------------------
  1486. void cmMakefileTargetGenerator
  1487. ::CloseFileStreams()
  1488. {
  1489. delete this->BuildFileStream;
  1490. delete this->InfoFileStream;
  1491. delete this->FlagFileStream;
  1492. }
  1493. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1494. const std::string& linkLang,
  1495. std::string& linkFlags)
  1496. {
  1497. // check for language flags that are not allowed at link time, and
  1498. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1499. // which fails, there may be more]
  1500. std::string removeFlags = "CMAKE_";
  1501. removeFlags += linkLang;
  1502. removeFlags += flagVar;
  1503. std::string removeflags =
  1504. this->Makefile->GetSafeDefinition(removeFlags);
  1505. std::vector<std::string> removeList;
  1506. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1507. for(std::vector<std::string>::iterator i = removeList.begin();
  1508. i != removeList.end(); ++i)
  1509. {
  1510. std::string tmp;
  1511. std::string::size_type lastPosition = 0;
  1512. for(;;)
  1513. {
  1514. std::string::size_type position = linkFlags.find(*i, lastPosition);
  1515. if(position == std::string::npos)
  1516. {
  1517. tmp += linkFlags.substr(lastPosition);
  1518. break;
  1519. }
  1520. else
  1521. {
  1522. std::string::size_type prefixLength = position - lastPosition;
  1523. tmp += linkFlags.substr(lastPosition, prefixLength);
  1524. lastPosition = position + i->length();
  1525. bool validFlagStart = position == 0 ||
  1526. isspace(linkFlags[position - 1]);
  1527. bool validFlagEnd = lastPosition == linkFlags.size() ||
  1528. isspace(linkFlags[lastPosition]);
  1529. if(!validFlagStart || !validFlagEnd)
  1530. {
  1531. tmp += *i;
  1532. }
  1533. }
  1534. }
  1535. linkFlags = tmp;
  1536. }
  1537. }
  1538. //----------------------------------------------------------------------------
  1539. void
  1540. cmMakefileTargetGenerator
  1541. ::CreateLinkScript(const char* name,
  1542. std::vector<std::string> const& link_commands,
  1543. std::vector<std::string>& makefile_commands,
  1544. std::vector<std::string>& makefile_depends)
  1545. {
  1546. // Create the link script file.
  1547. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1548. linkScriptName += "/";
  1549. linkScriptName += name;
  1550. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1551. linkScriptStream.SetCopyIfDifferent(true);
  1552. for(std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1553. cmd != link_commands.end(); ++cmd)
  1554. {
  1555. // Do not write out empty commands or commands beginning in the
  1556. // shell no-op ":".
  1557. if(!cmd->empty() && (*cmd)[0] != ':')
  1558. {
  1559. linkScriptStream << *cmd << "\n";
  1560. }
  1561. }
  1562. // Create the makefile command to invoke the link script.
  1563. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1564. link_command += this->Convert(linkScriptName,
  1565. cmLocalGenerator::START_OUTPUT,
  1566. cmLocalGenerator::SHELL);
  1567. link_command += " --verbose=$(VERBOSE)";
  1568. makefile_commands.push_back(link_command);
  1569. makefile_depends.push_back(linkScriptName);
  1570. }
  1571. //----------------------------------------------------------------------------
  1572. std::string
  1573. cmMakefileTargetGenerator
  1574. ::CreateResponseFile(const char* name, std::string const& options,
  1575. std::vector<std::string>& makefile_depends)
  1576. {
  1577. // Create the response file.
  1578. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1579. responseFileNameFull += "/";
  1580. responseFileNameFull += name;
  1581. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1582. responseStream.SetCopyIfDifferent(true);
  1583. responseStream << options << "\n";
  1584. // Add a dependency so the target will rebuild when the set of
  1585. // objects changes.
  1586. makefile_depends.push_back(responseFileNameFull);
  1587. // Construct the name to be used on the command line.
  1588. std::string responseFileName = this->TargetBuildDirectory;
  1589. responseFileName += "/";
  1590. responseFileName += name;
  1591. return responseFileName;
  1592. }
  1593. //----------------------------------------------------------------------------
  1594. void
  1595. cmMakefileTargetGenerator
  1596. ::CreateLinkLibs(std::string& linkLibs, bool relink,
  1597. bool useResponseFile,
  1598. std::vector<std::string>& makefile_depends,
  1599. bool useWatcomQuote)
  1600. {
  1601. std::string frameworkPath;
  1602. std::string linkPath;
  1603. this->LocalGenerator
  1604. ->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1605. *this->GeneratorTarget, relink,
  1606. useResponseFile,
  1607. useWatcomQuote);
  1608. linkLibs = frameworkPath + linkPath + linkLibs;
  1609. if(useResponseFile && linkLibs.find_first_not_of(" ") != linkLibs.npos)
  1610. {
  1611. // Lookup the response file reference flag.
  1612. std::string responseFlagVar = "CMAKE_";
  1613. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1614. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1615. const char* responseFlag =
  1616. this->Makefile->GetDefinition(responseFlagVar);
  1617. if(!responseFlag)
  1618. {
  1619. responseFlag = "@";
  1620. }
  1621. // Create this response file.
  1622. std::string link_rsp =
  1623. this->CreateResponseFile("linklibs.rsp", linkLibs, makefile_depends);
  1624. // Reference the response file.
  1625. linkLibs = responseFlag;
  1626. linkLibs += this->Convert(link_rsp,
  1627. cmLocalGenerator::NONE,
  1628. cmLocalGenerator::SHELL);
  1629. }
  1630. }
  1631. //----------------------------------------------------------------------------
  1632. void
  1633. cmMakefileTargetGenerator
  1634. ::CreateObjectLists(bool useLinkScript, bool useArchiveRules,
  1635. bool useResponseFile, std::string& buildObjs,
  1636. std::vector<std::string>& makefile_depends,
  1637. bool useWatcomQuote)
  1638. {
  1639. std::string variableName;
  1640. std::string variableNameExternal;
  1641. this->WriteObjectsVariable(variableName, variableNameExternal,
  1642. useWatcomQuote);
  1643. if(useResponseFile)
  1644. {
  1645. // MSVC response files cannot exceed 128K.
  1646. std::string::size_type const responseFileLimit = 131000;
  1647. // Construct the individual object list strings.
  1648. std::vector<std::string> object_strings;
  1649. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1650. // Lookup the response file reference flag.
  1651. std::string responseFlagVar = "CMAKE_";
  1652. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1653. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1654. const char* responseFlag =
  1655. this->Makefile->GetDefinition(responseFlagVar);
  1656. if(!responseFlag)
  1657. {
  1658. responseFlag = "@";
  1659. }
  1660. // Write a response file for each string.
  1661. const char* sep = "";
  1662. for(unsigned int i = 0; i < object_strings.size(); ++i)
  1663. {
  1664. // Number the response files.
  1665. char rsp[32];
  1666. sprintf(rsp, "objects%u.rsp", i+1);
  1667. // Create this response file.
  1668. std::string objects_rsp =
  1669. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1670. // Separate from previous response file references.
  1671. buildObjs += sep;
  1672. sep = " ";
  1673. // Reference the response file.
  1674. buildObjs += responseFlag;
  1675. buildObjs += this->Convert(objects_rsp,
  1676. cmLocalGenerator::NONE,
  1677. cmLocalGenerator::SHELL);
  1678. }
  1679. }
  1680. else if(useLinkScript)
  1681. {
  1682. if(!useArchiveRules)
  1683. {
  1684. this->WriteObjectsString(buildObjs);
  1685. }
  1686. }
  1687. else
  1688. {
  1689. buildObjs = "$(";
  1690. buildObjs += variableName;
  1691. buildObjs += ") $(";
  1692. buildObjs += variableNameExternal;
  1693. buildObjs += ")";
  1694. }
  1695. }
  1696. //----------------------------------------------------------------------------
  1697. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1698. const std::string& lang)
  1699. {
  1700. std::string responseVar = "CMAKE_";
  1701. responseVar += lang;
  1702. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1703. bool useResponseFile = this->Makefile->IsOn(responseVar);
  1704. std::vector<std::string> includes;
  1705. const std::string& config =
  1706. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1707. this->LocalGenerator->GetIncludeDirectories(includes,
  1708. this->GeneratorTarget,
  1709. lang, config);
  1710. std::string includeFlags =
  1711. this->LocalGenerator->GetIncludeFlags(includes, this->GeneratorTarget,
  1712. lang, false, useResponseFile,
  1713. config);
  1714. if(includeFlags.empty())
  1715. {
  1716. return;
  1717. }
  1718. if(useResponseFile)
  1719. {
  1720. std::string name = "includes_";
  1721. name += lang;
  1722. name += ".rsp";
  1723. std::string arg = "@" +
  1724. this->CreateResponseFile(name.c_str(), includeFlags,
  1725. this->FlagFileDepends[lang]);
  1726. this->LocalGenerator->AppendFlags(flags, arg);
  1727. }
  1728. else
  1729. {
  1730. this->LocalGenerator->AppendFlags(flags, includeFlags);
  1731. }
  1732. }
  1733. //----------------------------------------------------------------------------
  1734. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1735. {
  1736. // Compute the module directory.
  1737. if(!this->FortranModuleDirectoryComputed)
  1738. {
  1739. const char* target_mod_dir =
  1740. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1741. const char* moddir_flag =
  1742. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1743. if(target_mod_dir && moddir_flag)
  1744. {
  1745. // Compute the full path to the module directory.
  1746. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1747. {
  1748. // Already a full path.
  1749. this->FortranModuleDirectory = target_mod_dir;
  1750. }
  1751. else
  1752. {
  1753. // Interpret relative to the current output directory.
  1754. this->FortranModuleDirectory =
  1755. this->Makefile->GetCurrentOutputDirectory();
  1756. this->FortranModuleDirectory += "/";
  1757. this->FortranModuleDirectory += target_mod_dir;
  1758. }
  1759. // Make sure the module output directory exists.
  1760. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1761. }
  1762. this->FortranModuleDirectoryComputed = true;
  1763. }
  1764. // Return the computed directory.
  1765. if(this->FortranModuleDirectory.empty())
  1766. {
  1767. return 0;
  1768. }
  1769. else
  1770. {
  1771. return this->FortranModuleDirectory.c_str();
  1772. }
  1773. }
  1774. //----------------------------------------------------------------------------
  1775. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1776. {
  1777. // Enable module output if necessary.
  1778. if(const char* modout_flag =
  1779. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  1780. {
  1781. this->LocalGenerator->AppendFlags(flags, modout_flag);
  1782. }
  1783. // Add a module output directory flag if necessary.
  1784. const char* mod_dir = this->GetFortranModuleDirectory();
  1785. if(!mod_dir)
  1786. {
  1787. mod_dir = this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  1788. }
  1789. if(mod_dir)
  1790. {
  1791. const char* moddir_flag =
  1792. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1793. std::string modflag = moddir_flag;
  1794. modflag += this->Convert(mod_dir,
  1795. cmLocalGenerator::START_OUTPUT,
  1796. cmLocalGenerator::SHELL);
  1797. this->LocalGenerator->AppendFlags(flags, modflag);
  1798. }
  1799. // If there is a separate module path flag then duplicate the
  1800. // include path with it. This compiler does not search the include
  1801. // path for modules.
  1802. if(const char* modpath_flag =
  1803. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1804. {
  1805. std::vector<std::string> includes;
  1806. const std::string& config =
  1807. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1808. this->LocalGenerator->GetIncludeDirectories(includes,
  1809. this->GeneratorTarget,
  1810. "C", config);
  1811. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1812. idi != includes.end(); ++idi)
  1813. {
  1814. std::string flg = modpath_flag;
  1815. flg += this->Convert(*idi,
  1816. cmLocalGenerator::NONE,
  1817. cmLocalGenerator::SHELL);
  1818. this->LocalGenerator->AppendFlags(flags, flg);
  1819. }
  1820. }
  1821. }
  1822. //----------------------------------------------------------------------------
  1823. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1824. {
  1825. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1826. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1827. if(def.empty())
  1828. {
  1829. return;
  1830. }
  1831. // TODO: Create a per-language flag variable.
  1832. const char* defFileFlag =
  1833. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1834. if(!defFileFlag)
  1835. {
  1836. return;
  1837. }
  1838. // Append the flag and value. Use ConvertToLinkReference to help
  1839. // vs6's "cl -link" pass it to the linker.
  1840. std::string flag = defFileFlag;
  1841. flag += (this->LocalGenerator->ConvertToLinkReference(def));
  1842. this->LocalGenerator->AppendFlags(flags, flag);
  1843. }
  1844. //----------------------------------------------------------------------------
  1845. const char* cmMakefileTargetGenerator::GetFeature(const std::string& feature)
  1846. {
  1847. return this->Target->GetFeature(feature, this->ConfigName);
  1848. }
  1849. //----------------------------------------------------------------------------
  1850. bool cmMakefileTargetGenerator::GetFeatureAsBool(const std::string& feature)
  1851. {
  1852. return this->Target->GetFeatureAsBool(feature, this->ConfigName);
  1853. }
  1854. //----------------------------------------------------------------------------
  1855. void cmMakefileTargetGenerator::AddFeatureFlags(
  1856. std::string& flags, const std::string& lang
  1857. )
  1858. {
  1859. // Add language-specific flags.
  1860. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1861. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1862. {
  1863. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1864. }
  1865. }