cmMakefileTargetGenerator.cxx 66 KB

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