cmMakefileTargetGenerator.cxx 62 KB

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