cmMakefileTargetGenerator.cxx 65 KB

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