cmMakefileTargetGenerator.cxx 52 KB

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