cmMakefileTargetGenerator.cxx 65 KB

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