cmMakefileTargetGenerator.cxx 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakefileTargetGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmGlobalUnixMakefileGenerator3.h"
  17. #include "cmLocalUnixMakefileGenerator3.h"
  18. #include "cmMakefile.h"
  19. #include "cmSourceFile.h"
  20. #include "cmTarget.h"
  21. #include "cmMakefileExecutableTargetGenerator.h"
  22. #include "cmMakefileLibraryTargetGenerator.h"
  23. #include "cmMakefileUtilityTargetGenerator.h"
  24. cmMakefileTargetGenerator::cmMakefileTargetGenerator()
  25. {
  26. this->BuildFileStream = 0;
  27. this->InfoFileStream = 0;
  28. this->FlagFileStream = 0;
  29. }
  30. cmMakefileTargetGenerator *
  31. cmMakefileTargetGenerator::New(cmLocalUnixMakefileGenerator3 *lg,
  32. cmStdString tgtName, cmTarget *tgt)
  33. {
  34. cmMakefileTargetGenerator *result = 0;
  35. switch (tgt->GetType())
  36. {
  37. case cmTarget::EXECUTABLE:
  38. result = new cmMakefileExecutableTargetGenerator;
  39. break;
  40. case cmTarget::STATIC_LIBRARY:
  41. case cmTarget::SHARED_LIBRARY:
  42. case cmTarget::MODULE_LIBRARY:
  43. result = new cmMakefileLibraryTargetGenerator;
  44. break;
  45. case cmTarget::UTILITY:
  46. result = new cmMakefileUtilityTargetGenerator;
  47. break;
  48. default:
  49. return result;
  50. break;
  51. }
  52. result->TargetName = tgtName;
  53. result->Target = tgt;
  54. result->LocalGenerator = lg;
  55. result->GlobalGenerator = lg->GetGlobalGenerator();
  56. result->Makefile = lg->GetMakefile();
  57. return result;
  58. }
  59. //----------------------------------------------------------------------------
  60. void cmMakefileTargetGenerator::CreateRuleFile()
  61. {
  62. // Create a directory for this target.
  63. this->TargetBuildDirectory =
  64. this->LocalGenerator->GetTargetDirectory(*this->Target);
  65. this->TargetBuildDirectoryFull =
  66. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  67. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  68. // Construct the rule file name.
  69. this->BuildFileName = this->TargetBuildDirectory;
  70. this->BuildFileName += "/build.make";
  71. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  72. this->BuildFileNameFull += "/build.make";
  73. // Open the rule file. This should be copy-if-different because the
  74. // rules may depend on this file itself.
  75. this->BuildFileStream =
  76. new cmGeneratedFileStream(this->BuildFileNameFull.c_str());
  77. this->BuildFileStream->SetCopyIfDifferent(true);
  78. if(!this->BuildFileStream)
  79. {
  80. return;
  81. }
  82. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  83. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  84. this->LocalGenerator->WriteMakeVariables(*this->BuildFileStream);
  85. }
  86. //----------------------------------------------------------------------------
  87. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  88. {
  89. // write the custom commands for this target
  90. // Look for files registered for cleaning in this directory.
  91. if(const char* additional_clean_files =
  92. this->Makefile->GetProperty
  93. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  94. {
  95. cmSystemTools::ExpandListArgument(additional_clean_files,
  96. this->CleanFiles);
  97. }
  98. // add custom commands to the clean rules?
  99. const char* clean_no_custom =
  100. this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  101. bool clean = cmSystemTools::IsOff(clean_no_custom);
  102. // First generate the object rule files. Save a list of all object
  103. // files for this target.
  104. const std::vector<cmSourceFile*>& sources = this->Target->GetSourceFiles();
  105. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  106. source != sources.end(); ++source)
  107. {
  108. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  109. {
  110. this->GenerateCustomRuleFile(*cc);
  111. if (clean)
  112. {
  113. const std::vector<std::string>& outputs = cc->GetOutputs();
  114. for(std::vector<std::string>::const_iterator o = outputs.begin();
  115. o != outputs.end(); ++o)
  116. {
  117. this->CleanFiles.push_back
  118. (this->Convert(o->c_str(),
  119. cmLocalGenerator::START_OUTPUT,
  120. cmLocalGenerator::UNCHANGED));
  121. }
  122. }
  123. }
  124. else if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  125. {
  126. if(!this->GlobalGenerator->IgnoreFile
  127. ((*source)->GetSourceExtension().c_str()))
  128. {
  129. // Generate this object file's rule file.
  130. this->WriteObjectRuleFiles(*(*source));
  131. }
  132. else if((*source)->GetPropertyAsBool("EXTERNAL_OBJECT"))
  133. {
  134. // This is an external object file. Just add it.
  135. this->ExternalObjects.push_back((*source)->GetFullPath());
  136. }
  137. else
  138. {
  139. // We only get here if a source file is not an external object
  140. // and has an extension that is listed as an ignored file type
  141. // for this language. No message or diagnosis should be
  142. // given.
  143. }
  144. }
  145. }
  146. }
  147. //----------------------------------------------------------------------------
  148. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  149. {
  150. // Include the dependencies for the target.
  151. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  152. dependFileNameFull += "/depend.make";
  153. *this->BuildFileStream
  154. << "# Include any dependencies generated for this target.\n"
  155. << this->LocalGenerator->IncludeDirective << " "
  156. << this->Convert(dependFileNameFull.c_str(),
  157. cmLocalGenerator::HOME_OUTPUT,
  158. cmLocalGenerator::MAKEFILE)
  159. << "\n\n";
  160. // make sure the depend file exists
  161. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  162. {
  163. // Write an empty dependency file.
  164. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  165. depFileStream
  166. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  167. << "# This may be replaced when dependencies are built." << std::endl;
  168. }
  169. // Open the flags file. This should be copy-if-different because the
  170. // rules may depend on this file itself.
  171. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  172. this->FlagFileNameFull += "/flags.make";
  173. this->FlagFileStream =
  174. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  175. this->FlagFileStream->SetCopyIfDifferent(true);
  176. if(!this->FlagFileStream)
  177. {
  178. return;
  179. }
  180. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  181. // Include the flags for the target.
  182. *this->BuildFileStream
  183. << "# Include the compile flags for this target's objects.\n"
  184. << this->LocalGenerator->IncludeDirective << " "
  185. << this->Convert(this->FlagFileNameFull.c_str(),
  186. cmLocalGenerator::HOME_OUTPUT,
  187. cmLocalGenerator::MAKEFILE)
  188. << "\n\n";
  189. }
  190. //----------------------------------------------------------------------------
  191. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  192. {
  193. // write language flags for target
  194. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  195. checkSet =
  196. this->LocalGenerator->GetIntegrityCheckSet()[this->Target->GetName()];
  197. for(std::map<cmStdString,
  198. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  199. l = checkSet.begin(); l != checkSet.end(); ++l)
  200. {
  201. const char *lang = l->first.c_str();
  202. std::string flags;
  203. // Add the export symbol definition for shared library objects.
  204. bool shared = ((this->Target->GetType() == cmTarget::SHARED_LIBRARY) ||
  205. (this->Target->GetType() == cmTarget::MODULE_LIBRARY));
  206. if(shared)
  207. {
  208. flags += "-D";
  209. if(const char* custom_export_name =
  210. this->Target->GetProperty("DEFINE_SYMBOL"))
  211. {
  212. flags += custom_export_name;
  213. }
  214. else
  215. {
  216. std::string in = this->Target->GetName();
  217. in += "_EXPORTS";
  218. flags += cmSystemTools::MakeCindentifier(in.c_str());
  219. }
  220. }
  221. // Add language-specific flags.
  222. this->LocalGenerator
  223. ->AddLanguageFlags(flags, lang,
  224. this->LocalGenerator->ConfigurationName.c_str());
  225. // Add shared-library flags if needed.
  226. this->LocalGenerator->AddSharedFlags(flags, lang, shared);
  227. // Add include directory flags.
  228. this->LocalGenerator->
  229. AppendFlags(flags, this->LocalGenerator->GetIncludeFlags(lang));
  230. // Add include directory flags.
  231. this->LocalGenerator->
  232. AppendFlags(flags,this->GetFrameworkFlags().c_str());
  233. *this->FlagFileStream << lang << "_FLAGS = " << flags << "\n\n";
  234. }
  235. }
  236. //----------------------------------------------------------------------------
  237. void cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile& source)
  238. {
  239. // Identify the language of the source file.
  240. const char* lang = this->LocalGenerator->GetSourceFileLanguage(source);
  241. if(!lang)
  242. {
  243. // don't know anything about this file so skip it
  244. return;
  245. }
  246. // Get the full path name of the object file.
  247. std::string objNoTargetDir;
  248. std::string obj =
  249. this->LocalGenerator->GetObjectFileName(*this->Target, source,
  250. &objNoTargetDir);
  251. // Avoid generating duplicate rules.
  252. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  253. {
  254. this->ObjectFiles.insert(obj);
  255. }
  256. else
  257. {
  258. cmOStringStream err;
  259. err << "Warning: Source file \""
  260. << source.GetSourceName().c_str() << "."
  261. << source.GetSourceExtension().c_str()
  262. << "\" is listed multiple times for target \""
  263. << this->Target->GetName()
  264. << "\".";
  265. cmSystemTools::Message(err.str().c_str(), "Warning");
  266. return;
  267. }
  268. // Create the directory containing the object file. This may be a
  269. // subdirectory under the target's directory.
  270. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  271. cmSystemTools::MakeDirectory
  272. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  273. // Save this in the target's list of object files.
  274. if ( source.GetPropertyAsBool("EXTRA_CONTENT") )
  275. {
  276. this->ExtraContent.insert(obj);
  277. }
  278. this->Objects.push_back(obj);
  279. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  280. relativeObj += obj;
  281. // we compute some depends when writing the depend.make that we will also
  282. // use in the build.make, same with depMakeFile
  283. std::vector<std::string> depends;
  284. std::string depMakeFile;
  285. // generate the build rule file
  286. this->WriteObjectBuildFile(obj, lang, source, depends);
  287. // The object file should be checked for dependency integrity.
  288. this->LocalGenerator->
  289. CheckDependFiles[this->Target->GetName()][lang].insert(&source);
  290. // add this to the list of objects for this local generator
  291. if(cmSystemTools::FileIsFullPath(objNoTargetDir.c_str()))
  292. {
  293. objNoTargetDir = cmSystemTools::GetFilenameName(objNoTargetDir);
  294. }
  295. this->LocalGenerator->LocalObjectFiles[objNoTargetDir].
  296. push_back(this->Target);
  297. }
  298. //----------------------------------------------------------------------------
  299. void
  300. cmMakefileTargetGenerator
  301. ::WriteObjectBuildFile(std::string &obj,
  302. const char *lang,
  303. cmSourceFile& source,
  304. std::vector<std::string>& depends)
  305. {
  306. this->LocalGenerator->AppendRuleDepend(depends,
  307. this->FlagFileNameFull.c_str());
  308. // generate the depend scanning rule
  309. this->WriteObjectDependRules(source, depends);
  310. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  311. relativeObj += obj;
  312. if(this->Makefile->GetDefinition("CMAKE_WINDOWS_OBJECT_PATH"))
  313. {
  314. relativeObj = cmSystemTools::ConvertToOutputPath(relativeObj.c_str());
  315. }
  316. // Write the build rule.
  317. // Build the set of compiler flags.
  318. std::string flags;
  319. // Add language-specific flags.
  320. std::string langFlags = "$(";
  321. langFlags += lang;
  322. langFlags += "_FLAGS)";
  323. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  324. // Add target-specific flags.
  325. if(this->Target->GetProperty("COMPILE_FLAGS"))
  326. {
  327. this->LocalGenerator->AppendFlags
  328. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  329. }
  330. // Add flags from source file properties.
  331. if (source.GetProperty("COMPILE_FLAGS"))
  332. {
  333. this->LocalGenerator->AppendFlags
  334. (flags, source.GetProperty("COMPILE_FLAGS"));
  335. *this->FlagFileStream << "# Custom flags: "
  336. << relativeObj << "_FLAGS = "
  337. << source.GetProperty("COMPILE_FLAGS")
  338. << "\n"
  339. << "\n";
  340. }
  341. // Get the output paths for source and object files.
  342. std::string sourceFile = source.GetFullPath();
  343. if(this->LocalGenerator->UseRelativePaths)
  344. {
  345. sourceFile = this->Convert(sourceFile.c_str(),
  346. cmLocalGenerator::HOME_OUTPUT);
  347. }
  348. sourceFile = this->Convert(sourceFile.c_str(),
  349. cmLocalGenerator::NONE,
  350. cmLocalGenerator::SHELL);
  351. std::string objectFile = this->Convert(obj.c_str(),
  352. cmLocalGenerator::START_OUTPUT,
  353. cmLocalGenerator::SHELL);
  354. // Construct the build message.
  355. std::vector<std::string> no_commands;
  356. std::vector<std::string> commands;
  357. std::string buildEcho = "Building ";
  358. buildEcho += lang;
  359. buildEcho += " object ";
  360. buildEcho += relativeObj;
  361. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  362. cmLocalUnixMakefileGenerator3::EchoBuild);
  363. // add in a progress call if needed
  364. cmGlobalUnixMakefileGenerator3* gg =
  365. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  366. int prog = gg->ShouldAddProgressRule();
  367. if (prog)
  368. {
  369. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  370. progressDir += "/CMakeFiles";
  371. cmOStringStream progCmd;
  372. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  373. progCmd << this->LocalGenerator->Convert(progressDir.c_str(),
  374. cmLocalGenerator::FULL,
  375. cmLocalGenerator::SHELL);
  376. progCmd << " " << prog;
  377. commands.push_back(progCmd.str());
  378. this->LocalGenerator->ProgressFiles[this->Target->GetName()].
  379. push_back(prog);
  380. }
  381. // Construct the compile rules.
  382. std::string compileRuleVar = "CMAKE_";
  383. compileRuleVar += lang;
  384. compileRuleVar += "_COMPILE_OBJECT";
  385. std::string compileRule =
  386. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  387. cmSystemTools::ExpandListArgument(compileRule, commands);
  388. std::string outpath = this->Makefile->GetStartOutputDirectory();
  389. outpath += "/";
  390. outpath += this->Target->GetName();
  391. outpath += ".pdb";
  392. outpath = this->Convert(outpath.c_str(), cmLocalGenerator::FULL,
  393. cmLocalGenerator::MAKEFILE);
  394. cmLocalGenerator::RuleVariables vars;
  395. vars.Language = lang;
  396. vars.TargetPDB = outpath.c_str();
  397. vars.Source = sourceFile.c_str();
  398. vars.Object = relativeObj.c_str();
  399. std::string objdir = this->LocalGenerator->GetHomeRelativeOutputPath();
  400. objdir = this->Convert(objdir.c_str(),
  401. cmLocalGenerator::START_OUTPUT,
  402. cmLocalGenerator::SHELL);
  403. std::string objectDir = cmSystemTools::GetFilenamePath(obj);
  404. vars.ObjectDir = objectDir.c_str();
  405. vars.Flags = flags.c_str();
  406. // Expand placeholders in the commands.
  407. for(std::vector<std::string>::iterator i = commands.begin();
  408. i != commands.end(); ++i)
  409. {
  410. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  411. }
  412. // Make the target dependency scanning rule include cmake-time-known
  413. // dependencies. The others are handled by the check-build-system
  414. // path.
  415. std::string depMark =
  416. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  417. depMark += "/depend.make.mark";
  418. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  419. depMark.c_str(),
  420. depends, no_commands, false);
  421. // Write the rule.
  422. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  423. relativeObj.c_str(),
  424. depends, commands, false);
  425. // If the language needs provides-requires mode, create the
  426. // corresponding targets.
  427. std::string objectRequires = relativeObj;
  428. objectRequires += ".requires";
  429. std::vector<std::string> p_depends;
  430. // always provide an empty requires target
  431. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  432. objectRequires.c_str(), p_depends,
  433. no_commands, true);
  434. // write a build rule to recursively build what this obj provides
  435. std::string objectProvides = relativeObj;
  436. objectProvides += ".provides";
  437. std::string temp = relativeObj;
  438. temp += ".provides.build";
  439. std::vector<std::string> r_commands;
  440. std::string tgtMakefileName =
  441. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  442. tgtMakefileName += "/build.make";
  443. r_commands.push_back
  444. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  445. temp.c_str()));
  446. p_depends.clear();
  447. p_depends.push_back(objectRequires);
  448. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  449. objectProvides.c_str(), p_depends,
  450. r_commands, true);
  451. // write the provides.build rule dependency on the obj file
  452. p_depends.clear();
  453. p_depends.push_back(relativeObj);
  454. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  455. temp.c_str(), p_depends, no_commands,
  456. true);
  457. }
  458. //----------------------------------------------------------------------------
  459. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  460. {
  461. std::vector<std::string> depends;
  462. std::vector<std::string> no_commands;
  463. // Construct the name of the dependency generation target.
  464. std::string depTarget =
  465. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  466. depTarget += "/requires";
  467. // This target drives dependency generation for all object files.
  468. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  469. std::string objTarget;
  470. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  471. obj != this->Objects.end(); ++obj)
  472. {
  473. objTarget = relPath;
  474. objTarget += *obj;
  475. objTarget += ".requires";
  476. depends.push_back(objTarget);
  477. }
  478. // Write the rule.
  479. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  480. depTarget.c_str(),
  481. depends, no_commands, true);
  482. }
  483. //----------------------------------------------------------------------------
  484. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  485. {
  486. std::vector<std::string> depends;
  487. std::vector<std::string> commands;
  488. // Construct the clean target name.
  489. std::string cleanTarget =
  490. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  491. cleanTarget += "/clean";
  492. // Construct the clean command.
  493. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  494. *this->Target);
  495. this->LocalGenerator->CreateCDCommand
  496. (commands,
  497. this->Makefile->GetStartOutputDirectory(),
  498. this->Makefile->GetHomeOutputDirectory());
  499. // Write the rule.
  500. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  501. cleanTarget.c_str(),
  502. depends, commands, true);
  503. }
  504. //----------------------------------------------------------------------------
  505. void cmMakefileTargetGenerator::WriteTargetDependRules()
  506. {
  507. // must write the targets depend info file
  508. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  509. this->InfoFileNameFull = dir;
  510. this->InfoFileNameFull += "/DependInfo.cmake";
  511. this->InfoFileNameFull =
  512. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  513. this->InfoFileStream =
  514. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  515. this->InfoFileStream->SetCopyIfDifferent(true);
  516. if(!*this->InfoFileStream)
  517. {
  518. return;
  519. }
  520. this->LocalGenerator->
  521. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  522. // and now write the rule to use it
  523. std::vector<std::string> depends;
  524. std::vector<std::string> commands;
  525. // Construct the name of the dependency generation target.
  526. std::string depTarget =
  527. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  528. depTarget += "/depend";
  529. std::string depMark = depTarget;
  530. depMark += ".make.mark";
  531. depends.push_back(depMark);
  532. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  533. depTarget.c_str(),
  534. depends, commands, true);
  535. depends.clear();
  536. // Write the dependency generation rule.
  537. std::string depEcho = "Scanning dependencies of target ";
  538. depEcho += this->Target->GetName();
  539. this->LocalGenerator->AppendEcho(commands, depEcho.c_str(),
  540. cmLocalUnixMakefileGenerator3::EchoDepend);
  541. // Add a command to call CMake to scan dependencies. CMake will
  542. // touch the corresponding depends file after scanning dependencies.
  543. cmOStringStream depCmd;
  544. // TODO: Account for source file properties and directory-level
  545. // definitions when scanning for dependencies.
  546. #if !defined(_WIN32) || defined(__CYGWIN__)
  547. // This platform supports symlinks, so cmSystemTools will translate
  548. // paths. Make sure PWD is set to the original name of the home
  549. // output directory to help cmSystemTools to create the same
  550. // translation table for the dependency scanning process.
  551. depCmd << "cd "
  552. << (this->LocalGenerator->Convert(
  553. this->Makefile->GetHomeOutputDirectory(),
  554. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  555. << " && ";
  556. #endif
  557. // Generate a call this signature:
  558. //
  559. // cmake -E cmake_depends <generator>
  560. // <home-src-dir> <start-src-dir>
  561. // <home-out-dir> <start-out-dir>
  562. // <dep-info>
  563. //
  564. // This gives the dependency scanner enough information to recreate
  565. // the state of our local generator sufficiently for its needs.
  566. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  567. << this->GlobalGenerator->GetName() << "\" "
  568. << this->Convert(this->Makefile->GetHomeDirectory(),
  569. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  570. << " "
  571. << this->Convert(this->Makefile->GetStartDirectory(),
  572. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  573. << " "
  574. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  575. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  576. << " "
  577. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  578. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  579. << " "
  580. << this->Convert(this->InfoFileNameFull.c_str(),
  581. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  582. commands.push_back(depCmd.str());
  583. // Write the rule.
  584. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  585. depMark.c_str(),
  586. depends, commands, false);
  587. }
  588. //----------------------------------------------------------------------------
  589. void cmMakefileTargetGenerator
  590. ::WriteObjectDependRules(cmSourceFile& source,
  591. std::vector<std::string>& depends)
  592. {
  593. // Create the list of dependencies known at cmake time. These are
  594. // shared between the object file and dependency scanning rule.
  595. depends.push_back(source.GetFullPath());
  596. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  597. {
  598. std::vector<std::string> deps;
  599. cmSystemTools::ExpandListArgument(objectDeps, deps);
  600. for(std::vector<std::string>::iterator i = deps.begin();
  601. i != deps.end(); ++i)
  602. {
  603. depends.push_back(i->c_str());
  604. }
  605. }
  606. }
  607. //----------------------------------------------------------------------------
  608. void cmMakefileTargetGenerator
  609. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  610. {
  611. // Collect the commands.
  612. std::vector<std::string> commands;
  613. std::string comment = this->LocalGenerator->ConstructComment(cc);
  614. if(!comment.empty())
  615. {
  616. this->LocalGenerator
  617. ->AppendEcho(commands, comment.c_str(),
  618. cmLocalUnixMakefileGenerator3::EchoGenerate);
  619. }
  620. this->LocalGenerator->AppendCustomCommand(commands, cc);
  621. // Collect the dependencies.
  622. std::vector<std::string> depends;
  623. this->LocalGenerator->AppendCustomDepend(depends, cc);
  624. // Write the rule.
  625. const std::vector<std::string>& outputs = cc.GetOutputs();
  626. std::vector<std::string>::const_iterator o = outputs.begin();
  627. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  628. o->c_str(), depends, commands,
  629. false);
  630. // If the rule has multiple outputs, add a rule for the extra
  631. // outputs to just depend on the first output with no command. Also
  632. // register the extra outputs as paired with the first output so
  633. // that the check-build-system step will remove the primary output
  634. // if any extra outputs are missing, forcing the rule to regenerate
  635. // all outputs.
  636. depends.clear();
  637. depends.push_back(*o);
  638. commands.clear();
  639. cmGlobalUnixMakefileGenerator3* gg =
  640. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  641. std::string emptyCommand = gg->GetEmptyCommandHack();
  642. if(!emptyCommand.empty())
  643. {
  644. commands.push_back(emptyCommand);
  645. }
  646. for(++o; o != outputs.end(); ++o)
  647. {
  648. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  649. o->c_str(), depends, commands,
  650. false);
  651. gg->AddMultipleOutputPair(o->c_str(), depends[0].c_str());
  652. }
  653. }
  654. //----------------------------------------------------------------------------
  655. void
  656. cmMakefileTargetGenerator
  657. ::WriteObjectsVariable(std::string& variableName,
  658. std::string& variableNameExternal)
  659. {
  660. // Write a make variable assignment that lists all objects for the
  661. // target.
  662. variableName =
  663. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  664. "_OBJECTS");
  665. *this->BuildFileStream
  666. << "# Object files for target " << this->Target->GetName() << "\n"
  667. << variableName.c_str() << " =";
  668. std::string object;
  669. const char* objName =
  670. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  671. const char* lineContinue =
  672. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  673. if(!lineContinue)
  674. {
  675. lineContinue = "\\";
  676. }
  677. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  678. i != this->Objects.end(); ++i)
  679. {
  680. if ( this->ExtraContent.find(i->c_str()) != this->ExtraContent.end() )
  681. {
  682. continue;
  683. }
  684. *this->BuildFileStream << " " << lineContinue << "\n";
  685. if(objName)
  686. {
  687. *this->BuildFileStream <<
  688. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  689. cmLocalGenerator::MAKEFILE);
  690. }
  691. else
  692. {
  693. *this->BuildFileStream <<
  694. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  695. }
  696. }
  697. *this->BuildFileStream << "\n";
  698. // Write a make variable assignment that lists all external objects
  699. // for the target.
  700. variableNameExternal =
  701. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  702. "_EXTERNAL_OBJECTS");
  703. *this->BuildFileStream
  704. << "\n"
  705. << "# External object files for target "
  706. << this->Target->GetName() << "\n"
  707. << variableNameExternal.c_str() << " =";
  708. for(std::vector<std::string>::const_iterator i =
  709. this->ExternalObjects.begin();
  710. i != this->ExternalObjects.end(); ++i)
  711. {
  712. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  713. *this->BuildFileStream
  714. << " " << lineContinue << "\n"
  715. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  716. if(objName)
  717. {
  718. *this->BuildFileStream <<
  719. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  720. cmLocalGenerator::MAKEFILE);
  721. }
  722. else
  723. {
  724. *this->BuildFileStream <<
  725. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  726. }
  727. }
  728. *this->BuildFileStream << "\n" << "\n";
  729. }
  730. //----------------------------------------------------------------------------
  731. void cmMakefileTargetGenerator::WriteTargetDriverRule(const char* main_output,
  732. bool relink)
  733. {
  734. // Compute the name of the driver target.
  735. std::string dir = this->Makefile->GetStartOutputDirectory();
  736. dir += "/";
  737. dir += this->LocalGenerator->GetTargetDirectory(*this->Target);
  738. std::string buildTargetRuleName = dir;
  739. buildTargetRuleName += relink?"/preinstall":"/build";
  740. buildTargetRuleName = this->Convert(buildTargetRuleName.c_str(),
  741. cmLocalGenerator::HOME_OUTPUT,
  742. cmLocalGenerator::MAKEFILE);
  743. // Build the list of target outputs to drive.
  744. std::vector<std::string> depends;
  745. if(main_output)
  746. {
  747. depends.push_back(main_output);
  748. }
  749. const char* comment = 0;
  750. if(relink)
  751. {
  752. // Setup the comment for the preinstall driver.
  753. comment = "Rule to relink during preinstall.";
  754. }
  755. else
  756. {
  757. // Setup the comment for the main build driver.
  758. comment = "Rule to build all files generated by this target.";
  759. // Make sure all custom command outputs in this target are built.
  760. const std::vector<cmSourceFile*>& sources = this->Target->GetSourceFiles();
  761. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  762. source != sources.end(); ++source)
  763. {
  764. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  765. {
  766. const std::vector<std::string>& outputs = cc->GetOutputs();
  767. for(std::vector<std::string>::const_iterator o = outputs.begin();
  768. o != outputs.end(); ++o)
  769. {
  770. depends.push_back(*o);
  771. }
  772. }
  773. }
  774. }
  775. // Write the driver rule.
  776. std::vector<std::string> no_commands;
  777. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  778. buildTargetRuleName.c_str(),
  779. depends, no_commands, true);
  780. }
  781. //----------------------------------------------------------------------------
  782. std::string cmMakefileTargetGenerator::GetFrameworkFlags()
  783. {
  784. #ifndef __APPLE__
  785. return std::string();
  786. #else
  787. std::set<cmStdString> emitted;
  788. std::vector<std::string> includes;
  789. this->LocalGenerator->GetIncludeDirectories(includes);
  790. std::vector<std::string>::iterator i;
  791. // check all include directories for frameworks as this
  792. // will already have added a -F for the framework
  793. for(i = includes.begin(); i != includes.end(); ++i)
  794. {
  795. if(cmSystemTools::IsPathToFramework(i->c_str()))
  796. {
  797. std::string frameworkDir = *i;
  798. frameworkDir += "/../";
  799. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  800. emitted.insert(frameworkDir);
  801. }
  802. }
  803. std::string flags;
  804. std::vector<std::string>& frameworks = this->Target->GetFrameworks();
  805. for(i = frameworks.begin();
  806. i != frameworks.end(); ++i)
  807. {
  808. if(emitted.insert(*i).second)
  809. {
  810. flags += "-F";
  811. flags += this->LocalGenerator->ConvertToOutputForExisting(i->c_str());
  812. flags += " ";
  813. }
  814. }
  815. return flags;
  816. #endif
  817. }
  818. //----------------------------------------------------------------------------
  819. void cmMakefileTargetGenerator
  820. ::AppendTargetDepends(std::vector<std::string>& depends)
  821. {
  822. // Static libraries never depend on anything for linking.
  823. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  824. {
  825. return;
  826. }
  827. // Keep track of dependencies already listed.
  828. std::set<cmStdString> emitted;
  829. // A target should not depend on itself.
  830. emitted.insert(this->Target->GetName());
  831. // Loop over all library dependencies.
  832. const cmTarget::LinkLibraryVectorType& tlibs =
  833. this->Target->GetLinkLibraries();
  834. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  835. lib != tlibs.end(); ++lib)
  836. {
  837. // Don't emit the same library twice for this target.
  838. if(emitted.insert(lib->first).second)
  839. {
  840. // Depend only on other CMake targets.
  841. if(cmTarget* tgt =
  842. this->GlobalGenerator->FindTarget(0, lib->first.c_str()))
  843. {
  844. if(const char* location =
  845. tgt->GetLocation(this->LocalGenerator->ConfigurationName.c_str()))
  846. {
  847. depends.push_back(location);
  848. }
  849. }
  850. }
  851. }
  852. }
  853. //----------------------------------------------------------------------------
  854. void cmMakefileTargetGenerator
  855. ::CloseFileStreams()
  856. {
  857. delete this->BuildFileStream;
  858. delete this->InfoFileStream;
  859. delete this->FlagFileStream;
  860. }
  861. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  862. const char* linkLang,
  863. std::string& linkFlags)
  864. {
  865. // check for language flags that are not allowed at link time, and
  866. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  867. // which fails, there may be more]
  868. std::string removeFlags = "CMAKE_";
  869. removeFlags += linkLang;
  870. removeFlags += flagVar;
  871. std::string removeflags =
  872. this->Makefile->GetSafeDefinition(removeFlags.c_str());
  873. std::vector<std::string> removeList;
  874. cmSystemTools::ExpandListArgument(removeflags, removeList);
  875. for(std::vector<std::string>::iterator i = removeList.begin();
  876. i != removeList.end(); ++i)
  877. {
  878. cmSystemTools::ReplaceString(linkFlags, i->c_str(), "");
  879. }
  880. }