cmMakefileTargetGenerator.cxx 64 KB

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