cmMakefileTargetGenerator.cxx 65 KB

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