cmMakefileTargetGenerator.cxx 66 KB

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