cmMakefileTargetGenerator.cxx 64 KB

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