cmMakefileTargetGenerator.cxx 65 KB

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