cmMakefileTargetGenerator.cxx 31 KB

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