cmMakefileTargetGenerator.cxx 31 KB

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