cmMakefileTargetGenerator.cxx 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  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(this)
  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. }
  50. cmMakefileTargetGenerator *
  51. cmMakefileTargetGenerator::New(cmTarget *tgt)
  52. {
  53. cmMakefileTargetGenerator *result = 0;
  54. switch (tgt->GetType())
  55. {
  56. case cmTarget::EXECUTABLE:
  57. result = new cmMakefileExecutableTargetGenerator(tgt);
  58. break;
  59. case cmTarget::STATIC_LIBRARY:
  60. case cmTarget::SHARED_LIBRARY:
  61. case cmTarget::MODULE_LIBRARY:
  62. case cmTarget::OBJECT_LIBRARY:
  63. result = new cmMakefileLibraryTargetGenerator(tgt);
  64. break;
  65. case cmTarget::UTILITY:
  66. result = new cmMakefileUtilityTargetGenerator(tgt);
  67. break;
  68. default:
  69. return result;
  70. // break; /* unreachable */
  71. }
  72. return result;
  73. }
  74. //----------------------------------------------------------------------------
  75. void cmMakefileTargetGenerator::CreateRuleFile()
  76. {
  77. // Create a directory for this target.
  78. this->TargetBuildDirectory =
  79. this->LocalGenerator->GetTargetDirectory(*this->Target);
  80. this->TargetBuildDirectoryFull =
  81. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  82. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  83. // Construct the rule file name.
  84. this->BuildFileName = this->TargetBuildDirectory;
  85. this->BuildFileName += "/build.make";
  86. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  87. this->BuildFileNameFull += "/build.make";
  88. // Construct the rule file name.
  89. this->ProgressFileNameFull = this->TargetBuildDirectoryFull;
  90. this->ProgressFileNameFull += "/progress.make";
  91. // reset the progress count
  92. this->NumberOfProgressActions = 0;
  93. // Open the rule file. This should be copy-if-different because the
  94. // rules may depend on this file itself.
  95. this->BuildFileStream =
  96. new cmGeneratedFileStream(this->BuildFileNameFull.c_str());
  97. this->BuildFileStream->SetCopyIfDifferent(true);
  98. if(!this->BuildFileStream)
  99. {
  100. return;
  101. }
  102. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  103. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  104. }
  105. //----------------------------------------------------------------------------
  106. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  107. {
  108. // write the custom commands for this target
  109. // Look for files registered for cleaning in this directory.
  110. if(const char* additional_clean_files =
  111. this->Makefile->GetProperty
  112. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  113. {
  114. cmSystemTools::ExpandListArgument(additional_clean_files,
  115. this->CleanFiles);
  116. }
  117. // add custom commands to the clean rules?
  118. const char* clean_no_custom =
  119. this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  120. bool clean = cmSystemTools::IsOff(clean_no_custom);
  121. // First generate the object rule files. Save a list of all object
  122. // files for this target.
  123. for(std::vector<cmSourceFile*>::const_iterator
  124. si = this->GeneratorTarget->CustomCommands.begin();
  125. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  126. {
  127. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  128. this->GenerateCustomRuleFile(*cc);
  129. if (clean)
  130. {
  131. const std::vector<std::string>& outputs = cc->GetOutputs();
  132. for(std::vector<std::string>::const_iterator o = outputs.begin();
  133. o != outputs.end(); ++o)
  134. {
  135. this->CleanFiles.push_back
  136. (this->Convert(o->c_str(),
  137. cmLocalGenerator::START_OUTPUT,
  138. cmLocalGenerator::UNCHANGED));
  139. }
  140. }
  141. }
  142. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  143. this->GeneratorTarget->HeaderSources,
  144. &this->MacOSXContentGenerator);
  145. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  146. this->GeneratorTarget->ExtraSources,
  147. &this->MacOSXContentGenerator);
  148. for(std::vector<cmSourceFile*>::const_iterator
  149. si = this->GeneratorTarget->ExternalObjects.begin();
  150. si != this->GeneratorTarget->ExternalObjects.end(); ++si)
  151. {
  152. this->ExternalObjects.push_back((*si)->GetFullPath());
  153. }
  154. for(std::vector<cmSourceFile*>::const_iterator
  155. si = this->GeneratorTarget->ObjectSources.begin();
  156. si != this->GeneratorTarget->ObjectSources.end(); ++si)
  157. {
  158. // Generate this object file's rule file.
  159. this->WriteObjectRuleFiles(**si);
  160. }
  161. // Add object library contents as external objects.
  162. this->GeneratorTarget->UseObjectLibraries(this->ExternalObjects);
  163. }
  164. //----------------------------------------------------------------------------
  165. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  166. {
  167. const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")?
  168. "$(CMAKE_BINARY_DIR)/" : "");
  169. // Include the dependencies for the target.
  170. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  171. dependFileNameFull += "/depend.make";
  172. *this->BuildFileStream
  173. << "# Include any dependencies generated for this target.\n"
  174. << this->LocalGenerator->IncludeDirective << " " << root
  175. << this->Convert(dependFileNameFull.c_str(),
  176. cmLocalGenerator::HOME_OUTPUT,
  177. cmLocalGenerator::MAKEFILE)
  178. << "\n\n";
  179. if(!this->NoRuleMessages)
  180. {
  181. // Include the progress variables for the target.
  182. *this->BuildFileStream
  183. << "# Include the progress variables for this target.\n"
  184. << this->LocalGenerator->IncludeDirective << " " << root
  185. << this->Convert(this->ProgressFileNameFull.c_str(),
  186. cmLocalGenerator::HOME_OUTPUT,
  187. cmLocalGenerator::MAKEFILE)
  188. << "\n\n";
  189. }
  190. // make sure the depend file exists
  191. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  192. {
  193. // Write an empty dependency file.
  194. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  195. depFileStream
  196. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  197. << "# This may be replaced when dependencies are built." << std::endl;
  198. }
  199. // Open the flags file. This should be copy-if-different because the
  200. // rules may depend on this file itself.
  201. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  202. this->FlagFileNameFull += "/flags.make";
  203. this->FlagFileStream =
  204. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  205. this->FlagFileStream->SetCopyIfDifferent(true);
  206. if(!this->FlagFileStream)
  207. {
  208. return;
  209. }
  210. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  211. // Include the flags for the target.
  212. *this->BuildFileStream
  213. << "# Include the compile flags for this target's objects.\n"
  214. << this->LocalGenerator->IncludeDirective << " " << root
  215. << this->Convert(this->FlagFileNameFull.c_str(),
  216. cmLocalGenerator::HOME_OUTPUT,
  217. cmLocalGenerator::MAKEFILE)
  218. << "\n\n";
  219. }
  220. //----------------------------------------------------------------------------
  221. std::string cmMakefileTargetGenerator::GetFlags(const std::string &l)
  222. {
  223. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  224. if (i == this->FlagsByLanguage.end())
  225. {
  226. std::string flags;
  227. const char *lang = l.c_str();
  228. // Add language feature flags.
  229. this->AddFeatureFlags(flags, lang);
  230. this->LocalGenerator->AddArchitectureFlags(flags, this->Target,
  231. lang, this->ConfigName);
  232. // Fortran-specific flags computed for this target.
  233. if(l == "Fortran")
  234. {
  235. this->AddFortranFlags(flags);
  236. }
  237. this->LocalGenerator->AddCMP0018Flags(flags, this->Target, lang);
  238. // Add include directory flags.
  239. this->AddIncludeFlags(flags, lang);
  240. // Append old-style preprocessor definition flags.
  241. this->LocalGenerator->
  242. AppendFlags(flags, this->Makefile->GetDefineFlags());
  243. // Add include directory flags.
  244. this->LocalGenerator->
  245. AppendFlags(flags,this->GetFrameworkFlags().c_str());
  246. ByLanguageMap::value_type entry(l, flags);
  247. i = this->FlagsByLanguage.insert(entry).first;
  248. }
  249. return i->second;
  250. }
  251. std::string cmMakefileTargetGenerator::GetDefines(const std::string &l)
  252. {
  253. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  254. if (i == this->DefinesByLanguage.end())
  255. {
  256. std::string defines;
  257. const char *lang = l.c_str();
  258. // Add the export symbol definition for shared library objects.
  259. if(const char* exportMacro = this->Target->GetExportMacro())
  260. {
  261. this->LocalGenerator->AppendDefines(defines, exportMacro, lang);
  262. }
  263. // Add preprocessor definitions for this target and configuration.
  264. this->LocalGenerator->AppendDefines
  265. (defines, this->Makefile->GetProperty("COMPILE_DEFINITIONS"), lang);
  266. this->LocalGenerator->AppendDefines
  267. (defines, this->Target->GetProperty("COMPILE_DEFINITIONS"), lang);
  268. std::string defPropName = "COMPILE_DEFINITIONS_";
  269. defPropName +=
  270. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  271. this->LocalGenerator->AppendDefines
  272. (defines, this->Makefile->GetProperty(defPropName.c_str()), lang);
  273. this->LocalGenerator->AppendDefines
  274. (defines, this->Target->GetProperty(defPropName.c_str()), lang);
  275. ByLanguageMap::value_type entry(l, defines);
  276. i = this->DefinesByLanguage.insert(entry).first;
  277. }
  278. return i->second;
  279. }
  280. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  281. {
  282. // write language flags for target
  283. std::set<cmStdString> languages;
  284. this->Target->GetLanguages(languages);
  285. // put the compiler in the rules.make file so that if it changes
  286. // things rebuild
  287. for(std::set<cmStdString>::const_iterator l = languages.begin();
  288. l != languages.end(); ++l)
  289. {
  290. cmStdString compiler = "CMAKE_";
  291. compiler += *l;
  292. compiler += "_COMPILER";
  293. *this->FlagFileStream << "# compile " << l->c_str() << " with " <<
  294. this->Makefile->GetSafeDefinition(compiler.c_str()) << "\n";
  295. }
  296. for(std::set<cmStdString>::const_iterator l = languages.begin();
  297. l != languages.end(); ++l)
  298. {
  299. *this->FlagFileStream << *l << "_FLAGS = " << this->GetFlags(*l) << "\n\n";
  300. *this->FlagFileStream << *l << "_DEFINES = " << this->GetDefines(*l) <<
  301. "\n\n";
  302. }
  303. // Add target-specific flags.
  304. if(this->Target->GetProperty("COMPILE_FLAGS"))
  305. {
  306. std::string flags;
  307. this->LocalGenerator->AppendFlags
  308. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  309. *this->FlagFileStream << "# TARGET_FLAGS = " << flags << "\n\n";
  310. }
  311. }
  312. //----------------------------------------------------------------------------
  313. cmMakefileTargetGenerator::MacOSXContentGeneratorType::
  314. MacOSXContentGeneratorType(cmMakefileTargetGenerator* generator)
  315. : cmOSXBundleGenerator::MacOSXContentGeneratorType()
  316. , Generator(generator)
  317. {
  318. }
  319. //----------------------------------------------------------------------------
  320. void
  321. cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()
  322. (cmSourceFile& source, const char* pkgloc)
  323. {
  324. // Skip OS X content when not building a Framework or Bundle.
  325. if(this->Generator->MacContentDirectory.empty())
  326. {
  327. return;
  328. }
  329. std::string macdir =
  330. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  331. // Get the input file location.
  332. std::string input = source.GetFullPath();
  333. // Get the output file location.
  334. std::string output = macdir;
  335. output += "/";
  336. output += cmSystemTools::GetFilenameName(input);
  337. this->Generator->CleanFiles.push_back(
  338. this->Generator->Convert(output.c_str(),
  339. cmLocalGenerator::START_OUTPUT));
  340. output = this->Generator->Convert(output.c_str(),
  341. cmLocalGenerator::HOME_OUTPUT);
  342. // Create a rule to copy the content into the bundle.
  343. std::vector<std::string> depends;
  344. std::vector<std::string> commands;
  345. depends.push_back(input);
  346. std::string copyEcho = "Copying OS X content ";
  347. copyEcho += output;
  348. this->Generator->LocalGenerator->AppendEcho(
  349. commands, copyEcho.c_str(),
  350. cmLocalUnixMakefileGenerator3::EchoBuild);
  351. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  352. copyCommand += this->Generator->Convert(input.c_str(),
  353. cmLocalGenerator::NONE,
  354. cmLocalGenerator::SHELL);
  355. copyCommand += " ";
  356. copyCommand += this->Generator->Convert(output.c_str(),
  357. cmLocalGenerator::NONE,
  358. cmLocalGenerator::SHELL);
  359. commands.push_back(copyCommand);
  360. this->Generator->LocalGenerator->WriteMakeRule(
  361. *this->Generator->BuildFileStream, 0,
  362. output.c_str(),
  363. depends, commands, false);
  364. this->Generator->ExtraFiles.insert(output);
  365. }
  366. //----------------------------------------------------------------------------
  367. void cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile& source)
  368. {
  369. // Identify the language of the source file.
  370. const char* lang = this->LocalGenerator->GetSourceFileLanguage(source);
  371. if(!lang)
  372. {
  373. // don't know anything about this file so skip it
  374. return;
  375. }
  376. // Get the full path name of the object file.
  377. std::string const& objectName = this->GeneratorTarget->Objects[&source];
  378. std::string obj = this->LocalGenerator->GetTargetDirectory(*this->Target);
  379. obj += "/";
  380. obj += objectName;
  381. // Avoid generating duplicate rules.
  382. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  383. {
  384. this->ObjectFiles.insert(obj);
  385. }
  386. else
  387. {
  388. cmOStringStream err;
  389. err << "Warning: Source file \""
  390. << source.GetFullPath()
  391. << "\" is listed multiple times for target \""
  392. << this->Target->GetName()
  393. << "\".";
  394. cmSystemTools::Message(err.str().c_str(), "Warning");
  395. return;
  396. }
  397. // Create the directory containing the object file. This may be a
  398. // subdirectory under the target's directory.
  399. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  400. cmSystemTools::MakeDirectory
  401. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  402. // Save this in the target's list of object files.
  403. this->Objects.push_back(obj);
  404. this->CleanFiles.push_back(obj);
  405. // TODO: Remove
  406. //std::string relativeObj
  407. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  408. //relativeObj += obj;
  409. // we compute some depends when writing the depend.make that we will also
  410. // use in the build.make, same with depMakeFile
  411. std::vector<std::string> depends;
  412. std::string depMakeFile;
  413. // generate the build rule file
  414. this->WriteObjectBuildFile(obj, lang, source, depends);
  415. // The object file should be checked for dependency integrity.
  416. std::string objFullPath = this->Makefile->GetCurrentOutputDirectory();
  417. objFullPath += "/";
  418. objFullPath += obj;
  419. objFullPath =
  420. this->Convert(objFullPath.c_str(), cmLocalGenerator::FULL);
  421. std::string srcFullPath =
  422. this->Convert(source.GetFullPath().c_str(), cmLocalGenerator::FULL);
  423. this->LocalGenerator->
  424. AddImplicitDepends(*this->Target, lang,
  425. objFullPath.c_str(),
  426. srcFullPath.c_str());
  427. }
  428. //----------------------------------------------------------------------------
  429. void
  430. cmMakefileTargetGenerator
  431. ::AppendFortranFormatFlags(std::string& flags, cmSourceFile& source)
  432. {
  433. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  434. cmLocalGenerator::FortranFormat format =
  435. this->LocalGenerator->GetFortranFormat(srcfmt);
  436. if(format == cmLocalGenerator::FortranFormatNone)
  437. {
  438. const char* tgtfmt = this->Target->GetProperty("Fortran_FORMAT");
  439. format = this->LocalGenerator->GetFortranFormat(tgtfmt);
  440. }
  441. const char* var = 0;
  442. switch (format)
  443. {
  444. case cmLocalGenerator::FortranFormatFixed:
  445. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG"; break;
  446. case cmLocalGenerator::FortranFormatFree:
  447. var = "CMAKE_Fortran_FORMAT_FREE_FLAG"; break;
  448. default: break;
  449. }
  450. if(var)
  451. {
  452. this->LocalGenerator->AppendFlags(
  453. flags, this->Makefile->GetDefinition(var));
  454. }
  455. }
  456. //----------------------------------------------------------------------------
  457. void
  458. cmMakefileTargetGenerator
  459. ::WriteObjectBuildFile(std::string &obj,
  460. const char *lang,
  461. cmSourceFile& source,
  462. std::vector<std::string>& depends)
  463. {
  464. this->LocalGenerator->AppendRuleDepend(depends,
  465. this->FlagFileNameFull.c_str());
  466. this->LocalGenerator->AppendRuleDepends(depends,
  467. this->FlagFileDepends[lang]);
  468. // generate the depend scanning rule
  469. this->WriteObjectDependRules(source, depends);
  470. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  471. relativeObj += obj;
  472. // Write the build rule.
  473. // Build the set of compiler flags.
  474. std::string flags;
  475. // Add language-specific flags.
  476. std::string langFlags = "$(";
  477. langFlags += lang;
  478. langFlags += "_FLAGS)";
  479. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  480. // Add target-specific flags.
  481. if(this->Target->GetProperty("COMPILE_FLAGS"))
  482. {
  483. std::string langIncludeExpr = "CMAKE_";
  484. langIncludeExpr += lang;
  485. langIncludeExpr += "_FLAG_REGEX";
  486. const char* regex = this->Makefile->
  487. GetDefinition(langIncludeExpr.c_str());
  488. if(regex)
  489. {
  490. cmsys::RegularExpression r(regex);
  491. std::vector<std::string> args;
  492. cmSystemTools::ParseWindowsCommandLine(
  493. this->Target->GetProperty("COMPILE_FLAGS"),
  494. args);
  495. for(std::vector<std::string>::iterator i = args.begin();
  496. i != args.end(); ++i)
  497. {
  498. if(r.find(i->c_str()))
  499. {
  500. this->LocalGenerator->AppendFlags
  501. (flags, i->c_str());
  502. }
  503. }
  504. }
  505. else
  506. {
  507. this->LocalGenerator->AppendFlags
  508. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  509. }
  510. }
  511. // Add Fortran format flags.
  512. if(strcmp(lang, "Fortran") == 0)
  513. {
  514. this->AppendFortranFormatFlags(flags, source);
  515. }
  516. // Add flags from source file properties.
  517. if (source.GetProperty("COMPILE_FLAGS"))
  518. {
  519. this->LocalGenerator->AppendFlags
  520. (flags, source.GetProperty("COMPILE_FLAGS"));
  521. *this->FlagFileStream << "# Custom flags: "
  522. << relativeObj << "_FLAGS = "
  523. << source.GetProperty("COMPILE_FLAGS")
  524. << "\n"
  525. << "\n";
  526. }
  527. // Add language-specific defines.
  528. std::string defines = "$(";
  529. defines += lang;
  530. defines += "_DEFINES)";
  531. // Add source-sepcific preprocessor definitions.
  532. if(const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS"))
  533. {
  534. this->LocalGenerator->AppendDefines(defines, compile_defs, lang);
  535. *this->FlagFileStream << "# Custom defines: "
  536. << relativeObj << "_DEFINES = "
  537. << compile_defs << "\n"
  538. << "\n";
  539. }
  540. std::string configUpper =
  541. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  542. std::string defPropName = "COMPILE_DEFINITIONS_";
  543. defPropName += configUpper;
  544. if(const char* config_compile_defs =
  545. source.GetProperty(defPropName.c_str()))
  546. {
  547. this->LocalGenerator->AppendDefines(defines, config_compile_defs, lang);
  548. *this->FlagFileStream
  549. << "# Custom defines: "
  550. << relativeObj << "_DEFINES_" << configUpper
  551. << " = " << config_compile_defs << "\n"
  552. << "\n";
  553. }
  554. // Get the output paths for source and object files.
  555. std::string sourceFile = source.GetFullPath();
  556. if(this->LocalGenerator->UseRelativePaths)
  557. {
  558. sourceFile = this->Convert(sourceFile.c_str(),
  559. cmLocalGenerator::START_OUTPUT);
  560. }
  561. sourceFile = this->Convert(sourceFile.c_str(),
  562. cmLocalGenerator::NONE,
  563. cmLocalGenerator::SHELL);
  564. // Construct the build message.
  565. std::vector<std::string> no_commands;
  566. std::vector<std::string> commands;
  567. // add in a progress call if needed
  568. this->AppendProgress(commands);
  569. if(!this->NoRuleMessages)
  570. {
  571. std::string buildEcho = "Building ";
  572. buildEcho += lang;
  573. buildEcho += " object ";
  574. buildEcho += relativeObj;
  575. this->LocalGenerator->AppendEcho
  576. (commands, buildEcho.c_str(), cmLocalUnixMakefileGenerator3::EchoBuild);
  577. }
  578. std::string targetOutPathPDB;
  579. {
  580. std::string targetFullPathPDB;
  581. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  582. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  583. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  584. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  585. {
  586. targetFullPathPDB = this->Target->GetDirectory(this->ConfigName);
  587. targetFullPathPDB += "/";
  588. targetFullPathPDB += this->Target->GetPDBName(this->ConfigName);
  589. }
  590. targetOutPathPDB =
  591. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
  592. cmLocalGenerator::SHELL);
  593. }
  594. cmLocalGenerator::RuleVariables vars;
  595. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  596. vars.CMTarget = this->Target;
  597. vars.Language = lang;
  598. vars.TargetPDB = targetOutPathPDB.c_str();
  599. vars.Source = sourceFile.c_str();
  600. std::string shellObj =
  601. this->Convert(obj.c_str(),
  602. cmLocalGenerator::NONE,
  603. cmLocalGenerator::SHELL).c_str();
  604. vars.Object = shellObj.c_str();
  605. std::string objectDir = cmSystemTools::GetFilenamePath(obj);
  606. objectDir = this->Convert(objectDir.c_str(),
  607. cmLocalGenerator::START_OUTPUT,
  608. cmLocalGenerator::SHELL);
  609. vars.ObjectDir = objectDir.c_str();
  610. vars.Flags = flags.c_str();
  611. vars.Defines = defines.c_str();
  612. bool lang_is_c_or_cxx = ((strcmp(lang, "C") == 0) ||
  613. (strcmp(lang, "CXX") == 0));
  614. // Construct the compile rules.
  615. {
  616. std::string compileRuleVar = "CMAKE_";
  617. compileRuleVar += lang;
  618. compileRuleVar += "_COMPILE_OBJECT";
  619. std::string compileRule =
  620. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  621. std::vector<std::string> compileCommands;
  622. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  623. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  624. lang_is_c_or_cxx && compileCommands.size() == 1)
  625. {
  626. std::string compileCommand = compileCommands[0];
  627. this->LocalGenerator->ExpandRuleVariables(compileCommand, vars);
  628. std::string workingDirectory =
  629. this->LocalGenerator->Convert(
  630. this->Makefile->GetStartOutputDirectory(), cmLocalGenerator::FULL);
  631. compileCommand.replace(compileCommand.find(langFlags),
  632. langFlags.size(), this->GetFlags(lang));
  633. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  634. compileCommand.replace(compileCommand.find(langDefines),
  635. langDefines.size(), this->GetDefines(lang));
  636. this->GlobalGenerator->AddCXXCompileCommand(
  637. source.GetFullPath(), workingDirectory, compileCommand);
  638. }
  639. // Expand placeholders in the commands.
  640. for(std::vector<std::string>::iterator i = compileCommands.begin();
  641. i != compileCommands.end(); ++i)
  642. {
  643. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  644. }
  645. // Change the command working directory to the local build tree.
  646. this->LocalGenerator->CreateCDCommand
  647. (compileCommands,
  648. this->Makefile->GetStartOutputDirectory(),
  649. cmLocalGenerator::HOME_OUTPUT);
  650. commands.insert(commands.end(),
  651. compileCommands.begin(), compileCommands.end());
  652. }
  653. // Write the rule.
  654. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  655. relativeObj.c_str(),
  656. depends, commands, false);
  657. // Check for extra outputs created by the compilation.
  658. if(const char* extra_outputs_str =
  659. source.GetProperty("OBJECT_OUTPUTS"))
  660. {
  661. std::vector<std::string> extra_outputs;
  662. cmSystemTools::ExpandListArgument(extra_outputs_str, extra_outputs);
  663. for(std::vector<std::string>::const_iterator eoi = extra_outputs.begin();
  664. eoi != extra_outputs.end(); ++eoi)
  665. {
  666. // Register this as an extra output for the object file rule.
  667. // This will cause the object file to be rebuilt if the extra
  668. // output is missing.
  669. this->GenerateExtraOutput(eoi->c_str(), relativeObj.c_str(), false);
  670. // Register this as an extra file to clean.
  671. this->CleanFiles.push_back(eoi->c_str());
  672. }
  673. }
  674. bool do_preprocess_rules = lang_is_c_or_cxx &&
  675. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  676. bool do_assembly_rules = lang_is_c_or_cxx &&
  677. this->LocalGenerator->GetCreateAssemblySourceRules();
  678. if(do_preprocess_rules || do_assembly_rules)
  679. {
  680. std::vector<std::string> force_depends;
  681. force_depends.push_back("cmake_force");
  682. std::string::size_type dot_pos = relativeObj.rfind(".");
  683. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  684. dot_pos = obj.rfind(".");
  685. std::string objBase = obj.substr(0, dot_pos);
  686. if(do_preprocess_rules)
  687. {
  688. commands.clear();
  689. std::string relativeObjI = relativeObjBase + ".i";
  690. std::string objI = objBase + ".i";
  691. std::string preprocessEcho = "Preprocessing ";
  692. preprocessEcho += lang;
  693. preprocessEcho += " source to ";
  694. preprocessEcho += objI;
  695. this->LocalGenerator->AppendEcho(
  696. commands, preprocessEcho.c_str(),
  697. cmLocalUnixMakefileGenerator3::EchoBuild
  698. );
  699. std::string preprocessRuleVar = "CMAKE_";
  700. preprocessRuleVar += lang;
  701. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  702. if(const char* preprocessRule =
  703. this->Makefile->GetDefinition(preprocessRuleVar.c_str()))
  704. {
  705. std::vector<std::string> preprocessCommands;
  706. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  707. std::string shellObjI =
  708. this->Convert(objI.c_str(),
  709. cmLocalGenerator::NONE,
  710. cmLocalGenerator::SHELL).c_str();
  711. vars.PreprocessedSource = shellObjI.c_str();
  712. // Expand placeholders in the commands.
  713. for(std::vector<std::string>::iterator i = preprocessCommands.begin();
  714. i != preprocessCommands.end(); ++i)
  715. {
  716. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  717. }
  718. this->LocalGenerator->CreateCDCommand
  719. (preprocessCommands,
  720. this->Makefile->GetStartOutputDirectory(),
  721. cmLocalGenerator::HOME_OUTPUT);
  722. commands.insert(commands.end(),
  723. preprocessCommands.begin(),
  724. preprocessCommands.end());
  725. }
  726. else
  727. {
  728. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  729. cmd += preprocessRuleVar;
  730. commands.push_back(cmd);
  731. }
  732. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  733. relativeObjI.c_str(),
  734. force_depends, commands, false);
  735. }
  736. if(do_assembly_rules)
  737. {
  738. commands.clear();
  739. std::string relativeObjS = relativeObjBase + ".s";
  740. std::string objS = objBase + ".s";
  741. std::string assemblyEcho = "Compiling ";
  742. assemblyEcho += lang;
  743. assemblyEcho += " source to assembly ";
  744. assemblyEcho += objS;
  745. this->LocalGenerator->AppendEcho(
  746. commands, assemblyEcho.c_str(),
  747. cmLocalUnixMakefileGenerator3::EchoBuild
  748. );
  749. std::string assemblyRuleVar = "CMAKE_";
  750. assemblyRuleVar += lang;
  751. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  752. if(const char* assemblyRule =
  753. this->Makefile->GetDefinition(assemblyRuleVar.c_str()))
  754. {
  755. std::vector<std::string> assemblyCommands;
  756. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  757. std::string shellObjS =
  758. this->Convert(objS.c_str(),
  759. cmLocalGenerator::NONE,
  760. cmLocalGenerator::SHELL).c_str();
  761. vars.AssemblySource = shellObjS.c_str();
  762. // Expand placeholders in the commands.
  763. for(std::vector<std::string>::iterator i = assemblyCommands.begin();
  764. i != assemblyCommands.end(); ++i)
  765. {
  766. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  767. }
  768. this->LocalGenerator->CreateCDCommand
  769. (assemblyCommands,
  770. this->Makefile->GetStartOutputDirectory(),
  771. cmLocalGenerator::HOME_OUTPUT);
  772. commands.insert(commands.end(),
  773. assemblyCommands.begin(),
  774. assemblyCommands.end());
  775. }
  776. else
  777. {
  778. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  779. cmd += assemblyRuleVar;
  780. commands.push_back(cmd);
  781. }
  782. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  783. relativeObjS.c_str(),
  784. force_depends, commands, false);
  785. }
  786. }
  787. // If the language needs provides-requires mode, create the
  788. // corresponding targets.
  789. std::string objectRequires = relativeObj;
  790. objectRequires += ".requires";
  791. std::vector<std::string> p_depends;
  792. // always provide an empty requires target
  793. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  794. objectRequires.c_str(), p_depends,
  795. no_commands, true);
  796. // write a build rule to recursively build what this obj provides
  797. std::string objectProvides = relativeObj;
  798. objectProvides += ".provides";
  799. std::string temp = relativeObj;
  800. temp += ".provides.build";
  801. std::vector<std::string> r_commands;
  802. std::string tgtMakefileName =
  803. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  804. tgtMakefileName += "/build.make";
  805. r_commands.push_back
  806. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  807. temp.c_str()));
  808. p_depends.clear();
  809. p_depends.push_back(objectRequires);
  810. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  811. objectProvides.c_str(), p_depends,
  812. r_commands, true);
  813. // write the provides.build rule dependency on the obj file
  814. p_depends.clear();
  815. p_depends.push_back(relativeObj);
  816. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  817. temp.c_str(), p_depends, no_commands,
  818. false);
  819. }
  820. //----------------------------------------------------------------------------
  821. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  822. {
  823. std::vector<std::string> depends;
  824. std::vector<std::string> no_commands;
  825. // Construct the name of the dependency generation target.
  826. std::string depTarget =
  827. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  828. depTarget += "/requires";
  829. // This target drives dependency generation for all object files.
  830. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  831. std::string objTarget;
  832. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  833. obj != this->Objects.end(); ++obj)
  834. {
  835. objTarget = relPath;
  836. objTarget += *obj;
  837. objTarget += ".requires";
  838. depends.push_back(objTarget);
  839. }
  840. // Write the rule.
  841. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  842. depTarget.c_str(),
  843. depends, no_commands, true);
  844. }
  845. //----------------------------------------------------------------------------
  846. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  847. {
  848. std::vector<std::string> depends;
  849. std::vector<std::string> commands;
  850. // Construct the clean target name.
  851. std::string cleanTarget =
  852. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  853. cleanTarget += "/clean";
  854. // Construct the clean command.
  855. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  856. *this->Target);
  857. this->LocalGenerator->CreateCDCommand
  858. (commands,
  859. this->Makefile->GetStartOutputDirectory(),
  860. cmLocalGenerator::HOME_OUTPUT);
  861. // Write the rule.
  862. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  863. cleanTarget.c_str(),
  864. depends, commands, true);
  865. }
  866. //----------------------------------------------------------------------------
  867. void cmMakefileTargetGenerator::WriteTargetDependRules()
  868. {
  869. // must write the targets depend info file
  870. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  871. this->InfoFileNameFull = dir;
  872. this->InfoFileNameFull += "/DependInfo.cmake";
  873. this->InfoFileNameFull =
  874. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  875. this->InfoFileStream =
  876. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  877. this->InfoFileStream->SetCopyIfDifferent(true);
  878. if(!*this->InfoFileStream)
  879. {
  880. return;
  881. }
  882. this->LocalGenerator->
  883. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  884. // Store multiple output pairs in the depend info file.
  885. if(!this->MultipleOutputPairs.empty())
  886. {
  887. *this->InfoFileStream
  888. << "\n"
  889. << "# Pairs of files generated by the same build rule.\n"
  890. << "SET(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  891. for(MultipleOutputPairsType::const_iterator pi =
  892. this->MultipleOutputPairs.begin();
  893. pi != this->MultipleOutputPairs.end(); ++pi)
  894. {
  895. *this->InfoFileStream
  896. << " " << this->LocalGenerator->EscapeForCMake(pi->first.c_str())
  897. << " " << this->LocalGenerator->EscapeForCMake(pi->second.c_str())
  898. << "\n";
  899. }
  900. *this->InfoFileStream << " )\n\n";
  901. }
  902. // Store list of targets linked directly or transitively.
  903. {
  904. *this->InfoFileStream
  905. << "\n"
  906. << "# Targets to which this target links.\n"
  907. << "SET(CMAKE_TARGET_LINKED_INFO_FILES\n";
  908. std::set<cmTarget const*> emitted;
  909. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  910. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  911. {
  912. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  913. for(cmComputeLinkInformation::ItemVector::const_iterator
  914. i = items.begin(); i != items.end(); ++i)
  915. {
  916. cmTarget const* linkee = i->Target;
  917. if(linkee && !linkee->IsImported() && emitted.insert(linkee).second)
  918. {
  919. cmMakefile* mf = linkee->GetMakefile();
  920. cmLocalGenerator* lg = mf->GetLocalGenerator();
  921. std::string di = mf->GetStartOutputDirectory();
  922. di += "/";
  923. di += lg->GetTargetDirectory(*linkee);
  924. di += "/DependInfo.cmake";
  925. *this->InfoFileStream << " \"" << di << "\"\n";
  926. }
  927. }
  928. }
  929. *this->InfoFileStream
  930. << " )\n";
  931. }
  932. // Check for a target-specific module output directory.
  933. if(const char* mdir = this->GetFortranModuleDirectory())
  934. {
  935. *this->InfoFileStream
  936. << "\n"
  937. << "# Fortran module output directory.\n"
  938. << "SET(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  939. }
  940. // Target-specific include directories:
  941. *this->InfoFileStream
  942. << "\n"
  943. << "# The include file search paths:\n";
  944. *this->InfoFileStream
  945. << "SET(CMAKE_C_TARGET_INCLUDE_PATH\n";
  946. std::vector<std::string> includes;
  947. this->LocalGenerator->GetIncludeDirectories(includes, this->Target);
  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. this->LocalGenerator->GetIncludeDirectories(includes, this->Target);
  1399. std::vector<std::string>::iterator i;
  1400. // check all include directories for frameworks as this
  1401. // will already have added a -F for the framework
  1402. for(i = includes.begin(); i != includes.end(); ++i)
  1403. {
  1404. if(this->Target->NameResolvesToFramework(i->c_str()))
  1405. {
  1406. std::string frameworkDir = *i;
  1407. frameworkDir += "/../";
  1408. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1409. emitted.insert(frameworkDir);
  1410. }
  1411. }
  1412. std::string flags;
  1413. std::vector<std::string>& frameworks = this->Target->GetFrameworks();
  1414. for(i = frameworks.begin();
  1415. i != frameworks.end(); ++i)
  1416. {
  1417. if(emitted.insert(*i).second)
  1418. {
  1419. flags += "-F";
  1420. flags += this->Convert(i->c_str(),
  1421. cmLocalGenerator::START_OUTPUT,
  1422. cmLocalGenerator::SHELL, true);
  1423. flags += " ";
  1424. }
  1425. }
  1426. return flags;
  1427. }
  1428. //----------------------------------------------------------------------------
  1429. void cmMakefileTargetGenerator
  1430. ::AppendTargetDepends(std::vector<std::string>& depends)
  1431. {
  1432. // Static libraries never depend on anything for linking.
  1433. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1434. {
  1435. return;
  1436. }
  1437. // Loop over all library dependencies.
  1438. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1439. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1440. {
  1441. std::vector<std::string> const& libDeps = cli->GetDepends();
  1442. for(std::vector<std::string>::const_iterator j = libDeps.begin();
  1443. j != libDeps.end(); ++j)
  1444. {
  1445. depends.push_back(*j);
  1446. }
  1447. }
  1448. }
  1449. //----------------------------------------------------------------------------
  1450. void cmMakefileTargetGenerator
  1451. ::AppendObjectDepends(std::vector<std::string>& depends)
  1452. {
  1453. // Add dependencies on the compiled object files.
  1454. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1455. std::string objTarget;
  1456. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1457. obj != this->Objects.end(); ++obj)
  1458. {
  1459. objTarget = relPath;
  1460. objTarget += *obj;
  1461. depends.push_back(objTarget);
  1462. }
  1463. // Add dependencies on the external object files.
  1464. for(std::vector<std::string>::const_iterator obj
  1465. = this->ExternalObjects.begin();
  1466. obj != this->ExternalObjects.end(); ++obj)
  1467. {
  1468. depends.push_back(*obj);
  1469. }
  1470. // Add a dependency on the rule file itself.
  1471. this->LocalGenerator->AppendRuleDepend(depends,
  1472. this->BuildFileNameFull.c_str());
  1473. }
  1474. //----------------------------------------------------------------------------
  1475. void cmMakefileTargetGenerator
  1476. ::AppendLinkDepends(std::vector<std::string>& depends)
  1477. {
  1478. this->AppendObjectDepends(depends);
  1479. // Add dependencies on targets that must be built first.
  1480. this->AppendTargetDepends(depends);
  1481. // Add a dependency on the link definitions file, if any.
  1482. if(!this->GeneratorTarget->ModuleDefinitionFile.empty())
  1483. {
  1484. depends.push_back(this->GeneratorTarget->ModuleDefinitionFile);
  1485. }
  1486. // Add user-specified dependencies.
  1487. if(const char* linkDepends =
  1488. this->Target->GetProperty("LINK_DEPENDS"))
  1489. {
  1490. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1491. }
  1492. }
  1493. //----------------------------------------------------------------------------
  1494. std::string cmMakefileTargetGenerator::GetLinkRule(const char* linkRuleVar)
  1495. {
  1496. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1497. if(this->Target->HasImplibGNUtoMS())
  1498. {
  1499. std::string ruleVar = "CMAKE_";
  1500. ruleVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1501. ruleVar += "_GNUtoMS_RULE";
  1502. if(const char* rule = this->Makefile->GetDefinition(ruleVar.c_str()))
  1503. {
  1504. linkRule += rule;
  1505. }
  1506. }
  1507. return linkRule;
  1508. }
  1509. //----------------------------------------------------------------------------
  1510. void cmMakefileTargetGenerator
  1511. ::CloseFileStreams()
  1512. {
  1513. delete this->BuildFileStream;
  1514. delete this->InfoFileStream;
  1515. delete this->FlagFileStream;
  1516. }
  1517. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1518. const char* linkLang,
  1519. std::string& linkFlags)
  1520. {
  1521. // check for language flags that are not allowed at link time, and
  1522. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1523. // which fails, there may be more]
  1524. std::string removeFlags = "CMAKE_";
  1525. removeFlags += linkLang;
  1526. removeFlags += flagVar;
  1527. std::string removeflags =
  1528. this->Makefile->GetSafeDefinition(removeFlags.c_str());
  1529. std::vector<std::string> removeList;
  1530. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1531. for(std::vector<std::string>::iterator i = removeList.begin();
  1532. i != removeList.end(); ++i)
  1533. {
  1534. cmSystemTools::ReplaceString(linkFlags, i->c_str(), "");
  1535. }
  1536. }
  1537. //----------------------------------------------------------------------------
  1538. void
  1539. cmMakefileTargetGenerator
  1540. ::AddMultipleOutputPair(const char* depender, const char* dependee)
  1541. {
  1542. MultipleOutputPairsType::value_type p(depender, dependee);
  1543. this->MultipleOutputPairs.insert(p);
  1544. }
  1545. //----------------------------------------------------------------------------
  1546. void
  1547. cmMakefileTargetGenerator
  1548. ::CreateLinkScript(const char* name,
  1549. std::vector<std::string> const& link_commands,
  1550. std::vector<std::string>& makefile_commands,
  1551. std::vector<std::string>& makefile_depends)
  1552. {
  1553. // Create the link script file.
  1554. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1555. linkScriptName += "/";
  1556. linkScriptName += name;
  1557. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1558. linkScriptStream.SetCopyIfDifferent(true);
  1559. for(std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1560. cmd != link_commands.end(); ++cmd)
  1561. {
  1562. // Do not write out empty commands or commands beginning in the
  1563. // shell no-op ":".
  1564. if(!cmd->empty() && (*cmd)[0] != ':')
  1565. {
  1566. linkScriptStream << *cmd << "\n";
  1567. }
  1568. }
  1569. // Create the makefile command to invoke the link script.
  1570. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1571. link_command += this->Convert(linkScriptName.c_str(),
  1572. cmLocalGenerator::START_OUTPUT,
  1573. cmLocalGenerator::SHELL);
  1574. link_command += " --verbose=$(VERBOSE)";
  1575. makefile_commands.push_back(link_command);
  1576. makefile_depends.push_back(linkScriptName);
  1577. }
  1578. //----------------------------------------------------------------------------
  1579. std::string
  1580. cmMakefileTargetGenerator
  1581. ::CreateResponseFile(const char* name, std::string const& options,
  1582. std::vector<std::string>& makefile_depends)
  1583. {
  1584. // Create the response file.
  1585. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1586. responseFileNameFull += "/";
  1587. responseFileNameFull += name;
  1588. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1589. responseStream.SetCopyIfDifferent(true);
  1590. responseStream << options << "\n";
  1591. // Add a dependency so the target will rebuild when the set of
  1592. // objects changes.
  1593. makefile_depends.push_back(responseFileNameFull);
  1594. // Construct the name to be used on the command line.
  1595. std::string responseFileName = this->TargetBuildDirectory;
  1596. responseFileName += "/";
  1597. responseFileName += name;
  1598. return responseFileName;
  1599. }
  1600. //----------------------------------------------------------------------------
  1601. void
  1602. cmMakefileTargetGenerator
  1603. ::CreateObjectLists(bool useLinkScript, bool useArchiveRules,
  1604. bool useResponseFile, std::string& buildObjs,
  1605. std::vector<std::string>& makefile_depends)
  1606. {
  1607. std::string variableName;
  1608. std::string variableNameExternal;
  1609. this->WriteObjectsVariable(variableName, variableNameExternal);
  1610. if(useResponseFile)
  1611. {
  1612. // MSVC response files cannot exceed 128K.
  1613. std::string::size_type const responseFileLimit = 131000;
  1614. // Construct the individual object list strings.
  1615. std::vector<std::string> object_strings;
  1616. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1617. // Lookup the response file reference flag.
  1618. std::string responseFlagVar = "CMAKE_";
  1619. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1620. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1621. const char* responseFlag =
  1622. this->Makefile->GetDefinition(responseFlagVar.c_str());
  1623. if(!responseFlag)
  1624. {
  1625. responseFlag = "@";
  1626. }
  1627. // Write a response file for each string.
  1628. const char* sep = "";
  1629. for(unsigned int i = 0; i < object_strings.size(); ++i)
  1630. {
  1631. // Number the response files.
  1632. char rsp[32];
  1633. sprintf(rsp, "objects%u.rsp", i+1);
  1634. // Create this response file.
  1635. std::string objects_rsp =
  1636. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1637. // Separate from previous response file references.
  1638. buildObjs += sep;
  1639. sep = " ";
  1640. // Reference the response file.
  1641. buildObjs += responseFlag;
  1642. buildObjs += this->Convert(objects_rsp.c_str(),
  1643. cmLocalGenerator::NONE,
  1644. cmLocalGenerator::SHELL);
  1645. }
  1646. }
  1647. else if(useLinkScript)
  1648. {
  1649. if(!useArchiveRules)
  1650. {
  1651. this->WriteObjectsString(buildObjs);
  1652. }
  1653. }
  1654. else
  1655. {
  1656. buildObjs = "$(";
  1657. buildObjs += variableName;
  1658. buildObjs += ") $(";
  1659. buildObjs += variableNameExternal;
  1660. buildObjs += ")";
  1661. }
  1662. }
  1663. //----------------------------------------------------------------------------
  1664. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1665. const char* lang)
  1666. {
  1667. std::string responseVar = "CMAKE_";
  1668. responseVar += lang;
  1669. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1670. bool useResponseFile = this->Makefile->IsOn(responseVar.c_str());
  1671. std::vector<std::string> includes;
  1672. this->LocalGenerator->GetIncludeDirectories(includes, this->Target, lang);
  1673. std::string includeFlags =
  1674. this->LocalGenerator->GetIncludeFlags(includes, lang, useResponseFile);
  1675. if(includeFlags.empty())
  1676. {
  1677. return;
  1678. }
  1679. if(useResponseFile)
  1680. {
  1681. std::string name = "includes_";
  1682. name += lang;
  1683. name += ".rsp";
  1684. std::string arg = "@" +
  1685. this->CreateResponseFile(name.c_str(), includeFlags,
  1686. this->FlagFileDepends[lang]);
  1687. this->LocalGenerator->AppendFlags(flags, arg.c_str());
  1688. }
  1689. else
  1690. {
  1691. this->LocalGenerator->AppendFlags(flags, includeFlags.c_str());
  1692. }
  1693. }
  1694. //----------------------------------------------------------------------------
  1695. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1696. {
  1697. // Compute the module directory.
  1698. if(!this->FortranModuleDirectoryComputed)
  1699. {
  1700. const char* target_mod_dir =
  1701. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1702. const char* moddir_flag =
  1703. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1704. if(target_mod_dir && moddir_flag)
  1705. {
  1706. // Compute the full path to the module directory.
  1707. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1708. {
  1709. // Already a full path.
  1710. this->FortranModuleDirectory = target_mod_dir;
  1711. }
  1712. else
  1713. {
  1714. // Interpret relative to the current output directory.
  1715. this->FortranModuleDirectory =
  1716. this->Makefile->GetCurrentOutputDirectory();
  1717. this->FortranModuleDirectory += "/";
  1718. this->FortranModuleDirectory += target_mod_dir;
  1719. }
  1720. // Make sure the module output directory exists.
  1721. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1722. }
  1723. this->FortranModuleDirectoryComputed = true;
  1724. }
  1725. // Return the computed directory.
  1726. if(this->FortranModuleDirectory.empty())
  1727. {
  1728. return 0;
  1729. }
  1730. else
  1731. {
  1732. return this->FortranModuleDirectory.c_str();
  1733. }
  1734. }
  1735. //----------------------------------------------------------------------------
  1736. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1737. {
  1738. // Enable module output if necessary.
  1739. if(const char* modout_flag =
  1740. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  1741. {
  1742. this->LocalGenerator->AppendFlags(flags, modout_flag);
  1743. }
  1744. // Add a module output directory flag if necessary.
  1745. const char* mod_dir = this->GetFortranModuleDirectory();
  1746. if(!mod_dir)
  1747. {
  1748. mod_dir = this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  1749. }
  1750. if(mod_dir)
  1751. {
  1752. const char* moddir_flag =
  1753. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1754. std::string modflag = moddir_flag;
  1755. modflag += this->Convert(mod_dir,
  1756. cmLocalGenerator::START_OUTPUT,
  1757. cmLocalGenerator::SHELL);
  1758. this->LocalGenerator->AppendFlags(flags, modflag.c_str());
  1759. }
  1760. // If there is a separate module path flag then duplicate the
  1761. // include path with it. This compiler does not search the include
  1762. // path for modules.
  1763. if(const char* modpath_flag =
  1764. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1765. {
  1766. std::vector<std::string> includes;
  1767. this->LocalGenerator->GetIncludeDirectories(includes, this->Target);
  1768. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1769. idi != includes.end(); ++idi)
  1770. {
  1771. std::string flg = modpath_flag;
  1772. flg += this->Convert(idi->c_str(),
  1773. cmLocalGenerator::NONE,
  1774. cmLocalGenerator::SHELL);
  1775. this->LocalGenerator->AppendFlags(flags, flg.c_str());
  1776. }
  1777. }
  1778. }
  1779. //----------------------------------------------------------------------------
  1780. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1781. {
  1782. if(this->GeneratorTarget->ModuleDefinitionFile.empty())
  1783. {
  1784. return;
  1785. }
  1786. // TODO: Create a per-language flag variable.
  1787. const char* defFileFlag =
  1788. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1789. if(!defFileFlag)
  1790. {
  1791. return;
  1792. }
  1793. // Append the flag and value. Use ConvertToLinkReference to help
  1794. // vs6's "cl -link" pass it to the linker.
  1795. std::string flag = defFileFlag;
  1796. flag += (this->LocalGenerator->ConvertToLinkReference(
  1797. this->GeneratorTarget->ModuleDefinitionFile.c_str()));
  1798. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  1799. }
  1800. //----------------------------------------------------------------------------
  1801. const char* cmMakefileTargetGenerator::GetFeature(const char* feature)
  1802. {
  1803. return this->Target->GetFeature(feature, this->ConfigName);
  1804. }
  1805. //----------------------------------------------------------------------------
  1806. bool cmMakefileTargetGenerator::GetFeatureAsBool(const char* feature)
  1807. {
  1808. return cmSystemTools::IsOn(this->GetFeature(feature));
  1809. }
  1810. //----------------------------------------------------------------------------
  1811. void cmMakefileTargetGenerator::AddFeatureFlags(
  1812. std::string& flags, const char* lang
  1813. )
  1814. {
  1815. // Add language-specific flags.
  1816. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1817. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1818. {
  1819. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1820. }
  1821. }