cmLocalUnixMakefileGenerator2.cxx 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmLocalUnixMakefileGenerator2.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. #include <queue>
  19. // Quick-switch for generating old makefiles.
  20. #if 0
  21. # define CMLUMG_MAKEFILE_NAME "Makefile"
  22. #else
  23. # define CMLUMG_WRITE_OLD_MAKEFILE
  24. # define CMLUMG_MAKEFILE_NAME "Makefile2"
  25. #endif
  26. //----------------------------------------------------------------------------
  27. cmLocalUnixMakefileGenerator2::cmLocalUnixMakefileGenerator2()
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. cmLocalUnixMakefileGenerator2::~cmLocalUnixMakefileGenerator2()
  32. {
  33. }
  34. //----------------------------------------------------------------------------
  35. void cmLocalUnixMakefileGenerator2::SetEmptyCommand(const char* cmd)
  36. {
  37. m_EmptyCommands.clear();
  38. if(cmd)
  39. {
  40. m_EmptyCommands.push_back(cmd);
  41. }
  42. }
  43. //----------------------------------------------------------------------------
  44. void cmLocalUnixMakefileGenerator2::Generate(bool fromTheTop)
  45. {
  46. #ifdef CMLUMG_WRITE_OLD_MAKEFILE
  47. // Generate old style for now.
  48. this->cmLocalUnixMakefileGenerator::Generate(fromTheTop);
  49. #else
  50. // Make sure we never run a local generate.
  51. if(!fromTheTop)
  52. {
  53. cmSystemTools::Error("Local generate invoked in ",
  54. m_Makefile->GetStartOutputDirectory());
  55. return;
  56. }
  57. // Handle EXECUTABLE_OUTPUT_PATH and LIBRARY_OUTPUT_PATH since
  58. // superclass generator is not called.
  59. this->ConfigureOutputPaths();
  60. #endif
  61. // Generate the rule files for each target.
  62. const cmTargets& targets = m_Makefile->GetTargets();
  63. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  64. {
  65. // TODO: Dispatch generation of each target type.
  66. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  67. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  68. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  69. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  70. {
  71. this->GenerateTargetRuleFile(t->second);
  72. }
  73. }
  74. // Generate the rule files for each custom command.
  75. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  76. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  77. i != sources.end(); ++i)
  78. {
  79. if((*i)->GetCustomCommand())
  80. {
  81. this->GenerateCustomRuleFile(*(*i));
  82. }
  83. }
  84. // Generate the main makefile.
  85. this->GenerateMakefile();
  86. // Generate the cmake file that keeps the makefile up to date.
  87. this->GenerateCMakefile();
  88. }
  89. //----------------------------------------------------------------------------
  90. void cmLocalUnixMakefileGenerator2::GenerateMakefile()
  91. {
  92. // Open the output file. This should not be copy-if-different
  93. // because the check-build-system step compares the makefile time to
  94. // see if the build system must be regenerated.
  95. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  96. makefileName += "/" CMLUMG_MAKEFILE_NAME;
  97. cmGeneratedFileStream makefileStream(makefileName.c_str());
  98. if(!makefileStream)
  99. {
  100. return;
  101. }
  102. // Write the do not edit header.
  103. this->WriteDisclaimer(makefileStream);
  104. // Write standard variables to the makefile.
  105. this->WriteMakeVariables(makefileStream);
  106. // Write special targets that belong at the top of the file.
  107. this->WriteSpecialTargetsTop(makefileStream);
  108. // Write rules to build dependencies and targets.
  109. this->WriteAllRules(makefileStream);
  110. // Write dependency generation rules.
  111. this->WriteDependRules(makefileStream);
  112. // Write main build rules.
  113. this->WriteBuildRules(makefileStream);
  114. // Write clean rules.
  115. this->WriteCleanRules(makefileStream);
  116. // TODO: Write install rules.
  117. //this->WriteInstallRules(makefileStream);
  118. // Write include statements to get rules for this directory.
  119. this->WriteRuleFileIncludes(makefileStream);
  120. // Write jump-and-build rules that were recorded in the map.
  121. this->WriteJumpAndBuildRules(makefileStream);
  122. // Write special targets that belong at the bottom of the file.
  123. this->WriteSpecialTargetsBottom(makefileStream);
  124. }
  125. //----------------------------------------------------------------------------
  126. void cmLocalUnixMakefileGenerator2::GenerateCMakefile()
  127. {
  128. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  129. makefileName += "/" CMLUMG_MAKEFILE_NAME;
  130. std::string cmakefileName = makefileName;
  131. cmakefileName += ".cmake";
  132. // TODO: Use relative paths in this generated file.
  133. // Open the output file.
  134. cmGeneratedFileStream cmakefileStream(cmakefileName.c_str());
  135. if(!cmakefileStream)
  136. {
  137. return;
  138. }
  139. // Write the do not edit header.
  140. this->WriteDisclaimer(cmakefileStream);
  141. // Get the list of files contributing to this generation step.
  142. // Sort the list and remove duplicates.
  143. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  144. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  145. std::vector<std::string>::iterator new_end = std::unique(lfiles.begin(),
  146. lfiles.end());
  147. lfiles.erase(new_end, lfiles.end());
  148. // Save the list to the cmake file.
  149. cmakefileStream
  150. << "# The corresponding makefile\n"
  151. << "# \"" << makefileName << "\"\n"
  152. << "# was generated from the following files:\n"
  153. << "SET(CMAKE_MAKEFILE_DEPENDS\n"
  154. << " \"" << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\"\n";
  155. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  156. i != lfiles.end(); ++i)
  157. {
  158. cmakefileStream
  159. << " \"" << i->c_str() << "\"\n";
  160. }
  161. cmakefileStream
  162. << " )\n\n";
  163. // Set the corresponding makefile in the cmake file.
  164. cmakefileStream
  165. << "# The corresponding makefile is:\n"
  166. << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
  167. << " \"" << makefileName.c_str() << "\"\n"
  168. << " \"" << m_Makefile->GetHomeOutputDirectory() << "/cmake.check_cache\"\n"
  169. << " )\n\n";
  170. // Set the set of files to check for dependency integrity.
  171. cmakefileStream
  172. << "# The set of files whose dependency integrity should be checked:\n"
  173. << "SET(CMAKE_DEPENDS_CHECK\n";
  174. for(std::set<cmStdString>::const_iterator i = m_CheckDependFiles.begin();
  175. i != m_CheckDependFiles.end(); ++i)
  176. {
  177. cmakefileStream
  178. << " \"" << i->c_str() << "\"\n";
  179. }
  180. cmakefileStream
  181. << " )\n";
  182. }
  183. //----------------------------------------------------------------------------
  184. void
  185. cmLocalUnixMakefileGenerator2
  186. ::GenerateTargetRuleFile(const cmTarget& target)
  187. {
  188. // Create a directory for this target.
  189. std::string dir = this->GetTargetDirectory(target);
  190. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  191. // First generate the object rule files. Save a list of all object
  192. // files for this target.
  193. std::vector<std::string> objects;
  194. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  195. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  196. source != sources.end(); ++source)
  197. {
  198. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  199. !(*source)->GetCustomCommand() &&
  200. !m_GlobalGenerator->IgnoreFile((*source)->GetSourceExtension().c_str()))
  201. {
  202. // Generate this object file's rule file.
  203. this->GenerateObjectRuleFile(target, *(*source));
  204. // Save the object file name.
  205. objects.push_back(this->GetObjectFileName(target, *(*source)));
  206. }
  207. }
  208. // Generate the build-time dependencies file for this target.
  209. std::string depBase = dir;
  210. depBase += "/";
  211. depBase += target.GetName();
  212. std::string depMakeFile = this->GenerateDependsMakeFile(depBase.c_str());
  213. // Construct the rule file name.
  214. std::string ruleFileName = dir;
  215. ruleFileName += "/";
  216. ruleFileName += target.GetName();
  217. ruleFileName += ".make";
  218. // The rule file must be included by the makefile.
  219. m_IncludeRuleFiles.push_back(ruleFileName);
  220. // Open the rule file. This should be copy-if-different because the
  221. // rules may depend on this file itself.
  222. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  223. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  224. ruleFileStream.SetCopyIfDifferent(true);
  225. if(!ruleFileStream)
  226. {
  227. return;
  228. }
  229. this->WriteDisclaimer(ruleFileStream);
  230. ruleFileStream
  231. << "# Rule file for target " << target.GetName() << ".\n\n";
  232. // Include the dependencies for the target.
  233. ruleFileStream
  234. << "# Include any dependencies generated for this rule.\n"
  235. << m_IncludeDirective << " "
  236. << this->ConvertToOutputForExisting(depMakeFile.c_str()).c_str()
  237. << "\n\n";
  238. // Include the rule file for each object.
  239. if(!objects.empty())
  240. {
  241. ruleFileStream
  242. << "# Include make rules for object files.\n";
  243. for(std::vector<std::string>::const_iterator obj = objects.begin();
  244. obj != objects.end(); ++obj)
  245. {
  246. std::string objRuleFileName = *obj;
  247. objRuleFileName += ".make";
  248. ruleFileStream
  249. << m_IncludeDirective << " "
  250. << this->ConvertToOutputForExisting(objRuleFileName.c_str()).c_str()
  251. << "\n";
  252. }
  253. ruleFileStream
  254. << "\n";
  255. }
  256. // Write the dependency generation rule.
  257. {
  258. std::vector<std::string> depends;
  259. std::vector<std::string> no_commands;
  260. std::string depEcho = "Building dependencies for ";
  261. depEcho += target.GetName();
  262. depEcho += "...";
  263. std::string depTarget = dir;
  264. depTarget += "/";
  265. depTarget += target.GetName();
  266. depTarget += ".depends";
  267. for(std::vector<std::string>::const_iterator obj = objects.begin();
  268. obj != objects.end(); ++obj)
  269. {
  270. depends.push_back((*obj)+".depends");
  271. }
  272. depends.push_back(ruleFileName);
  273. this->WriteMakeRule(ruleFileStream, 0, depEcho.c_str(),
  274. depTarget.c_str(), depends, no_commands);
  275. }
  276. // Write the build rule.
  277. switch(target.GetType())
  278. {
  279. case cmTarget::STATIC_LIBRARY:
  280. this->WriteStaticLibraryRule(ruleFileStream, ruleFileName.c_str(),
  281. target, objects);
  282. break;
  283. case cmTarget::SHARED_LIBRARY:
  284. this->WriteSharedLibraryRule(ruleFileStream, ruleFileName.c_str(),
  285. target, objects);
  286. break;
  287. case cmTarget::MODULE_LIBRARY:
  288. this->WriteModuleLibraryRule(ruleFileStream, ruleFileName.c_str(),
  289. target, objects);
  290. break;
  291. case cmTarget::EXECUTABLE:
  292. this->WriteExecutableRule(ruleFileStream, ruleFileName.c_str(),
  293. target, objects);
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. //----------------------------------------------------------------------------
  300. void
  301. cmLocalUnixMakefileGenerator2
  302. ::GenerateObjectRuleFile(const cmTarget& target, const cmSourceFile& source)
  303. {
  304. // Identify the language of the source file.
  305. const char* lang = this->GetSourceFileLanguage(source);
  306. if(!lang)
  307. {
  308. // If language is not known, this is an error.
  309. cmSystemTools::Error("Source file \"", source.GetFullPath().c_str(),
  310. "\" has unknown type.");
  311. return;
  312. }
  313. // Get the full path name of the object file.
  314. std::string obj = this->GetObjectFileName(target, source);
  315. // The object file should be checked for dependency integrity.
  316. m_CheckDependFiles.insert(obj);
  317. // Create the directory containing the object file. This may be a
  318. // subdirectory under the target's directory.
  319. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  320. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  321. // Generate the build-time dependencies file for this object file.
  322. std::string depMakeFile = this->GenerateDependsMakeFile(obj.c_str());
  323. // Open the rule file for writing. This should be copy-if-different
  324. // because the rules may depend on this file itself.
  325. std::string ruleFileName = obj;
  326. ruleFileName += ".make";
  327. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  328. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  329. ruleFileStream.SetCopyIfDifferent(true);
  330. if(!ruleFileStream)
  331. {
  332. return;
  333. }
  334. this->WriteDisclaimer(ruleFileStream);
  335. ruleFileStream
  336. << "# Rule file for object file " << obj.c_str() << ".\n\n";
  337. // Include the dependencies for the target.
  338. ruleFileStream
  339. << "# Include any dependencies generated for this rule.\n"
  340. << m_IncludeDirective << " "
  341. << this->ConvertToOutputForExisting(depMakeFile.c_str()).c_str()
  342. << "\n\n";
  343. // Create the list of dependencies known at cmake time. These are
  344. // shared between the object file and dependency scanning rule.
  345. std::vector<std::string> depends;
  346. depends.push_back(source.GetFullPath());
  347. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  348. {
  349. std::vector<std::string> deps;
  350. cmSystemTools::ExpandListArgument(objectDeps, deps);
  351. for(std::vector<std::string>::iterator i = deps.begin();
  352. i != deps.end(); ++i)
  353. {
  354. depends.push_back(this->ConvertToRelativeOutputPath(i->c_str()));
  355. }
  356. }
  357. depends.push_back(ruleFileName);
  358. // Write the dependency generation rule.
  359. std::string depTarget = obj;
  360. depTarget += ".depends";
  361. {
  362. std::string depEcho = "Scanning ";
  363. depEcho += lang;
  364. depEcho += " dependencies of ";
  365. depEcho += this->ConvertToRelativeOutputPath(obj.c_str());
  366. depEcho += "...";
  367. // Add a command to call CMake to scan dependencies. CMake will
  368. // touch the corresponding depends file after scanning dependencies.
  369. cmOStringStream depCmd;
  370. // TODO: Account for source file properties and directory-level
  371. // definitions when scanning for dependencies.
  372. depCmd << "$(CMAKE_COMMAND) -E cmake_depends " << lang << " "
  373. << this->ConvertToRelativeOutputPath(obj.c_str()) << " "
  374. << this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  375. std::vector<std::string> includeDirs;
  376. this->GetIncludeDirectories(includeDirs);
  377. for(std::vector<std::string>::iterator i = includeDirs.begin();
  378. i != includeDirs.end(); ++i)
  379. {
  380. depCmd << " -I" << this->ConvertToRelativeOutputPath(i->c_str());
  381. }
  382. std::vector<std::string> commands;
  383. commands.push_back(depCmd.str());
  384. // Write the rule.
  385. this->WriteMakeRule(ruleFileStream, 0, depEcho.c_str(),
  386. depTarget.c_str(), depends, commands);
  387. }
  388. // Write the build rule.
  389. {
  390. // Build the set of compiler flags.
  391. std::string flags;
  392. // Add the export symbol definition for shared library objects.
  393. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  394. (target.GetType() == cmTarget::MODULE_LIBRARY));
  395. if(shared)
  396. {
  397. flags += "-D";
  398. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  399. {
  400. flags += custom_export_name;
  401. }
  402. else
  403. {
  404. std::string in = target.GetName();
  405. in += "_EXPORTS";
  406. flags += cmSystemTools::MakeCindentifier(in.c_str());
  407. }
  408. }
  409. // Add flags from source file properties.
  410. this->AppendFlags(flags, source.GetProperty("COMPILE_FLAGS"));
  411. // Add language-specific flags.
  412. this->AddLanguageFlags(flags, lang);
  413. // Add shared-library flags if needed.
  414. this->AddSharedFlags(flags, lang, shared);
  415. // Add include directory flags.
  416. this->AppendFlags(flags, this->GetIncludeFlags(lang));
  417. // Get the output paths for source and object files.
  418. std::string sourceFile =
  419. this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  420. std::string objectFile =
  421. this->ConvertToRelativeOutputPath(obj.c_str());
  422. // Construct the compile rules.
  423. std::vector<std::string> commands;
  424. std::string compileRuleVar = "CMAKE_";
  425. compileRuleVar += lang;
  426. compileRuleVar += "_COMPILE_OBJECT";
  427. std::string compileRule =
  428. m_Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  429. cmSystemTools::ExpandListArgument(compileRule, commands);
  430. // Expand placeholders in the commands.
  431. for(std::vector<std::string>::iterator i = commands.begin();
  432. i != commands.end(); ++i)
  433. {
  434. this->ExpandRuleVariables(*i,
  435. lang,
  436. 0, // no objects
  437. 0, // no target
  438. 0, // no link libs
  439. sourceFile.c_str(),
  440. objectFile.c_str(),
  441. flags.c_str());
  442. }
  443. // Write the rule.
  444. std::string buildEcho = "Building ";
  445. buildEcho += lang;
  446. buildEcho += " object ";
  447. buildEcho += this->ConvertToRelativeOutputPath(obj.c_str());
  448. buildEcho += "...";
  449. this->WriteMakeRule(ruleFileStream, 0, buildEcho.c_str(),
  450. obj.c_str(), depends, commands);
  451. }
  452. }
  453. //----------------------------------------------------------------------------
  454. void
  455. cmLocalUnixMakefileGenerator2
  456. ::GenerateCustomRuleFile(const cmSourceFile& source)
  457. {
  458. // Get the custom command for the source.
  459. if(!source.GetCustomCommand())
  460. {
  461. cmSystemTools::Error("GenerateCustomRuleFile called for non-custom source.");
  462. return;
  463. }
  464. const cmCustomCommand& cc = *source.GetCustomCommand();
  465. // Construct the name of the rule file.
  466. std::string customName = this->GetCustomBaseName(cc);
  467. std::string ruleFileName = customName;
  468. ruleFileName += ".make";
  469. // If this is a duplicate rule produce an error.
  470. if(m_CustomRuleFiles.find(ruleFileName) != m_CustomRuleFiles.end())
  471. {
  472. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  473. cc.GetOutput().c_str());
  474. return;
  475. }
  476. m_CustomRuleFiles.insert(ruleFileName);
  477. // This rule should be included by the makefile.
  478. m_IncludeRuleFiles.push_back(ruleFileName);
  479. // TODO: Convert outputs/dependencies (arguments?) to relative paths.
  480. // Open the rule file. This should be copy-if-different because the
  481. // rules may depend on this file itself.
  482. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  483. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  484. ruleFileStream.SetCopyIfDifferent(true);
  485. if(!ruleFileStream)
  486. {
  487. return;
  488. }
  489. this->WriteDisclaimer(ruleFileStream);
  490. ruleFileStream
  491. << "# Custom command rule file for " << customName.c_str() << ".\n\n";
  492. // Build the command line in a single string.
  493. std::vector<std::string> commands;
  494. std::string cmd = cc.GetCommand();
  495. cmSystemTools::ReplaceString(cmd, "/./", "/");
  496. cmd = this->ConvertToRelativeOutputPath(cmd.c_str());
  497. if(cc.GetArguments().size() > 0)
  498. {
  499. cmd += " ";
  500. cmd += cc.GetArguments();
  501. }
  502. commands.push_back(cmd);
  503. // Collect the dependencies.
  504. std::vector<std::string> depends;
  505. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  506. d != cc.GetDepends().end(); ++d)
  507. {
  508. // Get the dependency with variables expanded.
  509. std::string dep = *d;
  510. m_Makefile->ExpandVariablesInString(dep);
  511. // If the rule depends on a target CMake knows how to build,
  512. // convert it to the path where it will be built.
  513. std::string libPath = dep + "_CMAKE_PATH";
  514. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  515. if(cacheValue && *cacheValue)
  516. {
  517. libPath = cacheValue;
  518. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH") &&
  519. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH")[0] != '\0')
  520. {
  521. libPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  522. }
  523. libPath += "/";
  524. libPath += dep;
  525. libPath += cmSystemTools::GetExecutableExtension();
  526. dep = libPath;
  527. }
  528. // Cleanup the dependency and add it.
  529. cmSystemTools::ReplaceString(dep, "/./", "/");
  530. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  531. dep = this->ConvertToRelativeOutputPath(dep.c_str());
  532. depends.push_back(dep.c_str());
  533. }
  534. // Add a dependency on the rule file itself.
  535. depends.push_back(ruleFileName);
  536. // Write the rule.
  537. const char* comment = 0;
  538. if(cc.GetComment().size())
  539. {
  540. comment = cc.GetComment().c_str();
  541. }
  542. std::string preEcho = "Generating ";
  543. preEcho += customName;
  544. preEcho += "...";
  545. this->WriteMakeRule(ruleFileStream, comment, preEcho.c_str(),
  546. cc.GetOutput().c_str(), depends, commands);
  547. }
  548. //----------------------------------------------------------------------------
  549. std::string
  550. cmLocalUnixMakefileGenerator2
  551. ::GenerateDependsMakeFile(const char* file)
  552. {
  553. // Check if the build-time dependencies file exists.
  554. std::string depMarkFile = file;
  555. depMarkFile += ".depends";
  556. std::string depMakeFile = depMarkFile;
  557. depMakeFile += ".make";
  558. std::string depMakeFileFull = this->ConvertToFullPath(depMakeFile);
  559. if(cmSystemTools::FileExists(depMakeFileFull.c_str()))
  560. {
  561. // The build-time dependencies file already exists. Check it.
  562. this->CheckDependencies(m_Makefile->GetStartOutputDirectory(), file);
  563. }
  564. else
  565. {
  566. // The build-time dependencies file does not exist. Create an
  567. // empty one.
  568. std::string depMarkFileFull = this->ConvertToFullPath(depMarkFile);
  569. this->WriteEmptyDependMakeFile(file, depMarkFileFull.c_str(),
  570. depMakeFileFull.c_str());
  571. }
  572. return depMakeFile;
  573. }
  574. //----------------------------------------------------------------------------
  575. void
  576. cmLocalUnixMakefileGenerator2
  577. ::WriteMakeRule(std::ostream& os,
  578. const char* comment,
  579. const char* preEcho,
  580. const char* target,
  581. const std::vector<std::string>& depends,
  582. const std::vector<std::string>& commands,
  583. const char* postEcho)
  584. {
  585. // Make sure there is a target.
  586. if(!target || !*target)
  587. {
  588. cmSystemTools::Error("No target for WriteMakeRule!");
  589. return;
  590. }
  591. std::string replace;
  592. // Write the comment describing the rule in the makefile.
  593. if(comment)
  594. {
  595. replace = comment;
  596. m_Makefile->ExpandVariablesInString(replace);
  597. std::string::size_type lpos = 0;
  598. std::string::size_type rpos;
  599. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  600. {
  601. os << "# " << replace.substr(lpos, rpos-lpos) << "\n";
  602. lpos = rpos+1;
  603. }
  604. os << "# " << replace.substr(lpos) << "\n";
  605. }
  606. // Construct the left hand side of the rule.
  607. replace = target;
  608. m_Makefile->ExpandVariablesInString(replace);
  609. std::string tgt = this->ConvertToRelativeOutputPath(replace.c_str());
  610. tgt = this->ConvertToMakeTarget(tgt.c_str());
  611. const char* space = "";
  612. if(tgt.size() == 1)
  613. {
  614. // Add a space before the ":" to avoid drive letter confusion on
  615. // Windows.
  616. space = " ";
  617. }
  618. // Write the rule.
  619. if(depends.empty())
  620. {
  621. // No dependencies. The commands will always run.
  622. os << tgt.c_str() << space << ":\n";
  623. }
  624. else
  625. {
  626. // Split dependencies into multiple rule lines. This allows for
  627. // very long dependency lists even on older make implementations.
  628. for(std::vector<std::string>::const_iterator dep = depends.begin();
  629. dep != depends.end(); ++dep)
  630. {
  631. replace = *dep;
  632. m_Makefile->ExpandVariablesInString(replace);
  633. replace = this->ConvertToRelativeOutputPath(replace.c_str());
  634. replace = this->ConvertToMakeTarget(replace.c_str());
  635. os << tgt.c_str() << space << ": " << replace.c_str() << "\n";
  636. }
  637. }
  638. // Write the list of commands.
  639. bool first = true;
  640. for(std::vector<std::string>::const_iterator i = commands.begin();
  641. i != commands.end(); ++i)
  642. {
  643. replace = *i;
  644. m_Makefile->ExpandVariablesInString(replace);
  645. if(first && preEcho)
  646. {
  647. this->OutputEcho(os, preEcho);
  648. }
  649. os << "\t" << replace.c_str() << "\n";
  650. first = false;
  651. }
  652. if(postEcho)
  653. {
  654. this->OutputEcho(os, postEcho);
  655. }
  656. os << "\n";
  657. }
  658. //----------------------------------------------------------------------------
  659. void cmLocalUnixMakefileGenerator2::WriteDivider(std::ostream& os)
  660. {
  661. os
  662. << "#======================================"
  663. << "=======================================\n";
  664. }
  665. //----------------------------------------------------------------------------
  666. void cmLocalUnixMakefileGenerator2::WriteDisclaimer(std::ostream& os)
  667. {
  668. os
  669. << "# CMAKE generated file: DO NOT EDIT!\n"
  670. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  671. << " Generator, CMake Version "
  672. << cmMakefile::GetMajorVersion() << "."
  673. << cmMakefile::GetMinorVersion() << "\n\n";
  674. }
  675. //----------------------------------------------------------------------------
  676. void
  677. cmLocalUnixMakefileGenerator2
  678. ::WriteMakeVariables(std::ostream& makefileStream)
  679. {
  680. this->WriteDivider(makefileStream);
  681. makefileStream
  682. << "# Set environment variables for the build.\n"
  683. << "\n";
  684. if(m_WindowsShell)
  685. {
  686. makefileStream
  687. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  688. << "NULL=\n"
  689. << "!ELSE\n"
  690. << "NULL=nul\n"
  691. << "!ENDIF\n";
  692. }
  693. else
  694. {
  695. makefileStream
  696. << "# The shell in which to execute make rules.\n"
  697. << "SHELL = /bin/sh\n"
  698. << "\n";
  699. }
  700. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  701. {
  702. makefileStream
  703. << "# Produce verbose output by default.\n"
  704. << "VERBOSE = 1\n"
  705. << "\n";
  706. }
  707. std::string cmakecommand =
  708. this->ConvertToOutputForExisting(
  709. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  710. makefileStream
  711. << "# The CMake executable.\n"
  712. << "CMAKE_COMMAND = " << cmakecommand.c_str() << "\n"
  713. << "\n";
  714. makefileStream
  715. << "# The command to remove a file.\n"
  716. << "RM = " << cmakecommand.c_str() << " -E remove -f\n"
  717. << "\n";
  718. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  719. {
  720. makefileStream
  721. << "# The program to use to edit the cache.\n"
  722. << "CMAKE_EDIT_COMMAND = "
  723. << (this->ConvertToOutputForExisting(
  724. m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
  725. << "\n";
  726. }
  727. makefileStream
  728. << "# The source directory corresponding to this makefile.\n"
  729. << "CMAKE_CURRENT_SOURCE = "
  730. << this->ConvertToRelativeOutputPath(m_Makefile->GetStartDirectory())
  731. << "\n"
  732. << "\n";
  733. makefileStream
  734. << "# The build directory corresponding to this makefile.\n"
  735. << "CMAKE_CURRENT_BINARY = "
  736. << this->ConvertToRelativeOutputPath(m_Makefile->GetStartOutputDirectory())
  737. << "\n"
  738. << "\n";
  739. makefileStream
  740. << "# The top-level source directory on which CMake was run.\n"
  741. << "CMAKE_SOURCE_DIR = "
  742. << this->ConvertToRelativeOutputPath(m_Makefile->GetHomeDirectory())
  743. << "\n"
  744. << "\n";
  745. makefileStream
  746. << "# The top-level build directory on which CMake was run.\n"
  747. << "CMAKE_BINARY_DIR = "
  748. << this->ConvertToRelativeOutputPath(m_Makefile->GetHomeOutputDirectory())
  749. << "\n"
  750. << "\n";
  751. }
  752. //----------------------------------------------------------------------------
  753. void
  754. cmLocalUnixMakefileGenerator2
  755. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  756. {
  757. this->WriteDivider(makefileStream);
  758. makefileStream
  759. << "# Special targets provided by cmake.\n"
  760. << "\n";
  761. // Write the main entry point target. This must be the VERY first
  762. // target so that make with no arguments will run it.
  763. {
  764. // Just depend on the all target to drive the build.
  765. std::vector<std::string> depends;
  766. std::vector<std::string> no_commands;
  767. depends.push_back("all");
  768. // Write the rule.
  769. this->WriteMakeRule(makefileStream,
  770. "Default target executed when no arguments are "
  771. "given to make.",
  772. 0,
  773. "default_target",
  774. depends,
  775. no_commands);
  776. }
  777. // Write special "rebuild_cache" target to re-run cmake.
  778. {
  779. std::vector<std::string> no_depends;
  780. std::vector<std::string> commands;
  781. commands.push_back(
  782. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  783. this->WriteMakeRule(makefileStream,
  784. "Special rule to re-run CMake using make.",
  785. "Running CMake to regenerate build system...",
  786. "rebuild_cache",
  787. no_depends,
  788. commands);
  789. }
  790. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  791. // Otherwise default to the interactive command-line interface.
  792. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  793. {
  794. std::vector<std::string> no_depends;
  795. std::vector<std::string> commands;
  796. commands.push_back(
  797. "$(CMAKE_EDIT_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  798. this->WriteMakeRule(makefileStream,
  799. "Special rule to re-run CMake cache editor using make.",
  800. "Running CMake cache editor...",
  801. "edit_cache",
  802. no_depends,
  803. commands);
  804. }
  805. else
  806. {
  807. std::vector<std::string> no_depends;
  808. std::vector<std::string> commands;
  809. commands.push_back(
  810. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  811. this->WriteMakeRule(makefileStream,
  812. "Special rule to re-run CMake cache editor using make.",
  813. "Running interactive CMake command-line interface...",
  814. "edit_cache",
  815. no_depends,
  816. commands);
  817. }
  818. }
  819. //----------------------------------------------------------------------------
  820. void
  821. cmLocalUnixMakefileGenerator2
  822. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  823. {
  824. this->WriteDivider(makefileStream);
  825. makefileStream
  826. << "# Special targets to cleanup operation of make.\n"
  827. << "\n";
  828. // Write special "cmake_check_build_system" target to run cmake with
  829. // the --check-build-system flag.
  830. {
  831. // Build command to run CMake to check if anything needs regenerating.
  832. std::string cmakefileName = m_Makefile->GetStartOutputDirectory();
  833. cmakefileName += "/" CMLUMG_MAKEFILE_NAME ".cmake";
  834. std::string runRule =
  835. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  836. runRule += " --check-build-system ";
  837. runRule += this->ConvertToRelativeOutputPath(cmakefileName.c_str());
  838. std::vector<std::string> no_depends;
  839. std::vector<std::string> commands;
  840. commands.push_back(runRule);
  841. std::string preEcho = "Checking build system in ";
  842. preEcho += m_Makefile->GetStartOutputDirectory();
  843. preEcho += "...";
  844. this->WriteMakeRule(makefileStream,
  845. "Special rule to run CMake to check the build system "
  846. "integrity.\n"
  847. "No rule that depends on this can have "
  848. "commands that come from listfiles\n"
  849. "because they might be regenerated.",
  850. preEcho.c_str(),
  851. "cmake_check_build_system",
  852. no_depends,
  853. commands);
  854. }
  855. std::vector<std::string> no_commands;
  856. // Write special target to silence make output. This must be after
  857. // the default target in case VERBOSE is set (which changes the
  858. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  859. // "VERBOSE=1" to be added as a make variable which will change the
  860. // name of this special target. This gives a make-time choice to
  861. // the user.
  862. std::vector<std::string> no_depends;
  863. this->WriteMakeRule(makefileStream,
  864. "Suppress display of executed commands.",
  865. 0,
  866. "$(VERBOSE).SILENT",
  867. no_depends,
  868. no_commands);
  869. // Special target to cleanup operation of make tool.
  870. std::vector<std::string> depends;
  871. this->WriteMakeRule(makefileStream,
  872. "Disable implicit rules so canoncical targets will work.",
  873. 0,
  874. ".SUFFIXES",
  875. depends,
  876. no_commands);
  877. depends.push_back(".hpux_make_must_have_suffixes_list");
  878. this->WriteMakeRule(makefileStream, 0, 0,
  879. ".SUFFIXES", depends, no_commands);
  880. }
  881. //----------------------------------------------------------------------------
  882. void
  883. cmLocalUnixMakefileGenerator2
  884. ::WriteAllRules(std::ostream& makefileStream)
  885. {
  886. // Write section header.
  887. this->WriteDivider(makefileStream);
  888. makefileStream
  889. << "# Rules to build dependencies and targets.\n"
  890. << "\n";
  891. // Write rules to traverse the directory tree building dependencies
  892. // and targets.
  893. this->WriteDriverRules(makefileStream, "all", "depend.local", "build.local");
  894. }
  895. //----------------------------------------------------------------------------
  896. void
  897. cmLocalUnixMakefileGenerator2::WriteDependRules(std::ostream& makefileStream)
  898. {
  899. // Write section header.
  900. this->WriteDivider(makefileStream);
  901. makefileStream
  902. << "# Rules for computing dependencies.\n"
  903. << "\n";
  904. // TODO: Unify WriteDependRules, WriteBuildRules, WriteCleanRules,
  905. // etc. They are mostly duplicate code.
  906. // TODO: Add registered files for cleaning.
  907. // Write rules to traverse the directory tree building dependencies.
  908. this->WriteDriverRules(makefileStream, "depend", "depend.local");
  909. // Write the rule to build dependencies in this directory. It just
  910. // depends on all targets' dependencies.
  911. const cmTargets& targets = m_Makefile->GetTargets();
  912. std::vector<std::string> depends;
  913. std::vector<std::string> commands;
  914. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  915. {
  916. // TODO: Dispatch generation of each target type.
  917. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  918. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  919. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  920. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  921. {
  922. if(t->second.IsInAll())
  923. {
  924. std::string dep = this->GetTargetDirectory(t->second);
  925. dep += "/";
  926. dep += t->first;
  927. dep += ".depends";
  928. depends.push_back(dep);
  929. }
  930. }
  931. }
  932. // If there are no dependencies, use empty commands.
  933. if(depends.empty())
  934. {
  935. commands = m_EmptyCommands;
  936. }
  937. // Write the rule.
  938. this->WriteMakeRule(makefileStream,
  939. "Build dependencies for this directory.",
  940. 0,
  941. "depend.local",
  942. depends,
  943. commands);
  944. }
  945. //----------------------------------------------------------------------------
  946. void
  947. cmLocalUnixMakefileGenerator2::WriteBuildRules(std::ostream& makefileStream)
  948. {
  949. // Write section header.
  950. this->WriteDivider(makefileStream);
  951. makefileStream
  952. << "# Rules for building targets.\n"
  953. << "\n";
  954. // Write rules to traverse the directory tree building targets.
  955. this->WriteDriverRules(makefileStream, "build", "build.local");
  956. // Write the rule to build targets in this directory. It just
  957. // depends on all targets.
  958. const cmTargets& targets = m_Makefile->GetTargets();
  959. std::vector<std::string> depends;
  960. std::vector<std::string> commands;
  961. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  962. {
  963. // TODO: Dispatch generation of each target type.
  964. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  965. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  966. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  967. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  968. {
  969. if(t->second.IsInAll())
  970. {
  971. depends.push_back(t->first);
  972. }
  973. }
  974. }
  975. // If there are no dependencies, use empty commands.
  976. if(depends.empty())
  977. {
  978. commands = m_EmptyCommands;
  979. }
  980. // Write the rule.
  981. this->WriteMakeRule(makefileStream,
  982. "Build targets in this directory.",
  983. 0,
  984. "build.local",
  985. depends,
  986. commands);
  987. }
  988. //----------------------------------------------------------------------------
  989. void
  990. cmLocalUnixMakefileGenerator2::WriteCleanRules(std::ostream& makefileStream)
  991. {
  992. // Write section header.
  993. this->WriteDivider(makefileStream);
  994. makefileStream
  995. << "# Rules for cleaning targets.\n"
  996. << "\n";
  997. // Write rules to traverse the directory tree cleaning targets.
  998. this->WriteDriverRules(makefileStream, "clean", "clean.local");
  999. // Write the rule to clean targets in this directory. It just
  1000. // depends on all targets' clean rules.
  1001. const cmTargets& targets = m_Makefile->GetTargets();
  1002. std::vector<std::string> depends;
  1003. std::vector<std::string> commands;
  1004. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  1005. {
  1006. // TODO: Dispatch generation of each target type.
  1007. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  1008. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  1009. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  1010. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  1011. {
  1012. if(t->second.IsInAll())
  1013. {
  1014. std::string clean = t->first;
  1015. clean += ".clean";
  1016. depends.push_back(clean);
  1017. }
  1018. }
  1019. }
  1020. // If there are no dependencies, use empty commands.
  1021. if(depends.empty())
  1022. {
  1023. commands = m_EmptyCommands;
  1024. }
  1025. // Write the rule.
  1026. this->WriteMakeRule(makefileStream,
  1027. "Clean targets in this directory.",
  1028. 0,
  1029. "clean.local",
  1030. depends,
  1031. commands);
  1032. }
  1033. //----------------------------------------------------------------------------
  1034. void
  1035. cmLocalUnixMakefileGenerator2
  1036. ::WriteDriverRules(std::ostream& makefileStream, const char* pass,
  1037. const char* local1, const char* local2)
  1038. {
  1039. // Write a set of targets that will traverse the directory structure
  1040. // in order and build given local targets in each directory.
  1041. // The dependencies and commands generated for this rule must not
  1042. // depend on listfile contents because the build system check might
  1043. // regenerate the makefile but it might not get read again by make
  1044. // before the commands run.
  1045. std::vector<std::string> depends;
  1046. std::vector<std::string> commands;
  1047. // Check the build system in this directory.
  1048. depends.push_back("cmake_check_build_system");
  1049. // Recursively handle pre-order directories.
  1050. std::string preTarget = pass;
  1051. preTarget += ".pre-order";
  1052. commands.push_back(this->GetRecursiveMakeCall(preTarget.c_str()));
  1053. // Recursively build the local targets in this directory.
  1054. if(local1)
  1055. {
  1056. commands.push_back(this->GetRecursiveMakeCall(local1));
  1057. }
  1058. if(local2)
  1059. {
  1060. commands.push_back(this->GetRecursiveMakeCall(local2));
  1061. }
  1062. // Recursively handle post-order directories.
  1063. std::string postTarget = pass;
  1064. postTarget += ".post-order";
  1065. commands.push_back(this->GetRecursiveMakeCall(postTarget.c_str()));
  1066. // Write the rule.
  1067. std::string preEcho = "Entering directory ";
  1068. preEcho += m_Makefile->GetStartOutputDirectory();
  1069. std::string postEcho = "Finished directory ";
  1070. postEcho += m_Makefile->GetStartOutputDirectory();
  1071. this->WriteMakeRule(makefileStream, 0, preEcho.c_str(),
  1072. pass, depends, commands, postEcho.c_str());
  1073. // Write the subdirectory traversal rules.
  1074. this->WriteSubdirRules(makefileStream, pass);
  1075. }
  1076. //----------------------------------------------------------------------------
  1077. void
  1078. cmLocalUnixMakefileGenerator2
  1079. ::WriteSubdirRules(std::ostream& makefileStream, const char* pass)
  1080. {
  1081. // Iterate through subdirectories. Only entries in which the
  1082. // boolean is true should be included. Keep track of the last
  1083. // pre-order and last post-order rule created so that ordering can
  1084. // be enforced.
  1085. const std::vector<std::pair<cmStdString, bool> >&
  1086. subdirs = m_Makefile->GetSubDirectories();
  1087. std::string lastPre = "";
  1088. std::string lastPost = "";
  1089. for(std::vector<std::pair<cmStdString, bool> >::const_iterator
  1090. i = subdirs.begin(); i != subdirs.end(); ++i)
  1091. {
  1092. if(i->second)
  1093. {
  1094. // Add the subdirectory rule either for pre-order or post-order.
  1095. if(m_Makefile->IsDirectoryPreOrder(i->first.c_str()))
  1096. {
  1097. this->WriteSubdirRule(makefileStream, pass, i->first.c_str(), lastPre);
  1098. }
  1099. else
  1100. {
  1101. this->WriteSubdirRule(makefileStream, pass, i->first.c_str(), lastPost);
  1102. }
  1103. }
  1104. }
  1105. // Write the subdir driver rules. Hook them to the last
  1106. // subdirectory of their corresponding order.
  1107. this->WriteSubdirDriverRule(makefileStream, pass, "pre", lastPre);
  1108. this->WriteSubdirDriverRule(makefileStream, pass, "post", lastPost);
  1109. }
  1110. //----------------------------------------------------------------------------
  1111. void
  1112. cmLocalUnixMakefileGenerator2
  1113. ::WriteSubdirRule(std::ostream& makefileStream, const char* pass,
  1114. const char* subdir, std::string& last)
  1115. {
  1116. std::vector<std::string> depends;
  1117. std::vector<std::string> commands;
  1118. // Construct the name of the subdirectory rule.
  1119. std::string tgt = this->GetSubdirTargetName(pass, subdir);
  1120. if(m_WindowsShell)
  1121. {
  1122. // On Windows we must perform each step separately and then change
  1123. // back because the shell keeps the working directory between
  1124. // commands.
  1125. std::string cmd = "cd ";
  1126. cmd += this->ConvertToOutputForExisting(subdir);
  1127. commands.push_back(cmd);
  1128. // Build the target for this pass.
  1129. commands.push_back(this->GetRecursiveMakeCall(pass));
  1130. // Change back to the starting directory. Any trailing slash must
  1131. // be removed to avoid problems with Borland Make.
  1132. std::string destFull = m_Makefile->GetStartOutputDirectory();
  1133. destFull += "/";
  1134. destFull += subdir;
  1135. std::string back =
  1136. cmSystemTools::RelativePath(destFull.c_str(),
  1137. m_Makefile->GetStartOutputDirectory());
  1138. if(back.size() && back[back.size()-1] == '/')
  1139. {
  1140. back = back.substr(0, back.size()-1);
  1141. }
  1142. cmd = "cd ";
  1143. cmd += this->ConvertToOutputForExisting(back.c_str());
  1144. commands.push_back(cmd);
  1145. }
  1146. else
  1147. {
  1148. // On UNIX we must construct a single shell command to change
  1149. // directory and build because make resets the directory between
  1150. // each command.
  1151. std::string cmd = "cd ";
  1152. cmd += this->ConvertToOutputForExisting(subdir);
  1153. // Build the target for this pass.
  1154. cmd += " && ";
  1155. cmd += this->GetRecursiveMakeCall(pass);
  1156. // Add the command as a single line.
  1157. commands.push_back(cmd);
  1158. }
  1159. // Depend on the last directory written out to enforce ordering.
  1160. if(last.size() > 0)
  1161. {
  1162. depends.push_back(last);
  1163. }
  1164. // Write the rule.
  1165. this->WriteMakeRule(makefileStream, 0, 0, tgt.c_str(), depends, commands);
  1166. // This rule is now the last one written.
  1167. last = tgt;
  1168. }
  1169. //----------------------------------------------------------------------------
  1170. void
  1171. cmLocalUnixMakefileGenerator2
  1172. ::WriteSubdirDriverRule(std::ostream& makefileStream, const char* pass,
  1173. const char* order, const std::string& last)
  1174. {
  1175. // This rule corresponds to a particular pass (all, clean, etc). It
  1176. // dispatches the build into subdirectories in pre- or post-order.
  1177. std::vector<std::string> depends;
  1178. std::vector<std::string> commands;
  1179. if(last.size())
  1180. {
  1181. // Use the dependency to drive subdirectory processing.
  1182. depends.push_back(last);
  1183. }
  1184. else
  1185. {
  1186. // There are no subdirectories. Use the empty commands to avoid
  1187. // make errors on some platforms.
  1188. commands = m_EmptyCommands;
  1189. }
  1190. // Build comment to describe purpose.
  1191. std::string comment = "Driver target for ";
  1192. comment += order;
  1193. comment += "-order subdirectories during ";
  1194. comment += pass;
  1195. comment += ".";
  1196. // Build the make target name.
  1197. std::string tgt = pass;
  1198. tgt += ".";
  1199. tgt += order;
  1200. tgt += "-order";
  1201. // Write the rule.
  1202. this->WriteMakeRule(makefileStream, comment.c_str(), 0,
  1203. tgt.c_str(), depends, commands);
  1204. }
  1205. //----------------------------------------------------------------------------
  1206. void
  1207. cmLocalUnixMakefileGenerator2
  1208. ::WriteConvenienceRules(std::ostream& ruleFileStream, const cmTarget& target,
  1209. const char* targetFullPath)
  1210. {
  1211. // Add a rule to build the target by name.
  1212. std::string localName = this->GetFullTargetName(target.GetName(), target);
  1213. localName = this->ConvertToRelativeOutputPath(localName.c_str());
  1214. this->WriteConvenienceRule(ruleFileStream, targetFullPath,
  1215. localName.c_str());
  1216. // Add a target with the canonical name (no prefix, suffix or path).
  1217. if(localName != target.GetName())
  1218. {
  1219. this->WriteConvenienceRule(ruleFileStream, targetFullPath,
  1220. target.GetName());
  1221. }
  1222. }
  1223. //----------------------------------------------------------------------------
  1224. void
  1225. cmLocalUnixMakefileGenerator2
  1226. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  1227. const char* realTarget,
  1228. const char* helpTarget)
  1229. {
  1230. // A rule is only needed if the names are different.
  1231. if(strcmp(realTarget, helpTarget) != 0)
  1232. {
  1233. // The helper target depends on the real target.
  1234. std::vector<std::string> depends;
  1235. depends.push_back(realTarget);
  1236. // There are no commands.
  1237. std::vector<std::string> no_commands;
  1238. // Write the rule.
  1239. this->WriteMakeRule(ruleFileStream, "Convenience name for target.", 0,
  1240. helpTarget, depends, no_commands);
  1241. }
  1242. }
  1243. //----------------------------------------------------------------------------
  1244. void
  1245. cmLocalUnixMakefileGenerator2
  1246. ::WriteRuleFileIncludes(std::ostream& makefileStream)
  1247. {
  1248. // Make sure we have some rules to include.
  1249. if(m_IncludeRuleFiles.empty())
  1250. {
  1251. return;
  1252. }
  1253. // Write section header.
  1254. this->WriteDivider(makefileStream);
  1255. makefileStream
  1256. << "# Include rule files for this directory.\n"
  1257. << "\n";
  1258. // Write the include rules.
  1259. for(std::vector<std::string>::const_iterator i = m_IncludeRuleFiles.begin();
  1260. i != m_IncludeRuleFiles.end(); ++i)
  1261. {
  1262. makefileStream
  1263. << m_IncludeDirective << " "
  1264. << this->ConvertToOutputForExisting(i->c_str()).c_str()
  1265. << "\n";
  1266. }
  1267. makefileStream << "\n";
  1268. }
  1269. //----------------------------------------------------------------------------
  1270. void
  1271. cmLocalUnixMakefileGenerator2
  1272. ::WriteExecutableRule(std::ostream& ruleFileStream,
  1273. const char* ruleFileName,
  1274. const cmTarget& target,
  1275. std::vector<std::string>& objects)
  1276. {
  1277. std::vector<std::string> commands;
  1278. // Build list of dependencies.
  1279. std::vector<std::string> depends;
  1280. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1281. obj != objects.end(); ++obj)
  1282. {
  1283. depends.push_back(*obj);
  1284. }
  1285. // Add dependencies on libraries that will be linked.
  1286. this->AppendLibDepends(target, depends);
  1287. // Add a dependency on the rule file itself.
  1288. depends.push_back(ruleFileName);
  1289. // Construct the full path to the executable that will be generated.
  1290. // TODO: Try to convert this to use a relative path.
  1291. std::string targetFullPath = m_ExecutableOutputPath;
  1292. if(targetFullPath.length() == 0)
  1293. {
  1294. targetFullPath = m_Makefile->GetStartOutputDirectory();
  1295. targetFullPath += "/";
  1296. }
  1297. #ifdef __APPLE__
  1298. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1299. {
  1300. // Make bundle directories
  1301. targetFullPath += target.GetName();
  1302. targetFullPath += ".app/Contents/MacOS/";
  1303. }
  1304. #endif
  1305. targetFullPath += target.GetName();
  1306. targetFullPath += cmSystemTools::GetExecutableExtension();
  1307. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  1308. // Get the language to use for linking this executable.
  1309. const char* linkLanguage =
  1310. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1311. // Build a list of compiler flags and linker flags.
  1312. std::string flags;
  1313. std::string linkFlags;
  1314. // Add flags to create an executable.
  1315. this->AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS");
  1316. if(target.GetPropertyAsBool("WIN32_EXECUTABLE"))
  1317. {
  1318. this->AppendFlags(linkFlags,
  1319. m_Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  1320. }
  1321. else
  1322. {
  1323. this->AppendFlags(linkFlags,
  1324. m_Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  1325. }
  1326. // Add language-specific flags.
  1327. this->AddLanguageFlags(flags, linkLanguage);
  1328. // Add flags to deal with shared libraries. Any library being
  1329. // linked in might be shared, so always use shared flags for an
  1330. // executable.
  1331. this->AddSharedFlags(flags, linkLanguage, true);
  1332. // Add target-specific linker flags.
  1333. this->AppendFlags(linkFlags, target.GetProperty("LINK_FLAGS"));
  1334. // TODO: Pre-build and pre-link rules.
  1335. // Construct the main link rule.
  1336. std::string linkRuleVar = "CMAKE_";
  1337. linkRuleVar += linkLanguage;
  1338. linkRuleVar += "_LINK_EXECUTABLE";
  1339. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  1340. cmSystemTools::ExpandListArgument(linkRule, commands);
  1341. // TODO: Post-build rules.
  1342. // Collect up flags to link in needed libraries.
  1343. cmOStringStream linklibs;
  1344. this->OutputLinkLibraries(linklibs, 0, target);
  1345. // Construct object file lists that may be needed to expand the
  1346. // rule.
  1347. this->WriteObjectsVariable(ruleFileStream, target, objects);
  1348. std::string objs = "$(";
  1349. objs += target.GetName();
  1350. objs += "_OBJECTS)";
  1351. std::string objsQuoted = "$(";
  1352. objsQuoted += target.GetName();
  1353. objsQuoted += "_OBJECTS_QUOTED)";
  1354. // Expand placeholders in the commands.
  1355. for(std::vector<std::string>::iterator i = commands.begin();
  1356. i != commands.end(); ++i)
  1357. {
  1358. this->ExpandRuleVariables(*i,
  1359. linkLanguage,
  1360. objs.c_str(),
  1361. targetFullPath.c_str(),
  1362. linklibs.str().c_str(),
  1363. 0,
  1364. 0,
  1365. flags.c_str(),
  1366. 0,
  1367. 0,
  1368. 0,
  1369. linkFlags.c_str());
  1370. }
  1371. // Write the build rule.
  1372. std::string buildEcho = "Linking ";
  1373. buildEcho += linkLanguage;
  1374. buildEcho += " executable ";
  1375. buildEcho += this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  1376. buildEcho += "...";
  1377. this->WriteMakeRule(ruleFileStream, 0, buildEcho.c_str(),
  1378. targetFullPath.c_str(), depends, commands);
  1379. // Write convenience targets.
  1380. this->WriteConvenienceRules(ruleFileStream, target, targetFullPath.c_str());
  1381. // Write clean target. TODO: Unify with all target and object file
  1382. // cleaning rules.
  1383. std::string remove = "$(CMAKE_COMMAND) -E remove -f ";
  1384. remove += this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  1385. remove += " ";
  1386. remove += objs;
  1387. std::string cleanTarget = target.GetName();
  1388. cleanTarget += ".clean";
  1389. commands.clear();
  1390. depends.clear();
  1391. commands.push_back(remove);
  1392. this->WriteMakeRule(ruleFileStream, 0, 0, cleanTarget.c_str(),
  1393. depends, commands);
  1394. }
  1395. //----------------------------------------------------------------------------
  1396. void
  1397. cmLocalUnixMakefileGenerator2
  1398. ::WriteStaticLibraryRule(std::ostream& ruleFileStream,
  1399. const char* ruleFileName,
  1400. const cmTarget& target,
  1401. std::vector<std::string>& objects)
  1402. {
  1403. const char* linkLanguage =
  1404. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1405. std::string linkRuleVar = "CMAKE_";
  1406. linkRuleVar += linkLanguage;
  1407. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  1408. std::string extraFlags;
  1409. this->AppendFlags(extraFlags, target.GetProperty("STATIC_LIBRARY_FLAGS"));
  1410. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  1411. linkRuleVar.c_str(), extraFlags.c_str());
  1412. }
  1413. //----------------------------------------------------------------------------
  1414. void
  1415. cmLocalUnixMakefileGenerator2
  1416. ::WriteSharedLibraryRule(std::ostream& ruleFileStream,
  1417. const char* ruleFileName,
  1418. const cmTarget& target,
  1419. std::vector<std::string>& objects)
  1420. {
  1421. const char* linkLanguage =
  1422. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1423. std::string linkRuleVar = "CMAKE_";
  1424. linkRuleVar += linkLanguage;
  1425. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  1426. std::string extraFlags;
  1427. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1428. this->AddConfigVariableFlags(extraFlags, "CMAKE_SHARED_LINKER_FLAGS");
  1429. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1430. {
  1431. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1432. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1433. i != sources.end(); ++i)
  1434. {
  1435. if((*i)->GetSourceExtension() == "def")
  1436. {
  1437. extraFlags += " ";
  1438. extraFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1439. extraFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  1440. }
  1441. }
  1442. }
  1443. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  1444. linkRuleVar.c_str(), extraFlags.c_str());
  1445. }
  1446. //----------------------------------------------------------------------------
  1447. void
  1448. cmLocalUnixMakefileGenerator2
  1449. ::WriteModuleLibraryRule(std::ostream& ruleFileStream,
  1450. const char* ruleFileName,
  1451. const cmTarget& target,
  1452. std::vector<std::string>& objects)
  1453. {
  1454. const char* linkLanguage =
  1455. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1456. std::string linkRuleVar = "CMAKE_";
  1457. linkRuleVar += linkLanguage;
  1458. linkRuleVar += "_CREATE_SHARED_MODULE";
  1459. std::string extraFlags;
  1460. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1461. this->AddConfigVariableFlags(extraFlags, "CMAKE_MODULE_LINKER_FLAGS");
  1462. // TODO: .def files should be supported here also.
  1463. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  1464. linkRuleVar.c_str(), extraFlags.c_str());
  1465. }
  1466. //----------------------------------------------------------------------------
  1467. void
  1468. cmLocalUnixMakefileGenerator2
  1469. ::WriteLibraryRule(std::ostream& ruleFileStream,
  1470. const char* ruleFileName,
  1471. const cmTarget& target,
  1472. std::vector<std::string>& objects,
  1473. const char* linkRuleVar,
  1474. const char* extraFlags)
  1475. {
  1476. // TODO: Merge the methods that call this method to avoid
  1477. // code duplication.
  1478. std::vector<std::string> commands;
  1479. // Build list of dependencies.
  1480. std::vector<std::string> depends;
  1481. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1482. obj != objects.end(); ++obj)
  1483. {
  1484. depends.push_back(*obj);
  1485. }
  1486. // Add dependencies on libraries that will be linked.
  1487. this->AppendLibDepends(target, depends);
  1488. // Add a dependency on the rule file itself.
  1489. depends.push_back(ruleFileName);
  1490. const char* linkLanguage =
  1491. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1492. std::string linkFlags;
  1493. this->AppendFlags(linkFlags, extraFlags);
  1494. std::string targetName;
  1495. std::string targetNameSO;
  1496. std::string targetNameReal;
  1497. std::string targetNameBase;
  1498. this->GetLibraryNames(target.GetName(), target,
  1499. targetName, targetNameSO,
  1500. targetNameReal, targetNameBase);
  1501. std::string outpath;
  1502. std::string outdir;
  1503. if(m_UseRelativePaths)
  1504. {
  1505. outdir = this->ConvertToRelativeOutputPath(m_LibraryOutputPath.c_str());
  1506. }
  1507. else
  1508. {
  1509. outdir = m_LibraryOutputPath;
  1510. }
  1511. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  1512. {
  1513. outpath = "\"`cd ";
  1514. }
  1515. outpath += outdir;
  1516. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  1517. {
  1518. outpath += ";pwd`\"/";
  1519. }
  1520. if(outdir.size() == 0 && m_UseRelativePaths && !m_WindowsShell)
  1521. {
  1522. outpath = "\"`pwd`\"/";
  1523. }
  1524. // The full path versions of the names.
  1525. std::string targetFullPath = outpath + targetName;
  1526. std::string targetFullPathSO = outpath + targetNameSO;
  1527. std::string targetFullPathReal = outpath + targetNameReal;
  1528. std::string targetFullPathBase = outpath + targetNameBase;
  1529. // If not using relative paths then the output path needs to be
  1530. // converted here
  1531. if(!m_UseRelativePaths)
  1532. {
  1533. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  1534. targetFullPathSO = this->ConvertToRelativeOutputPath(targetFullPathSO.c_str());
  1535. targetFullPathReal = this->ConvertToRelativeOutputPath(targetFullPathReal.c_str());
  1536. targetFullPathBase = this->ConvertToRelativeOutputPath(targetFullPathBase.c_str());
  1537. }
  1538. // Add a command to remove any existing files for this library.
  1539. std::string remove = "$(CMAKE_COMMAND) -E remove -f ";
  1540. remove += targetFullPathReal;
  1541. if(targetFullPathSO != targetFullPathReal)
  1542. {
  1543. remove += " ";
  1544. remove += targetFullPathSO;
  1545. }
  1546. if(targetFullPath != targetFullPathSO &&
  1547. targetFullPath != targetFullPathReal)
  1548. {
  1549. remove += " ";
  1550. remove += targetFullPath;
  1551. }
  1552. commands.push_back(remove);
  1553. // TODO: Pre-build and pre-link rules.
  1554. // Construct the main link rule.
  1555. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar);
  1556. cmSystemTools::ExpandListArgument(linkRule, commands);
  1557. // Add a rule to create necessary symlinks for the library.
  1558. if(targetFullPath != targetFullPathReal)
  1559. {
  1560. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  1561. symlink += targetFullPathReal;
  1562. symlink += " ";
  1563. symlink += targetFullPathSO;
  1564. symlink += " ";
  1565. symlink += targetFullPath;
  1566. commands.push_back(symlink);
  1567. }
  1568. // TODO: Post-build rules.
  1569. // Collect up flags to link in needed libraries.
  1570. cmOStringStream linklibs;
  1571. this->OutputLinkLibraries(linklibs, target.GetName(), target);
  1572. // Construct object file lists that may be needed to expand the
  1573. // rule.
  1574. this->WriteObjectsVariable(ruleFileStream, target, objects);
  1575. std::string objs = "$(";
  1576. objs += target.GetName();
  1577. objs += "_OBJECTS)";
  1578. std::string objsQuoted = "$(";
  1579. objsQuoted += target.GetName();
  1580. objsQuoted += "_OBJECTS_QUOTED)";
  1581. // Expand placeholders in the commands.
  1582. for(std::vector<std::string>::iterator i = commands.begin();
  1583. i != commands.end(); ++i)
  1584. {
  1585. this->ExpandRuleVariables(*i,
  1586. linkLanguage,
  1587. objs.c_str(),
  1588. targetFullPathReal.c_str(),
  1589. linklibs.str().c_str(),
  1590. 0, 0, 0, objsQuoted.c_str(),
  1591. targetFullPathBase.c_str(),
  1592. targetNameSO.c_str(),
  1593. linkFlags.c_str());
  1594. }
  1595. // Write the build rule.
  1596. std::string buildEcho = "Linking ";
  1597. buildEcho += linkLanguage;
  1598. switch(target.GetType())
  1599. {
  1600. case cmTarget::STATIC_LIBRARY:
  1601. buildEcho += " static library "; break;
  1602. case cmTarget::SHARED_LIBRARY:
  1603. buildEcho += " shared library "; break;
  1604. case cmTarget::MODULE_LIBRARY:
  1605. buildEcho += " shared module "; break;
  1606. default:
  1607. buildEcho += " library "; break;
  1608. }
  1609. buildEcho += this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  1610. buildEcho += "...";
  1611. this->WriteMakeRule(ruleFileStream, 0, buildEcho.c_str(),
  1612. targetFullPath.c_str(), depends, commands);
  1613. // Write convenience targets.
  1614. this->WriteConvenienceRules(ruleFileStream, target, targetFullPath.c_str());
  1615. // Write clean target. TODO: Unify with all target and object file
  1616. // cleaning rules.
  1617. std::string cleanTarget = target.GetName();
  1618. cleanTarget += ".clean";
  1619. commands.clear();
  1620. depends.clear();
  1621. remove += " ";
  1622. remove += objs;
  1623. commands.push_back(remove);
  1624. this->WriteMakeRule(ruleFileStream, 0, 0, cleanTarget.c_str(),
  1625. depends, commands);
  1626. }
  1627. //----------------------------------------------------------------------------
  1628. void
  1629. cmLocalUnixMakefileGenerator2
  1630. ::WriteObjectsVariable(std::ostream& ruleFileStream,
  1631. const cmTarget& target,
  1632. std::vector<std::string>& objects)
  1633. {
  1634. // Write a make variable assignment that lists all objects for the
  1635. // target.
  1636. ruleFileStream
  1637. << "# Object files for target " << target.GetName() << "\n"
  1638. << target.GetName() << "_OBJECTS =";
  1639. for(std::vector<std::string>::iterator i = objects.begin();
  1640. i != objects.end(); ++i)
  1641. {
  1642. ruleFileStream
  1643. << " \\\n"
  1644. << this->ConvertToRelativeOutputPath(i->c_str());
  1645. }
  1646. ruleFileStream
  1647. << "\n"
  1648. << "\n";
  1649. ruleFileStream
  1650. << "# Object files for target " << target.GetName() << "\n"
  1651. << target.GetName() << "_OBJECTS_QUOTED =";
  1652. for(std::vector<std::string>::iterator i = objects.begin();
  1653. i != objects.end(); ++i)
  1654. {
  1655. ruleFileStream
  1656. << " \\\n"
  1657. << "\"" << this->ConvertToRelativeOutputPath(i->c_str()) << "\"";
  1658. }
  1659. ruleFileStream
  1660. << "\n"
  1661. << "\n";
  1662. }
  1663. //----------------------------------------------------------------------------
  1664. std::string
  1665. cmLocalUnixMakefileGenerator2
  1666. ::GetTargetDirectory(const cmTarget& target)
  1667. {
  1668. std::string dir = target.GetName();
  1669. dir += ".dir";
  1670. return dir;
  1671. }
  1672. //----------------------------------------------------------------------------
  1673. std::string
  1674. cmLocalUnixMakefileGenerator2
  1675. ::GetSubdirTargetName(const char* pass, const char* subdir)
  1676. {
  1677. // Convert the subdirectory name to a valid make target name.
  1678. std::string s = pass;
  1679. s += "_";
  1680. s += subdir;
  1681. // Replace "../" with 3 underscores. This allows one .. at the beginning.
  1682. size_t pos = s.find("../");
  1683. if(pos != std::string::npos)
  1684. {
  1685. s.replace(pos, 3, "___");
  1686. }
  1687. // Replace "/" directory separators with a single underscore.
  1688. while((pos = s.find('/')) != std::string::npos)
  1689. {
  1690. s.replace(pos, 1, "_");
  1691. }
  1692. return s;
  1693. }
  1694. //----------------------------------------------------------------------------
  1695. std::string
  1696. cmLocalUnixMakefileGenerator2
  1697. ::GetObjectFileName(const cmTarget& target,
  1698. const cmSourceFile& source)
  1699. {
  1700. // If the full path to the source file includes this directory,
  1701. // we want to use the relative path for the filename of the
  1702. // object file. Otherwise, we will use just the filename
  1703. // portion.
  1704. std::string objectName;
  1705. if((cmSystemTools::GetFilenamePath(
  1706. source.GetFullPath()).find(
  1707. m_Makefile->GetCurrentDirectory()) == 0)
  1708. || (cmSystemTools::GetFilenamePath(
  1709. source.GetFullPath()).find(
  1710. m_Makefile->GetStartOutputDirectory()) == 0))
  1711. {
  1712. objectName = source.GetSourceName();
  1713. }
  1714. else
  1715. {
  1716. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  1717. }
  1718. // Append the object file extension.
  1719. objectName +=
  1720. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1721. source.GetSourceExtension().c_str());
  1722. // Convert to a safe name.
  1723. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  1724. // Prepend the target directory.
  1725. std::string obj = this->GetTargetDirectory(target);
  1726. obj += "/";
  1727. obj += objectName;
  1728. return obj;
  1729. }
  1730. //----------------------------------------------------------------------------
  1731. std::string
  1732. cmLocalUnixMakefileGenerator2
  1733. ::GetCustomBaseName(const cmCustomCommand& cc)
  1734. {
  1735. // If the full path to the output file includes this build
  1736. // directory, we want to use the relative path for the filename of
  1737. // the custom file. Otherwise, we will use just the filename
  1738. // portion.
  1739. std::string customName;
  1740. if(cmSystemTools::FileIsFullPath(cc.GetOutput().c_str()) &&
  1741. (cc.GetOutput().find(m_Makefile->GetStartOutputDirectory()) == 0))
  1742. {
  1743. customName =
  1744. cmSystemTools::RelativePath(m_Makefile->GetStartOutputDirectory(),
  1745. cc.GetOutput().c_str());
  1746. }
  1747. else
  1748. {
  1749. customName = cmSystemTools::GetFilenameName(cc.GetOutput().c_str());
  1750. }
  1751. return customName;
  1752. }
  1753. //----------------------------------------------------------------------------
  1754. const char*
  1755. cmLocalUnixMakefileGenerator2
  1756. ::GetSourceFileLanguage(const cmSourceFile& source)
  1757. {
  1758. // Identify the language of the source file.
  1759. return (m_GlobalGenerator
  1760. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  1761. }
  1762. //----------------------------------------------------------------------------
  1763. std::string
  1764. cmLocalUnixMakefileGenerator2
  1765. ::ConvertToFullPath(const std::string& localPath)
  1766. {
  1767. std::string dir = m_Makefile->GetStartOutputDirectory();
  1768. dir += "/";
  1769. dir += localPath;
  1770. return dir;
  1771. }
  1772. //----------------------------------------------------------------------------
  1773. void cmLocalUnixMakefileGenerator2::AddLanguageFlags(std::string& flags,
  1774. const char* lang)
  1775. {
  1776. // Add language-specific flags.
  1777. std::string flagsVar = "CMAKE_";
  1778. flagsVar += lang;
  1779. flagsVar += "_FLAGS";
  1780. this->AddConfigVariableFlags(flags, flagsVar.c_str());
  1781. }
  1782. //----------------------------------------------------------------------------
  1783. void cmLocalUnixMakefileGenerator2::AddSharedFlags(std::string& flags,
  1784. const char* lang,
  1785. bool shared)
  1786. {
  1787. std::string flagsVar;
  1788. // Add flags for dealing with shared libraries for this language.
  1789. if(shared)
  1790. {
  1791. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1792. flagsVar += lang;
  1793. flagsVar += "_FLAGS";
  1794. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1795. }
  1796. // Add flags specific to shared builds.
  1797. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1798. {
  1799. flagsVar = "CMAKE_SHARED_BUILD_";
  1800. flagsVar += lang;
  1801. flagsVar += "_FLAGS";
  1802. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1803. }
  1804. }
  1805. //----------------------------------------------------------------------------
  1806. void cmLocalUnixMakefileGenerator2::AddConfigVariableFlags(std::string& flags,
  1807. const char* var)
  1808. {
  1809. // Add the flags from the variable itself.
  1810. std::string flagsVar = var;
  1811. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1812. // Add the flags from the build-type specific variable.
  1813. const char* buildType = m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1814. if(buildType && *buildType)
  1815. {
  1816. flagsVar += "_";
  1817. flagsVar += cmSystemTools::UpperCase(buildType);
  1818. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1819. }
  1820. }
  1821. //----------------------------------------------------------------------------
  1822. void cmLocalUnixMakefileGenerator2::AppendFlags(std::string& flags,
  1823. const char* newFlags)
  1824. {
  1825. if(newFlags && *newFlags)
  1826. {
  1827. if(flags.size())
  1828. {
  1829. flags += " ";
  1830. }
  1831. flags += newFlags;
  1832. }
  1833. }
  1834. //----------------------------------------------------------------------------
  1835. void
  1836. cmLocalUnixMakefileGenerator2
  1837. ::AppendLibDepends(const cmTarget& target,
  1838. std::vector<std::string>& depends)
  1839. {
  1840. // Do not bother with dependencies for static libraries.
  1841. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  1842. {
  1843. return;
  1844. }
  1845. // Keep track of dependencies already listed.
  1846. std::set<cmStdString> emitted;
  1847. // A target should not depend on itself.
  1848. emitted.insert(target.GetName());
  1849. // Loop over all dependencies.
  1850. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  1851. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1852. lib != tlibs.end(); ++lib)
  1853. {
  1854. // Don't emit the same library twice for this target.
  1855. if(emitted.insert(lib->first).second)
  1856. {
  1857. // Add this dependency.
  1858. this->AppendLibDepend(depends, lib->first.c_str());
  1859. }
  1860. }
  1861. }
  1862. //----------------------------------------------------------------------------
  1863. void
  1864. cmLocalUnixMakefileGenerator2
  1865. ::AppendLibDepend(std::vector<std::string>& depends, const char* name)
  1866. {
  1867. // There are a few cases for the name of the target:
  1868. // - CMake target in this directory: depend on it.
  1869. // - CMake target in another directory: depend and add jump-and-build.
  1870. // - Full path to an outside file: depend on it.
  1871. // - Other format (like -lm): do nothing.
  1872. // If it is a CMake target there will be a definition for it.
  1873. std::string dirVar = name;
  1874. dirVar += "_CMAKE_PATH";
  1875. const char* dir = m_Makefile->GetDefinition(dirVar.c_str());
  1876. if(dir && *dir)
  1877. {
  1878. // This is a CMake target somewhere in this project.
  1879. bool jumpAndBuild = false;
  1880. // Get the path to the library.
  1881. std::string libPath;
  1882. if(this->SamePath(m_Makefile->GetStartOutputDirectory(), dir))
  1883. {
  1884. // The target is in the current directory so this makefile will
  1885. // know about it already.
  1886. libPath = m_LibraryOutputPath;
  1887. }
  1888. else
  1889. {
  1890. // The target is in another directory. Get the path to it.
  1891. if(m_LibraryOutputPath.size())
  1892. {
  1893. libPath = m_LibraryOutputPath;
  1894. }
  1895. else
  1896. {
  1897. libPath = dir;
  1898. libPath += "/";
  1899. }
  1900. // We need to add a jump-and-build rule for this library.
  1901. jumpAndBuild = true;
  1902. }
  1903. // Add the name of the library's file. This depends on the type
  1904. // of the library.
  1905. std::string typeVar = name;
  1906. typeVar += "_LIBRARY_TYPE";
  1907. std::string libType = m_Makefile->GetSafeDefinition(typeVar.c_str());
  1908. std::string prefix;
  1909. std::string suffix;
  1910. if(libType == "SHARED")
  1911. {
  1912. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1913. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1914. }
  1915. else if(libType == "MODULE")
  1916. {
  1917. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1918. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1919. }
  1920. else if(libType == "STATIC")
  1921. {
  1922. prefix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1923. suffix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1924. }
  1925. libPath += prefix;
  1926. libPath += name;
  1927. libPath += suffix;
  1928. if(jumpAndBuild)
  1929. {
  1930. // We need to add a jump-and-build rule for this library.
  1931. cmLocalUnixMakefileGenerator2::RemoteTarget rt;
  1932. rt.m_BuildDirectory = dir;
  1933. rt.m_FilePath =libPath;
  1934. m_JumpAndBuild[name] = rt;
  1935. }
  1936. // Add a dependency on the library.
  1937. depends.push_back(this->ConvertToRelativeOutputPath(libPath.c_str()));
  1938. }
  1939. else
  1940. {
  1941. // This is not a CMake target. If it exists and is a full path we
  1942. // can depend on it.
  1943. if(cmSystemTools::FileExists(name) && cmSystemTools::FileIsFullPath(name))
  1944. {
  1945. depends.push_back(this->ConvertToRelativeOutputPath(name));
  1946. }
  1947. }
  1948. }
  1949. //----------------------------------------------------------------------------
  1950. std::string
  1951. cmLocalUnixMakefileGenerator2
  1952. ::GetRecursiveMakeCall(const char* tgt)
  1953. {
  1954. // Call make on the given file.
  1955. std::string cmd;
  1956. cmd += "$(MAKE) -f " CMLUMG_MAKEFILE_NAME " ";
  1957. // Pass down verbosity level.
  1958. if(m_MakeSilentFlag.size())
  1959. {
  1960. cmd += m_MakeSilentFlag;
  1961. cmd += " ";
  1962. }
  1963. // Most unix makes will pass the command line flags to make down to
  1964. // sub-invoked makes via an environment variable. However, some
  1965. // makes do not support that, so you have to pass the flags
  1966. // explicitly.
  1967. if(m_PassMakeflags)
  1968. {
  1969. cmd += "-$(MAKEFLAGS) ";
  1970. }
  1971. // Add the target.
  1972. cmd += tgt;
  1973. return cmd;
  1974. }
  1975. //----------------------------------------------------------------------------
  1976. void
  1977. cmLocalUnixMakefileGenerator2
  1978. ::WriteJumpAndBuildRules(std::ostream& makefileStream)
  1979. {
  1980. // Write the header for this section.
  1981. if(!m_JumpAndBuild.empty())
  1982. {
  1983. this->WriteDivider(makefileStream);
  1984. makefileStream
  1985. << "# Targets to make sure needed libraries exist.\n"
  1986. << "# These will jump to other directories to build targets.\n"
  1987. << "\n";
  1988. }
  1989. std::vector<std::string> depends;
  1990. std::vector<std::string> commands;
  1991. for(std::map<cmStdString, RemoteTarget>::iterator
  1992. jump = m_JumpAndBuild.begin(); jump != m_JumpAndBuild.end(); ++jump)
  1993. {
  1994. const cmLocalUnixMakefileGenerator2::RemoteTarget& rt = jump->second;
  1995. const char* destination = rt.m_BuildDirectory.c_str();
  1996. // Construct the dependency and build target names.
  1997. std::string dep = jump->first;
  1998. dep += ".dir/";
  1999. dep += jump->first;
  2000. dep += ".depends";
  2001. dep = this->ConvertToRelativeOutputPath(dep.c_str());
  2002. std::string tgt = jump->first;
  2003. tgt = this->ConvertToRelativeOutputPath(tgt.c_str());
  2004. // Build the jump-and-build command list.
  2005. commands.clear();
  2006. if(m_WindowsShell)
  2007. {
  2008. // On Windows we must perform each step separately and then jump
  2009. // back because the shell keeps the working directory between
  2010. // commands.
  2011. std::string cmd = "cd ";
  2012. cmd += this->ConvertToOutputForExisting(destination);
  2013. commands.push_back(cmd);
  2014. // Check the build system in destination directory.
  2015. commands.push_back(this->GetRecursiveMakeCall("cmake_check_build_system"));
  2016. // Build the targets's dependencies.
  2017. commands.push_back(this->GetRecursiveMakeCall(dep.c_str()));
  2018. // Build the target.
  2019. commands.push_back(this->GetRecursiveMakeCall(tgt.c_str()));
  2020. // Jump back to the starting directory.
  2021. cmd = "cd ";
  2022. cmd += this->ConvertToOutputForExisting(m_Makefile->GetStartOutputDirectory());
  2023. commands.push_back(cmd);
  2024. }
  2025. else
  2026. {
  2027. // On UNIX we must construct a single shell command to jump and
  2028. // build because make resets the directory between each command.
  2029. std::string cmd = "cd ";
  2030. cmd += this->ConvertToOutputForExisting(destination);
  2031. // Check the build system in destination directory.
  2032. cmd += " && ";
  2033. cmd += this->GetRecursiveMakeCall("cmake_check_build_system");
  2034. // Build the targets's dependencies.
  2035. cmd += " && ";
  2036. cmd += this->GetRecursiveMakeCall(dep.c_str());
  2037. // Build the target.
  2038. cmd += " && ";
  2039. cmd += this->GetRecursiveMakeCall(tgt.c_str());
  2040. // Add the command as a single line.
  2041. commands.push_back(cmd);
  2042. }
  2043. // Write the rule.
  2044. std::string jumpPreEcho = "Jumping to ";
  2045. jumpPreEcho += rt.m_BuildDirectory.c_str();
  2046. jumpPreEcho += " to build ";
  2047. jumpPreEcho += jump->first;
  2048. jumpPreEcho += "...";
  2049. std::string jumpPostEcho = "Returning to ";
  2050. jumpPostEcho += m_Makefile->GetStartOutputDirectory();
  2051. jumpPostEcho += "...";
  2052. this->WriteMakeRule(makefileStream, 0, jumpPreEcho.c_str(),
  2053. rt.m_FilePath.c_str(), depends, commands,
  2054. jumpPostEcho.c_str());
  2055. }
  2056. }
  2057. //----------------------------------------------------------------------------
  2058. bool
  2059. cmLocalUnixMakefileGenerator2
  2060. ::ScanDependencies(std::vector<std::string> const& args)
  2061. {
  2062. // Format of arguments is:
  2063. // $(CMAKE_COMMAND), cmake_depends, <lang>, <obj>, <src>, [include-flags]
  2064. // The caller has ensured that all required arguments exist.
  2065. // The file to which to write dependencies.
  2066. const char* objFile = args[3].c_str();
  2067. // The source file at which to start the scan.
  2068. const char* srcFile = args[4].c_str();
  2069. // Convert the include flags to full paths.
  2070. std::vector<std::string> includes;
  2071. for(unsigned int i=5; i < args.size(); ++i)
  2072. {
  2073. if(args[i].substr(0, 2) == "-I")
  2074. {
  2075. // Get the include path without the -I flag.
  2076. std::string inc = args[i].substr(2);
  2077. includes.push_back(cmSystemTools::CollapseFullPath(inc.c_str()));
  2078. }
  2079. }
  2080. // Dispatch the scan for each language.
  2081. std::string const& lang = args[2];
  2082. if(lang == "C" || lang == "CXX" || lang == "RC")
  2083. {
  2084. // TODO: Handle RC (resource files) dependencies correctly.
  2085. return cmLocalUnixMakefileGenerator2::ScanDependenciesC(objFile, srcFile,
  2086. includes);
  2087. }
  2088. return false;
  2089. }
  2090. //----------------------------------------------------------------------------
  2091. void
  2092. cmLocalUnixMakefileGenerator2ScanDependenciesC(
  2093. std::ifstream& fin,
  2094. std::set<cmStdString>& encountered,
  2095. std::queue<cmStdString>& unscanned)
  2096. {
  2097. // Regular expression to identify C preprocessor include directives.
  2098. cmsys::RegularExpression
  2099. includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  2100. // Read one line at a time.
  2101. std::string line;
  2102. while(cmSystemTools::GetLineFromStream(fin, line))
  2103. {
  2104. // Match include directives. TODO: Support include regex and
  2105. // ignore regex. Possibly also support directory-based inclusion
  2106. // in dependencies.
  2107. if(includeLine.find(line.c_str()))
  2108. {
  2109. // Get the file being included.
  2110. std::string includeFile = includeLine.match(1);
  2111. // Queue the file if it has not yet been encountered.
  2112. if(encountered.find(includeFile) == encountered.end())
  2113. {
  2114. encountered.insert(includeFile);
  2115. unscanned.push(includeFile);
  2116. }
  2117. }
  2118. }
  2119. }
  2120. //----------------------------------------------------------------------------
  2121. bool
  2122. cmLocalUnixMakefileGenerator2
  2123. ::ScanDependenciesC(const char* objFile, const char* srcFile,
  2124. std::vector<std::string> const& includes)
  2125. {
  2126. // Walk the dependency graph starting with the source file.
  2127. std::set<cmStdString> dependencies;
  2128. std::set<cmStdString> encountered;
  2129. std::set<cmStdString> scanned;
  2130. std::queue<cmStdString> unscanned;
  2131. unscanned.push(srcFile);
  2132. encountered.insert(srcFile);
  2133. while(!unscanned.empty())
  2134. {
  2135. // Get the next file to scan.
  2136. std::string fname = unscanned.front();
  2137. unscanned.pop();
  2138. // If not a full path, find the file in the include path.
  2139. std::string fullName;
  2140. if(cmSystemTools::FileIsFullPath(fname.c_str()))
  2141. {
  2142. fullName = fname;
  2143. }
  2144. else
  2145. {
  2146. for(std::vector<std::string>::const_iterator i = includes.begin();
  2147. i != includes.end(); ++i)
  2148. {
  2149. std::string temp = *i;
  2150. temp += "/";
  2151. temp += fname;
  2152. if(cmSystemTools::FileExists(temp.c_str()))
  2153. {
  2154. fullName = temp;
  2155. break;
  2156. }
  2157. }
  2158. }
  2159. // Scan the file if it was found and has not been scanned already.
  2160. if(fullName.size() && (scanned.find(fullName) == scanned.end()))
  2161. {
  2162. // Record scanned files.
  2163. scanned.insert(fullName);
  2164. // Try to scan the file. Just leave it out if we cannot find
  2165. // it.
  2166. std::ifstream fin(fullName.c_str());
  2167. if(fin)
  2168. {
  2169. // Add this file as a dependency.
  2170. dependencies.insert(fullName);
  2171. // Scan this file for new dependencies.
  2172. cmLocalUnixMakefileGenerator2ScanDependenciesC(fin, encountered,
  2173. unscanned);
  2174. }
  2175. }
  2176. }
  2177. // Write the dependencies to the output file.
  2178. std::string depMarkFile = objFile;
  2179. std::string depMakeFile = objFile;
  2180. depMarkFile += ".depends";
  2181. depMakeFile += ".depends.make";
  2182. cmGeneratedFileStream fout(depMakeFile.c_str());
  2183. fout << "# Dependencies for " << objFile << std::endl;
  2184. for(std::set<cmStdString>::iterator i=dependencies.begin();
  2185. i != dependencies.end(); ++i)
  2186. {
  2187. fout << objFile << ": "
  2188. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  2189. << std::endl;
  2190. }
  2191. fout << std::endl;
  2192. fout << "# Dependencies for " << depMarkFile.c_str() << std::endl;
  2193. for(std::set<cmStdString>::iterator i=dependencies.begin();
  2194. i != dependencies.end(); ++i)
  2195. {
  2196. fout << depMarkFile.c_str() << ": "
  2197. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  2198. << std::endl;
  2199. }
  2200. // If we could write the dependencies, touch the corresponding
  2201. // depends file to mark dependencies up to date.
  2202. if(fout)
  2203. {
  2204. std::ofstream fmark(depMarkFile.c_str());
  2205. fmark << "Dependencies updated for " << objFile << "\n";
  2206. }
  2207. return true;
  2208. }
  2209. //----------------------------------------------------------------------------
  2210. void
  2211. cmLocalUnixMakefileGenerator2
  2212. ::CheckDependencies(const char* depCheck)
  2213. {
  2214. // Get the list of files to scan. This is given through the command
  2215. // line hook cmake file.
  2216. std::vector<std::string> files;
  2217. cmSystemTools::ExpandListArgument(depCheck, files);
  2218. // Check each file. The current working directory is already
  2219. // correct.
  2220. for(std::vector<std::string>::iterator f = files.begin();
  2221. f != files.end(); ++f)
  2222. {
  2223. cmLocalUnixMakefileGenerator2::CheckDependencies(".", f->c_str());
  2224. }
  2225. }
  2226. //----------------------------------------------------------------------------
  2227. void
  2228. cmLocalUnixMakefileGenerator2
  2229. ::CheckDependencies(const char* dir, const char* file)
  2230. {
  2231. // Check the dependencies associated with the given file whose path
  2232. // is specified relative to the given directory. If any dependency
  2233. // is missing then dependencies should be regenerated.
  2234. bool regenerate = false;
  2235. // Construct the names of the mark and make files.
  2236. std::string depMarkFileFull = dir;
  2237. depMarkFileFull += "/";
  2238. depMarkFileFull += file;
  2239. depMarkFileFull += ".depends";
  2240. std::string depMakeFileFull = depMarkFileFull;
  2241. depMakeFileFull += ".make";
  2242. // Open the dependency makefile.
  2243. std::ifstream fin(depMakeFileFull.c_str());
  2244. if(fin)
  2245. {
  2246. // Parse dependencies.
  2247. std::string line;
  2248. std::string depender;
  2249. std::string dependee;
  2250. while(cmSystemTools::GetLineFromStream(fin, line))
  2251. {
  2252. // Skip empty lines and comments.
  2253. std::string::size_type pos = line.find_first_not_of(" \t\r\n");
  2254. if(pos == std::string::npos || line[pos] == '#')
  2255. {
  2256. continue;
  2257. }
  2258. // Strip leading whitespace.
  2259. if(pos > 0)
  2260. {
  2261. line = line.substr(pos);
  2262. }
  2263. // Skip lines too short to have a dependency.
  2264. if(line.size() < 3)
  2265. {
  2266. continue;
  2267. }
  2268. // Find the colon on the line. Skip the first two characters to
  2269. // avoid finding the colon in a drive letter on Windows. Ignore
  2270. // the line if a colon cannot be found.
  2271. if((pos = line.find(':', 2)) == std::string::npos)
  2272. {
  2273. continue;
  2274. }
  2275. // Split the line into depender and dependee.
  2276. depender = line.substr(0, pos);
  2277. dependee = line.substr(pos+1);
  2278. // Strip whitespace from the dependee.
  2279. if((pos = dependee.find_first_not_of(" \t\r\n")) != std::string::npos &&
  2280. pos > 0)
  2281. {
  2282. dependee = dependee.substr(pos);
  2283. }
  2284. if((pos = dependee.find_last_not_of(" \t\r\n")) != std::string::npos)
  2285. {
  2286. dependee = dependee.substr(0, pos+1);
  2287. }
  2288. // Convert dependee to a full path.
  2289. if(!cmSystemTools::FileIsFullPath(dependee.c_str()))
  2290. {
  2291. dependee = cmSystemTools::CollapseFullPath(dependee.c_str(), dir);
  2292. }
  2293. // If the dependee does not exist, we need to regenerate
  2294. // dependencies and the depender should be removed.
  2295. if(!cmSystemTools::FileExists(dependee.c_str()))
  2296. {
  2297. // Strip whitespace from the depender.
  2298. if((pos = depender.find_last_not_of(" \t\r\n")) != std::string::npos)
  2299. {
  2300. depender = depender.substr(0, pos+1);
  2301. }
  2302. // Convert depender to a full path.
  2303. if(!cmSystemTools::FileIsFullPath(depender.c_str()))
  2304. {
  2305. depender = cmSystemTools::CollapseFullPath(depender.c_str(), dir);
  2306. }
  2307. // Remove the depender.
  2308. cmSystemTools::RemoveFile(depender.c_str());
  2309. // Mark the need for regeneration.
  2310. regenerate = true;
  2311. }
  2312. }
  2313. }
  2314. else
  2315. {
  2316. // Could not open the dependencies file. It needs to be
  2317. // regenerated.
  2318. regenerate = true;
  2319. }
  2320. // If the dependencies file needs to be regenerated, create an empty
  2321. // one and delete the mark file.
  2322. if(regenerate)
  2323. {
  2324. cmLocalUnixMakefileGenerator2
  2325. ::WriteEmptyDependMakeFile(file, depMarkFileFull.c_str(),
  2326. depMakeFileFull.c_str());
  2327. }
  2328. }
  2329. //----------------------------------------------------------------------------
  2330. void
  2331. cmLocalUnixMakefileGenerator2
  2332. ::WriteEmptyDependMakeFile(const char* file,
  2333. const char* depMarkFileFull,
  2334. const char* depMakeFileFull)
  2335. {
  2336. // Remove the dependency mark file to be sure dependencies will be
  2337. // regenerated.
  2338. cmSystemTools::RemoveFile(depMarkFileFull);
  2339. // Write an empty dependency file.
  2340. cmGeneratedFileStream depFileStream(depMakeFileFull);
  2341. depFileStream
  2342. << "# Empty dependencies file for " << file << ".\n"
  2343. << "# This may be replaced when dependencies are built.\n";
  2344. }