cmMakefileTargetGenerator.cxx 66 KB

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