cmMakefileTargetGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 "cmLocalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmTarget.h"
  20. #include "cmMakefileExecutableTargetGenerator.h"
  21. #include "cmMakefileLibraryTargetGenerator.h"
  22. #include "cmMakefileUtilityTargetGenerator.h"
  23. cmMakefileTargetGenerator::cmMakefileTargetGenerator()
  24. {
  25. this->BuildFileStream = 0;
  26. this->InfoFileStream = 0;
  27. this->FlagFileStream = 0;
  28. }
  29. cmMakefileTargetGenerator *
  30. cmMakefileTargetGenerator::New(cmLocalUnixMakefileGenerator3 *lg,
  31. cmStdString tgtName, cmTarget *tgt)
  32. {
  33. cmMakefileTargetGenerator *result = 0;
  34. switch (tgt->GetType())
  35. {
  36. case cmTarget::EXECUTABLE:
  37. result = new cmMakefileExecutableTargetGenerator;
  38. break;
  39. case cmTarget::STATIC_LIBRARY:
  40. case cmTarget::SHARED_LIBRARY:
  41. case cmTarget::MODULE_LIBRARY:
  42. result = new cmMakefileLibraryTargetGenerator;
  43. break;
  44. case cmTarget::UTILITY:
  45. result = new cmMakefileUtilityTargetGenerator;
  46. break;
  47. default:
  48. return 0;
  49. break;
  50. }
  51. result->TargetName = tgtName;
  52. result->Target = tgt;
  53. result->LocalGenerator = lg;
  54. result->GlobalGenerator = lg->GetGlobalGenerator();
  55. result->Makefile = lg->GetMakefile();
  56. return result;
  57. }
  58. //----------------------------------------------------------------------------
  59. void cmMakefileTargetGenerator::CreateRuleFile()
  60. {
  61. // Create a directory for this target.
  62. this->TargetBuildDirectory =
  63. this->LocalGenerator->GetTargetDirectory(*this->Target);
  64. this->TargetBuildDirectoryFull =
  65. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  66. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  67. // Construct the rule file name.
  68. this->BuildFileName = this->TargetBuildDirectory;
  69. this->BuildFileName += "/build.make";
  70. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  71. this->BuildFileNameFull += "/build.make";
  72. // Open the rule file. This should be copy-if-different because the
  73. // rules may depend on this file itself.
  74. this->BuildFileStream =
  75. new cmGeneratedFileStream(this->BuildFileNameFull.c_str());
  76. this->BuildFileStream->SetCopyIfDifferent(true);
  77. if(!this->BuildFileStream)
  78. {
  79. return;
  80. }
  81. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  82. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  83. this->LocalGenerator->WriteMakeVariables(*this->BuildFileStream);
  84. }
  85. //----------------------------------------------------------------------------
  86. void cmMakefileTargetGenerator::WriteCustomCommandsForTarget()
  87. {
  88. // write the custom commands for this target
  89. // Look for files registered for cleaning in this directory.
  90. if(const char* additional_clean_files =
  91. this->Makefile->GetProperty
  92. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  93. {
  94. cmSystemTools::ExpandListArgument(additional_clean_files,
  95. this->CleanFiles);
  96. }
  97. this->WriteCustomCommands();
  98. }
  99. //----------------------------------------------------------------------------
  100. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  101. {
  102. // Include the dependencies for the target.
  103. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  104. dependFileNameFull += "/depend.make";
  105. *this->BuildFileStream
  106. << "# Include any dependencies generated for this target.\n"
  107. << this->LocalGenerator->m_IncludeDirective << " "
  108. << this->Convert(dependFileNameFull.c_str(),
  109. cmLocalGenerator::HOME_OUTPUT,
  110. cmLocalGenerator::MAKEFILE)
  111. << "\n\n";
  112. // make sure the depend file exists
  113. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  114. {
  115. // Write an empty dependency file.
  116. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  117. depFileStream
  118. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  119. << "# This may be replaced when dependencies are built." << std::endl;
  120. }
  121. // Open the flags file. This should be copy-if-different because the
  122. // rules may depend on this file itself.
  123. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  124. this->FlagFileNameFull += "/flags.make";
  125. this->FlagFileStream =
  126. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  127. this->FlagFileStream->SetCopyIfDifferent(true);
  128. if(!this->FlagFileStream)
  129. {
  130. return;
  131. }
  132. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  133. // Include the flags for the target.
  134. *this->BuildFileStream
  135. << "# Include the compile flags for this target's objects.\n"
  136. << this->LocalGenerator->m_IncludeDirective << " "
  137. << this->Convert(this->FlagFileNameFull.c_str(),
  138. cmLocalGenerator::HOME_OUTPUT,
  139. cmLocalGenerator::MAKEFILE)
  140. << "\n\n";
  141. // First generate the object rule files. Save a list of all object
  142. // files for this target.
  143. const std::vector<cmSourceFile*>& sources = this->Target->GetSourceFiles();
  144. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  145. source != sources.end(); ++source)
  146. {
  147. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  148. !(*source)->GetCustomCommand())
  149. {
  150. if(!this->GlobalGenerator->IgnoreFile
  151. ((*source)->GetSourceExtension().c_str()))
  152. {
  153. // Generate this object file's rule file.
  154. this->WriteObjectRuleFiles(*(*source));
  155. }
  156. else if((*source)->GetPropertyAsBool("EXTERNAL_OBJECT"))
  157. {
  158. // This is an external object file. Just add it.
  159. this->ExternalObjects.push_back((*source)->GetFullPath());
  160. }
  161. else
  162. {
  163. // We only get here if a source file is not an external object
  164. // and has an extension that is listed as an ignored file type
  165. // for this language. No message or diagnosis should be
  166. // given.
  167. }
  168. }
  169. }
  170. // write language flags for target
  171. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  172. checkSet =
  173. this->LocalGenerator->GetIntegrityCheckSet()[this->Target->GetName()];
  174. for(std::map<cmStdString,
  175. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  176. l = checkSet.begin(); l != checkSet.end(); ++l)
  177. {
  178. const char *lang = l->first.c_str();
  179. std::string flags;
  180. // Add the export symbol definition for shared library objects.
  181. bool shared = ((this->Target->GetType() == cmTarget::SHARED_LIBRARY) ||
  182. (this->Target->GetType() == cmTarget::MODULE_LIBRARY));
  183. if(shared)
  184. {
  185. flags += "-D";
  186. if(const char* custom_export_name =
  187. this->Target->GetProperty("DEFINE_SYMBOL"))
  188. {
  189. flags += custom_export_name;
  190. }
  191. else
  192. {
  193. std::string in = this->Target->GetName();
  194. in += "_EXPORTS";
  195. flags += cmSystemTools::MakeCindentifier(in.c_str());
  196. }
  197. }
  198. // Add language-specific flags.
  199. this->LocalGenerator->AddLanguageFlags(flags, lang);
  200. // Add shared-library flags if needed.
  201. this->LocalGenerator->AddSharedFlags(flags, lang, shared);
  202. // Add include directory flags.
  203. this->LocalGenerator->
  204. AppendFlags(flags, this->LocalGenerator->GetIncludeFlags(lang));
  205. // Add include directory flags.
  206. this->LocalGenerator->
  207. AppendFlags(flags,this->GetFrameworkFlags().c_str());
  208. *this->FlagFileStream << lang << "_FLAGS = " << flags << "\n\n";
  209. }
  210. }
  211. //----------------------------------------------------------------------------
  212. void cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile& source)
  213. {
  214. // Identify the language of the source file.
  215. const char* lang = this->LocalGenerator->GetSourceFileLanguage(source);
  216. if(!lang)
  217. {
  218. // If language is not known, this is an error.
  219. cmSystemTools::Error("Source file \"", source.GetFullPath().c_str(),
  220. "\" has unknown type.");
  221. return;
  222. }
  223. // Get the full path name of the object file.
  224. std::string objNoTargetDir;
  225. std::string obj =
  226. this->LocalGenerator->GetObjectFileName(*this->Target, source, &objNoTargetDir);
  227. // Avoid generating duplicate rules.
  228. if(m_ObjectFiles.find(obj) == m_ObjectFiles.end())
  229. {
  230. m_ObjectFiles.insert(obj);
  231. }
  232. else
  233. {
  234. cmOStringStream err;
  235. err << "Warning: Source file \""
  236. << source.GetSourceName().c_str() << "."
  237. << source.GetSourceExtension().c_str()
  238. << "\" is listed multiple times for target \""
  239. << this->Target->GetName()
  240. << "\".";
  241. cmSystemTools::Message(err.str().c_str(), "Warning");
  242. return;
  243. }
  244. // Create the directory containing the object file. This may be a
  245. // subdirectory under the target's directory.
  246. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  247. cmSystemTools::MakeDirectory
  248. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  249. // Save this in the target's list of object files.
  250. this->Objects.push_back(obj);
  251. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  252. relativeObj += obj;
  253. // we compute some depends when writing the depend.make that we will also
  254. // use in the build.make, same with depMakeFile
  255. std::vector<std::string> depends;
  256. std::string depMakeFile;
  257. // generate the build rule file
  258. this->WriteObjectBuildFile(obj, lang, source, depends);
  259. // The object file should be checked for dependency integrity.
  260. this->LocalGenerator->m_CheckDependFiles[this->Target->GetName()][lang].insert(&source);
  261. // add this to the list of objects for this local generator
  262. if(cmSystemTools::FileIsFullPath(objNoTargetDir.c_str()))
  263. {
  264. objNoTargetDir = cmSystemTools::GetFilenameName(objNoTargetDir);
  265. }
  266. this->LocalGenerator->m_LocalObjectFiles[objNoTargetDir].push_back(this->Target);
  267. }
  268. //----------------------------------------------------------------------------
  269. void
  270. cmMakefileTargetGenerator
  271. ::WriteObjectBuildFile(std::string &obj,
  272. const char *lang,
  273. cmSourceFile& source,
  274. std::vector<std::string>& depends)
  275. {
  276. this->LocalGenerator->AppendRuleDepend(depends, this->FlagFileNameFull.c_str());
  277. // generate the depend scanning rule
  278. this->WriteObjectDependRules(source, depends);
  279. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  280. relativeObj += obj;
  281. if(this->Makefile->GetDefinition("CMAKE_WINDOWS_OBJECT_PATH"))
  282. {
  283. relativeObj = cmSystemTools::ConvertToOutputPath(relativeObj.c_str());
  284. }
  285. // Write the build rule.
  286. // Build the set of compiler flags.
  287. std::string flags;
  288. if(this->Target->GetProperty("COMPILE_FLAGS"))
  289. {
  290. this->LocalGenerator->AppendFlags(flags, this->Target->GetProperty("COMPILE_FLAGS"));
  291. }
  292. // Add flags from source file properties.
  293. if (source.GetProperty("COMPILE_FLAGS"))
  294. {
  295. this->LocalGenerator->AppendFlags(flags, source.GetProperty("COMPILE_FLAGS"));
  296. *this->FlagFileStream << "# Custom flags: "
  297. << relativeObj << "_FLAGS = "
  298. << source.GetProperty("COMPILE_FLAGS")
  299. << "\n"
  300. << "\n";
  301. }
  302. // Add language-specific flags.
  303. std::string langFlags = "$(";
  304. langFlags += lang;
  305. langFlags += "_FLAGS)";
  306. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  307. // Get the output paths for source and object files.
  308. std::string sourceFile = source.GetFullPath();
  309. if(this->LocalGenerator->m_UseRelativePaths)
  310. {
  311. sourceFile = this->Convert(sourceFile.c_str(),
  312. cmLocalGenerator::HOME_OUTPUT);
  313. }
  314. sourceFile = this->Convert(sourceFile.c_str(),
  315. cmLocalGenerator::NONE,
  316. cmLocalGenerator::SHELL);
  317. std::string objectFile =
  318. this->Convert(obj.c_str(),
  319. cmLocalGenerator::START_OUTPUT,
  320. cmLocalGenerator::SHELL);
  321. // Construct the build message.
  322. std::vector<std::string> no_commands;
  323. std::vector<std::string> commands;
  324. std::string buildEcho = "Building ";
  325. buildEcho += lang;
  326. buildEcho += " object ";
  327. buildEcho += relativeObj;
  328. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str());
  329. // Construct the compile rules.
  330. std::string compileRuleVar = "CMAKE_";
  331. compileRuleVar += lang;
  332. compileRuleVar += "_COMPILE_OBJECT";
  333. std::string compileRule =
  334. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  335. cmSystemTools::ExpandListArgument(compileRule, commands);
  336. // Expand placeholders in the commands.
  337. for(std::vector<std::string>::iterator i = commands.begin();
  338. i != commands.end(); ++i)
  339. {
  340. this->LocalGenerator->ExpandRuleVariables(*i,
  341. lang,
  342. 0, // no objects
  343. 0, // no target
  344. 0, // no link libs
  345. sourceFile.c_str(),
  346. relativeObj.c_str(),
  347. flags.c_str());
  348. }
  349. // Make the target dependency scanning rule include cmake-time-known
  350. // dependencies. The others are handled by the check-build-system
  351. // path.
  352. std::string depMark =
  353. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  354. depMark += "/depend.make.mark";
  355. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  356. depMark.c_str(), depends, no_commands);
  357. // Write the rule.
  358. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  359. relativeObj.c_str(), depends, commands);
  360. // If the language needs provides-requires mode, create the
  361. // corresponding targets.
  362. std::string objectRequires = relativeObj;
  363. objectRequires += ".requires";
  364. std::vector<std::string> p_depends;
  365. // always provide an empty requires target
  366. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  367. objectRequires.c_str(), p_depends,
  368. no_commands);
  369. // write a build rule to recursively build what this obj provides
  370. std::string objectProvides = relativeObj;
  371. objectProvides += ".provides";
  372. std::string temp = relativeObj;
  373. temp += ".provides.build";
  374. std::vector<std::string> r_commands;
  375. std::string tgtMakefileName =
  376. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  377. tgtMakefileName += "/build.make";
  378. r_commands.push_back
  379. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),temp.c_str()));
  380. p_depends.clear();
  381. p_depends.push_back(objectRequires);
  382. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  383. objectProvides.c_str(), p_depends,
  384. r_commands);
  385. // write the provides.build rule dependency on the obj file
  386. p_depends.clear();
  387. p_depends.push_back(relativeObj);
  388. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  389. temp.c_str(), p_depends, no_commands);
  390. }
  391. //----------------------------------------------------------------------------
  392. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  393. {
  394. std::vector<std::string> depends;
  395. std::vector<std::string> no_commands;
  396. // Construct the name of the dependency generation target.
  397. std::string depTarget =
  398. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  399. depTarget += "/requires";
  400. // This target drives dependency generation for all object files.
  401. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  402. std::string objTarget;
  403. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  404. obj != this->Objects.end(); ++obj)
  405. {
  406. objTarget = relPath;
  407. objTarget += *obj;
  408. objTarget += ".requires";
  409. depends.push_back(objTarget);
  410. }
  411. // Write the rule.
  412. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  413. depTarget.c_str(), depends, no_commands);
  414. }
  415. //----------------------------------------------------------------------------
  416. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  417. {
  418. std::vector<std::string> depends;
  419. const char* sym = this->Makefile->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE");
  420. if(sym)
  421. {
  422. depends.push_back(sym);
  423. }
  424. std::vector<std::string> commands;
  425. // Construct the clean target name.
  426. std::string cleanTarget =
  427. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  428. cleanTarget += "/clean";
  429. // Construct the clean command.
  430. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles);
  431. this->LocalGenerator->CreateCDCommand(commands,
  432. this->Makefile->GetStartOutputDirectory(),
  433. this->Makefile->GetHomeOutputDirectory());
  434. // Write the rule.
  435. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  436. cleanTarget.c_str(), depends, commands);
  437. }
  438. //----------------------------------------------------------------------------
  439. void cmMakefileTargetGenerator::WriteTargetDependRules()
  440. {
  441. // must write the targets depend info file
  442. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  443. this->InfoFileNameFull = dir;
  444. this->InfoFileNameFull += "/DependInfo.cmake";
  445. this->InfoFileNameFull =
  446. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  447. this->InfoFileStream =
  448. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  449. this->InfoFileStream->SetCopyIfDifferent(true);
  450. if(!*this->InfoFileStream)
  451. {
  452. return;
  453. }
  454. this->LocalGenerator->
  455. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  456. // and now write the rule to use it
  457. std::vector<std::string> depends;
  458. std::vector<std::string> commands;
  459. // Construct the name of the dependency generation target.
  460. std::string depTarget =
  461. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  462. depTarget += "/depend";
  463. std::string depMark = depTarget;
  464. depMark += ".make.mark";
  465. depends.push_back(depMark);
  466. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  467. depTarget.c_str(), depends, commands);
  468. depends.clear();
  469. // Write the dependency generation rule.
  470. std::string depEcho = "Scanning dependencies of target ";
  471. depEcho += this->Target->GetName();
  472. this->LocalGenerator->AppendEcho(commands, depEcho.c_str());
  473. // Add a command to call CMake to scan dependencies. CMake will
  474. // touch the corresponding depends file after scanning dependencies.
  475. cmOStringStream depCmd;
  476. // TODO: Account for source file properties and directory-level
  477. // definitions when scanning for dependencies.
  478. depCmd << "$(CMAKE_COMMAND) -E cmake_depends "
  479. << " \""
  480. << this->GlobalGenerator->GetName() << "\" "
  481. << this->LocalGenerator->Convert
  482. (this->Makefile->GetHomeOutputDirectory(),
  483. cmLocalGenerator::FULL,cmLocalGenerator::SHELL)
  484. << " "
  485. << this->LocalGenerator->Convert
  486. (this->Makefile->GetStartOutputDirectory(),
  487. cmLocalGenerator::FULL,cmLocalGenerator::SHELL)
  488. << " "
  489. << this->Convert(this->InfoFileNameFull.c_str(),
  490. cmLocalGenerator::FULL,
  491. cmLocalGenerator::SHELL);
  492. commands.push_back(depCmd.str());
  493. // Write the rule.
  494. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  495. depMark.c_str(), depends, commands);
  496. }
  497. //----------------------------------------------------------------------------
  498. void cmMakefileTargetGenerator
  499. ::WriteObjectDependRules(cmSourceFile& source,
  500. std::vector<std::string>& depends)
  501. {
  502. // Create the list of dependencies known at cmake time. These are
  503. // shared between the object file and dependency scanning rule.
  504. depends.push_back(source.GetFullPath());
  505. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  506. {
  507. std::vector<std::string> deps;
  508. cmSystemTools::ExpandListArgument(objectDeps, deps);
  509. for(std::vector<std::string>::iterator i = deps.begin();
  510. i != deps.end(); ++i)
  511. {
  512. depends.push_back(i->c_str());
  513. }
  514. }
  515. }
  516. void cmMakefileTargetGenerator::WriteCustomCommands()
  517. {
  518. // add custom commands to the clean rules?
  519. const char* clean_no_custom = this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  520. bool clean = cmSystemTools::IsOff(clean_no_custom);
  521. // Generate the rule files for each custom command.
  522. const std::vector<cmSourceFile*> &classes = this->Makefile->GetSourceFiles();
  523. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  524. i != classes.end(); i++)
  525. {
  526. if(cmCustomCommand* cc = (*i)->GetCustomCommand())
  527. {
  528. this->GenerateCustomRuleFile(*cc);
  529. if (clean)
  530. {
  531. this->CleanFiles.push_back
  532. (this->Convert(cc->GetOutput(),
  533. cmLocalGenerator::START_OUTPUT,
  534. cmLocalGenerator::SHELL));
  535. }
  536. }
  537. }
  538. }
  539. //----------------------------------------------------------------------------
  540. void cmMakefileTargetGenerator
  541. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  542. {
  543. // Convert the output name to a relative path if possible.
  544. std::string output = this->Convert(cc.GetOutput(),
  545. cmLocalGenerator::START_OUTPUT);
  546. // Collect the commands.
  547. std::vector<std::string> commands;
  548. std::string preEcho = "Generating ";
  549. preEcho += output;
  550. this->LocalGenerator->AppendEcho(commands, preEcho.c_str());
  551. this->LocalGenerator->AppendCustomCommand(commands, cc);
  552. // Collect the dependencies.
  553. std::vector<std::string> depends;
  554. this->LocalGenerator->AppendCustomDepend(depends, cc);
  555. // Write the rule.
  556. const char* comment = 0;
  557. if(cc.GetComment() && *cc.GetComment())
  558. {
  559. comment = cc.GetComment();
  560. }
  561. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  562. cc.GetOutput(), depends, commands);
  563. }
  564. //----------------------------------------------------------------------------
  565. void
  566. cmMakefileTargetGenerator
  567. ::WriteObjectsVariable(std::string& variableName,
  568. std::string& variableNameExternal)
  569. {
  570. // Write a make variable assignment that lists all objects for the
  571. // target.
  572. variableName =
  573. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(), "_OBJECTS");
  574. *this->BuildFileStream
  575. << "# Object files for target " << this->Target->GetName() << "\n"
  576. << variableName.c_str() << " =";
  577. std::string object;
  578. const char* objName =
  579. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  580. const char* lineContinue =
  581. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  582. if(!lineContinue)
  583. {
  584. lineContinue = "\\";
  585. }
  586. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  587. i != this->Objects.end(); ++i)
  588. {
  589. *this->BuildFileStream << " " << lineContinue << "\n";
  590. if(objName)
  591. {
  592. *this->BuildFileStream <<
  593. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  594. cmLocalGenerator::MAKEFILE);
  595. }
  596. else
  597. {
  598. *this->BuildFileStream <<
  599. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  600. }
  601. }
  602. *this->BuildFileStream << "\n";
  603. // Write a make variable assignment that lists all external objects
  604. // for the target.
  605. variableNameExternal =
  606. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),"_EXTERNAL_OBJECTS");
  607. *this->BuildFileStream
  608. << "\n"
  609. << "# External object files for target " << this->Target->GetName() << "\n"
  610. << variableNameExternal.c_str() << " =";
  611. for(std::vector<std::string>::const_iterator i =
  612. this->ExternalObjects.begin();
  613. i != this->ExternalObjects.end(); ++i)
  614. {
  615. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  616. *this->BuildFileStream
  617. << " " << lineContinue << "\n"
  618. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  619. if(objName)
  620. {
  621. *this->BuildFileStream << this->Convert(i->c_str(),
  622. cmLocalGenerator::START_OUTPUT,
  623. cmLocalGenerator::MAKEFILE);
  624. }
  625. else
  626. {
  627. *this->BuildFileStream <<
  628. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  629. }
  630. }
  631. *this->BuildFileStream << "\n" << "\n";
  632. }
  633. //----------------------------------------------------------------------------
  634. std::string cmMakefileTargetGenerator::GetFrameworkFlags()
  635. {
  636. #ifndef __APPLE__
  637. return std::string();
  638. #else
  639. std::set<cmStdString> emitted;
  640. std::vector<std::string> includes;
  641. this->LocalGenerator->GetIncludeDirectories(includes);
  642. std::vector<std::string>::iterator i;
  643. // check all include directories for frameworks as this
  644. // will already have added a -F for the framework
  645. for(i = includes.begin(); i != includes.end(); ++i)
  646. {
  647. if(cmSystemTools::IsPathToFramework(i->c_str()))
  648. {
  649. std::string frameworkDir = *i;
  650. frameworkDir += "/../";
  651. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  652. emitted.insert(frameworkDir);
  653. }
  654. }
  655. std::string flags;
  656. std::vector<std::string>& frameworks = this->Target->GetFrameworks();
  657. for(i = frameworks.begin();
  658. i != frameworks.end(); ++i)
  659. {
  660. if(emitted.insert(*i).second)
  661. {
  662. flags += "-F";
  663. flags += this->LocalGenerator->ConvertToOutputForExisting(i->c_str());
  664. flags += " ";
  665. }
  666. }
  667. return flags;
  668. #endif
  669. }
  670. //----------------------------------------------------------------------------
  671. void cmMakefileTargetGenerator
  672. ::AppendTargetDepends(std::vector<std::string>& depends)
  673. {
  674. // Static libraries never depend on anything for linking.
  675. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  676. {
  677. return;
  678. }
  679. // Keep track of dependencies already listed.
  680. std::set<cmStdString> emitted;
  681. // A target should not depend on itself.
  682. emitted.insert(this->Target->GetName());
  683. // Loop over all library dependencies.
  684. const cmTarget::LinkLibraries& tlibs = this->Target->GetLinkLibraries();
  685. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  686. lib != tlibs.end(); ++lib)
  687. {
  688. // Don't emit the same library twice for this target.
  689. if(emitted.insert(lib->first).second)
  690. {
  691. // Depend only on other CMake targets.
  692. if(cmTarget* tgt =
  693. this->GlobalGenerator->FindTarget(0, lib->first.c_str()))
  694. {
  695. if(const char* location =
  696. tgt->GetLocation(this->LocalGenerator->m_ConfigurationName.c_str()))
  697. {
  698. depends.push_back(location);
  699. }
  700. }
  701. }
  702. }
  703. }
  704. //----------------------------------------------------------------------------
  705. void cmMakefileTargetGenerator
  706. ::CloseFileStreams()
  707. {
  708. delete this->BuildFileStream;
  709. delete this->InfoFileStream;
  710. delete this->FlagFileStream;
  711. }