1
0

cmMakefileTargetGenerator.cxx 65 KB

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