cmMakefileTargetGenerator.cxx 59 KB

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