cmMakefileTargetGenerator.cxx 69 KB

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