cmMakefileTargetGenerator.cxx 33 KB

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