cmMakefileTargetGenerator.cxx 65 KB

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