cmMakefileTargetGenerator.cxx 63 KB

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