cmMakefileTargetGenerator.cxx 60 KB

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