cmMakefileTargetGenerator.cxx 58 KB

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