cmMakefileTargetGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. cmLocalGenerator::RuleVariables vars;
  346. vars.Language = lang;
  347. vars.Source = sourceFile.c_str();
  348. vars.Object = relativeObj.c_str();
  349. vars.Flags = flags.c_str();
  350. // Expand placeholders in the commands.
  351. for(std::vector<std::string>::iterator i = commands.begin();
  352. i != commands.end(); ++i)
  353. {
  354. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  355. }
  356. // Make the target dependency scanning rule include cmake-time-known
  357. // dependencies. The others are handled by the check-build-system
  358. // path.
  359. std::string depMark =
  360. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  361. depMark += "/depend.make.mark";
  362. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  363. depMark.c_str(),
  364. depends, no_commands, false);
  365. // Write the rule.
  366. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  367. relativeObj.c_str(),
  368. depends, commands, false);
  369. // If the language needs provides-requires mode, create the
  370. // corresponding targets.
  371. std::string objectRequires = relativeObj;
  372. objectRequires += ".requires";
  373. std::vector<std::string> p_depends;
  374. // always provide an empty requires target
  375. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  376. objectRequires.c_str(), p_depends,
  377. no_commands, true);
  378. // write a build rule to recursively build what this obj provides
  379. std::string objectProvides = relativeObj;
  380. objectProvides += ".provides";
  381. std::string temp = relativeObj;
  382. temp += ".provides.build";
  383. std::vector<std::string> r_commands;
  384. std::string tgtMakefileName =
  385. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  386. tgtMakefileName += "/build.make";
  387. r_commands.push_back
  388. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),temp.c_str()));
  389. p_depends.clear();
  390. p_depends.push_back(objectRequires);
  391. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  392. objectProvides.c_str(), p_depends,
  393. r_commands, true);
  394. // write the provides.build rule dependency on the obj file
  395. p_depends.clear();
  396. p_depends.push_back(relativeObj);
  397. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  398. temp.c_str(), p_depends, no_commands,
  399. true);
  400. }
  401. //----------------------------------------------------------------------------
  402. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  403. {
  404. std::vector<std::string> depends;
  405. std::vector<std::string> no_commands;
  406. // Construct the name of the dependency generation target.
  407. std::string depTarget =
  408. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  409. depTarget += "/requires";
  410. // This target drives dependency generation for all object files.
  411. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  412. std::string objTarget;
  413. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  414. obj != this->Objects.end(); ++obj)
  415. {
  416. objTarget = relPath;
  417. objTarget += *obj;
  418. objTarget += ".requires";
  419. depends.push_back(objTarget);
  420. }
  421. // Write the rule.
  422. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  423. depTarget.c_str(),
  424. depends, no_commands, true);
  425. }
  426. //----------------------------------------------------------------------------
  427. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  428. {
  429. std::vector<std::string> depends;
  430. std::vector<std::string> commands;
  431. // Construct the clean target name.
  432. std::string cleanTarget =
  433. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  434. cleanTarget += "/clean";
  435. // Construct the clean command.
  436. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  437. *this->Target);
  438. this->LocalGenerator->CreateCDCommand(commands,
  439. this->Makefile->GetStartOutputDirectory(),
  440. this->Makefile->GetHomeOutputDirectory());
  441. // Write the rule.
  442. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  443. cleanTarget.c_str(),
  444. depends, commands, true);
  445. }
  446. //----------------------------------------------------------------------------
  447. void cmMakefileTargetGenerator::WriteTargetDependRules()
  448. {
  449. // must write the targets depend info file
  450. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  451. this->InfoFileNameFull = dir;
  452. this->InfoFileNameFull += "/DependInfo.cmake";
  453. this->InfoFileNameFull =
  454. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  455. this->InfoFileStream =
  456. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  457. this->InfoFileStream->SetCopyIfDifferent(true);
  458. if(!*this->InfoFileStream)
  459. {
  460. return;
  461. }
  462. this->LocalGenerator->
  463. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  464. // and now write the rule to use it
  465. std::vector<std::string> depends;
  466. std::vector<std::string> commands;
  467. // Construct the name of the dependency generation target.
  468. std::string depTarget =
  469. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  470. depTarget += "/depend";
  471. std::string depMark = depTarget;
  472. depMark += ".make.mark";
  473. depends.push_back(depMark);
  474. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  475. depTarget.c_str(),
  476. depends, commands, true);
  477. depends.clear();
  478. // Write the dependency generation rule.
  479. std::string depEcho = "Scanning dependencies of target ";
  480. depEcho += this->Target->GetName();
  481. this->LocalGenerator->AppendEcho(commands, depEcho.c_str(),
  482. cmLocalUnixMakefileGenerator3::EchoDepend);
  483. // Add a command to call CMake to scan dependencies. CMake will
  484. // touch the corresponding depends file after scanning dependencies.
  485. cmOStringStream depCmd;
  486. // TODO: Account for source file properties and directory-level
  487. // definitions when scanning for dependencies.
  488. depCmd << "$(CMAKE_COMMAND) -E cmake_depends "
  489. << " \""
  490. << this->GlobalGenerator->GetName() << "\" "
  491. << this->LocalGenerator->Convert
  492. (this->Makefile->GetHomeOutputDirectory(),
  493. cmLocalGenerator::FULL,cmLocalGenerator::SHELL)
  494. << " "
  495. << this->LocalGenerator->Convert
  496. (this->Makefile->GetStartOutputDirectory(),
  497. cmLocalGenerator::FULL,cmLocalGenerator::SHELL)
  498. << " "
  499. << this->Convert(this->InfoFileNameFull.c_str(),
  500. cmLocalGenerator::FULL,
  501. cmLocalGenerator::SHELL);
  502. commands.push_back(depCmd.str());
  503. // Write the rule.
  504. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  505. depMark.c_str(),
  506. depends, commands, false);
  507. }
  508. //----------------------------------------------------------------------------
  509. void cmMakefileTargetGenerator
  510. ::WriteObjectDependRules(cmSourceFile& source,
  511. std::vector<std::string>& depends)
  512. {
  513. // Create the list of dependencies known at cmake time. These are
  514. // shared between the object file and dependency scanning rule.
  515. depends.push_back(source.GetFullPath());
  516. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  517. {
  518. std::vector<std::string> deps;
  519. cmSystemTools::ExpandListArgument(objectDeps, deps);
  520. for(std::vector<std::string>::iterator i = deps.begin();
  521. i != deps.end(); ++i)
  522. {
  523. depends.push_back(i->c_str());
  524. }
  525. }
  526. }
  527. //----------------------------------------------------------------------------
  528. void cmMakefileTargetGenerator::WriteCustomCommands()
  529. {
  530. // add custom commands to the clean rules?
  531. const char* clean_no_custom = this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  532. bool clean = cmSystemTools::IsOff(clean_no_custom);
  533. // Generate the rule files for each custom command.
  534. const std::vector<cmSourceFile*> &classes = this->Makefile->GetSourceFiles();
  535. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  536. i != classes.end(); i++)
  537. {
  538. if(cmCustomCommand* cc = (*i)->GetCustomCommand())
  539. {
  540. this->GenerateCustomRuleFile(*cc);
  541. if (clean)
  542. {
  543. const std::vector<std::string>& outputs = cc->GetOutputs();
  544. for(std::vector<std::string>::const_iterator o = outputs.begin();
  545. o != outputs.end(); ++o)
  546. {
  547. this->CleanFiles.push_back
  548. (this->Convert(o->c_str(),
  549. cmLocalGenerator::START_OUTPUT,
  550. cmLocalGenerator::UNCHANGED));
  551. }
  552. }
  553. }
  554. }
  555. }
  556. //----------------------------------------------------------------------------
  557. void cmMakefileTargetGenerator
  558. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  559. {
  560. // Collect the commands.
  561. std::vector<std::string> commands;
  562. std::string comment = this->LocalGenerator->ConstructComment(cc);
  563. if(!comment.empty())
  564. {
  565. this->LocalGenerator
  566. ->AppendEcho(commands, comment.c_str(),
  567. cmLocalUnixMakefileGenerator3::EchoGenerate);
  568. }
  569. this->LocalGenerator->AppendCustomCommand(commands, cc);
  570. // Collect the dependencies.
  571. std::vector<std::string> depends;
  572. this->LocalGenerator->AppendCustomDepend(depends, cc);
  573. // Write the rule.
  574. const std::vector<std::string>& outputs = cc.GetOutputs();
  575. std::vector<std::string>::const_iterator o = outputs.begin();
  576. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  577. o->c_str(), depends, commands,
  578. false);
  579. // If the rule has multiple outputs, add a rule for the extra
  580. // outputs to just depend on the first output with no command. Also
  581. // register the extra outputs as paired with the first output so
  582. // that the check-build-system step will remove the primary output
  583. // if any extra outputs are missing, forcing the rule to regenerate
  584. // all outputs.
  585. depends.clear();
  586. depends.push_back(*o);
  587. commands.clear();
  588. cmGlobalUnixMakefileGenerator3* gg =
  589. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  590. std::string emptyCommand = gg->GetEmptyCommandHack();
  591. if(!emptyCommand.empty())
  592. {
  593. commands.push_back(emptyCommand);
  594. }
  595. for(++o; o != outputs.end(); ++o)
  596. {
  597. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  598. o->c_str(), depends, commands,
  599. false);
  600. gg->AddMultipleOutputPair(o->c_str(), depends[0].c_str());
  601. }
  602. }
  603. //----------------------------------------------------------------------------
  604. void
  605. cmMakefileTargetGenerator
  606. ::WriteObjectsVariable(std::string& variableName,
  607. std::string& variableNameExternal)
  608. {
  609. // Write a make variable assignment that lists all objects for the
  610. // target.
  611. variableName =
  612. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(), "_OBJECTS");
  613. *this->BuildFileStream
  614. << "# Object files for target " << this->Target->GetName() << "\n"
  615. << variableName.c_str() << " =";
  616. std::string object;
  617. const char* objName =
  618. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  619. const char* lineContinue =
  620. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  621. if(!lineContinue)
  622. {
  623. lineContinue = "\\";
  624. }
  625. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  626. i != this->Objects.end(); ++i)
  627. {
  628. if ( this->ExtraContent.find(i->c_str()) != this->ExtraContent.end() )
  629. {
  630. continue;
  631. }
  632. *this->BuildFileStream << " " << lineContinue << "\n";
  633. if(objName)
  634. {
  635. *this->BuildFileStream <<
  636. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  637. cmLocalGenerator::MAKEFILE);
  638. }
  639. else
  640. {
  641. *this->BuildFileStream <<
  642. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  643. }
  644. }
  645. *this->BuildFileStream << "\n";
  646. // Write a make variable assignment that lists all external objects
  647. // for the target.
  648. variableNameExternal =
  649. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),"_EXTERNAL_OBJECTS");
  650. *this->BuildFileStream
  651. << "\n"
  652. << "# External object files for target " << this->Target->GetName() << "\n"
  653. << variableNameExternal.c_str() << " =";
  654. for(std::vector<std::string>::const_iterator i =
  655. this->ExternalObjects.begin();
  656. i != this->ExternalObjects.end(); ++i)
  657. {
  658. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  659. *this->BuildFileStream
  660. << " " << lineContinue << "\n"
  661. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  662. if(objName)
  663. {
  664. *this->BuildFileStream << this->Convert(i->c_str(),
  665. cmLocalGenerator::START_OUTPUT,
  666. cmLocalGenerator::MAKEFILE);
  667. }
  668. else
  669. {
  670. *this->BuildFileStream <<
  671. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  672. }
  673. }
  674. *this->BuildFileStream << "\n" << "\n";
  675. }
  676. //----------------------------------------------------------------------------
  677. std::string cmMakefileTargetGenerator::GetFrameworkFlags()
  678. {
  679. #ifndef __APPLE__
  680. return std::string();
  681. #else
  682. std::set<cmStdString> emitted;
  683. std::vector<std::string> includes;
  684. this->LocalGenerator->GetIncludeDirectories(includes);
  685. std::vector<std::string>::iterator i;
  686. // check all include directories for frameworks as this
  687. // will already have added a -F for the framework
  688. for(i = includes.begin(); i != includes.end(); ++i)
  689. {
  690. if(cmSystemTools::IsPathToFramework(i->c_str()))
  691. {
  692. std::string frameworkDir = *i;
  693. frameworkDir += "/../";
  694. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  695. emitted.insert(frameworkDir);
  696. }
  697. }
  698. std::string flags;
  699. std::vector<std::string>& frameworks = this->Target->GetFrameworks();
  700. for(i = frameworks.begin();
  701. i != frameworks.end(); ++i)
  702. {
  703. if(emitted.insert(*i).second)
  704. {
  705. flags += "-F";
  706. flags += this->LocalGenerator->ConvertToOutputForExisting(i->c_str());
  707. flags += " ";
  708. }
  709. }
  710. return flags;
  711. #endif
  712. }
  713. //----------------------------------------------------------------------------
  714. void cmMakefileTargetGenerator
  715. ::AppendTargetDepends(std::vector<std::string>& depends)
  716. {
  717. // Static libraries never depend on anything for linking.
  718. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  719. {
  720. return;
  721. }
  722. // Keep track of dependencies already listed.
  723. std::set<cmStdString> emitted;
  724. // A target should not depend on itself.
  725. emitted.insert(this->Target->GetName());
  726. // Loop over all library dependencies.
  727. const cmTarget::LinkLibraryVectorType& tlibs =
  728. this->Target->GetLinkLibraries();
  729. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  730. lib != tlibs.end(); ++lib)
  731. {
  732. // Don't emit the same library twice for this target.
  733. if(emitted.insert(lib->first).second)
  734. {
  735. // Depend only on other CMake targets.
  736. if(cmTarget* tgt =
  737. this->GlobalGenerator->FindTarget(0, lib->first.c_str()))
  738. {
  739. if(const char* location =
  740. tgt->GetLocation(this->LocalGenerator->ConfigurationName.c_str()))
  741. {
  742. depends.push_back(location);
  743. }
  744. }
  745. }
  746. }
  747. }
  748. //----------------------------------------------------------------------------
  749. void cmMakefileTargetGenerator
  750. ::CloseFileStreams()
  751. {
  752. delete this->BuildFileStream;
  753. delete this->InfoFileStream;
  754. delete this->FlagFileStream;
  755. }
  756. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  757. const char* linkLang,
  758. std::string& linkFlags)
  759. {
  760. // check for language flags that are not allowed at link time, and
  761. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  762. // which fails, there may be more]
  763. std::string removeFlags = "CMAKE_";
  764. removeFlags += linkLang;
  765. removeFlags += flagVar;
  766. std::string removeflags =
  767. this->Makefile->GetSafeDefinition(removeFlags.c_str());
  768. std::vector<std::string> removeList;
  769. cmSystemTools::ExpandListArgument(removeflags, removeList);
  770. for(std::vector<std::string>::iterator i = removeList.begin();
  771. i != removeList.end(); ++i)
  772. {
  773. cmSystemTools::ReplaceString(linkFlags, i->c_str(), "");
  774. }
  775. }