cmMakefileTargetGenerator.cxx 69 KB

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