cmMakefileTargetGenerator.cxx 66 KB

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