cmMakefileTargetGenerator.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. // If language is not known, this is an error.
  222. cmSystemTools::Error("Source file \"", source.GetFullPath().c_str(),
  223. "\" has unknown type.");
  224. return;
  225. }
  226. // Get the full path name of the object file.
  227. std::string objNoTargetDir;
  228. std::string obj =
  229. this->LocalGenerator->GetObjectFileName(*this->Target, source, &objNoTargetDir);
  230. // Avoid generating duplicate rules.
  231. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  232. {
  233. this->ObjectFiles.insert(obj);
  234. }
  235. else
  236. {
  237. cmOStringStream err;
  238. err << "Warning: Source file \""
  239. << source.GetSourceName().c_str() << "."
  240. << source.GetSourceExtension().c_str()
  241. << "\" is listed multiple times for target \""
  242. << this->Target->GetName()
  243. << "\".";
  244. cmSystemTools::Message(err.str().c_str(), "Warning");
  245. return;
  246. }
  247. // Create the directory containing the object file. This may be a
  248. // subdirectory under the target's directory.
  249. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  250. cmSystemTools::MakeDirectory
  251. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  252. // Save this in the target's list of object files.
  253. if ( source.GetPropertyAsBool("EXTRA_CONTENT") )
  254. {
  255. this->ExtraContent.insert(obj);
  256. }
  257. this->Objects.push_back(obj);
  258. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  259. relativeObj += obj;
  260. // we compute some depends when writing the depend.make that we will also
  261. // use in the build.make, same with depMakeFile
  262. std::vector<std::string> depends;
  263. std::string depMakeFile;
  264. // generate the build rule file
  265. this->WriteObjectBuildFile(obj, lang, source, depends);
  266. // The object file should be checked for dependency integrity.
  267. this->LocalGenerator->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].push_back(this->Target);
  274. }
  275. //----------------------------------------------------------------------------
  276. void
  277. cmMakefileTargetGenerator
  278. ::WriteObjectBuildFile(std::string &obj,
  279. const char *lang,
  280. cmSourceFile& source,
  281. std::vector<std::string>& depends)
  282. {
  283. this->LocalGenerator->AppendRuleDepend(depends, this->FlagFileNameFull.c_str());
  284. // generate the depend scanning rule
  285. this->WriteObjectDependRules(source, depends);
  286. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  287. relativeObj += obj;
  288. if(this->Makefile->GetDefinition("CMAKE_WINDOWS_OBJECT_PATH"))
  289. {
  290. relativeObj = cmSystemTools::ConvertToOutputPath(relativeObj.c_str());
  291. }
  292. // Write the build rule.
  293. // Build the set of compiler flags.
  294. std::string flags;
  295. // Add language-specific flags.
  296. std::string langFlags = "$(";
  297. langFlags += lang;
  298. langFlags += "_FLAGS)";
  299. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  300. // Add target-specific flags.
  301. if(this->Target->GetProperty("COMPILE_FLAGS"))
  302. {
  303. this->LocalGenerator->AppendFlags(flags, this->Target->GetProperty("COMPILE_FLAGS"));
  304. }
  305. // Add flags from source file properties.
  306. if (source.GetProperty("COMPILE_FLAGS"))
  307. {
  308. this->LocalGenerator->AppendFlags(flags, source.GetProperty("COMPILE_FLAGS"));
  309. *this->FlagFileStream << "# Custom flags: "
  310. << relativeObj << "_FLAGS = "
  311. << source.GetProperty("COMPILE_FLAGS")
  312. << "\n"
  313. << "\n";
  314. }
  315. // Get the output paths for source and object files.
  316. std::string sourceFile = source.GetFullPath();
  317. if(this->LocalGenerator->UseRelativePaths)
  318. {
  319. sourceFile = this->Convert(sourceFile.c_str(),
  320. cmLocalGenerator::HOME_OUTPUT);
  321. }
  322. sourceFile = this->Convert(sourceFile.c_str(),
  323. cmLocalGenerator::NONE,
  324. cmLocalGenerator::SHELL);
  325. std::string objectFile =
  326. this->Convert(obj.c_str(),
  327. cmLocalGenerator::START_OUTPUT,
  328. cmLocalGenerator::SHELL);
  329. // Construct the build message.
  330. std::vector<std::string> no_commands;
  331. std::vector<std::string> commands;
  332. std::string buildEcho = "Building ";
  333. buildEcho += lang;
  334. buildEcho += " object ";
  335. buildEcho += relativeObj;
  336. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  337. cmLocalUnixMakefileGenerator3::EchoBuild);
  338. // Construct the compile rules.
  339. std::string compileRuleVar = "CMAKE_";
  340. compileRuleVar += lang;
  341. compileRuleVar += "_COMPILE_OBJECT";
  342. std::string compileRule =
  343. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  344. cmSystemTools::ExpandListArgument(compileRule, commands);
  345. std::string outpath = this->Makefile->GetStartOutputDirectory();
  346. outpath += "/";
  347. outpath += this->Target->GetName();
  348. outpath += ".pdb";
  349. outpath = this->Convert(outpath.c_str(), cmLocalGenerator::FULL,
  350. cmLocalGenerator::MAKEFILE);
  351. cmLocalGenerator::RuleVariables vars;
  352. vars.Language = lang;
  353. vars.TargetPDB = outpath.c_str();
  354. vars.Source = sourceFile.c_str();
  355. vars.Object = relativeObj.c_str();
  356. std::string objdir = this->LocalGenerator->GetHomeRelativeOutputPath();
  357. objdir = this->Convert(objdir.c_str(),
  358. cmLocalGenerator::START_OUTPUT,
  359. cmLocalGenerator::SHELL);
  360. std::string objectDir = cmSystemTools::GetFilenamePath(obj);
  361. vars.ObjectDir = objectDir.c_str();
  362. vars.Flags = flags.c_str();
  363. // Expand placeholders in the commands.
  364. for(std::vector<std::string>::iterator i = commands.begin();
  365. i != commands.end(); ++i)
  366. {
  367. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  368. }
  369. // Make the target dependency scanning rule include cmake-time-known
  370. // dependencies. The others are handled by the check-build-system
  371. // path.
  372. std::string depMark =
  373. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  374. depMark += "/depend.make.mark";
  375. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  376. depMark.c_str(),
  377. depends, no_commands, false);
  378. // Write the rule.
  379. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  380. relativeObj.c_str(),
  381. depends, commands, false);
  382. // If the language needs provides-requires mode, create the
  383. // corresponding targets.
  384. std::string objectRequires = relativeObj;
  385. objectRequires += ".requires";
  386. std::vector<std::string> p_depends;
  387. // always provide an empty requires target
  388. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  389. objectRequires.c_str(), p_depends,
  390. no_commands, true);
  391. // write a build rule to recursively build what this obj provides
  392. std::string objectProvides = relativeObj;
  393. objectProvides += ".provides";
  394. std::string temp = relativeObj;
  395. temp += ".provides.build";
  396. std::vector<std::string> r_commands;
  397. std::string tgtMakefileName =
  398. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  399. tgtMakefileName += "/build.make";
  400. r_commands.push_back
  401. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),temp.c_str()));
  402. p_depends.clear();
  403. p_depends.push_back(objectRequires);
  404. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  405. objectProvides.c_str(), p_depends,
  406. r_commands, true);
  407. // write the provides.build rule dependency on the obj file
  408. p_depends.clear();
  409. p_depends.push_back(relativeObj);
  410. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  411. temp.c_str(), p_depends, no_commands,
  412. true);
  413. }
  414. //----------------------------------------------------------------------------
  415. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  416. {
  417. std::vector<std::string> depends;
  418. std::vector<std::string> no_commands;
  419. // Construct the name of the dependency generation target.
  420. std::string depTarget =
  421. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  422. depTarget += "/requires";
  423. // This target drives dependency generation for all object files.
  424. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  425. std::string objTarget;
  426. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  427. obj != this->Objects.end(); ++obj)
  428. {
  429. objTarget = relPath;
  430. objTarget += *obj;
  431. objTarget += ".requires";
  432. depends.push_back(objTarget);
  433. }
  434. // Write the rule.
  435. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  436. depTarget.c_str(),
  437. depends, no_commands, true);
  438. }
  439. //----------------------------------------------------------------------------
  440. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  441. {
  442. std::vector<std::string> depends;
  443. std::vector<std::string> commands;
  444. // Construct the clean target name.
  445. std::string cleanTarget =
  446. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  447. cleanTarget += "/clean";
  448. // Construct the clean command.
  449. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  450. *this->Target);
  451. this->LocalGenerator->CreateCDCommand(commands,
  452. this->Makefile->GetStartOutputDirectory(),
  453. this->Makefile->GetHomeOutputDirectory());
  454. // Write the rule.
  455. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  456. cleanTarget.c_str(),
  457. depends, commands, true);
  458. }
  459. //----------------------------------------------------------------------------
  460. void cmMakefileTargetGenerator::WriteTargetDependRules()
  461. {
  462. // must write the targets depend info file
  463. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  464. this->InfoFileNameFull = dir;
  465. this->InfoFileNameFull += "/DependInfo.cmake";
  466. this->InfoFileNameFull =
  467. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  468. this->InfoFileStream =
  469. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  470. this->InfoFileStream->SetCopyIfDifferent(true);
  471. if(!*this->InfoFileStream)
  472. {
  473. return;
  474. }
  475. this->LocalGenerator->
  476. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  477. // and now write the rule to use it
  478. std::vector<std::string> depends;
  479. std::vector<std::string> commands;
  480. // Construct the name of the dependency generation target.
  481. std::string depTarget =
  482. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  483. depTarget += "/depend";
  484. std::string depMark = depTarget;
  485. depMark += ".make.mark";
  486. depends.push_back(depMark);
  487. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  488. depTarget.c_str(),
  489. depends, commands, true);
  490. depends.clear();
  491. // Write the dependency generation rule.
  492. std::string depEcho = "Scanning dependencies of target ";
  493. depEcho += this->Target->GetName();
  494. this->LocalGenerator->AppendEcho(commands, depEcho.c_str(),
  495. cmLocalUnixMakefileGenerator3::EchoDepend);
  496. // Add a command to call CMake to scan dependencies. CMake will
  497. // touch the corresponding depends file after scanning dependencies.
  498. cmOStringStream depCmd;
  499. // TODO: Account for source file properties and directory-level
  500. // definitions when scanning for dependencies.
  501. depCmd << "$(CMAKE_COMMAND) -E cmake_depends "
  502. << " \""
  503. << this->GlobalGenerator->GetName() << "\" "
  504. << this->LocalGenerator->Convert
  505. (this->Makefile->GetHomeOutputDirectory(),
  506. cmLocalGenerator::FULL,cmLocalGenerator::SHELL)
  507. << " "
  508. << this->LocalGenerator->Convert
  509. (this->Makefile->GetStartOutputDirectory(),
  510. cmLocalGenerator::FULL,cmLocalGenerator::SHELL)
  511. << " "
  512. << this->Convert(this->InfoFileNameFull.c_str(),
  513. cmLocalGenerator::FULL,
  514. cmLocalGenerator::SHELL);
  515. commands.push_back(depCmd.str());
  516. // Write the rule.
  517. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  518. depMark.c_str(),
  519. depends, commands, false);
  520. }
  521. //----------------------------------------------------------------------------
  522. void cmMakefileTargetGenerator
  523. ::WriteObjectDependRules(cmSourceFile& source,
  524. std::vector<std::string>& depends)
  525. {
  526. // Create the list of dependencies known at cmake time. These are
  527. // shared between the object file and dependency scanning rule.
  528. depends.push_back(source.GetFullPath());
  529. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  530. {
  531. std::vector<std::string> deps;
  532. cmSystemTools::ExpandListArgument(objectDeps, deps);
  533. for(std::vector<std::string>::iterator i = deps.begin();
  534. i != deps.end(); ++i)
  535. {
  536. depends.push_back(i->c_str());
  537. }
  538. }
  539. }
  540. //----------------------------------------------------------------------------
  541. void cmMakefileTargetGenerator::WriteCustomCommands()
  542. {
  543. // add custom commands to the clean rules?
  544. const char* clean_no_custom = this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  545. bool clean = cmSystemTools::IsOff(clean_no_custom);
  546. // Generate the rule files for each custom command.
  547. const std::vector<cmSourceFile*> &classes = this->Makefile->GetSourceFiles();
  548. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  549. i != classes.end(); i++)
  550. {
  551. if(cmCustomCommand* cc = (*i)->GetCustomCommand())
  552. {
  553. this->GenerateCustomRuleFile(*cc);
  554. if (clean)
  555. {
  556. const std::vector<std::string>& outputs = cc->GetOutputs();
  557. for(std::vector<std::string>::const_iterator o = outputs.begin();
  558. o != outputs.end(); ++o)
  559. {
  560. this->CleanFiles.push_back
  561. (this->Convert(o->c_str(),
  562. cmLocalGenerator::START_OUTPUT,
  563. cmLocalGenerator::UNCHANGED));
  564. }
  565. }
  566. }
  567. }
  568. }
  569. //----------------------------------------------------------------------------
  570. void cmMakefileTargetGenerator
  571. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  572. {
  573. // Collect the commands.
  574. std::vector<std::string> commands;
  575. std::string comment = this->LocalGenerator->ConstructComment(cc);
  576. if(!comment.empty())
  577. {
  578. this->LocalGenerator
  579. ->AppendEcho(commands, comment.c_str(),
  580. cmLocalUnixMakefileGenerator3::EchoGenerate);
  581. }
  582. this->LocalGenerator->AppendCustomCommand(commands, cc);
  583. // Collect the dependencies.
  584. std::vector<std::string> depends;
  585. this->LocalGenerator->AppendCustomDepend(depends, cc);
  586. // Write the rule.
  587. const std::vector<std::string>& outputs = cc.GetOutputs();
  588. std::vector<std::string>::const_iterator o = outputs.begin();
  589. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  590. o->c_str(), depends, commands,
  591. false);
  592. // If the rule has multiple outputs, add a rule for the extra
  593. // outputs to just depend on the first output with no command. Also
  594. // register the extra outputs as paired with the first output so
  595. // that the check-build-system step will remove the primary output
  596. // if any extra outputs are missing, forcing the rule to regenerate
  597. // all outputs.
  598. depends.clear();
  599. depends.push_back(*o);
  600. commands.clear();
  601. cmGlobalUnixMakefileGenerator3* gg =
  602. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  603. std::string emptyCommand = gg->GetEmptyCommandHack();
  604. if(!emptyCommand.empty())
  605. {
  606. commands.push_back(emptyCommand);
  607. }
  608. for(++o; o != outputs.end(); ++o)
  609. {
  610. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  611. o->c_str(), depends, commands,
  612. false);
  613. gg->AddMultipleOutputPair(o->c_str(), depends[0].c_str());
  614. }
  615. }
  616. //----------------------------------------------------------------------------
  617. void
  618. cmMakefileTargetGenerator
  619. ::WriteObjectsVariable(std::string& variableName,
  620. std::string& variableNameExternal)
  621. {
  622. // Write a make variable assignment that lists all objects for the
  623. // target.
  624. variableName =
  625. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(), "_OBJECTS");
  626. *this->BuildFileStream
  627. << "# Object files for target " << this->Target->GetName() << "\n"
  628. << variableName.c_str() << " =";
  629. std::string object;
  630. const char* objName =
  631. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  632. const char* lineContinue =
  633. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  634. if(!lineContinue)
  635. {
  636. lineContinue = "\\";
  637. }
  638. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  639. i != this->Objects.end(); ++i)
  640. {
  641. if ( this->ExtraContent.find(i->c_str()) != this->ExtraContent.end() )
  642. {
  643. continue;
  644. }
  645. *this->BuildFileStream << " " << lineContinue << "\n";
  646. if(objName)
  647. {
  648. *this->BuildFileStream <<
  649. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  650. cmLocalGenerator::MAKEFILE);
  651. }
  652. else
  653. {
  654. *this->BuildFileStream <<
  655. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  656. }
  657. }
  658. *this->BuildFileStream << "\n";
  659. // Write a make variable assignment that lists all external objects
  660. // for the target.
  661. variableNameExternal =
  662. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),"_EXTERNAL_OBJECTS");
  663. *this->BuildFileStream
  664. << "\n"
  665. << "# External object files for target " << this->Target->GetName() << "\n"
  666. << variableNameExternal.c_str() << " =";
  667. for(std::vector<std::string>::const_iterator i =
  668. this->ExternalObjects.begin();
  669. i != this->ExternalObjects.end(); ++i)
  670. {
  671. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  672. *this->BuildFileStream
  673. << " " << lineContinue << "\n"
  674. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  675. if(objName)
  676. {
  677. *this->BuildFileStream << this->Convert(i->c_str(),
  678. cmLocalGenerator::START_OUTPUT,
  679. cmLocalGenerator::MAKEFILE);
  680. }
  681. else
  682. {
  683. *this->BuildFileStream <<
  684. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  685. }
  686. }
  687. *this->BuildFileStream << "\n" << "\n";
  688. }
  689. //----------------------------------------------------------------------------
  690. std::string cmMakefileTargetGenerator::GetFrameworkFlags()
  691. {
  692. #ifndef __APPLE__
  693. return std::string();
  694. #else
  695. std::set<cmStdString> emitted;
  696. std::vector<std::string> includes;
  697. this->LocalGenerator->GetIncludeDirectories(includes);
  698. std::vector<std::string>::iterator i;
  699. // check all include directories for frameworks as this
  700. // will already have added a -F for the framework
  701. for(i = includes.begin(); i != includes.end(); ++i)
  702. {
  703. if(cmSystemTools::IsPathToFramework(i->c_str()))
  704. {
  705. std::string frameworkDir = *i;
  706. frameworkDir += "/../";
  707. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  708. emitted.insert(frameworkDir);
  709. }
  710. }
  711. std::string flags;
  712. std::vector<std::string>& frameworks = this->Target->GetFrameworks();
  713. for(i = frameworks.begin();
  714. i != frameworks.end(); ++i)
  715. {
  716. if(emitted.insert(*i).second)
  717. {
  718. flags += "-F";
  719. flags += this->LocalGenerator->ConvertToOutputForExisting(i->c_str());
  720. flags += " ";
  721. }
  722. }
  723. return flags;
  724. #endif
  725. }
  726. //----------------------------------------------------------------------------
  727. void cmMakefileTargetGenerator
  728. ::AppendTargetDepends(std::vector<std::string>& depends)
  729. {
  730. // Static libraries never depend on anything for linking.
  731. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  732. {
  733. return;
  734. }
  735. // Keep track of dependencies already listed.
  736. std::set<cmStdString> emitted;
  737. // A target should not depend on itself.
  738. emitted.insert(this->Target->GetName());
  739. // Loop over all library dependencies.
  740. const cmTarget::LinkLibraryVectorType& tlibs =
  741. this->Target->GetLinkLibraries();
  742. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  743. lib != tlibs.end(); ++lib)
  744. {
  745. // Don't emit the same library twice for this target.
  746. if(emitted.insert(lib->first).second)
  747. {
  748. // Depend only on other CMake targets.
  749. if(cmTarget* tgt =
  750. this->GlobalGenerator->FindTarget(0, lib->first.c_str()))
  751. {
  752. if(const char* location =
  753. tgt->GetLocation(this->LocalGenerator->ConfigurationName.c_str()))
  754. {
  755. depends.push_back(location);
  756. }
  757. }
  758. }
  759. }
  760. }
  761. //----------------------------------------------------------------------------
  762. void cmMakefileTargetGenerator
  763. ::CloseFileStreams()
  764. {
  765. delete this->BuildFileStream;
  766. delete this->InfoFileStream;
  767. delete this->FlagFileStream;
  768. }
  769. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  770. const char* linkLang,
  771. std::string& linkFlags)
  772. {
  773. // check for language flags that are not allowed at link time, and
  774. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  775. // which fails, there may be more]
  776. std::string removeFlags = "CMAKE_";
  777. removeFlags += linkLang;
  778. removeFlags += flagVar;
  779. std::string removeflags =
  780. this->Makefile->GetSafeDefinition(removeFlags.c_str());
  781. std::vector<std::string> removeList;
  782. cmSystemTools::ExpandListArgument(removeflags, removeList);
  783. for(std::vector<std::string>::iterator i = removeList.begin();
  784. i != removeList.end(); ++i)
  785. {
  786. cmSystemTools::ReplaceString(linkFlags, i->c_str(), "");
  787. }
  788. }