cmMakefileTargetGenerator.cxx 52 KB

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