cmMakefileTargetGenerator.cxx 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmMakefileTargetGenerator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmComputeLinkInformation.h"
  6. #include "cmCustomCommand.h"
  7. #include "cmCustomCommandGenerator.h"
  8. #include "cmGeneratedFileStream.h"
  9. #include "cmGeneratorExpression.h"
  10. #include "cmGeneratorTarget.h"
  11. #include "cmGlobalUnixMakefileGenerator3.h"
  12. #include "cmLinkLineComputer.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmLocalUnixMakefileGenerator3.h"
  15. #include "cmMakefile.h"
  16. #include "cmMakefileExecutableTargetGenerator.h"
  17. #include "cmMakefileLibraryTargetGenerator.h"
  18. #include "cmMakefileUtilityTargetGenerator.h"
  19. #include "cmOutputConverter.h"
  20. #include "cmRulePlaceholderExpander.h"
  21. #include "cmSourceFile.h"
  22. #include "cmState.h"
  23. #include "cmSystemTools.h"
  24. #include "cm_auto_ptr.hxx"
  25. #include "cmake.h"
  26. #include <algorithm>
  27. #include <ctype.h>
  28. #include <sstream>
  29. #include <stdio.h>
  30. #include <utility>
  31. #ifndef _WIN32
  32. #include <unistd.h>
  33. #endif
  34. cmMakefileTargetGenerator::cmMakefileTargetGenerator(cmGeneratorTarget* target)
  35. : cmCommonTargetGenerator(target)
  36. , OSXBundleGenerator(CM_NULLPTR)
  37. , MacOSXContentGenerator(CM_NULLPTR)
  38. {
  39. this->BuildFileStream = CM_NULLPTR;
  40. this->InfoFileStream = CM_NULLPTR;
  41. this->FlagFileStream = CM_NULLPTR;
  42. this->CustomCommandDriver = OnBuild;
  43. this->LocalGenerator =
  44. static_cast<cmLocalUnixMakefileGenerator3*>(target->GetLocalGenerator());
  45. this->GlobalGenerator = static_cast<cmGlobalUnixMakefileGenerator3*>(
  46. this->LocalGenerator->GetGlobalGenerator());
  47. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  48. this->NoRuleMessages = false;
  49. if (const char* ruleStatus =
  50. cm->GetState()->GetGlobalProperty("RULE_MESSAGES")) {
  51. this->NoRuleMessages = cmSystemTools::IsOff(ruleStatus);
  52. }
  53. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  54. }
  55. cmMakefileTargetGenerator::~cmMakefileTargetGenerator()
  56. {
  57. delete MacOSXContentGenerator;
  58. }
  59. cmMakefileTargetGenerator* cmMakefileTargetGenerator::New(
  60. cmGeneratorTarget* tgt)
  61. {
  62. cmMakefileTargetGenerator* result = CM_NULLPTR;
  63. switch (tgt->GetType()) {
  64. case cmStateEnums::EXECUTABLE:
  65. result = new cmMakefileExecutableTargetGenerator(tgt);
  66. break;
  67. case cmStateEnums::STATIC_LIBRARY:
  68. case cmStateEnums::SHARED_LIBRARY:
  69. case cmStateEnums::MODULE_LIBRARY:
  70. case cmStateEnums::OBJECT_LIBRARY:
  71. result = new cmMakefileLibraryTargetGenerator(tgt);
  72. break;
  73. case cmStateEnums::UTILITY:
  74. result = new cmMakefileUtilityTargetGenerator(tgt);
  75. break;
  76. default:
  77. return result;
  78. // break; /* unreachable */
  79. }
  80. return result;
  81. }
  82. void cmMakefileTargetGenerator::CreateRuleFile()
  83. {
  84. // Create a directory for this target.
  85. this->TargetBuildDirectory =
  86. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  87. this->TargetBuildDirectoryFull =
  88. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  89. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  90. // Construct the rule file name.
  91. this->BuildFileName = this->TargetBuildDirectory;
  92. this->BuildFileName += "/build.make";
  93. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  94. this->BuildFileNameFull += "/build.make";
  95. // Construct the rule file name.
  96. this->ProgressFileNameFull = this->TargetBuildDirectoryFull;
  97. this->ProgressFileNameFull += "/progress.make";
  98. // reset the progress count
  99. this->NumberOfProgressActions = 0;
  100. // Open the rule file. This should be copy-if-different because the
  101. // rules may depend on this file itself.
  102. this->BuildFileStream =
  103. new cmGeneratedFileStream(this->BuildFileNameFull.c_str(), false,
  104. this->GlobalGenerator->GetMakefileEncoding());
  105. this->BuildFileStream->SetCopyIfDifferent(true);
  106. if (!this->BuildFileStream) {
  107. return;
  108. }
  109. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  110. if (this->GlobalGenerator->AllowDeleteOnError()) {
  111. std::vector<std::string> no_depends;
  112. std::vector<std::string> no_commands;
  113. this->LocalGenerator->WriteMakeRule(
  114. *this->BuildFileStream, "Delete rule output on recipe failure.",
  115. ".DELETE_ON_ERROR", no_depends, no_commands, false);
  116. }
  117. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  118. }
  119. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  120. {
  121. const std::string& config =
  122. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  123. // write the custom commands for this target
  124. // Look for files registered for cleaning in this directory.
  125. if (const char* additional_clean_files =
  126. this->Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES")) {
  127. cmGeneratorExpression ge;
  128. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge =
  129. ge.Parse(additional_clean_files);
  130. cmSystemTools::ExpandListArgument(
  131. cge->Evaluate(this->LocalGenerator, config, false, this->GeneratorTarget,
  132. CM_NULLPTR, CM_NULLPTR),
  133. this->CleanFiles);
  134. }
  135. // add custom commands to the clean rules?
  136. const char* clean_no_custom = this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  137. bool clean = cmSystemTools::IsOff(clean_no_custom);
  138. // First generate the object rule files. Save a list of all object
  139. // files for this target.
  140. std::vector<cmSourceFile const*> customCommands;
  141. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  142. std::string currentBinDir =
  143. this->LocalGenerator->GetCurrentBinaryDirectory();
  144. for (std::vector<cmSourceFile const*>::const_iterator si =
  145. customCommands.begin();
  146. si != customCommands.end(); ++si) {
  147. cmCustomCommandGenerator ccg(*(*si)->GetCustomCommand(), this->ConfigName,
  148. this->LocalGenerator);
  149. this->GenerateCustomRuleFile(ccg);
  150. if (clean) {
  151. const std::vector<std::string>& outputs = ccg.GetOutputs();
  152. for (std::vector<std::string>::const_iterator o = outputs.begin();
  153. o != outputs.end(); ++o) {
  154. this->CleanFiles.push_back(
  155. this->LocalGenerator->MaybeConvertToRelativePath(currentBinDir, *o));
  156. }
  157. }
  158. }
  159. std::vector<cmSourceFile const*> headerSources;
  160. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  161. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  162. headerSources, this->MacOSXContentGenerator);
  163. std::vector<cmSourceFile const*> extraSources;
  164. this->GeneratorTarget->GetExtraSources(extraSources, config);
  165. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  166. extraSources, this->MacOSXContentGenerator);
  167. std::vector<cmSourceFile const*> externalObjects;
  168. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  169. for (std::vector<cmSourceFile const*>::const_iterator si =
  170. externalObjects.begin();
  171. si != externalObjects.end(); ++si) {
  172. this->ExternalObjects.push_back((*si)->GetFullPath());
  173. }
  174. std::vector<cmSourceFile const*> objectSources;
  175. this->GeneratorTarget->GetObjectSources(objectSources, config);
  176. for (std::vector<cmSourceFile const*>::const_iterator si =
  177. objectSources.begin();
  178. si != objectSources.end(); ++si) {
  179. // Generate this object file's rule file.
  180. this->WriteObjectRuleFiles(**si);
  181. }
  182. }
  183. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  184. {
  185. const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")
  186. ? "$(CMAKE_BINARY_DIR)/"
  187. : "");
  188. // Include the dependencies for the target.
  189. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  190. dependFileNameFull += "/depend.make";
  191. *this->BuildFileStream
  192. << "# Include any dependencies generated for this target.\n"
  193. << this->GlobalGenerator->IncludeDirective << " " << root
  194. << cmSystemTools::ConvertToOutputPath(
  195. this->LocalGenerator
  196. ->MaybeConvertToRelativePath(
  197. this->LocalGenerator->GetBinaryDirectory(), dependFileNameFull)
  198. .c_str())
  199. << "\n\n";
  200. if (!this->NoRuleMessages) {
  201. // Include the progress variables for the target.
  202. *this->BuildFileStream
  203. << "# Include the progress variables for this target.\n"
  204. << this->GlobalGenerator->IncludeDirective << " " << root
  205. << cmSystemTools::ConvertToOutputPath(
  206. this->LocalGenerator
  207. ->MaybeConvertToRelativePath(
  208. this->LocalGenerator->GetBinaryDirectory(),
  209. this->ProgressFileNameFull)
  210. .c_str())
  211. << "\n\n";
  212. }
  213. // make sure the depend file exists
  214. if (!cmSystemTools::FileExists(dependFileNameFull.c_str())) {
  215. // Write an empty dependency file.
  216. cmGeneratedFileStream depFileStream(
  217. dependFileNameFull.c_str(), false,
  218. this->GlobalGenerator->GetMakefileEncoding());
  219. depFileStream << "# Empty dependencies file for "
  220. << this->GeneratorTarget->GetName() << ".\n"
  221. << "# This may be replaced when dependencies are built."
  222. << std::endl;
  223. }
  224. // Open the flags file. This should be copy-if-different because the
  225. // rules may depend on this file itself.
  226. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  227. this->FlagFileNameFull += "/flags.make";
  228. this->FlagFileStream =
  229. new cmGeneratedFileStream(this->FlagFileNameFull.c_str(), false,
  230. this->GlobalGenerator->GetMakefileEncoding());
  231. this->FlagFileStream->SetCopyIfDifferent(true);
  232. if (!this->FlagFileStream) {
  233. return;
  234. }
  235. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  236. // Include the flags for the target.
  237. *this->BuildFileStream
  238. << "# Include the compile flags for this target's objects.\n"
  239. << this->GlobalGenerator->IncludeDirective << " " << root
  240. << cmSystemTools::ConvertToOutputPath(
  241. this->LocalGenerator
  242. ->MaybeConvertToRelativePath(
  243. this->LocalGenerator->GetBinaryDirectory(),
  244. this->FlagFileNameFull)
  245. .c_str())
  246. << "\n\n";
  247. }
  248. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  249. {
  250. // write language flags for target
  251. std::set<std::string> languages;
  252. this->GeneratorTarget->GetLanguages(
  253. languages, this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  254. // put the compiler in the rules.make file so that if it changes
  255. // things rebuild
  256. for (std::set<std::string>::const_iterator l = languages.begin();
  257. l != languages.end(); ++l) {
  258. std::string compiler = "CMAKE_";
  259. compiler += *l;
  260. compiler += "_COMPILER";
  261. *this->FlagFileStream << "# compile " << *l << " with "
  262. << this->Makefile->GetSafeDefinition(compiler)
  263. << "\n";
  264. }
  265. for (std::set<std::string>::const_iterator l = languages.begin();
  266. l != languages.end(); ++l) {
  267. std::string flags = this->GetFlags(*l);
  268. std::string defines = this->GetDefines(*l);
  269. std::string includes = this->GetIncludes(*l);
  270. // Escape comment characters so they do not terminate assignment.
  271. cmSystemTools::ReplaceString(flags, "#", "\\#");
  272. cmSystemTools::ReplaceString(defines, "#", "\\#");
  273. cmSystemTools::ReplaceString(includes, "#", "\\#");
  274. *this->FlagFileStream << *l << "_FLAGS = " << flags << "\n\n";
  275. *this->FlagFileStream << *l << "_DEFINES = " << defines << "\n\n";
  276. *this->FlagFileStream << *l << "_INCLUDES = " << includes << "\n\n";
  277. }
  278. }
  279. void cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()(
  280. cmSourceFile const& source, const char* pkgloc)
  281. {
  282. // Skip OS X content when not building a Framework or Bundle.
  283. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  284. return;
  285. }
  286. std::string macdir =
  287. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  288. // Get the input file location.
  289. std::string const& input = source.GetFullPath();
  290. // Get the output file location.
  291. std::string output = macdir;
  292. output += "/";
  293. output += cmSystemTools::GetFilenameName(input);
  294. this->Generator->CleanFiles.push_back(
  295. this->Generator->LocalGenerator->MaybeConvertToRelativePath(
  296. this->Generator->LocalGenerator->GetCurrentBinaryDirectory(), output));
  297. output = this->Generator->LocalGenerator->MaybeConvertToRelativePath(
  298. this->Generator->LocalGenerator->GetBinaryDirectory(), output);
  299. // Create a rule to copy the content into the bundle.
  300. std::vector<std::string> depends;
  301. std::vector<std::string> commands;
  302. depends.push_back(input);
  303. std::string copyEcho = "Copying OS X content ";
  304. copyEcho += output;
  305. this->Generator->LocalGenerator->AppendEcho(
  306. commands, copyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
  307. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  308. copyCommand += this->Generator->LocalGenerator->ConvertToOutputFormat(
  309. input, cmOutputConverter::SHELL);
  310. copyCommand += " ";
  311. copyCommand += this->Generator->LocalGenerator->ConvertToOutputFormat(
  312. output, cmOutputConverter::SHELL);
  313. commands.push_back(copyCommand);
  314. this->Generator->LocalGenerator->WriteMakeRule(
  315. *this->Generator->BuildFileStream, CM_NULLPTR, output, depends, commands,
  316. false);
  317. this->Generator->ExtraFiles.insert(output);
  318. }
  319. void cmMakefileTargetGenerator::WriteObjectRuleFiles(
  320. cmSourceFile const& source)
  321. {
  322. // Identify the language of the source file.
  323. const std::string& lang =
  324. this->LocalGenerator->GetSourceFileLanguage(source);
  325. if (lang.empty()) {
  326. // don't know anything about this file so skip it
  327. return;
  328. }
  329. // Get the full path name of the object file.
  330. std::string const& objectName =
  331. this->GeneratorTarget->GetObjectName(&source);
  332. std::string obj =
  333. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  334. obj += "/";
  335. obj += objectName;
  336. // Avoid generating duplicate rules.
  337. if (this->ObjectFiles.find(obj) == this->ObjectFiles.end()) {
  338. this->ObjectFiles.insert(obj);
  339. } else {
  340. std::ostringstream err;
  341. err << "Warning: Source file \"" << source.GetFullPath()
  342. << "\" is listed multiple times for target \""
  343. << this->GeneratorTarget->GetName() << "\".";
  344. cmSystemTools::Message(err.str().c_str(), "Warning");
  345. return;
  346. }
  347. // Create the directory containing the object file. This may be a
  348. // subdirectory under the target's directory.
  349. std::string dir = cmSystemTools::GetFilenamePath(obj);
  350. cmSystemTools::MakeDirectory(
  351. this->LocalGenerator->ConvertToFullPath(dir).c_str());
  352. // Save this in the target's list of object files.
  353. this->Objects.push_back(obj);
  354. this->CleanFiles.push_back(obj);
  355. // TODO: Remove
  356. // std::string relativeObj
  357. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  358. // relativeObj += obj;
  359. // we compute some depends when writing the depend.make that we will also
  360. // use in the build.make, same with depMakeFile
  361. std::vector<std::string> depends;
  362. // generate the build rule file
  363. this->WriteObjectBuildFile(obj, lang, source, depends);
  364. // The object file should be checked for dependency integrity.
  365. std::string objFullPath = this->LocalGenerator->GetCurrentBinaryDirectory();
  366. objFullPath += "/";
  367. objFullPath += obj;
  368. objFullPath = cmSystemTools::CollapseFullPath(objFullPath);
  369. std::string srcFullPath =
  370. cmSystemTools::CollapseFullPath(source.GetFullPath());
  371. this->LocalGenerator->AddImplicitDepends(
  372. this->GeneratorTarget, lang, objFullPath.c_str(), srcFullPath.c_str());
  373. }
  374. void cmMakefileTargetGenerator::WriteObjectBuildFile(
  375. std::string& obj, const std::string& lang, cmSourceFile const& source,
  376. std::vector<std::string>& depends)
  377. {
  378. this->LocalGenerator->AppendRuleDepend(depends,
  379. this->FlagFileNameFull.c_str());
  380. this->LocalGenerator->AppendRuleDepends(depends,
  381. this->FlagFileDepends[lang]);
  382. // generate the depend scanning rule
  383. this->WriteObjectDependRules(source, depends);
  384. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  385. relativeObj += obj;
  386. // Write the build rule.
  387. // Build the set of compiler flags.
  388. std::string flags;
  389. // Add language-specific flags.
  390. std::string langFlags = "$(";
  391. langFlags += lang;
  392. langFlags += "_FLAGS)";
  393. this->LocalGenerator->AppendFlags(flags, langFlags);
  394. std::string configUpper =
  395. cmSystemTools::UpperCase(this->LocalGenerator->GetConfigName());
  396. // Add Fortran format flags.
  397. if (lang == "Fortran") {
  398. this->AppendFortranFormatFlags(flags, source);
  399. }
  400. // Add flags from source file properties.
  401. if (source.GetProperty("COMPILE_FLAGS")) {
  402. this->LocalGenerator->AppendFlags(flags,
  403. source.GetProperty("COMPILE_FLAGS"));
  404. *this->FlagFileStream << "# Custom flags: " << relativeObj
  405. << "_FLAGS = " << source.GetProperty("COMPILE_FLAGS")
  406. << "\n"
  407. << "\n";
  408. }
  409. // Add language-specific defines.
  410. std::set<std::string> defines;
  411. // Add source-sepcific preprocessor definitions.
  412. if (const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS")) {
  413. this->LocalGenerator->AppendDefines(defines, compile_defs);
  414. *this->FlagFileStream << "# Custom defines: " << relativeObj
  415. << "_DEFINES = " << compile_defs << "\n"
  416. << "\n";
  417. }
  418. std::string defPropName = "COMPILE_DEFINITIONS_";
  419. defPropName += configUpper;
  420. if (const char* config_compile_defs = source.GetProperty(defPropName)) {
  421. this->LocalGenerator->AppendDefines(defines, config_compile_defs);
  422. *this->FlagFileStream << "# Custom defines: " << relativeObj << "_DEFINES_"
  423. << configUpper << " = " << config_compile_defs
  424. << "\n"
  425. << "\n";
  426. }
  427. // Get the output paths for source and object files.
  428. std::string sourceFile = this->LocalGenerator->ConvertToOutputFormat(
  429. source.GetFullPath(), cmOutputConverter::SHELL);
  430. // Construct the build message.
  431. std::vector<std::string> no_commands;
  432. std::vector<std::string> commands;
  433. // add in a progress call if needed
  434. this->NumberOfProgressActions++;
  435. if (!this->NoRuleMessages) {
  436. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  437. this->MakeEchoProgress(progress);
  438. std::string buildEcho = "Building ";
  439. buildEcho += lang;
  440. buildEcho += " object ";
  441. buildEcho += relativeObj;
  442. this->LocalGenerator->AppendEcho(commands, buildEcho,
  443. cmLocalUnixMakefileGenerator3::EchoBuild,
  444. &progress);
  445. }
  446. std::string targetOutPathReal;
  447. std::string targetOutPathPDB;
  448. std::string targetOutPathCompilePDB;
  449. {
  450. std::string targetFullPathReal;
  451. std::string targetFullPathPDB;
  452. std::string targetFullPathCompilePDB;
  453. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  454. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  455. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  456. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  457. targetFullPathReal =
  458. this->GeneratorTarget->GetFullPath(this->ConfigName, false, true);
  459. targetFullPathPDB =
  460. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  461. targetFullPathPDB += "/";
  462. targetFullPathPDB += this->GeneratorTarget->GetPDBName(this->ConfigName);
  463. }
  464. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  465. targetFullPathCompilePDB =
  466. this->GeneratorTarget->GetCompilePDBPath(this->ConfigName);
  467. if (targetFullPathCompilePDB.empty()) {
  468. targetFullPathCompilePDB =
  469. this->GeneratorTarget->GetSupportDirectory() + "/";
  470. }
  471. }
  472. targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  473. this->LocalGenerator->MaybeConvertToRelativePath(
  474. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  475. cmOutputConverter::SHELL);
  476. targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  477. targetFullPathPDB, cmOutputConverter::SHELL);
  478. targetOutPathCompilePDB = this->LocalGenerator->ConvertToOutputFormat(
  479. this->LocalGenerator->MaybeConvertToRelativePath(
  480. this->LocalGenerator->GetCurrentBinaryDirectory(),
  481. targetFullPathCompilePDB),
  482. cmOutputConverter::SHELL);
  483. if (this->LocalGenerator->IsMinGWMake() &&
  484. cmHasLiteralSuffix(targetOutPathCompilePDB, "\\")) {
  485. // mingw32-make incorrectly interprets 'a\ b c' as 'a b' and 'c'
  486. // (but 'a\ b "c"' as 'a\', 'b', and 'c'!). Workaround this by
  487. // avoiding a trailing backslash in the argument.
  488. targetOutPathCompilePDB[targetOutPathCompilePDB.size() - 1] = '/';
  489. }
  490. }
  491. cmRulePlaceholderExpander::RuleVariables vars;
  492. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  493. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  494. vars.CMTargetType =
  495. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  496. vars.Language = lang.c_str();
  497. vars.Target = targetOutPathReal.c_str();
  498. vars.TargetPDB = targetOutPathPDB.c_str();
  499. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  500. vars.Source = sourceFile.c_str();
  501. std::string shellObj =
  502. this->LocalGenerator->ConvertToOutputFormat(obj, cmOutputConverter::SHELL);
  503. vars.Object = shellObj.c_str();
  504. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  505. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  506. this->LocalGenerator->MaybeConvertToRelativePath(
  507. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  508. cmOutputConverter::SHELL);
  509. vars.ObjectDir = objectDir.c_str();
  510. std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
  511. objectFileDir = this->LocalGenerator->ConvertToOutputFormat(
  512. this->LocalGenerator->MaybeConvertToRelativePath(
  513. this->LocalGenerator->GetCurrentBinaryDirectory(), objectFileDir),
  514. cmOutputConverter::SHELL);
  515. vars.ObjectFileDir = objectFileDir.c_str();
  516. vars.Flags = flags.c_str();
  517. std::string definesString = "$(";
  518. definesString += lang;
  519. definesString += "_DEFINES)";
  520. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  521. vars.Defines = definesString.c_str();
  522. std::string const includesString = "$(" + lang + "_INCLUDES)";
  523. vars.Includes = includesString.c_str();
  524. // At the moment, it is assumed that C, C++, and Fortran have both
  525. // assembly and preprocessor capabilities. The same is true for the
  526. // ability to export compile commands
  527. bool lang_has_preprocessor =
  528. ((lang == "C") || (lang == "CXX") || (lang == "Fortran"));
  529. bool const lang_has_assembly = lang_has_preprocessor;
  530. bool const lang_can_export_cmds = lang_has_preprocessor;
  531. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  532. this->LocalGenerator->CreateRulePlaceholderExpander());
  533. // Construct the compile rules.
  534. {
  535. std::string compileRuleVar = "CMAKE_";
  536. compileRuleVar += lang;
  537. compileRuleVar += "_COMPILE_OBJECT";
  538. std::string compileRule =
  539. this->Makefile->GetRequiredDefinition(compileRuleVar);
  540. std::vector<std::string> compileCommands;
  541. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  542. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  543. lang_can_export_cmds && compileCommands.size() == 1) {
  544. std::string compileCommand = compileCommands[0];
  545. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  546. compileCommand, vars);
  547. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  548. this->LocalGenerator->GetCurrentBinaryDirectory());
  549. compileCommand.replace(compileCommand.find(langFlags), langFlags.size(),
  550. this->GetFlags(lang));
  551. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  552. compileCommand.replace(compileCommand.find(langDefines),
  553. langDefines.size(), this->GetDefines(lang));
  554. std::string langIncludes = std::string("$(") + lang + "_INCLUDES)";
  555. compileCommand.replace(compileCommand.find(langIncludes),
  556. langIncludes.size(), this->GetIncludes(lang));
  557. this->GlobalGenerator->AddCXXCompileCommand(
  558. source.GetFullPath(), workingDirectory, compileCommand);
  559. }
  560. // Maybe insert an include-what-you-use runner.
  561. if (!compileCommands.empty() && (lang == "C" || lang == "CXX")) {
  562. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  563. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  564. std::string const tidy_prop = lang + "_CLANG_TIDY";
  565. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  566. if ((iwyu && *iwyu) || (tidy && *tidy)) {
  567. std::string run_iwyu = "$(CMAKE_COMMAND) -E __run_iwyu";
  568. if (iwyu && *iwyu) {
  569. run_iwyu += " --iwyu=";
  570. run_iwyu += this->LocalGenerator->EscapeForShell(iwyu);
  571. }
  572. if (tidy && *tidy) {
  573. run_iwyu += " --tidy=";
  574. run_iwyu += this->LocalGenerator->EscapeForShell(tidy);
  575. run_iwyu += " --source=";
  576. run_iwyu += sourceFile;
  577. }
  578. run_iwyu += " -- ";
  579. compileCommands.front().insert(0, run_iwyu);
  580. }
  581. }
  582. // Maybe insert a compiler launcher like ccache or distcc
  583. if (!compileCommands.empty() && (lang == "C" || lang == "CXX")) {
  584. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  585. const char* clauncher =
  586. this->GeneratorTarget->GetProperty(clauncher_prop);
  587. if (clauncher && *clauncher) {
  588. std::vector<std::string> launcher_cmd;
  589. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  590. for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
  591. e = launcher_cmd.end();
  592. i != e; ++i) {
  593. *i = this->LocalGenerator->EscapeForShell(*i);
  594. }
  595. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  596. compileCommands.front().insert(0, run_launcher);
  597. }
  598. }
  599. // Expand placeholders in the commands.
  600. for (std::vector<std::string>::iterator i = compileCommands.begin();
  601. i != compileCommands.end(); ++i) {
  602. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  603. vars);
  604. }
  605. // Change the command working directory to the local build tree.
  606. this->LocalGenerator->CreateCDCommand(
  607. compileCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
  608. this->LocalGenerator->GetBinaryDirectory());
  609. commands.insert(commands.end(), compileCommands.begin(),
  610. compileCommands.end());
  611. }
  612. // Check for extra outputs created by the compilation.
  613. std::vector<std::string> outputs(1, relativeObj);
  614. if (const char* extra_outputs_str = source.GetProperty("OBJECT_OUTPUTS")) {
  615. // Register these as extra files to clean.
  616. cmSystemTools::ExpandListArgument(extra_outputs_str, outputs);
  617. this->CleanFiles.insert(this->CleanFiles.end(), outputs.begin() + 1,
  618. outputs.end());
  619. }
  620. // Write the rule.
  621. this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
  622. commands);
  623. bool do_preprocess_rules = lang_has_preprocessor &&
  624. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  625. bool do_assembly_rules =
  626. lang_has_assembly && this->LocalGenerator->GetCreateAssemblySourceRules();
  627. if (do_preprocess_rules || do_assembly_rules) {
  628. std::vector<std::string> force_depends;
  629. force_depends.push_back("cmake_force");
  630. std::string::size_type dot_pos = relativeObj.rfind('.');
  631. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  632. dot_pos = obj.rfind('.');
  633. std::string objBase = obj.substr(0, dot_pos);
  634. if (do_preprocess_rules) {
  635. commands.clear();
  636. std::string relativeObjI = relativeObjBase + ".i";
  637. std::string objI = objBase + ".i";
  638. std::string preprocessEcho = "Preprocessing ";
  639. preprocessEcho += lang;
  640. preprocessEcho += " source to ";
  641. preprocessEcho += objI;
  642. this->LocalGenerator->AppendEcho(
  643. commands, preprocessEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
  644. std::string preprocessRuleVar = "CMAKE_";
  645. preprocessRuleVar += lang;
  646. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  647. if (const char* preprocessRule =
  648. this->Makefile->GetDefinition(preprocessRuleVar)) {
  649. std::vector<std::string> preprocessCommands;
  650. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  651. std::string shellObjI = this->LocalGenerator->ConvertToOutputFormat(
  652. objI, cmOutputConverter::SHELL);
  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. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  658. *i, vars);
  659. }
  660. this->LocalGenerator->CreateCDCommand(
  661. preprocessCommands,
  662. this->LocalGenerator->GetCurrentBinaryDirectory(),
  663. this->LocalGenerator->GetBinaryDirectory());
  664. commands.insert(commands.end(), preprocessCommands.begin(),
  665. preprocessCommands.end());
  666. } else {
  667. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  668. cmd += preprocessRuleVar;
  669. commands.push_back(cmd);
  670. }
  671. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  672. relativeObjI, force_depends,
  673. commands, false);
  674. }
  675. if (do_assembly_rules) {
  676. commands.clear();
  677. std::string relativeObjS = relativeObjBase + ".s";
  678. std::string objS = objBase + ".s";
  679. std::string assemblyEcho = "Compiling ";
  680. assemblyEcho += lang;
  681. assemblyEcho += " source to assembly ";
  682. assemblyEcho += objS;
  683. this->LocalGenerator->AppendEcho(
  684. commands, assemblyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
  685. std::string assemblyRuleVar = "CMAKE_";
  686. assemblyRuleVar += lang;
  687. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  688. if (const char* assemblyRule =
  689. this->Makefile->GetDefinition(assemblyRuleVar)) {
  690. std::vector<std::string> assemblyCommands;
  691. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  692. std::string shellObjS = this->LocalGenerator->ConvertToOutputFormat(
  693. objS, cmOutputConverter::SHELL);
  694. vars.AssemblySource = shellObjS.c_str();
  695. // Expand placeholders in the commands.
  696. for (std::vector<std::string>::iterator i = assemblyCommands.begin();
  697. i != assemblyCommands.end(); ++i) {
  698. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  699. *i, vars);
  700. }
  701. this->LocalGenerator->CreateCDCommand(
  702. assemblyCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
  703. this->LocalGenerator->GetBinaryDirectory());
  704. commands.insert(commands.end(), assemblyCommands.begin(),
  705. assemblyCommands.end());
  706. } else {
  707. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  708. cmd += assemblyRuleVar;
  709. commands.push_back(cmd);
  710. }
  711. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  712. relativeObjS, force_depends,
  713. commands, false);
  714. }
  715. }
  716. // If the language needs provides-requires mode, create the
  717. // corresponding targets.
  718. std::string objectRequires = relativeObj;
  719. objectRequires += ".requires";
  720. std::vector<std::string> p_depends;
  721. // always provide an empty requires target
  722. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  723. objectRequires, p_depends, no_commands,
  724. true);
  725. // write a build rule to recursively build what this obj provides
  726. std::string objectProvides = relativeObj;
  727. objectProvides += ".provides";
  728. std::string temp = relativeObj;
  729. temp += ".provides.build";
  730. std::vector<std::string> r_commands;
  731. std::string tgtMakefileName =
  732. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  733. tgtMakefileName += "/build.make";
  734. r_commands.push_back(
  735. this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(), temp));
  736. p_depends.clear();
  737. p_depends.push_back(objectRequires);
  738. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  739. objectProvides, p_depends, r_commands,
  740. true);
  741. // write the provides.build rule dependency on the obj file
  742. p_depends.clear();
  743. p_depends.push_back(relativeObj);
  744. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, temp,
  745. p_depends, no_commands, false);
  746. }
  747. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  748. {
  749. std::vector<std::string> depends;
  750. std::vector<std::string> no_commands;
  751. // Construct the name of the dependency generation target.
  752. std::string depTarget =
  753. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  754. depTarget += "/requires";
  755. // This target drives dependency generation for all object files.
  756. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  757. std::string objTarget;
  758. for (std::vector<std::string>::const_iterator obj = this->Objects.begin();
  759. obj != this->Objects.end(); ++obj) {
  760. objTarget = relPath;
  761. objTarget += *obj;
  762. objTarget += ".requires";
  763. depends.push_back(objTarget);
  764. }
  765. // Write the rule.
  766. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  767. depTarget, depends, no_commands, true);
  768. }
  769. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  770. {
  771. std::vector<std::string> depends;
  772. std::vector<std::string> commands;
  773. // Construct the clean target name.
  774. std::string cleanTarget =
  775. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  776. cleanTarget += "/clean";
  777. // Construct the clean command.
  778. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  779. this->GeneratorTarget);
  780. this->LocalGenerator->CreateCDCommand(
  781. commands, this->LocalGenerator->GetCurrentBinaryDirectory(),
  782. this->LocalGenerator->GetBinaryDirectory());
  783. // Write the rule.
  784. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  785. cleanTarget, depends, commands, true);
  786. }
  787. bool cmMakefileTargetGenerator::WriteMakeRule(
  788. std::ostream& os, const char* comment,
  789. const std::vector<std::string>& outputs,
  790. const std::vector<std::string>& depends,
  791. const std::vector<std::string>& commands, bool in_help)
  792. {
  793. bool symbolic = false;
  794. if (outputs.empty()) {
  795. return symbolic;
  796. }
  797. // Check whether we need to bother checking for a symbolic output.
  798. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  799. // Check whether the first output is marked as symbolic.
  800. if (need_symbolic) {
  801. if (cmSourceFile* sf = this->Makefile->GetSource(outputs[0])) {
  802. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  803. }
  804. }
  805. // We always attach the actual commands to the first output.
  806. this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
  807. commands, symbolic, in_help);
  808. // For single outputs, we are done.
  809. if (outputs.size() == 1) {
  810. return symbolic;
  811. }
  812. // For multiple outputs, make the extra ones depend on the first one.
  813. std::vector<std::string> const output_depends(1, outputs[0]);
  814. std::string binDir = this->LocalGenerator->GetBinaryDirectory();
  815. for (std::vector<std::string>::const_iterator o = outputs.begin() + 1;
  816. o != outputs.end(); ++o) {
  817. // Touch the extra output so "make" knows that it was updated,
  818. // but only if the output was acually created.
  819. std::string const out = this->LocalGenerator->ConvertToOutputFormat(
  820. this->LocalGenerator->MaybeConvertToRelativePath(binDir, *o),
  821. cmOutputConverter::SHELL);
  822. std::vector<std::string> output_commands;
  823. bool o_symbolic = false;
  824. if (need_symbolic) {
  825. if (cmSourceFile* sf = this->Makefile->GetSource(*o)) {
  826. o_symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  827. }
  828. }
  829. symbolic = symbolic && o_symbolic;
  830. if (!o_symbolic) {
  831. output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
  832. }
  833. this->LocalGenerator->WriteMakeRule(os, CM_NULLPTR, *o, output_depends,
  834. output_commands, o_symbolic, in_help);
  835. if (!o_symbolic) {
  836. // At build time, remove the first output if this one does not exist
  837. // so that "make" will rerun the real commands that create this one.
  838. MultipleOutputPairsType::value_type p(*o, outputs[0]);
  839. this->MultipleOutputPairs.insert(p);
  840. }
  841. }
  842. return symbolic;
  843. }
  844. void cmMakefileTargetGenerator::WriteTargetDependRules()
  845. {
  846. // must write the targets depend info file
  847. std::string dir =
  848. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  849. this->InfoFileNameFull = dir;
  850. this->InfoFileNameFull += "/DependInfo.cmake";
  851. this->InfoFileNameFull =
  852. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  853. this->InfoFileStream =
  854. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  855. this->InfoFileStream->SetCopyIfDifferent(true);
  856. if (!*this->InfoFileStream) {
  857. return;
  858. }
  859. this->LocalGenerator->WriteDependLanguageInfo(*this->InfoFileStream,
  860. this->GeneratorTarget);
  861. // Store multiple output pairs in the depend info file.
  862. if (!this->MultipleOutputPairs.empty()) {
  863. /* clang-format off */
  864. *this->InfoFileStream
  865. << "\n"
  866. << "# Pairs of files generated by the same build rule.\n"
  867. << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  868. /* clang-format on */
  869. for (MultipleOutputPairsType::const_iterator pi =
  870. this->MultipleOutputPairs.begin();
  871. pi != this->MultipleOutputPairs.end(); ++pi) {
  872. *this->InfoFileStream
  873. << " " << cmOutputConverter::EscapeForCMake(pi->first) << " "
  874. << cmOutputConverter::EscapeForCMake(pi->second) << "\n";
  875. }
  876. *this->InfoFileStream << " )\n\n";
  877. }
  878. // Store list of targets linked directly or transitively.
  879. {
  880. /* clang-format off */
  881. *this->InfoFileStream
  882. << "\n"
  883. << "# Targets to which this target links.\n"
  884. << "set(CMAKE_TARGET_LINKED_INFO_FILES\n";
  885. /* clang-format on */
  886. std::vector<std::string> dirs = this->GetLinkedTargetDirectories();
  887. for (std::vector<std::string>::iterator i = dirs.begin(); i != dirs.end();
  888. ++i) {
  889. *this->InfoFileStream << " \"" << *i << "/DependInfo.cmake\"\n";
  890. }
  891. *this->InfoFileStream << " )\n";
  892. }
  893. std::string const& working_dir =
  894. this->LocalGenerator->GetCurrentBinaryDirectory();
  895. /* clang-format off */
  896. *this->InfoFileStream
  897. << "\n"
  898. << "# Fortran module output directory.\n"
  899. << "set(CMAKE_Fortran_TARGET_MODULE_DIR \""
  900. << this->GeneratorTarget->GetFortranModuleDirectory(working_dir)
  901. << "\")\n";
  902. /* clang-format on */
  903. // and now write the rule to use it
  904. std::vector<std::string> depends;
  905. std::vector<std::string> commands;
  906. // Construct the name of the dependency generation target.
  907. std::string depTarget =
  908. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  909. depTarget += "/depend";
  910. // Add a command to call CMake to scan dependencies. CMake will
  911. // touch the corresponding depends file after scanning dependencies.
  912. std::ostringstream depCmd;
  913. // TODO: Account for source file properties and directory-level
  914. // definitions when scanning for dependencies.
  915. #if !defined(_WIN32) || defined(__CYGWIN__)
  916. // This platform supports symlinks, so cmSystemTools will translate
  917. // paths. Make sure PWD is set to the original name of the home
  918. // output directory to help cmSystemTools to create the same
  919. // translation table for the dependency scanning process.
  920. depCmd << "cd " << (this->LocalGenerator->ConvertToOutputFormat(
  921. cmSystemTools::CollapseFullPath(
  922. this->LocalGenerator->GetBinaryDirectory()),
  923. cmOutputConverter::SHELL))
  924. << " && ";
  925. #endif
  926. // Generate a call this signature:
  927. //
  928. // cmake -E cmake_depends <generator>
  929. // <home-src-dir> <start-src-dir>
  930. // <home-out-dir> <start-out-dir>
  931. // <dep-info> --color=$(COLOR)
  932. //
  933. // This gives the dependency scanner enough information to recreate
  934. // the state of our local generator sufficiently for its needs.
  935. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  936. << this->GlobalGenerator->GetName() << "\" "
  937. << this->LocalGenerator->ConvertToOutputFormat(
  938. cmSystemTools::CollapseFullPath(
  939. this->LocalGenerator->GetSourceDirectory()),
  940. cmOutputConverter::SHELL)
  941. << " "
  942. << this->LocalGenerator->ConvertToOutputFormat(
  943. cmSystemTools::CollapseFullPath(
  944. this->LocalGenerator->GetCurrentSourceDirectory()),
  945. cmOutputConverter::SHELL)
  946. << " "
  947. << this->LocalGenerator->ConvertToOutputFormat(
  948. cmSystemTools::CollapseFullPath(
  949. this->LocalGenerator->GetBinaryDirectory()),
  950. cmOutputConverter::SHELL)
  951. << " "
  952. << this->LocalGenerator->ConvertToOutputFormat(
  953. cmSystemTools::CollapseFullPath(
  954. this->LocalGenerator->GetCurrentBinaryDirectory()),
  955. cmOutputConverter::SHELL)
  956. << " "
  957. << this->LocalGenerator->ConvertToOutputFormat(
  958. cmSystemTools::CollapseFullPath(this->InfoFileNameFull),
  959. cmOutputConverter::SHELL);
  960. if (this->LocalGenerator->GetColorMakefile()) {
  961. depCmd << " --color=$(COLOR)";
  962. }
  963. commands.push_back(depCmd.str());
  964. // Make sure all custom command outputs in this target are built.
  965. if (this->CustomCommandDriver == OnDepends) {
  966. this->DriveCustomCommands(depends);
  967. }
  968. // Write the rule.
  969. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  970. depTarget, depends, commands, true);
  971. }
  972. void cmMakefileTargetGenerator::DriveCustomCommands(
  973. std::vector<std::string>& depends)
  974. {
  975. // Depend on all custom command outputs.
  976. std::vector<cmSourceFile*> sources;
  977. this->GeneratorTarget->GetSourceFiles(
  978. sources, this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  979. for (std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  980. source != sources.end(); ++source) {
  981. if (cmCustomCommand* cc = (*source)->GetCustomCommand()) {
  982. cmCustomCommandGenerator ccg(*cc, this->ConfigName,
  983. this->LocalGenerator);
  984. const std::vector<std::string>& outputs = ccg.GetOutputs();
  985. depends.insert(depends.end(), outputs.begin(), outputs.end());
  986. }
  987. }
  988. }
  989. void cmMakefileTargetGenerator::WriteObjectDependRules(
  990. cmSourceFile const& source, std::vector<std::string>& depends)
  991. {
  992. // Create the list of dependencies known at cmake time. These are
  993. // shared between the object file and dependency scanning rule.
  994. depends.push_back(source.GetFullPath());
  995. if (const char* objectDeps = source.GetProperty("OBJECT_DEPENDS")) {
  996. cmSystemTools::ExpandListArgument(objectDeps, depends);
  997. }
  998. }
  999. void cmMakefileTargetGenerator::GenerateCustomRuleFile(
  1000. cmCustomCommandGenerator const& ccg)
  1001. {
  1002. // Collect the commands.
  1003. std::vector<std::string> commands;
  1004. std::string comment = this->LocalGenerator->ConstructComment(ccg);
  1005. if (!comment.empty()) {
  1006. // add in a progress call if needed
  1007. this->NumberOfProgressActions++;
  1008. if (!this->NoRuleMessages) {
  1009. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  1010. this->MakeEchoProgress(progress);
  1011. this->LocalGenerator->AppendEcho(
  1012. commands, comment, cmLocalUnixMakefileGenerator3::EchoGenerate,
  1013. &progress);
  1014. }
  1015. }
  1016. // Now append the actual user-specified commands.
  1017. std::ostringstream content;
  1018. this->LocalGenerator->AppendCustomCommand(
  1019. commands, ccg, this->GeneratorTarget,
  1020. this->LocalGenerator->GetBinaryDirectory(), false, &content);
  1021. // Collect the dependencies.
  1022. std::vector<std::string> depends;
  1023. this->LocalGenerator->AppendCustomDepend(depends, ccg);
  1024. // Write the rule.
  1025. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1026. bool symbolic = this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  1027. outputs, depends, commands);
  1028. // If the rule has changed make sure the output is rebuilt.
  1029. if (!symbolic) {
  1030. this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
  1031. }
  1032. // Setup implicit dependency scanning.
  1033. for (cmCustomCommand::ImplicitDependsList::const_iterator idi =
  1034. ccg.GetCC().GetImplicitDepends().begin();
  1035. idi != ccg.GetCC().GetImplicitDepends().end(); ++idi) {
  1036. std::string objFullPath = cmSystemTools::CollapseFullPath(outputs[0]);
  1037. std::string srcFullPath = cmSystemTools::CollapseFullPath(idi->second);
  1038. this->LocalGenerator->AddImplicitDepends(this->GeneratorTarget, idi->first,
  1039. objFullPath.c_str(),
  1040. srcFullPath.c_str());
  1041. }
  1042. }
  1043. void cmMakefileTargetGenerator::MakeEchoProgress(
  1044. cmLocalUnixMakefileGenerator3::EchoProgress& progress) const
  1045. {
  1046. progress.Dir = this->LocalGenerator->GetBinaryDirectory();
  1047. progress.Dir += cmake::GetCMakeFilesDirectory();
  1048. std::ostringstream progressArg;
  1049. progressArg << "$(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1050. progress.Arg = progressArg.str();
  1051. }
  1052. void cmMakefileTargetGenerator::WriteObjectsVariable(
  1053. std::string& variableName, std::string& variableNameExternal,
  1054. bool useWatcomQuote)
  1055. {
  1056. // Write a make variable assignment that lists all objects for the
  1057. // target.
  1058. variableName = this->LocalGenerator->CreateMakeVariable(
  1059. this->GeneratorTarget->GetName(), "_OBJECTS");
  1060. *this->BuildFileStream << "# Object files for target "
  1061. << this->GeneratorTarget->GetName() << "\n"
  1062. << variableName << " =";
  1063. std::string object;
  1064. const char* lineContinue =
  1065. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1066. if (!lineContinue) {
  1067. lineContinue = "\\";
  1068. }
  1069. for (std::vector<std::string>::const_iterator i = this->Objects.begin();
  1070. i != this->Objects.end(); ++i) {
  1071. *this->BuildFileStream << " " << lineContinue << "\n";
  1072. *this->BuildFileStream << this->LocalGenerator->ConvertToQuotedOutputPath(
  1073. i->c_str(), useWatcomQuote);
  1074. }
  1075. *this->BuildFileStream << "\n";
  1076. // Write a make variable assignment that lists all external objects
  1077. // for the target.
  1078. variableNameExternal = this->LocalGenerator->CreateMakeVariable(
  1079. this->GeneratorTarget->GetName(), "_EXTERNAL_OBJECTS");
  1080. /* clang-format off */
  1081. *this->BuildFileStream
  1082. << "\n"
  1083. << "# External object files for target "
  1084. << this->GeneratorTarget->GetName() << "\n"
  1085. << variableNameExternal << " =";
  1086. /* clang-format on */
  1087. std::string currentBinDir =
  1088. this->LocalGenerator->GetCurrentBinaryDirectory();
  1089. for (std::vector<std::string>::const_iterator i =
  1090. this->ExternalObjects.begin();
  1091. i != this->ExternalObjects.end(); ++i) {
  1092. object =
  1093. this->LocalGenerator->MaybeConvertToRelativePath(currentBinDir, *i);
  1094. *this->BuildFileStream << " " << lineContinue << "\n";
  1095. *this->BuildFileStream << this->LocalGenerator->ConvertToQuotedOutputPath(
  1096. i->c_str(), useWatcomQuote);
  1097. }
  1098. *this->BuildFileStream << "\n"
  1099. << "\n";
  1100. }
  1101. class cmMakefileTargetGeneratorObjectStrings
  1102. {
  1103. public:
  1104. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1105. cmOutputConverter* outputConverter,
  1106. cmState::Directory stateDir,
  1107. std::string::size_type limit)
  1108. : Strings(strings)
  1109. , OutputConverter(outputConverter)
  1110. , StateDir(stateDir)
  1111. , LengthLimit(limit)
  1112. {
  1113. this->Space = "";
  1114. }
  1115. void Feed(std::string const& obj)
  1116. {
  1117. // Construct the name of the next object.
  1118. this->NextObject = this->OutputConverter->ConvertToOutputFormat(
  1119. this->MaybeConvertToRelativePath(obj), cmOutputConverter::RESPONSE);
  1120. // Roll over to next string if the limit will be exceeded.
  1121. if (this->LengthLimit != std::string::npos &&
  1122. (this->CurrentString.length() + 1 + this->NextObject.length() >
  1123. this->LengthLimit)) {
  1124. this->Strings.push_back(this->CurrentString);
  1125. this->CurrentString = "";
  1126. this->Space = "";
  1127. }
  1128. // Separate from previous object.
  1129. this->CurrentString += this->Space;
  1130. this->Space = " ";
  1131. // Append this object.
  1132. this->CurrentString += this->NextObject;
  1133. }
  1134. void Done() { this->Strings.push_back(this->CurrentString); }
  1135. private:
  1136. std::string MaybeConvertToRelativePath(std::string const& obj)
  1137. {
  1138. if (!cmOutputConverter::ContainedInDirectory(
  1139. this->StateDir.GetCurrentBinary(), obj, this->StateDir)) {
  1140. return obj;
  1141. }
  1142. return cmOutputConverter::ForceToRelativePath(
  1143. this->StateDir.GetCurrentBinary(), obj);
  1144. }
  1145. std::vector<std::string>& Strings;
  1146. cmOutputConverter* OutputConverter;
  1147. cmState::Directory StateDir;
  1148. std::string::size_type LengthLimit;
  1149. std::string CurrentString;
  1150. std::string NextObject;
  1151. const char* Space;
  1152. };
  1153. void cmMakefileTargetGenerator::WriteObjectsStrings(
  1154. std::vector<std::string>& objStrings, std::string::size_type limit)
  1155. {
  1156. cmMakefileTargetGeneratorObjectStrings helper(
  1157. objStrings, this->LocalGenerator,
  1158. this->LocalGenerator->GetStateSnapshot().GetDirectory(), limit);
  1159. for (std::vector<std::string>::const_iterator i = this->Objects.begin();
  1160. i != this->Objects.end(); ++i) {
  1161. helper.Feed(*i);
  1162. }
  1163. for (std::vector<std::string>::const_iterator i =
  1164. this->ExternalObjects.begin();
  1165. i != this->ExternalObjects.end(); ++i) {
  1166. helper.Feed(*i);
  1167. }
  1168. helper.Done();
  1169. }
  1170. void cmMakefileTargetGenerator::WriteTargetDriverRule(
  1171. const std::string& main_output, bool relink)
  1172. {
  1173. // Compute the name of the driver target.
  1174. std::string dir =
  1175. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  1176. std::string buildTargetRuleName = dir;
  1177. buildTargetRuleName += relink ? "/preinstall" : "/build";
  1178. buildTargetRuleName = this->LocalGenerator->MaybeConvertToRelativePath(
  1179. this->LocalGenerator->GetBinaryDirectory(), buildTargetRuleName);
  1180. // Build the list of target outputs to drive.
  1181. std::vector<std::string> depends;
  1182. depends.push_back(main_output);
  1183. const char* comment = CM_NULLPTR;
  1184. if (relink) {
  1185. // Setup the comment for the preinstall driver.
  1186. comment = "Rule to relink during preinstall.";
  1187. } else {
  1188. // Setup the comment for the main build driver.
  1189. comment = "Rule to build all files generated by this target.";
  1190. // Make sure all custom command outputs in this target are built.
  1191. if (this->CustomCommandDriver == OnBuild) {
  1192. this->DriveCustomCommands(depends);
  1193. }
  1194. // Make sure the extra files are built.
  1195. depends.insert(depends.end(), this->ExtraFiles.begin(),
  1196. this->ExtraFiles.end());
  1197. }
  1198. // Write the driver rule.
  1199. std::vector<std::string> no_commands;
  1200. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1201. buildTargetRuleName, depends,
  1202. no_commands, true);
  1203. }
  1204. void cmMakefileTargetGenerator::AppendTargetDepends(
  1205. std::vector<std::string>& depends)
  1206. {
  1207. // Static libraries never depend on anything for linking.
  1208. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  1209. return;
  1210. }
  1211. // Loop over all library dependencies.
  1212. const char* cfg = this->LocalGenerator->GetConfigName().c_str();
  1213. if (cmComputeLinkInformation* cli =
  1214. this->GeneratorTarget->GetLinkInformation(cfg)) {
  1215. std::vector<std::string> const& libDeps = cli->GetDepends();
  1216. depends.insert(depends.end(), libDeps.begin(), libDeps.end());
  1217. }
  1218. }
  1219. void cmMakefileTargetGenerator::AppendObjectDepends(
  1220. std::vector<std::string>& depends)
  1221. {
  1222. // Add dependencies on the compiled object files.
  1223. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1224. std::string objTarget;
  1225. for (std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1226. obj != this->Objects.end(); ++obj) {
  1227. objTarget = relPath;
  1228. objTarget += *obj;
  1229. depends.push_back(objTarget);
  1230. }
  1231. // Add dependencies on the external object files.
  1232. depends.insert(depends.end(), this->ExternalObjects.begin(),
  1233. this->ExternalObjects.end());
  1234. // Add a dependency on the rule file itself.
  1235. this->LocalGenerator->AppendRuleDepend(depends,
  1236. this->BuildFileNameFull.c_str());
  1237. }
  1238. void cmMakefileTargetGenerator::AppendLinkDepends(
  1239. std::vector<std::string>& depends)
  1240. {
  1241. this->AppendObjectDepends(depends);
  1242. // Add dependencies on targets that must be built first.
  1243. this->AppendTargetDepends(depends);
  1244. // Add a dependency on the link definitions file, if any.
  1245. if (this->ModuleDefinitionFile) {
  1246. depends.push_back(this->ModuleDefinitionFile->GetFullPath());
  1247. }
  1248. // Add a dependency on user-specified manifest files, if any.
  1249. std::vector<cmSourceFile const*> manifest_srcs;
  1250. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  1251. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  1252. mi != manifest_srcs.end(); ++mi) {
  1253. depends.push_back((*mi)->GetFullPath());
  1254. }
  1255. // Add user-specified dependencies.
  1256. if (const char* linkDepends =
  1257. this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
  1258. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1259. }
  1260. }
  1261. std::string cmMakefileTargetGenerator::GetLinkRule(
  1262. const std::string& linkRuleVar)
  1263. {
  1264. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1265. if (this->GeneratorTarget->HasImplibGNUtoMS()) {
  1266. std::string ruleVar = "CMAKE_";
  1267. ruleVar += this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  1268. ruleVar += "_GNUtoMS_RULE";
  1269. if (const char* rule = this->Makefile->GetDefinition(ruleVar)) {
  1270. linkRule += rule;
  1271. }
  1272. }
  1273. return linkRule;
  1274. }
  1275. void cmMakefileTargetGenerator::CloseFileStreams()
  1276. {
  1277. delete this->BuildFileStream;
  1278. delete this->InfoFileStream;
  1279. delete this->FlagFileStream;
  1280. }
  1281. void cmMakefileTargetGenerator::CreateLinkScript(
  1282. const char* name, std::vector<std::string> const& link_commands,
  1283. std::vector<std::string>& makefile_commands,
  1284. std::vector<std::string>& makefile_depends)
  1285. {
  1286. // Create the link script file.
  1287. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1288. linkScriptName += "/";
  1289. linkScriptName += name;
  1290. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1291. linkScriptStream.SetCopyIfDifferent(true);
  1292. for (std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1293. cmd != link_commands.end(); ++cmd) {
  1294. // Do not write out empty commands or commands beginning in the
  1295. // shell no-op ":".
  1296. if (!cmd->empty() && (*cmd)[0] != ':') {
  1297. linkScriptStream << *cmd << "\n";
  1298. }
  1299. }
  1300. // Create the makefile command to invoke the link script.
  1301. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1302. link_command += this->LocalGenerator->ConvertToOutputFormat(
  1303. this->LocalGenerator->MaybeConvertToRelativePath(
  1304. this->LocalGenerator->GetCurrentBinaryDirectory(), linkScriptName),
  1305. cmOutputConverter::SHELL);
  1306. link_command += " --verbose=$(VERBOSE)";
  1307. makefile_commands.push_back(link_command);
  1308. makefile_depends.push_back(linkScriptName);
  1309. }
  1310. static size_t calculateCommandLineLengthLimit()
  1311. {
  1312. #if defined(_SC_ARG_MAX)
  1313. return ((size_t)sysconf(_SC_ARG_MAX)) - 1000;
  1314. #else
  1315. return 0;
  1316. #endif
  1317. }
  1318. bool cmMakefileTargetGenerator::CheckUseResponseFileForObjects(
  1319. std::string const& l) const
  1320. {
  1321. // Check for an explicit setting one way or the other.
  1322. std::string const responseVar =
  1323. "CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_OBJECTS";
  1324. if (const char* val = this->Makefile->GetDefinition(responseVar)) {
  1325. if (*val) {
  1326. return cmSystemTools::IsOn(val);
  1327. }
  1328. }
  1329. // Check for a system limit.
  1330. if (size_t const limit = calculateCommandLineLengthLimit()) {
  1331. // Compute the total length of our list of object files with room
  1332. // for argument separation and quoting. This does not convert paths
  1333. // relative to CMAKE_CURRENT_BINARY_DIR like the final list will be, so the
  1334. // actual list will likely be much shorter than this. However, in the
  1335. // worst case all objects will remain as absolute paths.
  1336. size_t length = 0;
  1337. for (std::vector<std::string>::const_iterator i = this->Objects.begin();
  1338. i != this->Objects.end(); ++i) {
  1339. length += i->size() + 3;
  1340. }
  1341. for (std::vector<std::string>::const_iterator i =
  1342. this->ExternalObjects.begin();
  1343. i != this->ExternalObjects.end(); ++i) {
  1344. length += i->size() + 3;
  1345. }
  1346. // We need to guarantee room for both objects and libraries, so
  1347. // if the objects take up more than half then use a response file
  1348. // for them.
  1349. if (length > (limit / 2)) {
  1350. return true;
  1351. }
  1352. }
  1353. // We do not need a response file for objects.
  1354. return false;
  1355. }
  1356. bool cmMakefileTargetGenerator::CheckUseResponseFileForLibraries(
  1357. std::string const& l) const
  1358. {
  1359. // Check for an explicit setting one way or the other.
  1360. std::string const responseVar =
  1361. "CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_LIBRARIES";
  1362. if (const char* val = this->Makefile->GetDefinition(responseVar)) {
  1363. if (*val) {
  1364. return cmSystemTools::IsOn(val);
  1365. }
  1366. }
  1367. // We do not need a response file for libraries.
  1368. return false;
  1369. }
  1370. std::string cmMakefileTargetGenerator::CreateResponseFile(
  1371. const char* name, std::string const& options,
  1372. std::vector<std::string>& makefile_depends)
  1373. {
  1374. // Create the response file.
  1375. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1376. responseFileNameFull += "/";
  1377. responseFileNameFull += name;
  1378. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1379. responseStream.SetCopyIfDifferent(true);
  1380. responseStream << options << "\n";
  1381. // Add a dependency so the target will rebuild when the set of
  1382. // objects changes.
  1383. makefile_depends.push_back(responseFileNameFull);
  1384. // Construct the name to be used on the command line.
  1385. std::string responseFileName = this->TargetBuildDirectory;
  1386. responseFileName += "/";
  1387. responseFileName += name;
  1388. return responseFileName;
  1389. }
  1390. cmLinkLineComputer* cmMakefileTargetGenerator::CreateLinkLineComputer(
  1391. cmOutputConverter* outputConverter, cmState::Directory stateDir)
  1392. {
  1393. if (this->Makefile->IsOn("MSVC60")) {
  1394. return this->GlobalGenerator->CreateMSVC60LinkLineComputer(outputConverter,
  1395. stateDir);
  1396. }
  1397. return this->GlobalGenerator->CreateLinkLineComputer(outputConverter,
  1398. stateDir);
  1399. }
  1400. void cmMakefileTargetGenerator::CreateLinkLibs(
  1401. cmLinkLineComputer* linkLineComputer, std::string& linkLibs,
  1402. bool useResponseFile, std::vector<std::string>& makefile_depends)
  1403. {
  1404. std::string frameworkPath;
  1405. std::string linkPath;
  1406. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1407. cmComputeLinkInformation* pcli =
  1408. this->GeneratorTarget->GetLinkInformation(config);
  1409. this->LocalGenerator->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  1410. frameworkPath, linkPath);
  1411. linkLibs = frameworkPath + linkPath + linkLibs;
  1412. if (useResponseFile && linkLibs.find_first_not_of(' ') != linkLibs.npos) {
  1413. // Lookup the response file reference flag.
  1414. std::string responseFlagVar = "CMAKE_";
  1415. responseFlagVar +=
  1416. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  1417. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1418. const char* responseFlag = this->Makefile->GetDefinition(responseFlagVar);
  1419. if (!responseFlag) {
  1420. responseFlag = "@";
  1421. }
  1422. // Create this response file.
  1423. std::string link_rsp =
  1424. this->CreateResponseFile("linklibs.rsp", linkLibs, makefile_depends);
  1425. // Reference the response file.
  1426. linkLibs = responseFlag;
  1427. linkLibs += this->LocalGenerator->ConvertToOutputFormat(
  1428. link_rsp, cmOutputConverter::SHELL);
  1429. }
  1430. }
  1431. void cmMakefileTargetGenerator::CreateObjectLists(
  1432. bool useLinkScript, bool useArchiveRules, bool useResponseFile,
  1433. std::string& buildObjs, std::vector<std::string>& makefile_depends,
  1434. bool useWatcomQuote)
  1435. {
  1436. std::string variableName;
  1437. std::string variableNameExternal;
  1438. this->WriteObjectsVariable(variableName, variableNameExternal,
  1439. useWatcomQuote);
  1440. if (useResponseFile) {
  1441. // MSVC response files cannot exceed 128K.
  1442. std::string::size_type const responseFileLimit = 131000;
  1443. // Construct the individual object list strings.
  1444. std::vector<std::string> object_strings;
  1445. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1446. // Lookup the response file reference flag.
  1447. std::string responseFlagVar = "CMAKE_";
  1448. responseFlagVar +=
  1449. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  1450. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1451. const char* responseFlag = this->Makefile->GetDefinition(responseFlagVar);
  1452. if (!responseFlag) {
  1453. responseFlag = "@";
  1454. }
  1455. // Write a response file for each string.
  1456. const char* sep = "";
  1457. for (unsigned int i = 0; i < object_strings.size(); ++i) {
  1458. // Number the response files.
  1459. char rsp[32];
  1460. sprintf(rsp, "objects%u.rsp", i + 1);
  1461. // Create this response file.
  1462. std::string objects_rsp =
  1463. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1464. // Separate from previous response file references.
  1465. buildObjs += sep;
  1466. sep = " ";
  1467. // Reference the response file.
  1468. buildObjs += responseFlag;
  1469. buildObjs += this->LocalGenerator->ConvertToOutputFormat(
  1470. objects_rsp, cmOutputConverter::SHELL);
  1471. }
  1472. } else if (useLinkScript) {
  1473. if (!useArchiveRules) {
  1474. std::vector<std::string> objStrings;
  1475. this->WriteObjectsStrings(objStrings);
  1476. buildObjs = objStrings[0];
  1477. }
  1478. } else {
  1479. buildObjs = "$(";
  1480. buildObjs += variableName;
  1481. buildObjs += ") $(";
  1482. buildObjs += variableNameExternal;
  1483. buildObjs += ")";
  1484. }
  1485. }
  1486. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1487. const std::string& lang)
  1488. {
  1489. std::string responseVar = "CMAKE_";
  1490. responseVar += lang;
  1491. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1492. bool useResponseFile = this->Makefile->IsOn(responseVar);
  1493. std::vector<std::string> includes;
  1494. const std::string& config =
  1495. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1496. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  1497. lang, config);
  1498. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  1499. includes, this->GeneratorTarget, lang, false, useResponseFile, config);
  1500. if (includeFlags.empty()) {
  1501. return;
  1502. }
  1503. if (useResponseFile) {
  1504. std::string name = "includes_";
  1505. name += lang;
  1506. name += ".rsp";
  1507. std::string arg = "@" +
  1508. this->CreateResponseFile(name.c_str(), includeFlags,
  1509. this->FlagFileDepends[lang]);
  1510. this->LocalGenerator->AppendFlags(flags, arg);
  1511. } else {
  1512. this->LocalGenerator->AppendFlags(flags, includeFlags);
  1513. }
  1514. }
  1515. void cmMakefileTargetGenerator::GenDefFile(
  1516. std::vector<std::string>& real_link_commands, std::string& linkFlags)
  1517. {
  1518. if (this->GeneratorTarget->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS")) {
  1519. std::string name_of_def_file =
  1520. this->GeneratorTarget->GetSupportDirectory();
  1521. name_of_def_file += std::string("/") + this->GeneratorTarget->GetName();
  1522. name_of_def_file += ".def";
  1523. std::string cmd = cmSystemTools::GetCMakeCommand();
  1524. cmd = this->LocalGenerator->ConvertToOutputFormat(
  1525. cmd, cmOutputConverter::SHELL);
  1526. cmd += " -E __create_def ";
  1527. cmd += this->LocalGenerator->ConvertToOutputFormat(
  1528. this->LocalGenerator->MaybeConvertToRelativePath(
  1529. this->LocalGenerator->GetCurrentBinaryDirectory(), name_of_def_file),
  1530. cmOutputConverter::SHELL);
  1531. cmd += " ";
  1532. std::string objlist_file = name_of_def_file;
  1533. objlist_file += ".objs";
  1534. cmd += this->LocalGenerator->ConvertToOutputFormat(
  1535. this->LocalGenerator->MaybeConvertToRelativePath(
  1536. this->LocalGenerator->GetCurrentBinaryDirectory(), objlist_file),
  1537. cmOutputConverter::SHELL);
  1538. real_link_commands.insert(real_link_commands.begin(), cmd);
  1539. // create a list of obj files for the -E __create_def to read
  1540. cmGeneratedFileStream fout(objlist_file.c_str());
  1541. for (std::vector<std::string>::const_iterator i = this->Objects.begin();
  1542. i != this->Objects.end(); ++i) {
  1543. if (cmHasLiteralSuffix(*i, ".obj")) {
  1544. fout << *i << "\n";
  1545. }
  1546. }
  1547. for (std::vector<std::string>::const_iterator i =
  1548. this->ExternalObjects.begin();
  1549. i != this->ExternalObjects.end(); ++i) {
  1550. fout << *i << "\n";
  1551. }
  1552. // now add the def file link flag
  1553. linkFlags += " ";
  1554. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1555. linkFlags += this->LocalGenerator->ConvertToOutputFormat(
  1556. this->LocalGenerator->MaybeConvertToRelativePath(
  1557. this->LocalGenerator->GetCurrentBinaryDirectory(), name_of_def_file),
  1558. cmOutputConverter::SHELL);
  1559. linkFlags += " ";
  1560. }
  1561. }