cmMakefileTargetGenerator.cxx 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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 "cmake.h"
  22. #include "cmMakefileExecutableTargetGenerator.h"
  23. #include "cmMakefileLibraryTargetGenerator.h"
  24. #include "cmMakefileUtilityTargetGenerator.h"
  25. cmMakefileTargetGenerator::cmMakefileTargetGenerator()
  26. {
  27. this->BuildFileStream = 0;
  28. this->InfoFileStream = 0;
  29. this->FlagFileStream = 0;
  30. this->CustomCommandDriver = OnBuild;
  31. }
  32. cmMakefileTargetGenerator *
  33. cmMakefileTargetGenerator::New(cmLocalUnixMakefileGenerator3 *lg,
  34. cmStdString tgtName, cmTarget *tgt)
  35. {
  36. cmMakefileTargetGenerator *result = 0;
  37. switch (tgt->GetType())
  38. {
  39. case cmTarget::EXECUTABLE:
  40. result = new cmMakefileExecutableTargetGenerator;
  41. break;
  42. case cmTarget::STATIC_LIBRARY:
  43. case cmTarget::SHARED_LIBRARY:
  44. case cmTarget::MODULE_LIBRARY:
  45. result = new cmMakefileLibraryTargetGenerator;
  46. break;
  47. case cmTarget::UTILITY:
  48. result = new cmMakefileUtilityTargetGenerator;
  49. break;
  50. default:
  51. return result;
  52. // break; /* unreachable */
  53. }
  54. result->TargetName = tgtName;
  55. result->Target = tgt;
  56. result->LocalGenerator = lg;
  57. result->GlobalGenerator =
  58. static_cast<cmGlobalUnixMakefileGenerator3*>(lg->GetGlobalGenerator());
  59. result->Makefile = lg->GetMakefile();
  60. return result;
  61. }
  62. //----------------------------------------------------------------------------
  63. void cmMakefileTargetGenerator::CreateRuleFile()
  64. {
  65. // Create a directory for this target.
  66. this->TargetBuildDirectory =
  67. this->LocalGenerator->GetTargetDirectory(*this->Target);
  68. this->TargetBuildDirectoryFull =
  69. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  70. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  71. // Construct the rule file name.
  72. this->BuildFileName = this->TargetBuildDirectory;
  73. this->BuildFileName += "/build.make";
  74. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  75. this->BuildFileNameFull += "/build.make";
  76. // Construct the rule file name.
  77. this->ProgressFileName = this->TargetBuildDirectory;
  78. this->ProgressFileName += "/progress.make";
  79. this->ProgressFileNameFull = this->TargetBuildDirectoryFull;
  80. this->ProgressFileNameFull += "/progress.make";
  81. // reset the progress count
  82. this->NumberOfProgressActions = 0;
  83. // Open the rule file. This should be copy-if-different because the
  84. // rules may depend on this file itself.
  85. this->BuildFileStream =
  86. new cmGeneratedFileStream(this->BuildFileNameFull.c_str());
  87. this->BuildFileStream->SetCopyIfDifferent(true);
  88. if(!this->BuildFileStream)
  89. {
  90. return;
  91. }
  92. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  93. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  94. }
  95. //----------------------------------------------------------------------------
  96. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  97. {
  98. // write the custom commands for this target
  99. // Look for files registered for cleaning in this directory.
  100. if(const char* additional_clean_files =
  101. this->Makefile->GetProperty
  102. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  103. {
  104. cmSystemTools::ExpandListArgument(additional_clean_files,
  105. this->CleanFiles);
  106. }
  107. // add custom commands to the clean rules?
  108. const char* clean_no_custom =
  109. this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  110. bool clean = cmSystemTools::IsOff(clean_no_custom);
  111. // First generate the object rule files. Save a list of all object
  112. // files for this target.
  113. const std::vector<cmSourceFile*>& sources = this->Target->GetSourceFiles();
  114. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  115. source != sources.end(); ++source)
  116. {
  117. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  118. {
  119. this->GenerateCustomRuleFile(*cc);
  120. if (clean)
  121. {
  122. const std::vector<std::string>& outputs = cc->GetOutputs();
  123. for(std::vector<std::string>::const_iterator o = outputs.begin();
  124. o != outputs.end(); ++o)
  125. {
  126. this->CleanFiles.push_back
  127. (this->Convert(o->c_str(),
  128. cmLocalGenerator::START_OUTPUT,
  129. cmLocalGenerator::UNCHANGED));
  130. }
  131. }
  132. }
  133. else if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  134. {
  135. if(!this->GlobalGenerator->IgnoreFile
  136. ((*source)->GetExtension().c_str()))
  137. {
  138. // Generate this object file's rule file.
  139. this->WriteObjectRuleFiles(*(*source));
  140. }
  141. else if((*source)->GetPropertyAsBool("EXTERNAL_OBJECT"))
  142. {
  143. // This is an external object file. Just add it.
  144. this->ExternalObjects.push_back((*source)->GetFullPath());
  145. }
  146. else
  147. {
  148. // We only get here if a source file is not an external object
  149. // and has an extension that is listed as an ignored file type
  150. // for this language. No message or diagnosis should be
  151. // given.
  152. }
  153. }
  154. }
  155. }
  156. //----------------------------------------------------------------------------
  157. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  158. {
  159. // Include the dependencies for the target.
  160. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  161. dependFileNameFull += "/depend.make";
  162. *this->BuildFileStream
  163. << "# Include any dependencies generated for this target.\n"
  164. << this->LocalGenerator->IncludeDirective << " "
  165. << this->Convert(dependFileNameFull.c_str(),
  166. cmLocalGenerator::HOME_OUTPUT,
  167. cmLocalGenerator::MAKEFILE)
  168. << "\n\n";
  169. // Include the progress variables for the target.
  170. *this->BuildFileStream
  171. << "# Include the progress variables for this target.\n"
  172. << this->LocalGenerator->IncludeDirective << " "
  173. << this->Convert(this->ProgressFileNameFull.c_str(),
  174. cmLocalGenerator::HOME_OUTPUT,
  175. cmLocalGenerator::MAKEFILE)
  176. << "\n\n";
  177. // make sure the depend file exists
  178. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  179. {
  180. // Write an empty dependency file.
  181. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  182. depFileStream
  183. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  184. << "# This may be replaced when dependencies are built." << std::endl;
  185. }
  186. // Open the flags file. This should be copy-if-different because the
  187. // rules may depend on this file itself.
  188. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  189. this->FlagFileNameFull += "/flags.make";
  190. this->FlagFileStream =
  191. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  192. this->FlagFileStream->SetCopyIfDifferent(true);
  193. if(!this->FlagFileStream)
  194. {
  195. return;
  196. }
  197. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  198. // Include the flags for the target.
  199. *this->BuildFileStream
  200. << "# Include the compile flags for this target's objects.\n"
  201. << this->LocalGenerator->IncludeDirective << " "
  202. << this->Convert(this->FlagFileNameFull.c_str(),
  203. cmLocalGenerator::HOME_OUTPUT,
  204. cmLocalGenerator::MAKEFILE)
  205. << "\n\n";
  206. }
  207. //----------------------------------------------------------------------------
  208. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  209. {
  210. // write language flags for target
  211. std::set<cmStdString> languages;
  212. this->Target->GetLanguages(languages);
  213. for(std::set<cmStdString>::const_iterator l = languages.begin();
  214. l != languages.end(); ++l)
  215. {
  216. const char *lang = l->c_str();
  217. std::string flags;
  218. bool shared = ((this->Target->GetType() == cmTarget::SHARED_LIBRARY) ||
  219. (this->Target->GetType() == cmTarget::MODULE_LIBRARY));
  220. // Add the export symbol definition for shared library objects.
  221. if(const char* exportMacro = this->Target->GetExportMacro())
  222. {
  223. flags += "-D";
  224. flags += exportMacro;
  225. }
  226. // Add language-specific flags.
  227. this->LocalGenerator
  228. ->AddLanguageFlags(flags, lang,
  229. this->LocalGenerator->ConfigurationName.c_str());
  230. // Add shared-library flags if needed.
  231. this->LocalGenerator->AddSharedFlags(flags, lang, shared);
  232. // Add include directory flags.
  233. this->LocalGenerator->
  234. AppendFlags(flags, this->LocalGenerator->GetIncludeFlags(lang));
  235. // Add include directory flags.
  236. this->LocalGenerator->
  237. AppendFlags(flags,this->GetFrameworkFlags().c_str());
  238. *this->FlagFileStream << lang << "_FLAGS = " << flags << "\n\n";
  239. }
  240. // Add target-specific flags.
  241. if(this->Target->GetProperty("COMPILE_FLAGS"))
  242. {
  243. std::string flags;
  244. this->LocalGenerator->AppendFlags
  245. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  246. *this->FlagFileStream << "# TARGET_FLAGS = " << flags << "\n\n";
  247. }
  248. }
  249. //----------------------------------------------------------------------------
  250. void cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile& source)
  251. {
  252. // Identify the language of the source file.
  253. const char* lang = this->LocalGenerator->GetSourceFileLanguage(source);
  254. if(!lang)
  255. {
  256. // don't know anything about this file so skip it
  257. return;
  258. }
  259. // Get the full path name of the object file.
  260. std::string objNoTargetDir;
  261. std::string obj =
  262. this->LocalGenerator->GetObjectFileName(*this->Target, source,
  263. &objNoTargetDir);
  264. // Avoid generating duplicate rules.
  265. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  266. {
  267. this->ObjectFiles.insert(obj);
  268. }
  269. else
  270. {
  271. cmOStringStream err;
  272. err << "Warning: Source file \""
  273. << source.GetFullPath()
  274. << "\" is listed multiple times for target \""
  275. << this->Target->GetName()
  276. << "\".";
  277. cmSystemTools::Message(err.str().c_str(), "Warning");
  278. return;
  279. }
  280. // Create the directory containing the object file. This may be a
  281. // subdirectory under the target's directory.
  282. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  283. cmSystemTools::MakeDirectory
  284. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  285. // Save this in the target's list of object files.
  286. if ( source.GetPropertyAsBool("EXTRA_CONTENT") )
  287. {
  288. this->ExtraContent.insert(obj);
  289. }
  290. this->Objects.push_back(obj);
  291. this->CleanFiles.push_back(obj);
  292. // TODO: Remove
  293. //std::string relativeObj
  294. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  295. //relativeObj += obj;
  296. // we compute some depends when writing the depend.make that we will also
  297. // use in the build.make, same with depMakeFile
  298. std::vector<std::string> depends;
  299. std::string depMakeFile;
  300. // generate the build rule file
  301. this->WriteObjectBuildFile(obj, lang, source, depends);
  302. // The object file should be checked for dependency integrity.
  303. std::string objFullPath = this->Makefile->GetCurrentOutputDirectory();
  304. objFullPath += "/";
  305. objFullPath += obj;
  306. objFullPath =
  307. this->Convert(objFullPath.c_str(), cmLocalGenerator::FULL);
  308. std::string srcFullPath =
  309. this->Convert(source.GetFullPath().c_str(), cmLocalGenerator::FULL);
  310. this->LocalGenerator->
  311. AddImplicitDepends(*this->Target, lang,
  312. objFullPath.c_str(),
  313. srcFullPath.c_str());
  314. // add this to the list of objects for this local generator
  315. if(cmSystemTools::FileIsFullPath(objNoTargetDir.c_str()))
  316. {
  317. objNoTargetDir = cmSystemTools::GetFilenameName(objNoTargetDir);
  318. }
  319. this->LocalGenerator->LocalObjectFiles[objNoTargetDir].
  320. push_back(
  321. cmLocalUnixMakefileGenerator3::LocalObjectEntry(this->Target, lang)
  322. );
  323. }
  324. //----------------------------------------------------------------------------
  325. void
  326. cmMakefileTargetGenerator
  327. ::WriteObjectBuildFile(std::string &obj,
  328. const char *lang,
  329. cmSourceFile& source,
  330. std::vector<std::string>& depends)
  331. {
  332. this->LocalGenerator->AppendRuleDepend(depends,
  333. this->FlagFileNameFull.c_str());
  334. // generate the depend scanning rule
  335. this->WriteObjectDependRules(source, depends);
  336. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  337. if ( source.GetPropertyAsBool("MACOSX_CONTENT") )
  338. {
  339. relativeObj = "";
  340. }
  341. relativeObj += obj;
  342. // Write the build rule.
  343. // Build the set of compiler flags.
  344. std::string flags;
  345. // Add language-specific flags.
  346. std::string langFlags = "$(";
  347. langFlags += lang;
  348. langFlags += "_FLAGS)";
  349. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  350. // Add target-specific flags.
  351. if(this->Target->GetProperty("COMPILE_FLAGS"))
  352. {
  353. this->LocalGenerator->AppendFlags
  354. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  355. }
  356. // Add flags from source file properties.
  357. if (source.GetProperty("COMPILE_FLAGS"))
  358. {
  359. this->LocalGenerator->AppendFlags
  360. (flags, source.GetProperty("COMPILE_FLAGS"));
  361. *this->FlagFileStream << "# Custom flags: "
  362. << relativeObj << "_FLAGS = "
  363. << source.GetProperty("COMPILE_FLAGS")
  364. << "\n"
  365. << "\n";
  366. }
  367. // Get the output paths for source and object files.
  368. std::string sourceFile = source.GetFullPath();
  369. if(this->LocalGenerator->UseRelativePaths)
  370. {
  371. sourceFile = this->Convert(sourceFile.c_str(),
  372. cmLocalGenerator::HOME_OUTPUT);
  373. }
  374. sourceFile = this->Convert(sourceFile.c_str(),
  375. cmLocalGenerator::NONE,
  376. cmLocalGenerator::SHELL);
  377. std::string objectFile = this->Convert(obj.c_str(),
  378. cmLocalGenerator::START_OUTPUT,
  379. cmLocalGenerator::SHELL);
  380. // Construct the build message.
  381. std::vector<std::string> no_commands;
  382. std::vector<std::string> commands;
  383. // add in a progress call if needed
  384. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  385. progressDir += cmake::GetCMakeFilesDirectory();
  386. cmOStringStream progCmd;
  387. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  388. progCmd << this->LocalGenerator->Convert(progressDir.c_str(),
  389. cmLocalGenerator::FULL,
  390. cmLocalGenerator::SHELL);
  391. this->NumberOfProgressActions++;
  392. progCmd << " $(CMAKE_PROGRESS_"
  393. << this->NumberOfProgressActions
  394. << ")";
  395. commands.push_back(progCmd.str());
  396. std::string buildEcho = "Building ";
  397. buildEcho += lang;
  398. buildEcho += " object ";
  399. buildEcho += relativeObj;
  400. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  401. cmLocalUnixMakefileGenerator3::EchoBuild);
  402. // Construct the compile rules.
  403. std::string compileRuleVar = "CMAKE_";
  404. compileRuleVar += lang;
  405. compileRuleVar += "_COMPILE_OBJECT";
  406. std::string compileRule =
  407. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  408. std::vector<std::string> compileCommands;
  409. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  410. // Change the command working directory to the local build tree.
  411. this->LocalGenerator->CreateCDCommand
  412. (compileCommands,
  413. this->Makefile->GetStartOutputDirectory(),
  414. this->Makefile->GetHomeOutputDirectory());
  415. commands.insert(commands.end(),
  416. compileCommands.begin(), compileCommands.end());
  417. std::string targetOutPathPDB;
  418. {
  419. std::string targetFullPathPDB;
  420. const char* configName = this->LocalGenerator->ConfigurationName.c_str();
  421. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  422. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  423. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  424. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  425. {
  426. targetFullPathPDB = this->Target->GetDirectory();
  427. targetFullPathPDB += "/";
  428. targetFullPathPDB += this->Target->GetPDBName(configName);
  429. }
  430. targetOutPathPDB =
  431. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::FULL,
  432. cmLocalGenerator::SHELL);
  433. }
  434. cmLocalGenerator::RuleVariables vars;
  435. vars.Language = lang;
  436. vars.TargetPDB = targetOutPathPDB.c_str();
  437. vars.Source = sourceFile.c_str();
  438. std::string shellObj =
  439. this->Convert(obj.c_str(),
  440. cmLocalGenerator::NONE,
  441. cmLocalGenerator::SHELL).c_str();
  442. vars.Object = shellObj.c_str();
  443. std::string objectDir = cmSystemTools::GetFilenamePath(obj);
  444. vars.ObjectDir = objectDir.c_str();
  445. vars.Flags = flags.c_str();
  446. // Expand placeholders in the commands.
  447. for(std::vector<std::string>::iterator i = commands.begin();
  448. i != commands.end(); ++i)
  449. {
  450. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  451. }
  452. // Make the target dependency scanning rule include cmake-time-known
  453. // dependencies. The others are handled by the check-build-system
  454. // path.
  455. std::string depMark =
  456. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  457. depMark += "/depend.make.mark";
  458. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  459. depMark.c_str(),
  460. depends, no_commands, false);
  461. // Write the rule.
  462. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  463. relativeObj.c_str(),
  464. depends, commands, false);
  465. // Check for extra outputs created by the compilation.
  466. if(const char* extra_outputs_str =
  467. source.GetProperty("OBJECT_OUTPUTS"))
  468. {
  469. std::vector<std::string> extra_outputs;
  470. cmSystemTools::ExpandListArgument(extra_outputs_str, extra_outputs);
  471. for(std::vector<std::string>::const_iterator eoi = extra_outputs.begin();
  472. eoi != extra_outputs.end(); ++eoi)
  473. {
  474. // Register this as an extra output for the object file rule.
  475. // This will cause the object file to be rebuilt if the extra
  476. // output is missing.
  477. this->GenerateExtraOutput(eoi->c_str(), relativeObj.c_str(), false);
  478. // Register this as an extra file to clean.
  479. this->CleanFiles.push_back(eoi->c_str());
  480. }
  481. }
  482. bool lang_is_c_or_cxx = ((strcmp(lang, "C") == 0) ||
  483. (strcmp(lang, "CXX") == 0));
  484. bool do_preprocess_rules = lang_is_c_or_cxx &&
  485. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  486. bool do_assembly_rules = lang_is_c_or_cxx &&
  487. this->LocalGenerator->GetCreateAssemblySourceRules();
  488. if(do_preprocess_rules || do_assembly_rules)
  489. {
  490. std::vector<std::string> force_depends;
  491. force_depends.push_back("cmake_force");
  492. std::string::size_type dot_pos = relativeObj.rfind(".");
  493. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  494. dot_pos = obj.rfind(".");
  495. std::string objBase = obj.substr(0, dot_pos);
  496. if(do_preprocess_rules)
  497. {
  498. commands.clear();
  499. std::string relativeObjI = relativeObjBase + ".i";
  500. std::string objI = objBase + ".i";
  501. std::string preprocessEcho = "Preprocessing ";
  502. preprocessEcho += lang;
  503. preprocessEcho += " source to ";
  504. preprocessEcho += objI;
  505. this->LocalGenerator->AppendEcho(
  506. commands, preprocessEcho.c_str(),
  507. cmLocalUnixMakefileGenerator3::EchoBuild
  508. );
  509. std::string preprocessRuleVar = "CMAKE_";
  510. preprocessRuleVar += lang;
  511. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  512. if(const char* preprocessRule =
  513. this->Makefile->GetDefinition(preprocessRuleVar.c_str()))
  514. {
  515. std::vector<std::string> preprocessCommands;
  516. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  517. this->LocalGenerator->CreateCDCommand
  518. (preprocessCommands,
  519. this->Makefile->GetStartOutputDirectory(),
  520. this->Makefile->GetHomeOutputDirectory());
  521. commands.insert(commands.end(),
  522. preprocessCommands.begin(),
  523. preprocessCommands.end());
  524. vars.PreprocessedSource = objI.c_str();
  525. // Expand placeholders in the commands.
  526. for(std::vector<std::string>::iterator i = commands.begin();
  527. i != commands.end(); ++i)
  528. {
  529. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  530. }
  531. }
  532. else
  533. {
  534. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  535. cmd += preprocessRuleVar;
  536. commands.push_back(cmd);
  537. }
  538. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  539. relativeObjI.c_str(),
  540. force_depends, commands, false);
  541. }
  542. if(do_assembly_rules)
  543. {
  544. commands.clear();
  545. std::string relativeObjS = relativeObjBase + ".s";
  546. std::string objS = objBase + ".s";
  547. std::string assemblyEcho = "Compiling ";
  548. assemblyEcho += lang;
  549. assemblyEcho += " source to assembly ";
  550. assemblyEcho += objS;
  551. this->LocalGenerator->AppendEcho(
  552. commands, assemblyEcho.c_str(),
  553. cmLocalUnixMakefileGenerator3::EchoBuild
  554. );
  555. std::string assemblyRuleVar = "CMAKE_";
  556. assemblyRuleVar += lang;
  557. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  558. if(const char* assemblyRule =
  559. this->Makefile->GetDefinition(assemblyRuleVar.c_str()))
  560. {
  561. std::vector<std::string> assemblyCommands;
  562. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  563. this->LocalGenerator->CreateCDCommand
  564. (assemblyCommands,
  565. this->Makefile->GetStartOutputDirectory(),
  566. this->Makefile->GetHomeOutputDirectory());
  567. commands.insert(commands.end(),
  568. assemblyCommands.begin(),
  569. assemblyCommands.end());
  570. vars.AssemblySource = objS.c_str();
  571. // Expand placeholders in the commands.
  572. for(std::vector<std::string>::iterator i = commands.begin();
  573. i != commands.end(); ++i)
  574. {
  575. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  576. }
  577. }
  578. else
  579. {
  580. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  581. cmd += assemblyRuleVar;
  582. commands.push_back(cmd);
  583. }
  584. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  585. relativeObjS.c_str(),
  586. force_depends, commands, false);
  587. }
  588. }
  589. // If the language needs provides-requires mode, create the
  590. // corresponding targets.
  591. std::string objectRequires = relativeObj;
  592. objectRequires += ".requires";
  593. std::vector<std::string> p_depends;
  594. // always provide an empty requires target
  595. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  596. objectRequires.c_str(), p_depends,
  597. no_commands, true);
  598. // write a build rule to recursively build what this obj provides
  599. std::string objectProvides = relativeObj;
  600. objectProvides += ".provides";
  601. std::string temp = relativeObj;
  602. temp += ".provides.build";
  603. std::vector<std::string> r_commands;
  604. std::string tgtMakefileName =
  605. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  606. tgtMakefileName += "/build.make";
  607. r_commands.push_back
  608. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  609. temp.c_str()));
  610. p_depends.clear();
  611. p_depends.push_back(objectRequires);
  612. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  613. objectProvides.c_str(), p_depends,
  614. r_commands, true);
  615. // write the provides.build rule dependency on the obj file
  616. p_depends.clear();
  617. p_depends.push_back(relativeObj);
  618. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  619. temp.c_str(), p_depends, no_commands,
  620. true);
  621. }
  622. //----------------------------------------------------------------------------
  623. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  624. {
  625. std::vector<std::string> depends;
  626. std::vector<std::string> no_commands;
  627. // Construct the name of the dependency generation target.
  628. std::string depTarget =
  629. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  630. depTarget += "/requires";
  631. // This target drives dependency generation for all object files.
  632. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  633. std::string objTarget;
  634. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  635. obj != this->Objects.end(); ++obj)
  636. {
  637. objTarget = relPath;
  638. objTarget += *obj;
  639. objTarget += ".requires";
  640. depends.push_back(objTarget);
  641. }
  642. // Write the rule.
  643. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  644. depTarget.c_str(),
  645. depends, no_commands, true);
  646. }
  647. //----------------------------------------------------------------------------
  648. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  649. {
  650. std::vector<std::string> depends;
  651. std::vector<std::string> commands;
  652. // Construct the clean target name.
  653. std::string cleanTarget =
  654. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  655. cleanTarget += "/clean";
  656. // Construct the clean command.
  657. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  658. *this->Target);
  659. this->LocalGenerator->CreateCDCommand
  660. (commands,
  661. this->Makefile->GetStartOutputDirectory(),
  662. this->Makefile->GetHomeOutputDirectory());
  663. // Write the rule.
  664. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  665. cleanTarget.c_str(),
  666. depends, commands, true);
  667. }
  668. //----------------------------------------------------------------------------
  669. void cmMakefileTargetGenerator::WriteTargetDependRules()
  670. {
  671. // must write the targets depend info file
  672. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  673. this->InfoFileNameFull = dir;
  674. this->InfoFileNameFull += "/DependInfo.cmake";
  675. this->InfoFileNameFull =
  676. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  677. this->InfoFileStream =
  678. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  679. this->InfoFileStream->SetCopyIfDifferent(true);
  680. if(!*this->InfoFileStream)
  681. {
  682. return;
  683. }
  684. this->LocalGenerator->
  685. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  686. // and now write the rule to use it
  687. std::vector<std::string> depends;
  688. std::vector<std::string> commands;
  689. // Construct the name of the dependency generation target.
  690. std::string depTarget =
  691. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  692. depTarget += "/depend";
  693. std::string depMark = depTarget;
  694. depMark += ".make.mark";
  695. depends.push_back(depMark);
  696. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  697. depTarget.c_str(),
  698. depends, commands, true);
  699. depends.clear();
  700. // Write the dependency generation rule.
  701. std::string depEcho = "Scanning dependencies of target ";
  702. depEcho += this->Target->GetName();
  703. this->LocalGenerator->AppendEcho(commands, depEcho.c_str(),
  704. cmLocalUnixMakefileGenerator3::EchoDepend);
  705. // Add a command to call CMake to scan dependencies. CMake will
  706. // touch the corresponding depends file after scanning dependencies.
  707. cmOStringStream depCmd;
  708. // TODO: Account for source file properties and directory-level
  709. // definitions when scanning for dependencies.
  710. #if !defined(_WIN32) || defined(__CYGWIN__)
  711. // This platform supports symlinks, so cmSystemTools will translate
  712. // paths. Make sure PWD is set to the original name of the home
  713. // output directory to help cmSystemTools to create the same
  714. // translation table for the dependency scanning process.
  715. depCmd << "cd "
  716. << (this->LocalGenerator->Convert(
  717. this->Makefile->GetHomeOutputDirectory(),
  718. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  719. << " && ";
  720. #endif
  721. // Generate a call this signature:
  722. //
  723. // cmake -E cmake_depends <generator>
  724. // <home-src-dir> <start-src-dir>
  725. // <home-out-dir> <start-out-dir>
  726. // <dep-info>
  727. //
  728. // This gives the dependency scanner enough information to recreate
  729. // the state of our local generator sufficiently for its needs.
  730. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  731. << this->GlobalGenerator->GetName() << "\" "
  732. << this->Convert(this->Makefile->GetHomeDirectory(),
  733. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  734. << " "
  735. << this->Convert(this->Makefile->GetStartDirectory(),
  736. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  737. << " "
  738. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  739. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  740. << " "
  741. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  742. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  743. << " "
  744. << this->Convert(this->InfoFileNameFull.c_str(),
  745. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  746. commands.push_back(depCmd.str());
  747. // Make sure all custom command outputs in this target are built.
  748. if(this->CustomCommandDriver == OnDepends)
  749. {
  750. this->DriveCustomCommands(depends);
  751. }
  752. // Write the rule.
  753. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  754. depMark.c_str(),
  755. depends, commands, false);
  756. }
  757. //----------------------------------------------------------------------------
  758. void
  759. cmMakefileTargetGenerator
  760. ::DriveCustomCommands(std::vector<std::string>& depends)
  761. {
  762. // Depend on all custom command outputs.
  763. const std::vector<cmSourceFile*>& sources =
  764. this->Target->GetSourceFiles();
  765. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  766. source != sources.end(); ++source)
  767. {
  768. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  769. {
  770. const std::vector<std::string>& outputs = cc->GetOutputs();
  771. for(std::vector<std::string>::const_iterator o = outputs.begin();
  772. o != outputs.end(); ++o)
  773. {
  774. depends.push_back(*o);
  775. }
  776. }
  777. }
  778. }
  779. //----------------------------------------------------------------------------
  780. void cmMakefileTargetGenerator
  781. ::WriteObjectDependRules(cmSourceFile& source,
  782. std::vector<std::string>& depends)
  783. {
  784. // Create the list of dependencies known at cmake time. These are
  785. // shared between the object file and dependency scanning rule.
  786. depends.push_back(source.GetFullPath());
  787. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  788. {
  789. std::vector<std::string> deps;
  790. cmSystemTools::ExpandListArgument(objectDeps, deps);
  791. for(std::vector<std::string>::iterator i = deps.begin();
  792. i != deps.end(); ++i)
  793. {
  794. depends.push_back(i->c_str());
  795. }
  796. }
  797. }
  798. //----------------------------------------------------------------------------
  799. void cmMakefileTargetGenerator
  800. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  801. {
  802. // Collect the commands.
  803. std::vector<std::string> commands;
  804. std::string comment = this->LocalGenerator->ConstructComment(cc);
  805. if(!comment.empty())
  806. {
  807. // add in a progress call if needed
  808. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  809. progressDir += cmake::GetCMakeFilesDirectory();
  810. cmOStringStream progCmd;
  811. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  812. progCmd << this->LocalGenerator->Convert(progressDir.c_str(),
  813. cmLocalGenerator::FULL,
  814. cmLocalGenerator::SHELL);
  815. this->NumberOfProgressActions++;
  816. progCmd << " $(CMAKE_PROGRESS_"
  817. << this->NumberOfProgressActions
  818. << ")";
  819. commands.push_back(progCmd.str());
  820. this->LocalGenerator
  821. ->AppendEcho(commands, comment.c_str(),
  822. cmLocalUnixMakefileGenerator3::EchoGenerate);
  823. }
  824. this->LocalGenerator->AppendCustomCommand(commands, cc);
  825. // Collect the dependencies.
  826. std::vector<std::string> depends;
  827. this->LocalGenerator->AppendCustomDepend(depends, cc);
  828. // Add a dependency on the rule file itself.
  829. this->LocalGenerator->AppendRuleDepend(depends,
  830. this->BuildFileNameFull.c_str());
  831. // Check whether we need to bother checking for a symbolic output.
  832. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  833. // Write the rule.
  834. const std::vector<std::string>& outputs = cc.GetOutputs();
  835. std::vector<std::string>::const_iterator o = outputs.begin();
  836. {
  837. bool symbolic = false;
  838. if(need_symbolic)
  839. {
  840. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  841. {
  842. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  843. }
  844. }
  845. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  846. o->c_str(), depends, commands,
  847. symbolic);
  848. }
  849. // Write rules to drive building any outputs beyond the first.
  850. const char* in = o->c_str();
  851. for(++o; o != outputs.end(); ++o)
  852. {
  853. bool symbolic = false;
  854. if(need_symbolic)
  855. {
  856. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  857. {
  858. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  859. }
  860. }
  861. this->GenerateExtraOutput(o->c_str(), in, symbolic);
  862. }
  863. // Setup implicit dependency scanning.
  864. for(cmCustomCommand::ImplicitDependsList::const_iterator
  865. idi = cc.GetImplicitDepends().begin();
  866. idi != cc.GetImplicitDepends().end(); ++idi)
  867. {
  868. std::string objFullPath =
  869. this->Convert(outputs[0].c_str(), cmLocalGenerator::FULL);
  870. std::string srcFullPath =
  871. this->Convert(idi->second.c_str(), cmLocalGenerator::FULL);
  872. this->LocalGenerator->
  873. AddImplicitDepends(*this->Target, idi->first.c_str(),
  874. objFullPath.c_str(),
  875. srcFullPath.c_str());
  876. }
  877. }
  878. //----------------------------------------------------------------------------
  879. void
  880. cmMakefileTargetGenerator
  881. ::GenerateExtraOutput(const char* out, const char* in, bool symbolic)
  882. {
  883. // Add a rule to build the primary output if the extra output needs
  884. // to be created.
  885. std::vector<std::string> commands;
  886. std::vector<std::string> depends;
  887. std::string emptyCommand = this->GlobalGenerator->GetEmptyRuleHackCommand();
  888. if(!emptyCommand.empty())
  889. {
  890. commands.push_back(emptyCommand);
  891. }
  892. depends.push_back(in);
  893. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  894. out, depends, commands,
  895. symbolic);
  896. // Register the extra output as paired with the first output so that
  897. // the check-build-system step will remove the primary output if any
  898. // extra outputs are missing. This forces the rule to regenerate
  899. // all outputs.
  900. this->GlobalGenerator->AddMultipleOutputPair(out, in);
  901. }
  902. //----------------------------------------------------------------------------
  903. void
  904. cmMakefileTargetGenerator
  905. ::WriteObjectsVariable(std::string& variableName,
  906. std::string& variableNameExternal)
  907. {
  908. // Write a make variable assignment that lists all objects for the
  909. // target.
  910. variableName =
  911. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  912. "_OBJECTS");
  913. *this->BuildFileStream
  914. << "# Object files for target " << this->Target->GetName() << "\n"
  915. << variableName.c_str() << " =";
  916. std::string object;
  917. const char* objName =
  918. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  919. const char* lineContinue =
  920. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  921. if(!lineContinue)
  922. {
  923. lineContinue = "\\";
  924. }
  925. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  926. i != this->Objects.end(); ++i)
  927. {
  928. if ( this->ExtraContent.find(i->c_str()) != this->ExtraContent.end() )
  929. {
  930. continue;
  931. }
  932. *this->BuildFileStream << " " << lineContinue << "\n";
  933. if(objName)
  934. {
  935. *this->BuildFileStream <<
  936. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  937. cmLocalGenerator::MAKEFILE);
  938. }
  939. else
  940. {
  941. *this->BuildFileStream <<
  942. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  943. }
  944. }
  945. *this->BuildFileStream << "\n";
  946. // Write a make variable assignment that lists all external objects
  947. // for the target.
  948. variableNameExternal =
  949. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  950. "_EXTERNAL_OBJECTS");
  951. *this->BuildFileStream
  952. << "\n"
  953. << "# External object files for target "
  954. << this->Target->GetName() << "\n"
  955. << variableNameExternal.c_str() << " =";
  956. for(std::vector<std::string>::const_iterator i =
  957. this->ExternalObjects.begin();
  958. i != this->ExternalObjects.end(); ++i)
  959. {
  960. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  961. *this->BuildFileStream
  962. << " " << lineContinue << "\n"
  963. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  964. if(objName)
  965. {
  966. *this->BuildFileStream <<
  967. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  968. cmLocalGenerator::MAKEFILE);
  969. }
  970. else
  971. {
  972. *this->BuildFileStream <<
  973. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  974. }
  975. }
  976. *this->BuildFileStream << "\n" << "\n";
  977. }
  978. //----------------------------------------------------------------------------
  979. void
  980. cmMakefileTargetGenerator
  981. ::WriteObjectsString(std::string& buildObjs)
  982. {
  983. std::string object;
  984. const char* no_quoted =
  985. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  986. const char* space = "";
  987. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  988. i != this->Objects.end(); ++i)
  989. {
  990. if ( this->ExtraContent.find(i->c_str()) != this->ExtraContent.end() )
  991. {
  992. continue;
  993. }
  994. buildObjs += space;
  995. space = " ";
  996. if(no_quoted)
  997. {
  998. buildObjs +=
  999. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1000. cmLocalGenerator::SHELL);
  1001. }
  1002. else
  1003. {
  1004. buildObjs +=
  1005. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1006. }
  1007. }
  1008. for(std::vector<std::string>::const_iterator i =
  1009. this->ExternalObjects.begin();
  1010. i != this->ExternalObjects.end(); ++i)
  1011. {
  1012. buildObjs += space;
  1013. space = " ";
  1014. if(no_quoted)
  1015. {
  1016. buildObjs +=
  1017. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1018. cmLocalGenerator::SHELL);
  1019. }
  1020. else
  1021. {
  1022. buildObjs +=
  1023. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1024. }
  1025. }
  1026. }
  1027. //----------------------------------------------------------------------------
  1028. void cmMakefileTargetGenerator::WriteTargetDriverRule(const char* main_output,
  1029. bool relink)
  1030. {
  1031. // Compute the name of the driver target.
  1032. std::string dir =
  1033. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1034. std::string buildTargetRuleName = dir;
  1035. buildTargetRuleName += relink?"/preinstall":"/build";
  1036. buildTargetRuleName = this->Convert(buildTargetRuleName.c_str(),
  1037. cmLocalGenerator::HOME_OUTPUT,
  1038. cmLocalGenerator::UNCHANGED);
  1039. // Build the list of target outputs to drive.
  1040. std::vector<std::string> depends;
  1041. if(main_output)
  1042. {
  1043. depends.push_back(main_output);
  1044. }
  1045. const char* comment = 0;
  1046. if(relink)
  1047. {
  1048. // Setup the comment for the preinstall driver.
  1049. comment = "Rule to relink during preinstall.";
  1050. }
  1051. else
  1052. {
  1053. // Setup the comment for the main build driver.
  1054. comment = "Rule to build all files generated by this target.";
  1055. // Make sure all custom command outputs in this target are built.
  1056. if(this->CustomCommandDriver == OnBuild)
  1057. {
  1058. this->DriveCustomCommands(depends);
  1059. }
  1060. }
  1061. // Write the driver rule.
  1062. std::vector<std::string> no_commands;
  1063. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1064. buildTargetRuleName.c_str(),
  1065. depends, no_commands, true);
  1066. }
  1067. //----------------------------------------------------------------------------
  1068. std::string cmMakefileTargetGenerator::GetFrameworkFlags()
  1069. {
  1070. #ifndef __APPLE__
  1071. return std::string();
  1072. #else
  1073. std::set<cmStdString> emitted;
  1074. emitted.insert("/System/Library/Frameworks");
  1075. std::vector<std::string> includes;
  1076. this->LocalGenerator->GetIncludeDirectories(includes);
  1077. std::vector<std::string>::iterator i;
  1078. // check all include directories for frameworks as this
  1079. // will already have added a -F for the framework
  1080. for(i = includes.begin(); i != includes.end(); ++i)
  1081. {
  1082. if(this->Target->NameResolvesToFramework(i->c_str()))
  1083. {
  1084. std::string frameworkDir = *i;
  1085. frameworkDir += "/../";
  1086. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1087. emitted.insert(frameworkDir);
  1088. }
  1089. }
  1090. std::string flags;
  1091. std::vector<std::string>& frameworks = this->Target->GetFrameworks();
  1092. for(i = frameworks.begin();
  1093. i != frameworks.end(); ++i)
  1094. {
  1095. if(emitted.insert(*i).second)
  1096. {
  1097. flags += "-F";
  1098. flags += this->LocalGenerator->ConvertToOutputForExisting(i->c_str());
  1099. flags += " ";
  1100. }
  1101. }
  1102. return flags;
  1103. #endif
  1104. }
  1105. //----------------------------------------------------------------------------
  1106. void cmMakefileTargetGenerator
  1107. ::AppendTargetDepends(std::vector<std::string>& depends)
  1108. {
  1109. // Static libraries never depend on anything for linking.
  1110. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1111. {
  1112. return;
  1113. }
  1114. // Compute which library configuration to link.
  1115. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  1116. if(cmSystemTools::UpperCase(
  1117. this->LocalGenerator->ConfigurationName.c_str()) == "DEBUG")
  1118. {
  1119. linkType = cmTarget::DEBUG;
  1120. }
  1121. // Keep track of dependencies already listed.
  1122. std::set<cmStdString> emitted;
  1123. // A target should not depend on itself.
  1124. emitted.insert(this->Target->GetName());
  1125. // Loop over all library dependencies.
  1126. const cmTarget::LinkLibraryVectorType& tlibs =
  1127. this->Target->GetLinkLibraries();
  1128. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  1129. lib != tlibs.end(); ++lib)
  1130. {
  1131. // skip the library if it is not general and the link type
  1132. // does not match the current target
  1133. if(lib->second != cmTarget::GENERAL &&
  1134. lib->second != linkType)
  1135. {
  1136. continue;
  1137. }
  1138. // Don't emit the same library twice for this target.
  1139. if(emitted.insert(lib->first).second)
  1140. {
  1141. // Depend on other CMake targets.
  1142. if(cmTarget* tgt =
  1143. this->GlobalGenerator->FindTarget(0, lib->first.c_str(), false))
  1144. {
  1145. if(const char* location =
  1146. tgt->GetLocation(this->LocalGenerator->ConfigurationName.c_str()))
  1147. {
  1148. depends.push_back(location);
  1149. }
  1150. }
  1151. // depend on full path libs as well
  1152. else if(cmSystemTools::FileIsFullPath(lib->first.c_str()))
  1153. {
  1154. depends.push_back(lib->first.c_str());
  1155. }
  1156. }
  1157. }
  1158. }
  1159. //----------------------------------------------------------------------------
  1160. void cmMakefileTargetGenerator
  1161. ::CloseFileStreams()
  1162. {
  1163. delete this->BuildFileStream;
  1164. delete this->InfoFileStream;
  1165. delete this->FlagFileStream;
  1166. }
  1167. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1168. const char* linkLang,
  1169. std::string& linkFlags)
  1170. {
  1171. // check for language flags that are not allowed at link time, and
  1172. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1173. // which fails, there may be more]
  1174. std::string removeFlags = "CMAKE_";
  1175. removeFlags += linkLang;
  1176. removeFlags += flagVar;
  1177. std::string removeflags =
  1178. this->Makefile->GetSafeDefinition(removeFlags.c_str());
  1179. std::vector<std::string> removeList;
  1180. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1181. for(std::vector<std::string>::iterator i = removeList.begin();
  1182. i != removeList.end(); ++i)
  1183. {
  1184. cmSystemTools::ReplaceString(linkFlags, i->c_str(), "");
  1185. }
  1186. }
  1187. void cmMakefileTargetGenerator::WriteProgressVariables(unsigned long total,
  1188. unsigned long &current)
  1189. {
  1190. cmGeneratedFileStream *progressFileStream =
  1191. new cmGeneratedFileStream(this->ProgressFileNameFull.c_str());
  1192. if(!progressFileStream)
  1193. {
  1194. return;
  1195. }
  1196. unsigned long num;
  1197. unsigned long i;
  1198. for (i = 1; i <= this->NumberOfProgressActions; ++i)
  1199. {
  1200. *progressFileStream
  1201. << "CMAKE_PROGRESS_" << i << " = ";
  1202. if (total <= 100)
  1203. {
  1204. num = i + current;
  1205. *progressFileStream << num;
  1206. this->LocalGenerator->ProgressFiles[this->Target->GetName()]
  1207. .push_back(num);
  1208. }
  1209. else if (((i+current)*100)/total > ((i-1+current)*100)/total)
  1210. {
  1211. num = ((i+current)*100)/total;
  1212. *progressFileStream << num;
  1213. this->LocalGenerator->ProgressFiles[this->Target->GetName()]
  1214. .push_back(num);
  1215. }
  1216. *progressFileStream << "\n";
  1217. }
  1218. *progressFileStream << "\n";
  1219. current += this->NumberOfProgressActions;
  1220. delete progressFileStream;
  1221. }