cmMakefileTargetGenerator.cxx 64 KB

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