cmMakefileTargetGenerator.cxx 67 KB

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