cmMakefileTargetGenerator.cxx 65 KB

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