cmMakefileTargetGenerator.cxx 67 KB

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