cmMakefileTargetGenerator.cxx 69 KB

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