cmMakefileTargetGenerator.cxx 71 KB

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