cmMakefileTargetGenerator.cxx 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  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->Target,
  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->Makefile->GetProperty("COMPILE_DEFINITIONS"));
  271. this->LocalGenerator->AppendDefines
  272. (defines, this->Target->GetProperty("COMPILE_DEFINITIONS"));
  273. std::string defPropName = "COMPILE_DEFINITIONS_";
  274. defPropName +=
  275. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  276. this->LocalGenerator->AppendDefines
  277. (defines, this->Makefile->GetProperty(defPropName.c_str()));
  278. this->LocalGenerator->AppendDefines
  279. (defines, this->Target->GetProperty(defPropName.c_str()));
  280. std::string definesString;
  281. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  282. ByLanguageMap::value_type entry(l, definesString);
  283. i = this->DefinesByLanguage.insert(entry).first;
  284. }
  285. return i->second;
  286. }
  287. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  288. {
  289. // write language flags for target
  290. std::set<cmStdString> languages;
  291. this->Target->GetLanguages(languages);
  292. // put the compiler in the rules.make file so that if it changes
  293. // things rebuild
  294. for(std::set<cmStdString>::const_iterator l = languages.begin();
  295. l != languages.end(); ++l)
  296. {
  297. cmStdString compiler = "CMAKE_";
  298. compiler += *l;
  299. compiler += "_COMPILER";
  300. *this->FlagFileStream << "# compile " << l->c_str() << " with " <<
  301. this->Makefile->GetSafeDefinition(compiler.c_str()) << "\n";
  302. }
  303. for(std::set<cmStdString>::const_iterator l = languages.begin();
  304. l != languages.end(); ++l)
  305. {
  306. *this->FlagFileStream << *l << "_FLAGS = " << this->GetFlags(*l) << "\n\n";
  307. *this->FlagFileStream << *l << "_DEFINES = " << this->GetDefines(*l) <<
  308. "\n\n";
  309. }
  310. // Add target-specific flags.
  311. if(this->Target->GetProperty("COMPILE_FLAGS"))
  312. {
  313. std::string flags;
  314. this->LocalGenerator->AppendFlags
  315. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  316. *this->FlagFileStream << "# TARGET_FLAGS = " << flags << "\n\n";
  317. }
  318. }
  319. //----------------------------------------------------------------------------
  320. void
  321. cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()
  322. (cmSourceFile& source, const char* pkgloc)
  323. {
  324. // Skip OS X content when not building a Framework or Bundle.
  325. if(this->Generator->MacContentDirectory.empty())
  326. {
  327. return;
  328. }
  329. std::string macdir =
  330. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  331. // Get the input file location.
  332. std::string input = source.GetFullPath();
  333. // Get the output file location.
  334. std::string output = macdir;
  335. output += "/";
  336. output += cmSystemTools::GetFilenameName(input);
  337. this->Generator->CleanFiles.push_back(
  338. this->Generator->Convert(output.c_str(),
  339. cmLocalGenerator::START_OUTPUT));
  340. output = this->Generator->Convert(output.c_str(),
  341. cmLocalGenerator::HOME_OUTPUT);
  342. // Create a rule to copy the content into the bundle.
  343. std::vector<std::string> depends;
  344. std::vector<std::string> commands;
  345. depends.push_back(input);
  346. std::string copyEcho = "Copying OS X content ";
  347. copyEcho += output;
  348. this->Generator->LocalGenerator->AppendEcho(
  349. commands, copyEcho.c_str(),
  350. cmLocalUnixMakefileGenerator3::EchoBuild);
  351. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  352. copyCommand += this->Generator->Convert(input.c_str(),
  353. cmLocalGenerator::NONE,
  354. cmLocalGenerator::SHELL);
  355. copyCommand += " ";
  356. copyCommand += this->Generator->Convert(output.c_str(),
  357. cmLocalGenerator::NONE,
  358. cmLocalGenerator::SHELL);
  359. commands.push_back(copyCommand);
  360. this->Generator->LocalGenerator->WriteMakeRule(
  361. *this->Generator->BuildFileStream, 0,
  362. output.c_str(),
  363. depends, commands, false);
  364. this->Generator->ExtraFiles.insert(output);
  365. }
  366. //----------------------------------------------------------------------------
  367. void cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile& source)
  368. {
  369. // Identify the language of the source file.
  370. const char* lang = this->LocalGenerator->GetSourceFileLanguage(source);
  371. if(!lang)
  372. {
  373. // don't know anything about this file so skip it
  374. return;
  375. }
  376. // Get the full path name of the object file.
  377. std::string const& objectName = this->GeneratorTarget->Objects[&source];
  378. std::string obj = this->LocalGenerator->GetTargetDirectory(*this->Target);
  379. obj += "/";
  380. obj += objectName;
  381. // Avoid generating duplicate rules.
  382. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  383. {
  384. this->ObjectFiles.insert(obj);
  385. }
  386. else
  387. {
  388. cmOStringStream err;
  389. err << "Warning: Source file \""
  390. << source.GetFullPath()
  391. << "\" is listed multiple times for target \""
  392. << this->Target->GetName()
  393. << "\".";
  394. cmSystemTools::Message(err.str().c_str(), "Warning");
  395. return;
  396. }
  397. // Create the directory containing the object file. This may be a
  398. // subdirectory under the target's directory.
  399. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  400. cmSystemTools::MakeDirectory
  401. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  402. // Save this in the target's list of object files.
  403. this->Objects.push_back(obj);
  404. this->CleanFiles.push_back(obj);
  405. // TODO: Remove
  406. //std::string relativeObj
  407. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  408. //relativeObj += obj;
  409. // we compute some depends when writing the depend.make that we will also
  410. // use in the build.make, same with depMakeFile
  411. std::vector<std::string> depends;
  412. std::string depMakeFile;
  413. // generate the build rule file
  414. this->WriteObjectBuildFile(obj, lang, source, depends);
  415. // The object file should be checked for dependency integrity.
  416. std::string objFullPath = this->Makefile->GetCurrentOutputDirectory();
  417. objFullPath += "/";
  418. objFullPath += obj;
  419. objFullPath =
  420. this->Convert(objFullPath.c_str(), cmLocalGenerator::FULL);
  421. std::string srcFullPath =
  422. this->Convert(source.GetFullPath().c_str(), cmLocalGenerator::FULL);
  423. this->LocalGenerator->
  424. AddImplicitDepends(*this->Target, lang,
  425. objFullPath.c_str(),
  426. srcFullPath.c_str());
  427. }
  428. //----------------------------------------------------------------------------
  429. void
  430. cmMakefileTargetGenerator
  431. ::AppendFortranFormatFlags(std::string& flags, cmSourceFile& source)
  432. {
  433. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  434. cmLocalGenerator::FortranFormat format =
  435. this->LocalGenerator->GetFortranFormat(srcfmt);
  436. if(format == cmLocalGenerator::FortranFormatNone)
  437. {
  438. const char* tgtfmt = this->Target->GetProperty("Fortran_FORMAT");
  439. format = this->LocalGenerator->GetFortranFormat(tgtfmt);
  440. }
  441. const char* var = 0;
  442. switch (format)
  443. {
  444. case cmLocalGenerator::FortranFormatFixed:
  445. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG"; break;
  446. case cmLocalGenerator::FortranFormatFree:
  447. var = "CMAKE_Fortran_FORMAT_FREE_FLAG"; break;
  448. default: break;
  449. }
  450. if(var)
  451. {
  452. this->LocalGenerator->AppendFlags(
  453. flags, this->Makefile->GetDefinition(var));
  454. }
  455. }
  456. //----------------------------------------------------------------------------
  457. void
  458. cmMakefileTargetGenerator
  459. ::WriteObjectBuildFile(std::string &obj,
  460. const char *lang,
  461. cmSourceFile& source,
  462. std::vector<std::string>& depends)
  463. {
  464. this->LocalGenerator->AppendRuleDepend(depends,
  465. this->FlagFileNameFull.c_str());
  466. this->LocalGenerator->AppendRuleDepends(depends,
  467. this->FlagFileDepends[lang]);
  468. // generate the depend scanning rule
  469. this->WriteObjectDependRules(source, depends);
  470. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  471. relativeObj += obj;
  472. // Write the build rule.
  473. // Build the set of compiler flags.
  474. std::string flags;
  475. // Add language-specific flags.
  476. std::string langFlags = "$(";
  477. langFlags += lang;
  478. langFlags += "_FLAGS)";
  479. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  480. // Add target-specific flags.
  481. if(this->Target->GetProperty("COMPILE_FLAGS"))
  482. {
  483. std::string langIncludeExpr = "CMAKE_";
  484. langIncludeExpr += lang;
  485. langIncludeExpr += "_FLAG_REGEX";
  486. const char* regex = this->Makefile->
  487. GetDefinition(langIncludeExpr.c_str());
  488. if(regex)
  489. {
  490. cmsys::RegularExpression r(regex);
  491. std::vector<std::string> args;
  492. cmSystemTools::ParseWindowsCommandLine(
  493. this->Target->GetProperty("COMPILE_FLAGS"),
  494. args);
  495. for(std::vector<std::string>::iterator i = args.begin();
  496. i != args.end(); ++i)
  497. {
  498. if(r.find(i->c_str()))
  499. {
  500. this->LocalGenerator->AppendFlags
  501. (flags, i->c_str());
  502. }
  503. }
  504. }
  505. else
  506. {
  507. this->LocalGenerator->AppendFlags
  508. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  509. }
  510. }
  511. // Add Fortran format flags.
  512. if(strcmp(lang, "Fortran") == 0)
  513. {
  514. this->AppendFortranFormatFlags(flags, source);
  515. }
  516. // Add flags from source file properties.
  517. if (source.GetProperty("COMPILE_FLAGS"))
  518. {
  519. this->LocalGenerator->AppendFlags
  520. (flags, source.GetProperty("COMPILE_FLAGS"));
  521. *this->FlagFileStream << "# Custom flags: "
  522. << relativeObj << "_FLAGS = "
  523. << source.GetProperty("COMPILE_FLAGS")
  524. << "\n"
  525. << "\n";
  526. }
  527. // Add language-specific defines.
  528. std::set<std::string> defines;
  529. // Add source-sepcific preprocessor definitions.
  530. if(const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS"))
  531. {
  532. this->LocalGenerator->AppendDefines(defines, compile_defs);
  533. *this->FlagFileStream << "# Custom defines: "
  534. << relativeObj << "_DEFINES = "
  535. << compile_defs << "\n"
  536. << "\n";
  537. }
  538. std::string configUpper =
  539. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  540. std::string defPropName = "COMPILE_DEFINITIONS_";
  541. defPropName += configUpper;
  542. if(const char* config_compile_defs =
  543. source.GetProperty(defPropName.c_str()))
  544. {
  545. this->LocalGenerator->AppendDefines(defines, config_compile_defs);
  546. *this->FlagFileStream
  547. << "# Custom defines: "
  548. << relativeObj << "_DEFINES_" << configUpper
  549. << " = " << config_compile_defs << "\n"
  550. << "\n";
  551. }
  552. // Get the output paths for source and object files.
  553. std::string sourceFile = source.GetFullPath();
  554. if(this->LocalGenerator->UseRelativePaths)
  555. {
  556. sourceFile = this->Convert(sourceFile.c_str(),
  557. cmLocalGenerator::START_OUTPUT);
  558. }
  559. sourceFile = this->Convert(sourceFile.c_str(),
  560. cmLocalGenerator::NONE,
  561. cmLocalGenerator::SHELL);
  562. // Construct the build message.
  563. std::vector<std::string> no_commands;
  564. std::vector<std::string> commands;
  565. // add in a progress call if needed
  566. this->AppendProgress(commands);
  567. if(!this->NoRuleMessages)
  568. {
  569. std::string buildEcho = "Building ";
  570. buildEcho += lang;
  571. buildEcho += " object ";
  572. buildEcho += relativeObj;
  573. this->LocalGenerator->AppendEcho
  574. (commands, buildEcho.c_str(), cmLocalUnixMakefileGenerator3::EchoBuild);
  575. }
  576. std::string targetOutPathPDB;
  577. {
  578. std::string targetFullPathPDB;
  579. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  580. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  581. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  582. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  583. {
  584. targetFullPathPDB = this->Target->GetPDBDirectory(this->ConfigName);
  585. targetFullPathPDB += "/";
  586. targetFullPathPDB += this->Target->GetPDBName(this->ConfigName);
  587. }
  588. targetOutPathPDB =
  589. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
  590. cmLocalGenerator::SHELL);
  591. }
  592. cmLocalGenerator::RuleVariables vars;
  593. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  594. vars.CMTarget = this->Target;
  595. vars.Language = lang;
  596. vars.TargetPDB = targetOutPathPDB.c_str();
  597. vars.Source = sourceFile.c_str();
  598. std::string shellObj =
  599. this->Convert(obj.c_str(),
  600. cmLocalGenerator::NONE,
  601. cmLocalGenerator::SHELL).c_str();
  602. vars.Object = shellObj.c_str();
  603. std::string objectDir = cmSystemTools::GetFilenamePath(obj);
  604. objectDir = this->Convert(objectDir.c_str(),
  605. cmLocalGenerator::START_OUTPUT,
  606. cmLocalGenerator::SHELL);
  607. vars.ObjectDir = objectDir.c_str();
  608. vars.Flags = flags.c_str();
  609. std::string definesString = "$(";
  610. definesString += lang;
  611. definesString += "_DEFINES)";
  612. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  613. vars.Defines = definesString.c_str();
  614. bool lang_is_c_or_cxx = ((strcmp(lang, "C") == 0) ||
  615. (strcmp(lang, "CXX") == 0));
  616. // Construct the compile rules.
  617. {
  618. std::string compileRuleVar = "CMAKE_";
  619. compileRuleVar += lang;
  620. compileRuleVar += "_COMPILE_OBJECT";
  621. std::string compileRule =
  622. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  623. std::vector<std::string> compileCommands;
  624. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  625. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  626. lang_is_c_or_cxx && compileCommands.size() == 1)
  627. {
  628. std::string compileCommand = compileCommands[0];
  629. this->LocalGenerator->ExpandRuleVariables(compileCommand, vars);
  630. std::string workingDirectory =
  631. this->LocalGenerator->Convert(
  632. this->Makefile->GetStartOutputDirectory(), cmLocalGenerator::FULL);
  633. compileCommand.replace(compileCommand.find(langFlags),
  634. langFlags.size(), this->GetFlags(lang));
  635. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  636. compileCommand.replace(compileCommand.find(langDefines),
  637. langDefines.size(), this->GetDefines(lang));
  638. this->GlobalGenerator->AddCXXCompileCommand(
  639. source.GetFullPath(), workingDirectory, compileCommand);
  640. }
  641. // Expand placeholders in the commands.
  642. for(std::vector<std::string>::iterator i = compileCommands.begin();
  643. i != compileCommands.end(); ++i)
  644. {
  645. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  646. }
  647. // Change the command working directory to the local build tree.
  648. this->LocalGenerator->CreateCDCommand
  649. (compileCommands,
  650. this->Makefile->GetStartOutputDirectory(),
  651. cmLocalGenerator::HOME_OUTPUT);
  652. commands.insert(commands.end(),
  653. compileCommands.begin(), compileCommands.end());
  654. }
  655. // Write the rule.
  656. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  657. relativeObj.c_str(),
  658. depends, commands, false);
  659. // Check for extra outputs created by the compilation.
  660. if(const char* extra_outputs_str =
  661. source.GetProperty("OBJECT_OUTPUTS"))
  662. {
  663. std::vector<std::string> extra_outputs;
  664. cmSystemTools::ExpandListArgument(extra_outputs_str, extra_outputs);
  665. for(std::vector<std::string>::const_iterator eoi = extra_outputs.begin();
  666. eoi != extra_outputs.end(); ++eoi)
  667. {
  668. // Register this as an extra output for the object file rule.
  669. // This will cause the object file to be rebuilt if the extra
  670. // output is missing.
  671. this->GenerateExtraOutput(eoi->c_str(), relativeObj.c_str(), false);
  672. // Register this as an extra file to clean.
  673. this->CleanFiles.push_back(eoi->c_str());
  674. }
  675. }
  676. bool do_preprocess_rules = lang_is_c_or_cxx &&
  677. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  678. bool do_assembly_rules = lang_is_c_or_cxx &&
  679. this->LocalGenerator->GetCreateAssemblySourceRules();
  680. if(do_preprocess_rules || do_assembly_rules)
  681. {
  682. std::vector<std::string> force_depends;
  683. force_depends.push_back("cmake_force");
  684. std::string::size_type dot_pos = relativeObj.rfind(".");
  685. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  686. dot_pos = obj.rfind(".");
  687. std::string objBase = obj.substr(0, dot_pos);
  688. if(do_preprocess_rules)
  689. {
  690. commands.clear();
  691. std::string relativeObjI = relativeObjBase + ".i";
  692. std::string objI = objBase + ".i";
  693. std::string preprocessEcho = "Preprocessing ";
  694. preprocessEcho += lang;
  695. preprocessEcho += " source to ";
  696. preprocessEcho += objI;
  697. this->LocalGenerator->AppendEcho(
  698. commands, preprocessEcho.c_str(),
  699. cmLocalUnixMakefileGenerator3::EchoBuild
  700. );
  701. std::string preprocessRuleVar = "CMAKE_";
  702. preprocessRuleVar += lang;
  703. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  704. if(const char* preprocessRule =
  705. this->Makefile->GetDefinition(preprocessRuleVar.c_str()))
  706. {
  707. std::vector<std::string> preprocessCommands;
  708. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  709. std::string shellObjI =
  710. this->Convert(objI.c_str(),
  711. cmLocalGenerator::NONE,
  712. cmLocalGenerator::SHELL).c_str();
  713. vars.PreprocessedSource = shellObjI.c_str();
  714. // Expand placeholders in the commands.
  715. for(std::vector<std::string>::iterator i = preprocessCommands.begin();
  716. i != preprocessCommands.end(); ++i)
  717. {
  718. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  719. }
  720. this->LocalGenerator->CreateCDCommand
  721. (preprocessCommands,
  722. this->Makefile->GetStartOutputDirectory(),
  723. cmLocalGenerator::HOME_OUTPUT);
  724. commands.insert(commands.end(),
  725. preprocessCommands.begin(),
  726. preprocessCommands.end());
  727. }
  728. else
  729. {
  730. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  731. cmd += preprocessRuleVar;
  732. commands.push_back(cmd);
  733. }
  734. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  735. relativeObjI.c_str(),
  736. force_depends, commands, false);
  737. }
  738. if(do_assembly_rules)
  739. {
  740. commands.clear();
  741. std::string relativeObjS = relativeObjBase + ".s";
  742. std::string objS = objBase + ".s";
  743. std::string assemblyEcho = "Compiling ";
  744. assemblyEcho += lang;
  745. assemblyEcho += " source to assembly ";
  746. assemblyEcho += objS;
  747. this->LocalGenerator->AppendEcho(
  748. commands, assemblyEcho.c_str(),
  749. cmLocalUnixMakefileGenerator3::EchoBuild
  750. );
  751. std::string assemblyRuleVar = "CMAKE_";
  752. assemblyRuleVar += lang;
  753. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  754. if(const char* assemblyRule =
  755. this->Makefile->GetDefinition(assemblyRuleVar.c_str()))
  756. {
  757. std::vector<std::string> assemblyCommands;
  758. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  759. std::string shellObjS =
  760. this->Convert(objS.c_str(),
  761. cmLocalGenerator::NONE,
  762. cmLocalGenerator::SHELL).c_str();
  763. vars.AssemblySource = shellObjS.c_str();
  764. // Expand placeholders in the commands.
  765. for(std::vector<std::string>::iterator i = assemblyCommands.begin();
  766. i != assemblyCommands.end(); ++i)
  767. {
  768. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  769. }
  770. this->LocalGenerator->CreateCDCommand
  771. (assemblyCommands,
  772. this->Makefile->GetStartOutputDirectory(),
  773. cmLocalGenerator::HOME_OUTPUT);
  774. commands.insert(commands.end(),
  775. assemblyCommands.begin(),
  776. assemblyCommands.end());
  777. }
  778. else
  779. {
  780. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  781. cmd += assemblyRuleVar;
  782. commands.push_back(cmd);
  783. }
  784. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  785. relativeObjS.c_str(),
  786. force_depends, commands, false);
  787. }
  788. }
  789. // If the language needs provides-requires mode, create the
  790. // corresponding targets.
  791. std::string objectRequires = relativeObj;
  792. objectRequires += ".requires";
  793. std::vector<std::string> p_depends;
  794. // always provide an empty requires target
  795. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  796. objectRequires.c_str(), p_depends,
  797. no_commands, true);
  798. // write a build rule to recursively build what this obj provides
  799. std::string objectProvides = relativeObj;
  800. objectProvides += ".provides";
  801. std::string temp = relativeObj;
  802. temp += ".provides.build";
  803. std::vector<std::string> r_commands;
  804. std::string tgtMakefileName =
  805. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  806. tgtMakefileName += "/build.make";
  807. r_commands.push_back
  808. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  809. temp.c_str()));
  810. p_depends.clear();
  811. p_depends.push_back(objectRequires);
  812. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  813. objectProvides.c_str(), p_depends,
  814. r_commands, true);
  815. // write the provides.build rule dependency on the obj file
  816. p_depends.clear();
  817. p_depends.push_back(relativeObj);
  818. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  819. temp.c_str(), p_depends, no_commands,
  820. false);
  821. }
  822. //----------------------------------------------------------------------------
  823. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  824. {
  825. std::vector<std::string> depends;
  826. std::vector<std::string> no_commands;
  827. // Construct the name of the dependency generation target.
  828. std::string depTarget =
  829. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  830. depTarget += "/requires";
  831. // This target drives dependency generation for all object files.
  832. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  833. std::string objTarget;
  834. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  835. obj != this->Objects.end(); ++obj)
  836. {
  837. objTarget = relPath;
  838. objTarget += *obj;
  839. objTarget += ".requires";
  840. depends.push_back(objTarget);
  841. }
  842. // Write the rule.
  843. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  844. depTarget.c_str(),
  845. depends, no_commands, true);
  846. }
  847. //----------------------------------------------------------------------------
  848. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  849. {
  850. std::vector<std::string> depends;
  851. std::vector<std::string> commands;
  852. // Construct the clean target name.
  853. std::string cleanTarget =
  854. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  855. cleanTarget += "/clean";
  856. // Construct the clean command.
  857. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  858. *this->Target);
  859. this->LocalGenerator->CreateCDCommand
  860. (commands,
  861. this->Makefile->GetStartOutputDirectory(),
  862. cmLocalGenerator::HOME_OUTPUT);
  863. // Write the rule.
  864. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  865. cleanTarget.c_str(),
  866. depends, commands, true);
  867. }
  868. //----------------------------------------------------------------------------
  869. void cmMakefileTargetGenerator::WriteTargetDependRules()
  870. {
  871. // must write the targets depend info file
  872. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  873. this->InfoFileNameFull = dir;
  874. this->InfoFileNameFull += "/DependInfo.cmake";
  875. this->InfoFileNameFull =
  876. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  877. this->InfoFileStream =
  878. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  879. this->InfoFileStream->SetCopyIfDifferent(true);
  880. if(!*this->InfoFileStream)
  881. {
  882. return;
  883. }
  884. this->LocalGenerator->
  885. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  886. // Store multiple output pairs in the depend info file.
  887. if(!this->MultipleOutputPairs.empty())
  888. {
  889. *this->InfoFileStream
  890. << "\n"
  891. << "# Pairs of files generated by the same build rule.\n"
  892. << "SET(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  893. for(MultipleOutputPairsType::const_iterator pi =
  894. this->MultipleOutputPairs.begin();
  895. pi != this->MultipleOutputPairs.end(); ++pi)
  896. {
  897. *this->InfoFileStream
  898. << " " << this->LocalGenerator->EscapeForCMake(pi->first.c_str())
  899. << " " << this->LocalGenerator->EscapeForCMake(pi->second.c_str())
  900. << "\n";
  901. }
  902. *this->InfoFileStream << " )\n\n";
  903. }
  904. // Store list of targets linked directly or transitively.
  905. {
  906. *this->InfoFileStream
  907. << "\n"
  908. << "# Targets to which this target links.\n"
  909. << "SET(CMAKE_TARGET_LINKED_INFO_FILES\n";
  910. std::set<cmTarget const*> emitted;
  911. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  912. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  913. {
  914. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  915. for(cmComputeLinkInformation::ItemVector::const_iterator
  916. i = items.begin(); i != items.end(); ++i)
  917. {
  918. cmTarget const* linkee = i->Target;
  919. if(linkee && !linkee->IsImported() && emitted.insert(linkee).second)
  920. {
  921. cmMakefile* mf = linkee->GetMakefile();
  922. cmLocalGenerator* lg = mf->GetLocalGenerator();
  923. std::string di = mf->GetStartOutputDirectory();
  924. di += "/";
  925. di += lg->GetTargetDirectory(*linkee);
  926. di += "/DependInfo.cmake";
  927. *this->InfoFileStream << " \"" << di << "\"\n";
  928. }
  929. }
  930. }
  931. *this->InfoFileStream
  932. << " )\n";
  933. }
  934. // Check for a target-specific module output directory.
  935. if(const char* mdir = this->GetFortranModuleDirectory())
  936. {
  937. *this->InfoFileStream
  938. << "\n"
  939. << "# Fortran module output directory.\n"
  940. << "SET(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  941. }
  942. // Target-specific include directories:
  943. *this->InfoFileStream
  944. << "\n"
  945. << "# The include file search paths:\n";
  946. *this->InfoFileStream
  947. << "SET(CMAKE_C_TARGET_INCLUDE_PATH\n";
  948. std::vector<std::string> includes;
  949. this->LocalGenerator->GetIncludeDirectories(includes, this->Target);
  950. for(std::vector<std::string>::iterator i = includes.begin();
  951. i != includes.end(); ++i)
  952. {
  953. *this->InfoFileStream
  954. << " \""
  955. << this->LocalGenerator->Convert(i->c_str(),
  956. cmLocalGenerator::HOME_OUTPUT)
  957. << "\"\n";
  958. }
  959. *this->InfoFileStream
  960. << " )\n";
  961. *this->InfoFileStream
  962. << "SET(CMAKE_CXX_TARGET_INCLUDE_PATH "
  963. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  964. *this->InfoFileStream
  965. << "SET(CMAKE_Fortran_TARGET_INCLUDE_PATH "
  966. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  967. *this->InfoFileStream
  968. << "SET(CMAKE_ASM_TARGET_INCLUDE_PATH "
  969. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  970. // and now write the rule to use it
  971. std::vector<std::string> depends;
  972. std::vector<std::string> commands;
  973. // Construct the name of the dependency generation target.
  974. std::string depTarget =
  975. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  976. depTarget += "/depend";
  977. // Add a command to call CMake to scan dependencies. CMake will
  978. // touch the corresponding depends file after scanning dependencies.
  979. cmOStringStream depCmd;
  980. // TODO: Account for source file properties and directory-level
  981. // definitions when scanning for dependencies.
  982. #if !defined(_WIN32) || defined(__CYGWIN__)
  983. // This platform supports symlinks, so cmSystemTools will translate
  984. // paths. Make sure PWD is set to the original name of the home
  985. // output directory to help cmSystemTools to create the same
  986. // translation table for the dependency scanning process.
  987. depCmd << "cd "
  988. << (this->LocalGenerator->Convert(
  989. this->Makefile->GetHomeOutputDirectory(),
  990. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  991. << " && ";
  992. #endif
  993. // Generate a call this signature:
  994. //
  995. // cmake -E cmake_depends <generator>
  996. // <home-src-dir> <start-src-dir>
  997. // <home-out-dir> <start-out-dir>
  998. // <dep-info> --color=$(COLOR)
  999. //
  1000. // This gives the dependency scanner enough information to recreate
  1001. // the state of our local generator sufficiently for its needs.
  1002. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  1003. << this->GlobalGenerator->GetName() << "\" "
  1004. << this->Convert(this->Makefile->GetHomeDirectory(),
  1005. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1006. << " "
  1007. << this->Convert(this->Makefile->GetStartDirectory(),
  1008. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1009. << " "
  1010. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  1011. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1012. << " "
  1013. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  1014. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1015. << " "
  1016. << this->Convert(this->InfoFileNameFull.c_str(),
  1017. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  1018. if(this->LocalGenerator->GetColorMakefile())
  1019. {
  1020. depCmd << " --color=$(COLOR)";
  1021. }
  1022. commands.push_back(depCmd.str());
  1023. // Make sure all custom command outputs in this target are built.
  1024. if(this->CustomCommandDriver == OnDepends)
  1025. {
  1026. this->DriveCustomCommands(depends);
  1027. }
  1028. // Write the rule.
  1029. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1030. depTarget.c_str(),
  1031. depends, commands, true);
  1032. }
  1033. //----------------------------------------------------------------------------
  1034. void
  1035. cmMakefileTargetGenerator
  1036. ::DriveCustomCommands(std::vector<std::string>& depends)
  1037. {
  1038. // Depend on all custom command outputs.
  1039. const std::vector<cmSourceFile*>& sources =
  1040. this->Target->GetSourceFiles();
  1041. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1042. source != sources.end(); ++source)
  1043. {
  1044. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  1045. {
  1046. const std::vector<std::string>& outputs = cc->GetOutputs();
  1047. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1048. o != outputs.end(); ++o)
  1049. {
  1050. depends.push_back(*o);
  1051. }
  1052. }
  1053. }
  1054. }
  1055. //----------------------------------------------------------------------------
  1056. void cmMakefileTargetGenerator
  1057. ::WriteObjectDependRules(cmSourceFile& source,
  1058. std::vector<std::string>& depends)
  1059. {
  1060. // Create the list of dependencies known at cmake time. These are
  1061. // shared between the object file and dependency scanning rule.
  1062. depends.push_back(source.GetFullPath());
  1063. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  1064. {
  1065. std::vector<std::string> deps;
  1066. cmSystemTools::ExpandListArgument(objectDeps, deps);
  1067. for(std::vector<std::string>::iterator i = deps.begin();
  1068. i != deps.end(); ++i)
  1069. {
  1070. depends.push_back(i->c_str());
  1071. }
  1072. }
  1073. }
  1074. //----------------------------------------------------------------------------
  1075. void cmMakefileTargetGenerator
  1076. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  1077. {
  1078. // Collect the commands.
  1079. std::vector<std::string> commands;
  1080. std::string comment = this->LocalGenerator->ConstructComment(cc);
  1081. if(!comment.empty())
  1082. {
  1083. // add in a progress call if needed
  1084. this->AppendProgress(commands);
  1085. if(!this->NoRuleMessages)
  1086. {
  1087. this->LocalGenerator
  1088. ->AppendEcho(commands, comment.c_str(),
  1089. cmLocalUnixMakefileGenerator3::EchoGenerate);
  1090. }
  1091. }
  1092. // Now append the actual user-specified commands.
  1093. cmOStringStream content;
  1094. this->LocalGenerator->AppendCustomCommand(commands, cc, this->Target, false,
  1095. cmLocalGenerator::HOME_OUTPUT,
  1096. &content);
  1097. // Collect the dependencies.
  1098. std::vector<std::string> depends;
  1099. this->LocalGenerator->AppendCustomDepend(depends, cc);
  1100. // Check whether we need to bother checking for a symbolic output.
  1101. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  1102. // Write the rule.
  1103. const std::vector<std::string>& outputs = cc.GetOutputs();
  1104. std::vector<std::string>::const_iterator o = outputs.begin();
  1105. {
  1106. bool symbolic = false;
  1107. if(need_symbolic)
  1108. {
  1109. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  1110. {
  1111. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1112. }
  1113. }
  1114. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1115. o->c_str(), depends, commands,
  1116. symbolic);
  1117. // If the rule has changed make sure the output is rebuilt.
  1118. if(!symbolic)
  1119. {
  1120. this->GlobalGenerator->AddRuleHash(cc.GetOutputs(), content.str());
  1121. }
  1122. }
  1123. // Write rules to drive building any outputs beyond the first.
  1124. const char* in = o->c_str();
  1125. for(++o; o != outputs.end(); ++o)
  1126. {
  1127. bool symbolic = false;
  1128. if(need_symbolic)
  1129. {
  1130. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  1131. {
  1132. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1133. }
  1134. }
  1135. this->GenerateExtraOutput(o->c_str(), in, symbolic);
  1136. }
  1137. // Setup implicit dependency scanning.
  1138. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1139. idi = cc.GetImplicitDepends().begin();
  1140. idi != cc.GetImplicitDepends().end(); ++idi)
  1141. {
  1142. std::string objFullPath =
  1143. this->Convert(outputs[0].c_str(), cmLocalGenerator::FULL);
  1144. std::string srcFullPath =
  1145. this->Convert(idi->second.c_str(), cmLocalGenerator::FULL);
  1146. this->LocalGenerator->
  1147. AddImplicitDepends(*this->Target, idi->first.c_str(),
  1148. objFullPath.c_str(),
  1149. srcFullPath.c_str());
  1150. }
  1151. }
  1152. //----------------------------------------------------------------------------
  1153. void
  1154. cmMakefileTargetGenerator
  1155. ::GenerateExtraOutput(const char* out, const char* in, bool symbolic)
  1156. {
  1157. // Add a rule to build the primary output if the extra output needs
  1158. // to be created.
  1159. std::vector<std::string> commands;
  1160. std::vector<std::string> depends;
  1161. std::string emptyCommand = this->GlobalGenerator->GetEmptyRuleHackCommand();
  1162. if(!emptyCommand.empty())
  1163. {
  1164. commands.push_back(emptyCommand);
  1165. }
  1166. depends.push_back(in);
  1167. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1168. out, depends, commands,
  1169. symbolic);
  1170. // Register the extra output as paired with the first output so that
  1171. // the check-build-system step will remove the primary output if any
  1172. // extra outputs are missing. This forces the rule to regenerate
  1173. // all outputs.
  1174. this->AddMultipleOutputPair(out, in);
  1175. }
  1176. //----------------------------------------------------------------------------
  1177. void
  1178. cmMakefileTargetGenerator::AppendProgress(std::vector<std::string>& commands)
  1179. {
  1180. this->NumberOfProgressActions++;
  1181. if(this->NoRuleMessages)
  1182. {
  1183. return;
  1184. }
  1185. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1186. progressDir += cmake::GetCMakeFilesDirectory();
  1187. cmOStringStream progCmd;
  1188. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  1189. progCmd << this->LocalGenerator->Convert(progressDir.c_str(),
  1190. cmLocalGenerator::FULL,
  1191. cmLocalGenerator::SHELL);
  1192. progCmd << " $(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1193. commands.push_back(progCmd.str());
  1194. }
  1195. //----------------------------------------------------------------------------
  1196. void
  1197. cmMakefileTargetGenerator
  1198. ::WriteObjectsVariable(std::string& variableName,
  1199. std::string& variableNameExternal)
  1200. {
  1201. // Write a make variable assignment that lists all objects for the
  1202. // target.
  1203. variableName =
  1204. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1205. "_OBJECTS");
  1206. *this->BuildFileStream
  1207. << "# Object files for target " << this->Target->GetName() << "\n"
  1208. << variableName.c_str() << " =";
  1209. std::string object;
  1210. const char* objName =
  1211. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  1212. const char* lineContinue =
  1213. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1214. if(!lineContinue)
  1215. {
  1216. lineContinue = "\\";
  1217. }
  1218. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1219. i != this->Objects.end(); ++i)
  1220. {
  1221. *this->BuildFileStream << " " << lineContinue << "\n";
  1222. if(objName)
  1223. {
  1224. *this->BuildFileStream <<
  1225. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1226. cmLocalGenerator::MAKEFILE);
  1227. }
  1228. else
  1229. {
  1230. *this->BuildFileStream <<
  1231. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1232. }
  1233. }
  1234. *this->BuildFileStream << "\n";
  1235. // Write a make variable assignment that lists all external objects
  1236. // for the target.
  1237. variableNameExternal =
  1238. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1239. "_EXTERNAL_OBJECTS");
  1240. *this->BuildFileStream
  1241. << "\n"
  1242. << "# External object files for target "
  1243. << this->Target->GetName() << "\n"
  1244. << variableNameExternal.c_str() << " =";
  1245. for(std::vector<std::string>::const_iterator i =
  1246. this->ExternalObjects.begin();
  1247. i != this->ExternalObjects.end(); ++i)
  1248. {
  1249. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  1250. *this->BuildFileStream
  1251. << " " << lineContinue << "\n"
  1252. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1253. if(objName)
  1254. {
  1255. *this->BuildFileStream <<
  1256. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1257. cmLocalGenerator::MAKEFILE);
  1258. }
  1259. else
  1260. {
  1261. *this->BuildFileStream <<
  1262. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1263. }
  1264. }
  1265. *this->BuildFileStream << "\n" << "\n";
  1266. }
  1267. //----------------------------------------------------------------------------
  1268. void
  1269. cmMakefileTargetGenerator
  1270. ::WriteObjectsString(std::string& buildObjs)
  1271. {
  1272. std::vector<std::string> objStrings;
  1273. this->WriteObjectsStrings(objStrings);
  1274. buildObjs = objStrings[0];
  1275. }
  1276. //----------------------------------------------------------------------------
  1277. class cmMakefileTargetGeneratorObjectStrings
  1278. {
  1279. public:
  1280. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1281. cmLocalUnixMakefileGenerator3* lg,
  1282. std::string::size_type limit):
  1283. Strings(strings), LocalGenerator(lg), LengthLimit(limit)
  1284. {
  1285. this->Space = "";
  1286. }
  1287. void Feed(std::string const& obj)
  1288. {
  1289. // Construct the name of the next object.
  1290. this->NextObject =
  1291. this->LocalGenerator->Convert(obj.c_str(),
  1292. cmLocalGenerator::START_OUTPUT,
  1293. cmLocalGenerator::RESPONSE);
  1294. // Roll over to next string if the limit will be exceeded.
  1295. if(this->LengthLimit != std::string::npos &&
  1296. (this->CurrentString.length() + 1 + this->NextObject.length()
  1297. > this->LengthLimit))
  1298. {
  1299. this->Strings.push_back(this->CurrentString);
  1300. this->CurrentString = "";
  1301. this->Space = "";
  1302. }
  1303. // Separate from previous object.
  1304. this->CurrentString += this->Space;
  1305. this->Space = " ";
  1306. // Append this object.
  1307. this->CurrentString += this->NextObject;
  1308. }
  1309. void Done()
  1310. {
  1311. this->Strings.push_back(this->CurrentString);
  1312. }
  1313. private:
  1314. std::vector<std::string>& Strings;
  1315. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1316. std::string::size_type LengthLimit;
  1317. std::string CurrentString;
  1318. std::string NextObject;
  1319. const char* Space;
  1320. };
  1321. //----------------------------------------------------------------------------
  1322. void
  1323. cmMakefileTargetGenerator
  1324. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1325. std::string::size_type limit)
  1326. {
  1327. cmMakefileTargetGeneratorObjectStrings
  1328. helper(objStrings, this->LocalGenerator, limit);
  1329. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1330. i != this->Objects.end(); ++i)
  1331. {
  1332. helper.Feed(*i);
  1333. }
  1334. for(std::vector<std::string>::const_iterator i =
  1335. this->ExternalObjects.begin();
  1336. i != this->ExternalObjects.end(); ++i)
  1337. {
  1338. helper.Feed(*i);
  1339. }
  1340. helper.Done();
  1341. }
  1342. //----------------------------------------------------------------------------
  1343. void cmMakefileTargetGenerator::WriteTargetDriverRule(const char* main_output,
  1344. bool relink)
  1345. {
  1346. // Compute the name of the driver target.
  1347. std::string dir =
  1348. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1349. std::string buildTargetRuleName = dir;
  1350. buildTargetRuleName += relink?"/preinstall":"/build";
  1351. buildTargetRuleName = this->Convert(buildTargetRuleName.c_str(),
  1352. cmLocalGenerator::HOME_OUTPUT,
  1353. cmLocalGenerator::UNCHANGED);
  1354. // Build the list of target outputs to drive.
  1355. std::vector<std::string> depends;
  1356. if(main_output)
  1357. {
  1358. depends.push_back(main_output);
  1359. }
  1360. const char* comment = 0;
  1361. if(relink)
  1362. {
  1363. // Setup the comment for the preinstall driver.
  1364. comment = "Rule to relink during preinstall.";
  1365. }
  1366. else
  1367. {
  1368. // Setup the comment for the main build driver.
  1369. comment = "Rule to build all files generated by this target.";
  1370. // Make sure all custom command outputs in this target are built.
  1371. if(this->CustomCommandDriver == OnBuild)
  1372. {
  1373. this->DriveCustomCommands(depends);
  1374. }
  1375. // Make sure the extra files are built.
  1376. for(std::set<cmStdString>::const_iterator i = this->ExtraFiles.begin();
  1377. i != this->ExtraFiles.end(); ++i)
  1378. {
  1379. depends.push_back(*i);
  1380. }
  1381. }
  1382. // Write the driver rule.
  1383. std::vector<std::string> no_commands;
  1384. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1385. buildTargetRuleName.c_str(),
  1386. depends, no_commands, true);
  1387. }
  1388. //----------------------------------------------------------------------------
  1389. std::string cmMakefileTargetGenerator::GetFrameworkFlags()
  1390. {
  1391. if(!this->Makefile->IsOn("APPLE"))
  1392. {
  1393. return std::string();
  1394. }
  1395. std::set<cmStdString> emitted;
  1396. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1397. emitted.insert("/System/Library/Frameworks");
  1398. #endif
  1399. std::vector<std::string> includes;
  1400. this->LocalGenerator->GetIncludeDirectories(includes, this->Target);
  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. this->LocalGenerator->GetIncludeDirectories(includes, this->Target, lang);
  1675. std::string includeFlags =
  1676. this->LocalGenerator->GetIncludeFlags(includes, lang, useResponseFile);
  1677. if(includeFlags.empty())
  1678. {
  1679. return;
  1680. }
  1681. if(useResponseFile)
  1682. {
  1683. std::string name = "includes_";
  1684. name += lang;
  1685. name += ".rsp";
  1686. std::string arg = "@" +
  1687. this->CreateResponseFile(name.c_str(), includeFlags,
  1688. this->FlagFileDepends[lang]);
  1689. this->LocalGenerator->AppendFlags(flags, arg.c_str());
  1690. }
  1691. else
  1692. {
  1693. this->LocalGenerator->AppendFlags(flags, includeFlags.c_str());
  1694. }
  1695. }
  1696. //----------------------------------------------------------------------------
  1697. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1698. {
  1699. // Compute the module directory.
  1700. if(!this->FortranModuleDirectoryComputed)
  1701. {
  1702. const char* target_mod_dir =
  1703. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1704. const char* moddir_flag =
  1705. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1706. if(target_mod_dir && moddir_flag)
  1707. {
  1708. // Compute the full path to the module directory.
  1709. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1710. {
  1711. // Already a full path.
  1712. this->FortranModuleDirectory = target_mod_dir;
  1713. }
  1714. else
  1715. {
  1716. // Interpret relative to the current output directory.
  1717. this->FortranModuleDirectory =
  1718. this->Makefile->GetCurrentOutputDirectory();
  1719. this->FortranModuleDirectory += "/";
  1720. this->FortranModuleDirectory += target_mod_dir;
  1721. }
  1722. // Make sure the module output directory exists.
  1723. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1724. }
  1725. this->FortranModuleDirectoryComputed = true;
  1726. }
  1727. // Return the computed directory.
  1728. if(this->FortranModuleDirectory.empty())
  1729. {
  1730. return 0;
  1731. }
  1732. else
  1733. {
  1734. return this->FortranModuleDirectory.c_str();
  1735. }
  1736. }
  1737. //----------------------------------------------------------------------------
  1738. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1739. {
  1740. // Enable module output if necessary.
  1741. if(const char* modout_flag =
  1742. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  1743. {
  1744. this->LocalGenerator->AppendFlags(flags, modout_flag);
  1745. }
  1746. // Add a module output directory flag if necessary.
  1747. const char* mod_dir = this->GetFortranModuleDirectory();
  1748. if(!mod_dir)
  1749. {
  1750. mod_dir = this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  1751. }
  1752. if(mod_dir)
  1753. {
  1754. const char* moddir_flag =
  1755. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1756. std::string modflag = moddir_flag;
  1757. modflag += this->Convert(mod_dir,
  1758. cmLocalGenerator::START_OUTPUT,
  1759. cmLocalGenerator::SHELL);
  1760. this->LocalGenerator->AppendFlags(flags, modflag.c_str());
  1761. }
  1762. // If there is a separate module path flag then duplicate the
  1763. // include path with it. This compiler does not search the include
  1764. // path for modules.
  1765. if(const char* modpath_flag =
  1766. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1767. {
  1768. std::vector<std::string> includes;
  1769. this->LocalGenerator->GetIncludeDirectories(includes, this->Target);
  1770. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1771. idi != includes.end(); ++idi)
  1772. {
  1773. std::string flg = modpath_flag;
  1774. flg += this->Convert(idi->c_str(),
  1775. cmLocalGenerator::NONE,
  1776. cmLocalGenerator::SHELL);
  1777. this->LocalGenerator->AppendFlags(flags, flg.c_str());
  1778. }
  1779. }
  1780. }
  1781. //----------------------------------------------------------------------------
  1782. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1783. {
  1784. if(this->GeneratorTarget->ModuleDefinitionFile.empty())
  1785. {
  1786. return;
  1787. }
  1788. // TODO: Create a per-language flag variable.
  1789. const char* defFileFlag =
  1790. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1791. if(!defFileFlag)
  1792. {
  1793. return;
  1794. }
  1795. // Append the flag and value. Use ConvertToLinkReference to help
  1796. // vs6's "cl -link" pass it to the linker.
  1797. std::string flag = defFileFlag;
  1798. flag += (this->LocalGenerator->ConvertToLinkReference(
  1799. this->GeneratorTarget->ModuleDefinitionFile.c_str()));
  1800. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  1801. }
  1802. //----------------------------------------------------------------------------
  1803. const char* cmMakefileTargetGenerator::GetFeature(const char* feature)
  1804. {
  1805. return this->Target->GetFeature(feature, this->ConfigName);
  1806. }
  1807. //----------------------------------------------------------------------------
  1808. bool cmMakefileTargetGenerator::GetFeatureAsBool(const char* feature)
  1809. {
  1810. return cmSystemTools::IsOn(this->GetFeature(feature));
  1811. }
  1812. //----------------------------------------------------------------------------
  1813. void cmMakefileTargetGenerator::AddFeatureFlags(
  1814. std::string& flags, const char* lang
  1815. )
  1816. {
  1817. // Add language-specific flags.
  1818. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1819. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1820. {
  1821. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1822. }
  1823. }