cmMakefileTargetGenerator.cxx 63 KB

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