cmMakefileTargetGenerator.cxx 65 KB

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