cmMakefileTargetGenerator.cxx 63 KB

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