cmLocalUnixMakefileGenerator3.cxx 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073
  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 "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmDepends.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmake.h"
  20. // Include dependency scanners for supported languages. Only the
  21. // C/C++ scanner is needed for bootstrapping CMake.
  22. #include "cmDependsC.h"
  23. #ifdef CMAKE_BUILD_WITH_CMAKE
  24. # include "cmDependsFortran.h"
  25. # include "cmDependsJava.h"
  26. #endif
  27. #include <memory> // auto_ptr
  28. #include <queue>
  29. // TODO: Add "help" target.
  30. // TODO: Identify remaining relative path violations.
  31. // TODO: Need test for separate executable/library output path.
  32. //----------------------------------------------------------------------------
  33. cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3()
  34. {
  35. m_WindowsShell = false;
  36. m_IncludeDirective = "include";
  37. m_MakefileVariableSize = 0;
  38. m_IgnoreLibPrefix = false;
  39. m_PassMakeflags = false;
  40. m_EchoNeedsQuote = true;
  41. }
  42. //----------------------------------------------------------------------------
  43. cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3()
  44. {
  45. }
  46. //----------------------------------------------------------------------------
  47. void cmLocalUnixMakefileGenerator3::Generate()
  48. {
  49. // Setup our configuration variables for this directory.
  50. this->ConfigureOutputPaths();
  51. // Generate the rule files for each target.
  52. const cmTargets& targets = m_Makefile->GetTargets();
  53. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  54. {
  55. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  56. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  57. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  58. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  59. {
  60. this->WriteTargetRuleFiles(t->second);
  61. }
  62. else if(t->second.GetType() == cmTarget::UTILITY)
  63. {
  64. this->WriteUtilityRuleFiles(t->second);
  65. }
  66. }
  67. this->WriteCustomCommands();
  68. // write the local Makefile
  69. this->WriteLocalMakefile();
  70. // Write the cmake file with information for this directory.
  71. this->WriteDirectoryInformationFile();
  72. }
  73. //----------------------------------------------------------------------------
  74. void cmLocalUnixMakefileGenerator3::ConfigureOutputPaths()
  75. {
  76. // Format the library and executable output paths.
  77. if(const char* libOut = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  78. {
  79. m_LibraryOutputPath = libOut;
  80. this->FormatOutputPath(m_LibraryOutputPath, "LIBRARY");
  81. }
  82. if(const char* exeOut = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  83. {
  84. m_ExecutableOutputPath = exeOut;
  85. this->FormatOutputPath(m_ExecutableOutputPath, "EXECUTABLE");
  86. }
  87. }
  88. //----------------------------------------------------------------------------
  89. void cmLocalUnixMakefileGenerator3::FormatOutputPath(std::string& path,
  90. const char* name)
  91. {
  92. if(!path.empty())
  93. {
  94. // Convert the output path to a full path in case it is
  95. // specified as a relative path. Treat a relative path as
  96. // relative to the current output directory for this makefile.
  97. path =
  98. cmSystemTools::CollapseFullPath(path.c_str(),
  99. m_Makefile->GetStartOutputDirectory());
  100. // Add a trailing slash for easy appending later.
  101. if(path.empty() || path[path.size()-1] != '/')
  102. {
  103. path += "/";
  104. }
  105. // Make sure the output path exists on disk.
  106. if(!cmSystemTools::MakeDirectory(path.c_str()))
  107. {
  108. cmSystemTools::Error("Error failed to create ",
  109. name, "_OUTPUT_PATH directory:", path.c_str());
  110. }
  111. // Add this as a link directory automatically.
  112. m_Makefile->AddLinkDirectory(path.c_str());
  113. }
  114. }
  115. void cmLocalUnixMakefileGenerator3::WriteCustomCommands()
  116. {
  117. // Generate the rule files for each custom command.
  118. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  119. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  120. i != sources.end(); ++i)
  121. {
  122. if(const cmCustomCommand* cc = (*i)->GetCustomCommand())
  123. {
  124. this->GenerateCustomRuleFile(*cc);
  125. }
  126. }
  127. // generate the includes
  128. std::string ruleFileName = "CMakeCustomRules.dir/build.make";
  129. // Open the rule file. This should be copy-if-different because the
  130. // rules may depend on this file itself.
  131. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  132. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  133. ruleFileStream.SetCopyIfDifferent(true);
  134. if(!ruleFileStream)
  135. {
  136. return;
  137. }
  138. this->WriteDisclaimer(ruleFileStream);
  139. std::string relPath = this->GetHomeRelativeOutputPath();
  140. std::string objTarget;
  141. for(std::set<cmStdString>::const_iterator i = m_CustomRuleFiles.begin();
  142. i != m_CustomRuleFiles.end(); ++i)
  143. {
  144. objTarget = relPath;
  145. objTarget += *i;
  146. ruleFileStream
  147. << m_IncludeDirective << " "
  148. << this->ConvertToOutputForExisting(objTarget.c_str()).c_str()
  149. << "\n";
  150. }
  151. // now do the clean
  152. ruleFileName = "CMakeCustomRules.dir/clean.make";
  153. // Open the rule file. This should be copy-if-different because the
  154. // rules may depend on this file itself.
  155. ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  156. cmGeneratedFileStream ruleFileStream2(ruleFileNameFull.c_str());
  157. ruleFileStream2.SetCopyIfDifferent(true);
  158. if(!ruleFileStream2)
  159. {
  160. return;
  161. }
  162. this->WriteDisclaimer(ruleFileStream2);
  163. std::vector<std::string> cleanFiles;
  164. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  165. i != sources.end(); ++i)
  166. {
  167. if(const cmCustomCommand* cc = (*i)->GetCustomCommand())
  168. {
  169. cleanFiles.push_back(cc->GetOutput());
  170. }
  171. }
  172. if (cleanFiles.size())
  173. {
  174. std::vector<std::string> commands;
  175. std::vector<std::string> depends;
  176. this->AppendCleanCommand(commands, cleanFiles);
  177. std::string dir = m_Makefile->GetStartOutputDirectory();
  178. dir += "/CMakeCustomRules.dir/clean";
  179. //dir = cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(), dir.c_str());
  180. //dir = cmSystemTools::ConvertToOutputPath(dir.c_str());
  181. dir = this->Convert(dir.c_str(),HOME_OUTPUT,SHELL,false);
  182. this->WriteMakeRule(ruleFileStream2,
  183. "Clean the output of this custom command.",
  184. dir.c_str(), depends, commands);
  185. // do the include
  186. commands.clear();
  187. depends.push_back(dir);
  188. this->WriteMakeRule(ruleFileStream2,
  189. "Clean the output of this custom command.",
  190. "clean", depends, commands);
  191. }
  192. }
  193. //----------------------------------------------------------------------------
  194. void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
  195. {
  196. std::string infoFileName = m_Makefile->GetStartOutputDirectory();
  197. infoFileName += "/CMakeDirectoryInformation.cmake";
  198. // Open the output file.
  199. cmGeneratedFileStream infoFileStream(infoFileName.c_str());
  200. if(!infoFileStream)
  201. {
  202. return;
  203. }
  204. // Write the do not edit header.
  205. this->WriteDisclaimer(infoFileStream);
  206. // Tell the dependency scanner to use unix paths if necessary.
  207. if(cmSystemTools::GetForceUnixPaths())
  208. {
  209. infoFileStream
  210. << "# Force unix paths in dependencies.\n"
  211. << "SET(CMAKE_FORCE_UNIX_PATHS 1)\n"
  212. << "\n";
  213. }
  214. // Store the include search path for this directory.
  215. infoFileStream
  216. << "# The C and CXX include file search paths:\n";
  217. infoFileStream
  218. << "SET(CMAKE_C_INCLUDE_PATH\n";
  219. std::vector<std::string> includeDirs;
  220. this->GetIncludeDirectories(includeDirs);
  221. for(std::vector<std::string>::iterator i = includeDirs.begin();
  222. i != includeDirs.end(); ++i)
  223. {
  224. infoFileStream
  225. << " \"" << this->Convert(i->c_str(),START_OUTPUT).c_str() << "\"\n";
  226. }
  227. infoFileStream
  228. << " )\n";
  229. infoFileStream
  230. << "SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
  231. // Store the include regular expressions for this directory.
  232. infoFileStream
  233. << "\n"
  234. << "# The C and CXX include file regular expressions for this directory.\n";
  235. infoFileStream
  236. << "SET(CMAKE_C_INCLUDE_REGEX_SCAN ";
  237. this->WriteCMakeArgument(infoFileStream,
  238. m_Makefile->GetIncludeRegularExpression());
  239. infoFileStream
  240. << ")\n";
  241. infoFileStream
  242. << "SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
  243. this->WriteCMakeArgument(infoFileStream,
  244. m_Makefile->GetComplainRegularExpression());
  245. infoFileStream
  246. << ")\n";
  247. infoFileStream
  248. << "SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
  249. infoFileStream
  250. << "SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
  251. }
  252. //----------------------------------------------------------------------------
  253. std::string
  254. cmLocalUnixMakefileGenerator3
  255. ::ConvertToFullPath(const std::string& localPath)
  256. {
  257. std::string dir = m_Makefile->GetStartOutputDirectory();
  258. dir += "/";
  259. dir += localPath;
  260. return dir;
  261. }
  262. //----------------------------------------------------------------------------
  263. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  264. {
  265. os
  266. << "# CMAKE generated file: DO NOT EDIT!\n"
  267. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  268. << " Generator, CMake Version "
  269. << cmMakefile::GetMajorVersion() << "."
  270. << cmMakefile::GetMinorVersion() << "\n\n";
  271. }
  272. std::string cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
  273. {
  274. // Include the rule file for each object.
  275. std::string relPath =
  276. cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(),
  277. m_Makefile->GetStartOutputDirectory());
  278. if (relPath.size())
  279. {
  280. relPath += "/";
  281. }
  282. return relPath;
  283. }
  284. //----------------------------------------------------------------------------
  285. void
  286. cmLocalUnixMakefileGenerator3
  287. ::WriteTargetRuleFiles(const cmTarget& target)
  288. {
  289. // Create a directory for this target.
  290. std::string dir = this->GetTargetDirectory(target);
  291. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  292. // First generate the object rule files. Save a list of all object
  293. // files for this target.
  294. std::vector<std::string> objects;
  295. std::vector<std::string> external_objects;
  296. std::vector<std::string> provides_requires;
  297. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  298. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  299. source != sources.end(); ++source)
  300. {
  301. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  302. !(*source)->GetCustomCommand())
  303. {
  304. if(!m_GlobalGenerator->IgnoreFile((*source)->GetSourceExtension().c_str()))
  305. {
  306. // Generate this object file's rule file.
  307. this->WriteObjectRuleFiles(target, *(*source), objects,provides_requires);
  308. }
  309. else if((*source)->GetPropertyAsBool("EXTERNAL_OBJECT"))
  310. {
  311. // This is an external object file. Just add it.
  312. external_objects.push_back((*source)->GetFullPath());
  313. }
  314. }
  315. }
  316. // Generate the build-time dependencies file for this target.
  317. std::string depBase = dir;
  318. depBase += "/";
  319. depBase += target.GetName();
  320. // Construct the rule file name.
  321. std::string ruleFileName = dir;
  322. ruleFileName += "/build.make";
  323. // Open the rule file. This should be copy-if-different because the
  324. // rules may depend on this file itself.
  325. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  326. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  327. ruleFileStream.SetCopyIfDifferent(true);
  328. if(!ruleFileStream)
  329. {
  330. return;
  331. }
  332. this->WriteDisclaimer(ruleFileStream);
  333. // Include the rule file for each object.
  334. std::string relPath = this->GetHomeRelativeOutputPath();
  335. std::string objTarget;
  336. if(!objects.empty())
  337. {
  338. ruleFileStream
  339. << "# Include make rules for object files.\n";
  340. for(std::vector<std::string>::const_iterator obj = objects.begin();
  341. obj != objects.end(); ++obj)
  342. {
  343. objTarget = relPath;
  344. objTarget += *obj;
  345. objTarget += ".build.make";
  346. ruleFileStream
  347. << m_IncludeDirective << " "
  348. << this->ConvertToOutputForExisting(objTarget.c_str()).c_str()
  349. << "\n";
  350. }
  351. ruleFileStream
  352. << "\n";
  353. }
  354. // Write the rule for this target type.
  355. switch(target.GetType())
  356. {
  357. case cmTarget::STATIC_LIBRARY:
  358. this->WriteStaticLibraryRule(ruleFileStream, ruleFileName.c_str(),
  359. target, objects, external_objects,
  360. provides_requires);
  361. break;
  362. case cmTarget::SHARED_LIBRARY:
  363. this->WriteSharedLibraryRule(ruleFileStream, ruleFileName.c_str(),
  364. target, objects, external_objects,
  365. provides_requires);
  366. break;
  367. case cmTarget::MODULE_LIBRARY:
  368. this->WriteModuleLibraryRule(ruleFileStream, ruleFileName.c_str(),
  369. target, objects, external_objects,
  370. provides_requires);
  371. break;
  372. case cmTarget::EXECUTABLE:
  373. this->WriteExecutableRule(ruleFileStream, ruleFileName.c_str(),
  374. target, objects, external_objects,
  375. provides_requires);
  376. break;
  377. default:
  378. break;
  379. }
  380. }
  381. //----------------------------------------------------------------------------
  382. void
  383. cmLocalUnixMakefileGenerator3
  384. ::WriteObjectDependRules(std::ostream& ruleFileStream,
  385. std::string &obj,
  386. const char * lang,
  387. const cmSourceFile& source,
  388. std::vector<std::string>& depends,
  389. std::string& depMakeFile)
  390. {
  391. // TODO: what the heck is this?
  392. // Generate the build-time dependencies file for this object file.
  393. std::string depMarkFile;
  394. if(!this->GenerateDependsMakeFile(lang, obj.c_str(),
  395. depMakeFile, depMarkFile))
  396. {
  397. cmSystemTools::Error("No dependency checker available for language \"",
  398. lang, "\".");
  399. return;
  400. }
  401. // Create the list of dependencies known at cmake time. These are
  402. // shared between the object file and dependency scanning rule.
  403. depends.push_back(source.GetFullPath());
  404. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  405. {
  406. std::vector<std::string> deps;
  407. cmSystemTools::ExpandListArgument(objectDeps, deps);
  408. for(std::vector<std::string>::iterator i = deps.begin();
  409. i != deps.end(); ++i)
  410. {
  411. depends.push_back(i->c_str());
  412. }
  413. }
  414. // Write the dependency generation rule.
  415. std::string relativeObj = this->GetHomeRelativeOutputPath();
  416. relativeObj += obj;
  417. std::vector<std::string> commands;
  418. std::string depEcho = "Scanning ";
  419. depEcho += lang;
  420. depEcho += " dependencies of ";
  421. depEcho += this->Convert(relativeObj.c_str(),NONE,SHELL);
  422. this->AppendEcho(commands, depEcho.c_str());
  423. // Add a command to call CMake to scan dependencies. CMake will
  424. // touch the corresponding depends file after scanning dependencies.
  425. cmOStringStream depCmd;
  426. // TODO: Account for source file properties and directory-level
  427. // definitions when scanning for dependencies.
  428. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  429. << m_GlobalGenerator->GetName() << "\" "
  430. << lang << " "
  431. << relativeObj.c_str() << " "
  432. << this->Convert(source.GetFullPath().c_str(),HOME_OUTPUT,SHELL);
  433. commands.push_back(depCmd.str());
  434. // compute the target
  435. std::string relPath = this->GetHomeRelativeOutputPath();
  436. relPath += depMarkFile;
  437. // Write the rule.
  438. this->WriteMakeRule(ruleFileStream, 0,
  439. relPath.c_str(), depends, commands);
  440. }
  441. //----------------------------------------------------------------------------
  442. void
  443. cmLocalUnixMakefileGenerator3
  444. ::WriteObjectBuildFile(std::string &obj,
  445. const char *lang,
  446. const cmTarget& target,
  447. const cmSourceFile& source,
  448. std::vector<std::string>& depends,
  449. std::string &depMakeFile,
  450. std::vector<std::string>& provides_requires)
  451. {
  452. // Open the rule file for writing. This should be copy-if-different
  453. // because the rules may depend on this file itself.
  454. std::string ruleFileName = obj;
  455. ruleFileName += ".build.make";
  456. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  457. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  458. ruleFileStream.SetCopyIfDifferent(true);
  459. if(!ruleFileStream)
  460. {
  461. return;
  462. }
  463. this->WriteDisclaimer(ruleFileStream);
  464. ruleFileStream
  465. << "# Rule file for object file " << obj.c_str() << ".\n\n";
  466. // generate the depend scanning rule
  467. this->WriteObjectDependRules(ruleFileStream, obj, lang, source,
  468. depends, depMakeFile);
  469. this->AppendRuleDepend(depends, ruleFileNameFull.c_str());
  470. // Include the dependencies for the target.
  471. std::string depPath = this->GetHomeRelativeOutputPath();
  472. depPath += depMakeFile;
  473. depMakeFile = this->Convert(depPath.c_str(),HOME_OUTPUT,MAKEFILE);
  474. ruleFileStream
  475. << "# Include any dependencies generated for this rule.\n"
  476. << m_IncludeDirective << " "
  477. << depMakeFile
  478. << "\n\n";
  479. // Write the build rule.
  480. // Build the set of compiler flags.
  481. std::string flags;
  482. // Add the export symbol definition for shared library objects.
  483. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  484. (target.GetType() == cmTarget::MODULE_LIBRARY));
  485. if(shared)
  486. {
  487. flags += "-D";
  488. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  489. {
  490. flags += custom_export_name;
  491. }
  492. else
  493. {
  494. std::string in = target.GetName();
  495. in += "_EXPORTS";
  496. flags += cmSystemTools::MakeCindentifier(in.c_str());
  497. }
  498. }
  499. // Add flags from source file properties.
  500. this->AppendFlags(flags, source.GetProperty("COMPILE_FLAGS"));
  501. // Add language-specific flags.
  502. this->AddLanguageFlags(flags, lang);
  503. // Add shared-library flags if needed.
  504. this->AddSharedFlags(flags, lang, shared);
  505. // Add include directory flags.
  506. this->AppendFlags(flags, this->GetIncludeFlags(lang));
  507. // Get the output paths for source and object files.
  508. std::string sourceFile = source.GetFullPath();
  509. if(m_UseRelativePaths)
  510. {
  511. sourceFile = this->Convert(sourceFile.c_str(),HOME_OUTPUT);
  512. }
  513. sourceFile = this->Convert(sourceFile.c_str(),NONE,SHELL);
  514. std::string objectFile = this->Convert(obj.c_str(),START_OUTPUT,SHELL);
  515. // Construct the build message.
  516. std::string relativeObj = this->GetHomeRelativeOutputPath();
  517. relativeObj += obj;
  518. std::vector<std::string> commands;
  519. std::string buildEcho = "Building ";
  520. buildEcho += lang;
  521. buildEcho += " object ";
  522. buildEcho += relativeObj;
  523. this->AppendEcho(commands, buildEcho.c_str());
  524. // Construct the compile rules.
  525. std::string compileRuleVar = "CMAKE_";
  526. compileRuleVar += lang;
  527. compileRuleVar += "_COMPILE_OBJECT";
  528. std::string compileRule =
  529. m_Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  530. cmSystemTools::ExpandListArgument(compileRule, commands);
  531. // Expand placeholders in the commands.
  532. for(std::vector<std::string>::iterator i = commands.begin();
  533. i != commands.end(); ++i)
  534. {
  535. this->ExpandRuleVariables(*i,
  536. lang,
  537. 0, // no objects
  538. 0, // no target
  539. 0, // no link libs
  540. sourceFile.c_str(),
  541. relativeObj.c_str(),
  542. flags.c_str());
  543. }
  544. // Write the rule.
  545. this->WriteMakeRule(ruleFileStream, 0,
  546. relativeObj.c_str(), depends, commands);
  547. // If the language needs provides-requires mode, create the
  548. // corresponding targets.
  549. std::string objectRequires = relativeObj;
  550. objectRequires += ".requires";
  551. std::vector<std::string> no_commands;
  552. // always provide an empty requires target
  553. this->WriteMakeRule(ruleFileStream, 0,
  554. objectRequires.c_str(), no_commands, no_commands);
  555. if(strcmp(lang, "Fortran") == 0)
  556. {
  557. std::string objectProvides = obj;
  558. objectProvides += ".provides";
  559. {
  560. // Add the provides target to build the object file.
  561. std::vector<std::string> p_depends;
  562. p_depends.push_back(obj);
  563. this->WriteMakeRule(ruleFileStream, 0,
  564. objectProvides.c_str(), p_depends, no_commands);
  565. }
  566. // Add this to the set of provides-requires objects on the target.
  567. provides_requires.push_back(objectRequires);
  568. {
  569. // Add the requires.build target to recursively build the provides
  570. // target after needed information is up to date.
  571. std::vector<std::string> no_depends;
  572. std::vector<std::string> r_commands;
  573. r_commands.push_back(this->GetRecursiveMakeCall("Makefile",objectProvides.c_str()));
  574. objectRequires += ".build";
  575. this->WriteMakeRule(ruleFileStream, 0,
  576. objectRequires.c_str(), no_depends, r_commands);
  577. }
  578. }
  579. }
  580. //----------------------------------------------------------------------------
  581. void
  582. cmLocalUnixMakefileGenerator3
  583. ::WriteObjectRuleFiles(const cmTarget& target, const cmSourceFile& source,
  584. std::vector<std::string>& objects,
  585. std::vector<std::string>& provides_requires)
  586. {
  587. // Identify the language of the source file.
  588. const char* lang = this->GetSourceFileLanguage(source);
  589. if(!lang)
  590. {
  591. // If language is not known, this is an error.
  592. cmSystemTools::Error("Source file \"", source.GetFullPath().c_str(),
  593. "\" has unknown type.");
  594. return;
  595. }
  596. // Get the full path name of the object file.
  597. std::string obj = this->GetObjectFileName(target, source);
  598. // Avoid generating duplicate rules.
  599. if(m_ObjectFiles.find(obj) == m_ObjectFiles.end())
  600. {
  601. m_ObjectFiles.insert(obj);
  602. }
  603. else
  604. {
  605. cmOStringStream err;
  606. err << "Warning: Source file \""
  607. << source.GetSourceName().c_str() << "."
  608. << source.GetSourceExtension().c_str()
  609. << "\" is listed multiple times for target \"" << target.GetName()
  610. << "\".";
  611. cmSystemTools::Message(err.str().c_str(), "Warning");
  612. return;
  613. }
  614. // Create the directory containing the object file. This may be a
  615. // subdirectory under the target's directory.
  616. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  617. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  618. // Save this in the target's list of object files.
  619. objects.push_back(obj);
  620. std::string relativeObj = this->GetHomeRelativeOutputPath();
  621. relativeObj += obj;
  622. // we compute some depends when writing the depend.make that we will also
  623. // use in the build.make, same with depMakeFile
  624. std::vector<std::string> depends;
  625. std::string depMakeFile;
  626. // generate the build rule file
  627. this->WriteObjectBuildFile(obj, lang, target, source, depends, depMakeFile,
  628. provides_requires);
  629. // The object file should be checked for dependency integrity.
  630. m_CheckDependFiles[lang].insert(relativeObj);
  631. }
  632. //----------------------------------------------------------------------------
  633. void
  634. cmLocalUnixMakefileGenerator3
  635. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  636. {
  637. // Create a directory for custom rule files.
  638. std::string dir = "CMakeCustomRules.dir";
  639. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  640. // Convert the output name to a relative path if possible.
  641. std::string output = this->Convert(cc.GetOutput(),START_OUTPUT);
  642. // Construct the name of the rule file by transforming the output
  643. // name to a valid file name. Since the output is already a file
  644. // everything but the path characters is valid.
  645. std::string customName = output;
  646. cmSystemTools::ReplaceString(customName, "../", "___");
  647. cmSystemTools::ReplaceString(customName, "/", "_");
  648. cmSystemTools::ReplaceString(customName, ":", "_");
  649. std::string ruleFileName = dir;
  650. ruleFileName += "/";
  651. ruleFileName += customName;
  652. ruleFileName += ".build.make";
  653. // If this is a duplicate rule produce an error.
  654. if(m_CustomRuleFiles.find(ruleFileName) != m_CustomRuleFiles.end())
  655. {
  656. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  657. cc.GetOutput());
  658. return;
  659. }
  660. m_CustomRuleFiles.insert(ruleFileName);
  661. // what is the relative path to the rule file
  662. std::string relRuleFile = this->GetHomeRelativeOutputPath();
  663. relRuleFile += ruleFileName;
  664. // Open the rule file. This should be copy-if-different because the
  665. // rules may depend on this file itself.
  666. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  667. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  668. ruleFileStream.SetCopyIfDifferent(true);
  669. if(!ruleFileStream)
  670. {
  671. return;
  672. }
  673. this->WriteDisclaimer(ruleFileStream);
  674. ruleFileStream
  675. << "# Custom command rule file for " << output.c_str() << ".\n\n";
  676. // Collect the commands.
  677. std::vector<std::string> commands;
  678. std::string preEcho = "Generating ";
  679. preEcho += output;
  680. this->AppendEcho(commands, preEcho.c_str());
  681. this->AppendCustomCommand(commands, cc);
  682. // Collect the dependencies.
  683. std::vector<std::string> depends;
  684. this->AppendCustomDepend(depends, cc);
  685. // Add a dependency on the rule file itself.
  686. this->AppendRuleDepend(depends, relRuleFile.c_str());
  687. // Write the rule.
  688. const char* comment = 0;
  689. if(cc.GetComment() && *cc.GetComment())
  690. {
  691. comment = cc.GetComment();
  692. }
  693. this->WriteMakeRule(ruleFileStream, comment,
  694. cc.GetOutput(), depends, commands);
  695. }
  696. //----------------------------------------------------------------------------
  697. void
  698. cmLocalUnixMakefileGenerator3
  699. ::WriteUtilityRuleFiles(const cmTarget& target)
  700. {
  701. // Create a directory for this target.
  702. std::string dir = this->GetTargetDirectory(target);
  703. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  704. // Construct the name of the rule file.
  705. std::string ruleFileName = dir;
  706. ruleFileName += "/build.make";
  707. // Open the rule file. This should be copy-if-different because the
  708. // rules may depend on this file itself.
  709. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  710. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  711. ruleFileStream.SetCopyIfDifferent(true);
  712. if(!ruleFileStream)
  713. {
  714. return;
  715. }
  716. this->WriteDisclaimer(ruleFileStream);
  717. ruleFileStream
  718. << "# Utility rule file for " << target.GetName() << ".\n\n";
  719. // Collect the commands and dependencies.
  720. std::vector<std::string> commands;
  721. std::vector<std::string> depends;
  722. // Utility targets store their rules in pre- and post-build commands.
  723. this->AppendCustomDepends(depends, target.GetPreBuildCommands());
  724. this->AppendCustomDepends(depends, target.GetPostBuildCommands());
  725. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  726. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  727. // Add dependencies on targets that must be built first.
  728. this->AppendTargetDepends(depends, target);
  729. // Add a dependency on the rule file itself.
  730. std::string relPath = this->GetHomeRelativeOutputPath();
  731. std::string objTarget = relPath;
  732. objTarget += ruleFileName;
  733. this->AppendRuleDepend(depends, objTarget.c_str());
  734. // Write the rule.
  735. this->WriteMakeRule(ruleFileStream, 0,
  736. target.GetName(), depends, commands);
  737. // Write convenience targets.
  738. dir = m_Makefile->GetStartOutputDirectory();
  739. dir += "/";
  740. dir += this->GetTargetDirectory(target);
  741. std::string buildTargetRuleName = dir;
  742. buildTargetRuleName += "/build";
  743. buildTargetRuleName =
  744. this->Convert(buildTargetRuleName.c_str(),HOME_OUTPUT,MAKEFILE);
  745. this->WriteConvenienceRule(ruleFileStream, target.GetName(),
  746. buildTargetRuleName.c_str());
  747. }
  748. //----------------------------------------------------------------------------
  749. bool
  750. cmLocalUnixMakefileGenerator3
  751. ::GenerateDependsMakeFile(const std::string& lang, const char* objFile,
  752. std::string& depMakeFile, std::string& depMarkFile)
  753. {
  754. // Construct a checker for the given language.
  755. std::auto_ptr<cmDepends>
  756. checker(this->GetDependsChecker(lang,
  757. m_Makefile->GetStartOutputDirectory(),
  758. objFile, false));
  759. if(checker.get())
  760. {
  761. // Save the make and mark file names.
  762. depMakeFile = checker->GetMakeFileName();
  763. depMarkFile = checker->GetMarkFileName();
  764. // Todo is this required???
  765. // Check the dependencies.
  766. checker->Check();
  767. return true;
  768. }
  769. return false;
  770. }
  771. //----------------------------------------------------------------------------
  772. void
  773. cmLocalUnixMakefileGenerator3
  774. ::WriteMakeRule(std::ostream& os,
  775. const char* comment,
  776. const char* target,
  777. const std::vector<std::string>& depends,
  778. const std::vector<std::string>& commands)
  779. {
  780. // Make sure there is a target.
  781. if(!target || !*target)
  782. {
  783. cmSystemTools::Error("No target for WriteMakeRule!");
  784. return;
  785. }
  786. std::string replace;
  787. // Write the comment describing the rule in the makefile.
  788. if(comment)
  789. {
  790. replace = comment;
  791. std::string::size_type lpos = 0;
  792. std::string::size_type rpos;
  793. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  794. {
  795. os << "# " << replace.substr(lpos, rpos-lpos) << "\n";
  796. lpos = rpos+1;
  797. }
  798. os << "# " << replace.substr(lpos) << "\n";
  799. }
  800. // Construct the left hand side of the rule.
  801. replace = target;
  802. std::string tgt = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
  803. tgt = this->ConvertToMakeTarget(tgt.c_str());
  804. const char* space = "";
  805. if(tgt.size() == 1)
  806. {
  807. // Add a space before the ":" to avoid drive letter confusion on
  808. // Windows.
  809. space = " ";
  810. }
  811. // Write the rule.
  812. if(depends.empty())
  813. {
  814. // No dependencies. The commands will always run.
  815. os << tgt.c_str() << space << ":\n";
  816. }
  817. else
  818. {
  819. // Split dependencies into multiple rule lines. This allows for
  820. // very long dependency lists even on older make implementations.
  821. for(std::vector<std::string>::const_iterator dep = depends.begin();
  822. dep != depends.end(); ++dep)
  823. {
  824. replace = *dep;
  825. replace = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
  826. replace = this->ConvertToMakeTarget(replace.c_str());
  827. os << tgt.c_str() << space << ": " << replace.c_str() << "\n";
  828. }
  829. }
  830. // Write the list of commands.
  831. for(std::vector<std::string>::const_iterator i = commands.begin();
  832. i != commands.end(); ++i)
  833. {
  834. replace = *i;
  835. os << "\t" << replace.c_str() << "\n";
  836. }
  837. os << "\n";
  838. }
  839. //----------------------------------------------------------------------------
  840. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  841. {
  842. os
  843. << "#======================================"
  844. << "=======================================\n";
  845. }
  846. //----------------------------------------------------------------------------
  847. void
  848. cmLocalUnixMakefileGenerator3
  849. ::WriteMakeVariables(std::ostream& makefileStream)
  850. {
  851. this->WriteDivider(makefileStream);
  852. makefileStream
  853. << "# Set environment variables for the build.\n"
  854. << "\n";
  855. if(m_WindowsShell)
  856. {
  857. makefileStream
  858. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  859. << "NULL=\n"
  860. << "!ELSE\n"
  861. << "NULL=nul\n"
  862. << "!ENDIF\n";
  863. }
  864. else
  865. {
  866. makefileStream
  867. << "# The shell in which to execute make rules.\n"
  868. << "SHELL = /bin/sh\n"
  869. << "\n";
  870. }
  871. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  872. {
  873. makefileStream
  874. << "# Produce verbose output by default.\n"
  875. << "VERBOSE = 1\n"
  876. << "\n";
  877. }
  878. std::string cmakecommand =
  879. this->ConvertToOutputForExisting(
  880. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  881. makefileStream
  882. << "# The CMake executable.\n"
  883. << "CMAKE_COMMAND = "
  884. << this->Convert(cmakecommand.c_str(),HOME_OUTPUT,MAKEFILE).c_str() << "\n"
  885. << "\n";
  886. makefileStream
  887. << "# The command to remove a file.\n"
  888. << "RM = "
  889. << this->Convert(cmakecommand.c_str(),HOME_OUTPUT,SHELL).c_str()
  890. << " -E remove -f\n"
  891. << "\n";
  892. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  893. {
  894. makefileStream
  895. << "# The program to use to edit the cache.\n"
  896. << "CMAKE_EDIT_COMMAND = "
  897. << (this->ConvertToOutputForExisting(
  898. m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
  899. << "\n";
  900. }
  901. makefileStream
  902. << "# The top-level source directory on which CMake was run.\n"
  903. << "CMAKE_SOURCE_DIR = "
  904. << this->Convert(m_Makefile->GetHomeDirectory(), HOME_OUTPUT, SHELL)
  905. << "\n"
  906. << "\n";
  907. makefileStream
  908. << "# The top-level build directory on which CMake was run.\n"
  909. << "CMAKE_BINARY_DIR = ."
  910. << "\n"
  911. << "\n";
  912. }
  913. void cmLocalUnixMakefileGenerator3::WriteMainTargetIncludes(std::ostream& makefileStream,
  914. const char *file,
  915. const char *rule)
  916. {
  917. std::vector<std::string> depends;
  918. std::vector<std::string> no_commands;
  919. // if this is the build rules also include the custom commands if there
  920. // were any
  921. if (!strcmp(rule,"build") && m_CustomRuleFiles.size())
  922. {
  923. // do the include
  924. std::string dir = m_Makefile->GetStartOutputDirectory();
  925. dir += "/CMakeCustomRules.dir/build.make";
  926. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  927. makefileStream
  928. << m_IncludeDirective << " "
  929. << this->ConvertToOutputForExisting(dir.c_str()).c_str()
  930. << "\n";
  931. }
  932. // if this is the clean rules also include the custom commands if there
  933. // were any
  934. const char* clean_no_custom = m_Makefile->GetProperty("CLEAN_NO_CUSTOM");
  935. if (!strcmp(rule,"clean") && cmSystemTools::IsOff(clean_no_custom))
  936. {
  937. // do the include
  938. std::string dir = m_Makefile->GetStartOutputDirectory();
  939. dir += "/CMakeCustomRules.dir/clean.make";
  940. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  941. makefileStream
  942. << m_IncludeDirective << " "
  943. << this->ConvertToOutputForExisting(dir.c_str()).c_str()
  944. << "\n";
  945. }
  946. for (cmTargets::const_iterator l = m_Makefile->GetTargets().begin();
  947. l != m_Makefile->GetTargets().end(); l++)
  948. {
  949. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  950. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  951. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  952. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  953. (l->second.GetType() == cmTarget::UTILITY && !strcmp(rule,"build")))
  954. {
  955. // do the include
  956. std::string tname = this->GetRelativeTargetDirectory(l->second);
  957. tname += "/";
  958. tname += file;
  959. makefileStream
  960. << m_IncludeDirective << " "
  961. << this->ConvertToOutputForExisting(tname.c_str()).c_str()
  962. << "\n";
  963. }
  964. }
  965. }
  966. void cmLocalUnixMakefileGenerator3::WriteMainTargetRules(std::ostream& makefileStream,
  967. const char * /* file */,
  968. const char *rule,
  969. bool inAll)
  970. {
  971. std::vector<std::string> all_tgts;
  972. std::vector<std::string> depends;
  973. std::vector<std::string> no_commands;
  974. if (inAll)
  975. {
  976. for (cmTargets::const_iterator l = m_Makefile->GetTargets().begin();
  977. l != m_Makefile->GetTargets().end(); l++)
  978. {
  979. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  980. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  981. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  982. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  983. (l->second.GetType() == cmTarget::UTILITY && !strcmp(rule,"build")))
  984. {
  985. // Add this to the list of depends rules in this directory.
  986. std::string tname = this->GetRelativeTargetDirectory(l->second);
  987. tname += "/";
  988. tname += rule;
  989. all_tgts.push_back(tname);
  990. if(l->second.IsInAll())
  991. {
  992. // add the dependency
  993. depends.clear();
  994. depends.push_back(tname);
  995. this->WriteMakeRule(makefileStream, 0,
  996. rule, depends, no_commands);
  997. }
  998. }
  999. }
  1000. }
  1001. // not for the top gen
  1002. if (this->GetParent())
  1003. {
  1004. // write the directory rule add in the subdirs
  1005. std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
  1006. std::string dir;
  1007. // for each subdir add the directory depend
  1008. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  1009. for (; sdi != subdirs.end(); ++sdi)
  1010. {
  1011. cmLocalUnixMakefileGenerator3 * lg =
  1012. static_cast<cmLocalUnixMakefileGenerator3 *>(*sdi);
  1013. dir = lg->GetMakefile()->GetStartOutputDirectory();
  1014. dir += "/";
  1015. dir += rule;
  1016. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1017. all_tgts.push_back(dir);
  1018. }
  1019. dir = m_Makefile->GetStartOutputDirectory();
  1020. dir += "/";
  1021. dir += rule;
  1022. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1023. this->WriteMakeRule(makefileStream, 0,
  1024. dir.c_str(), all_tgts, no_commands);
  1025. }
  1026. }
  1027. //----------------------------------------------------------------------------
  1028. void
  1029. cmLocalUnixMakefileGenerator3
  1030. ::WriteLocalCleanRule(std::ostream& makefileStream)
  1031. {
  1032. // Collect a list of extra files to clean in this directory.
  1033. std::vector<std::string> files;
  1034. // Look for files registered for cleaning in this directory.
  1035. if(const char* additional_clean_files =
  1036. m_Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES"))
  1037. {
  1038. cmSystemTools::ExpandListArgument(additional_clean_files, files);
  1039. }
  1040. // Write the local clean rule for this directory.
  1041. if(!files.empty())
  1042. {
  1043. // Have extra files to clean. Write the action to remove them.
  1044. std::string cleanTarget = this->GetHomeRelativeOutputPath();
  1045. cleanTarget += "clean.local";
  1046. std::vector<std::string> no_depends;
  1047. std::vector<std::string> commands;
  1048. this->AppendCleanCommand(commands, files);
  1049. this->WriteMakeRule(makefileStream,
  1050. "Clean extra files in this directory.",
  1051. cleanTarget.c_str(), no_depends, commands);
  1052. commands.clear();
  1053. no_depends.push_back(cleanTarget);
  1054. this->WriteMakeRule(makefileStream, 0,
  1055. "clean", no_depends, commands);
  1056. }
  1057. }
  1058. //----------------------------------------------------------------------------
  1059. void
  1060. cmLocalUnixMakefileGenerator3
  1061. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  1062. {
  1063. this->WriteDivider(makefileStream);
  1064. makefileStream
  1065. << "# Special targets provided by cmake.\n"
  1066. << "\n";
  1067. // Write the main entry point target. This must be the VERY first
  1068. // target so that make with no arguments will run it.
  1069. {
  1070. // Just depend on the all target to drive the build.
  1071. std::vector<std::string> depends;
  1072. std::vector<std::string> no_commands;
  1073. depends.push_back("all");
  1074. // Write the rule.
  1075. this->WriteMakeRule(makefileStream,
  1076. "Default target executed when no arguments are "
  1077. "given to make.",
  1078. "default_target",
  1079. depends,
  1080. no_commands);
  1081. }
  1082. // Write special "test" target to run ctest.
  1083. if(m_Makefile->IsOn("CMAKE_TESTING_ENABLED"))
  1084. {
  1085. std::string ctest;
  1086. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  1087. {
  1088. // We are building CMake itself. Use the ctest that comes with
  1089. // this version of CMake instead of the one used to build it.
  1090. ctest = m_ExecutableOutputPath;
  1091. ctest += "ctest";
  1092. ctest += cmSystemTools::GetExecutableExtension();
  1093. ctest = this->Convert(ctest.c_str(),START_OUTPUT,SHELL);
  1094. }
  1095. else
  1096. {
  1097. // We are building another project. Use the ctest that comes with
  1098. // the CMake building it.
  1099. ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  1100. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1101. ctest += "/";
  1102. ctest += "ctest";
  1103. ctest += cmSystemTools::GetExecutableExtension();
  1104. ctest = this->ConvertToOutputForExisting(ctest.c_str());
  1105. }
  1106. std::vector<std::string> no_depends;
  1107. std::vector<std::string> commands;
  1108. this->AppendEcho(commands, "Running tests...");
  1109. std::string cmd = ctest;
  1110. cmd += " $(ARGS)";
  1111. commands.push_back(cmd);
  1112. this->WriteMakeRule(makefileStream,
  1113. "Special rule to drive testing with ctest.",
  1114. "test", no_depends, commands);
  1115. }
  1116. // Write special "install" target to run cmake_install.cmake script.
  1117. {
  1118. std::vector<std::string> depends;
  1119. std::vector<std::string> commands;
  1120. std::string cmd;
  1121. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  1122. {
  1123. // We are building CMake itself. We cannot use the original
  1124. // executable to install over itself.
  1125. cmd = m_ExecutableOutputPath;
  1126. cmd += "cmake";
  1127. cmd = this->Convert(cmd.c_str(),START_OUTPUT,SHELL);
  1128. }
  1129. else
  1130. {
  1131. cmd = "$(CMAKE_COMMAND)";
  1132. }
  1133. cmd += " -P cmake_install.cmake";
  1134. commands.push_back(cmd);
  1135. const char* noall =
  1136. m_Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1137. if(!noall || cmSystemTools::IsOff(noall))
  1138. {
  1139. // Drive the build before installing.
  1140. depends.push_back("all");
  1141. }
  1142. this->WriteMakeRule(makefileStream,
  1143. "Special rule to run installation script.",
  1144. "install", depends, commands);
  1145. }
  1146. // Write special "rebuild_cache" target to re-run cmake.
  1147. {
  1148. std::vector<std::string> no_depends;
  1149. std::vector<std::string> commands;
  1150. this->AppendEcho(commands, "Running CMake to regenerate build system...");
  1151. commands.push_back(
  1152. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1153. this->WriteMakeRule(makefileStream,
  1154. "Special rule to re-run CMake using make.",
  1155. "rebuild_cache",
  1156. no_depends,
  1157. commands);
  1158. }
  1159. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1160. // Otherwise default to the interactive command-line interface.
  1161. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1162. {
  1163. std::vector<std::string> no_depends;
  1164. std::vector<std::string> commands;
  1165. this->AppendEcho(commands, "Running CMake cache editor...");
  1166. commands.push_back(
  1167. "$(CMAKE_EDIT_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1168. this->WriteMakeRule(makefileStream,
  1169. "Special rule to re-run CMake cache editor using make.",
  1170. "edit_cache",
  1171. no_depends,
  1172. commands);
  1173. }
  1174. else
  1175. {
  1176. std::vector<std::string> no_depends;
  1177. std::vector<std::string> commands;
  1178. this->AppendEcho(commands,
  1179. "Running interactive CMake command-line interface...");
  1180. commands.push_back(
  1181. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  1182. this->WriteMakeRule(makefileStream,
  1183. "Special rule to re-run CMake cache editor using make.",
  1184. "edit_cache",
  1185. no_depends,
  1186. commands);
  1187. }
  1188. }
  1189. //----------------------------------------------------------------------------
  1190. void
  1191. cmLocalUnixMakefileGenerator3
  1192. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  1193. {
  1194. this->WriteDivider(makefileStream);
  1195. makefileStream
  1196. << "# Special targets to cleanup operation of make.\n"
  1197. << "\n";
  1198. // Write special "cmake_check_build_system" target to run cmake with
  1199. // the --check-build-system flag.
  1200. {
  1201. // Build command to run CMake to check if anything needs regenerating.
  1202. std::string cmakefileName = "Makefile.cmake";
  1203. std::string runRule =
  1204. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1205. runRule += " --check-build-system ";
  1206. runRule += this->Convert(cmakefileName.c_str(),NONE,SHELL);
  1207. std::vector<std::string> no_depends;
  1208. std::vector<std::string> commands;
  1209. commands.push_back(runRule);
  1210. this->WriteMakeRule(makefileStream,
  1211. "Special rule to run CMake to check the build system "
  1212. "integrity.\n"
  1213. "No rule that depends on this can have "
  1214. "commands that come from listfiles\n"
  1215. "because they might be regenerated.",
  1216. "cmake_check_build_system",
  1217. no_depends,
  1218. commands);
  1219. }
  1220. std::vector<std::string> no_commands;
  1221. // Write special target to silence make output. This must be after
  1222. // the default target in case VERBOSE is set (which changes the
  1223. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  1224. // "VERBOSE=1" to be added as a make variable which will change the
  1225. // name of this special target. This gives a make-time choice to
  1226. // the user.
  1227. std::vector<std::string> no_depends;
  1228. this->WriteMakeRule(makefileStream,
  1229. "Suppress display of executed commands.",
  1230. "$(VERBOSE).SILENT",
  1231. no_depends,
  1232. no_commands);
  1233. // Special target to cleanup operation of make tool.
  1234. std::vector<std::string> depends;
  1235. this->WriteMakeRule(makefileStream,
  1236. "Disable implicit rules so canoncical targets will work.",
  1237. ".SUFFIXES",
  1238. depends,
  1239. no_commands);
  1240. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  1241. depends.push_back(".hpux_make_needs_suffix_list");
  1242. this->WriteMakeRule(makefileStream, 0,
  1243. ".SUFFIXES", depends, no_commands);
  1244. }
  1245. //----------------------------------------------------------------------------
  1246. void
  1247. cmLocalUnixMakefileGenerator3
  1248. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  1249. const char* realTarget,
  1250. const char* helpTarget)
  1251. {
  1252. // A rule is only needed if the names are different.
  1253. if(strcmp(realTarget, helpTarget) != 0)
  1254. {
  1255. // The helper target depends on the real target.
  1256. std::vector<std::string> depends;
  1257. depends.push_back(realTarget);
  1258. // There are no commands.
  1259. std::vector<std::string> no_commands;
  1260. // Write the rule.
  1261. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  1262. helpTarget, depends, no_commands);
  1263. }
  1264. }
  1265. //----------------------------------------------------------------------------
  1266. void
  1267. cmLocalUnixMakefileGenerator3
  1268. ::WriteTargetRequiresRule(std::ostream& ruleFileStream, const cmTarget& target,
  1269. const std::vector<std::string>& objects)
  1270. {
  1271. std::vector<std::string> depends;
  1272. std::vector<std::string> no_commands;
  1273. // Construct the name of the dependency generation target.
  1274. std::string depTarget = this->GetRelativeTargetDirectory(target);
  1275. depTarget += "/requires";
  1276. // This target drives dependency generation for all object files.
  1277. std::string relPath = this->GetHomeRelativeOutputPath();
  1278. std::string objTarget;
  1279. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1280. obj != objects.end(); ++obj)
  1281. {
  1282. objTarget = relPath;
  1283. objTarget += *obj;
  1284. objTarget += ".requires";
  1285. depends.push_back(objTarget);
  1286. }
  1287. // Write the rule.
  1288. this->WriteMakeRule(ruleFileStream, 0,
  1289. depTarget.c_str(), depends, no_commands);
  1290. }
  1291. //----------------------------------------------------------------------------
  1292. void
  1293. cmLocalUnixMakefileGenerator3
  1294. ::WriteExecutableRule(std::ostream& ruleFileStream,
  1295. const char* ruleFileName,
  1296. const cmTarget& target,
  1297. const std::vector<std::string>& objects,
  1298. const std::vector<std::string>& external_objects,
  1299. const std::vector<std::string>& provides_requires)
  1300. {
  1301. // Write the dependency generation rule.
  1302. this->WriteTargetDependRule(ruleFileStream, target, objects);
  1303. std::vector<std::string> commands;
  1304. std::string relPath = this->GetHomeRelativeOutputPath();
  1305. std::string objTarget;
  1306. // Build list of dependencies.
  1307. std::vector<std::string> depends;
  1308. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1309. obj != objects.end(); ++obj)
  1310. {
  1311. objTarget = relPath;
  1312. objTarget += *obj;
  1313. depends.push_back(objTarget);
  1314. }
  1315. // Add dependencies on targets that must be built first.
  1316. this->AppendTargetDepends(depends, target);
  1317. // Add a dependency on the rule file itself.
  1318. objTarget = relPath;
  1319. objTarget += ruleFileName;
  1320. this->AppendRuleDepend(depends, objTarget.c_str());
  1321. std::vector<std::string> cleanFiles;
  1322. // Construct the full path to the executable that will be generated.
  1323. std::string targetFullPath = m_ExecutableOutputPath;
  1324. if(targetFullPath.length() == 0)
  1325. {
  1326. targetFullPath = m_Makefile->GetStartOutputDirectory();
  1327. targetFullPath += "/";
  1328. }
  1329. #ifdef __APPLE__
  1330. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1331. {
  1332. // Make bundle directories
  1333. targetFullPath += target.GetName();
  1334. targetFullPath += ".app/Contents/MacOS/";
  1335. }
  1336. #endif
  1337. targetFullPath += target.GetName();
  1338. targetFullPath += cmSystemTools::GetExecutableExtension();
  1339. // Convert to the output path to use in constructing commands.
  1340. std::string targetOutPath = this->Convert(targetFullPath.c_str(),HOME_OUTPUT,MAKEFILE);
  1341. // Get the language to use for linking this executable.
  1342. const char* linkLanguage =
  1343. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1344. // Make sure we have a link language.
  1345. if(!linkLanguage)
  1346. {
  1347. cmSystemTools::Error("Cannot determine link language for target \"",
  1348. target.GetName(), "\".");
  1349. return;
  1350. }
  1351. // Add the link message.
  1352. std::string buildEcho = "Linking ";
  1353. buildEcho += linkLanguage;
  1354. buildEcho += " executable ";
  1355. buildEcho += targetOutPath;
  1356. this->AppendEcho(commands, buildEcho.c_str());
  1357. // Build a list of compiler flags and linker flags.
  1358. std::string flags;
  1359. std::string linkFlags;
  1360. // Add flags to create an executable.
  1361. this->AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS");
  1362. if(target.GetPropertyAsBool("WIN32_EXECUTABLE"))
  1363. {
  1364. this->AppendFlags(linkFlags,
  1365. m_Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  1366. }
  1367. else
  1368. {
  1369. this->AppendFlags(linkFlags,
  1370. m_Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  1371. }
  1372. // Add language-specific flags.
  1373. this->AddLanguageFlags(flags, linkLanguage);
  1374. // Add flags to deal with shared libraries. Any library being
  1375. // linked in might be shared, so always use shared flags for an
  1376. // executable.
  1377. this->AddSharedFlags(flags, linkLanguage, true);
  1378. // Add target-specific linker flags.
  1379. this->AppendFlags(linkFlags, target.GetProperty("LINK_FLAGS"));
  1380. // Add the pre-build and pre-link rules.
  1381. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  1382. this->AppendCustomCommands(commands, target.GetPreLinkCommands());
  1383. // Construct the main link rule.
  1384. std::string linkRuleVar = "CMAKE_";
  1385. linkRuleVar += linkLanguage;
  1386. linkRuleVar += "_LINK_EXECUTABLE";
  1387. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  1388. cmSystemTools::ExpandListArgument(linkRule, commands);
  1389. // Add the post-build rules.
  1390. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  1391. // Collect up flags to link in needed libraries.
  1392. cmOStringStream linklibs;
  1393. this->OutputLinkLibraries(linklibs, 0, target);
  1394. // Construct object file lists that may be needed to expand the
  1395. // rule.
  1396. std::string variableName;
  1397. std::string variableNameExternal;
  1398. this->WriteObjectsVariable(ruleFileStream, target, objects, external_objects,
  1399. variableName, variableNameExternal);
  1400. std::string buildObjs = "$(";
  1401. buildObjs += variableName;
  1402. buildObjs += ") $(";
  1403. buildObjs += variableNameExternal;
  1404. buildObjs += ")";
  1405. std::string cleanObjs = "$(";
  1406. cleanObjs += variableName;
  1407. cleanObjs += ")";
  1408. // Expand placeholders in the commands.
  1409. for(std::vector<std::string>::iterator i = commands.begin();
  1410. i != commands.end(); ++i)
  1411. {
  1412. this->ExpandRuleVariables(*i,
  1413. linkLanguage,
  1414. buildObjs.c_str(),
  1415. targetOutPath.c_str(),
  1416. linklibs.str().c_str(),
  1417. 0,
  1418. 0,
  1419. flags.c_str(),
  1420. 0,
  1421. 0,
  1422. 0,
  1423. linkFlags.c_str());
  1424. }
  1425. // Write the build rule.
  1426. this->WriteMakeRule(ruleFileStream, 0,
  1427. targetFullPath.c_str(), depends, commands);
  1428. // Write convenience targets.
  1429. std::string dir = m_Makefile->GetStartOutputDirectory();
  1430. dir += "/";
  1431. dir += this->GetTargetDirectory(target);
  1432. std::string buildTargetRuleName = dir;
  1433. buildTargetRuleName += "/build";
  1434. buildTargetRuleName =
  1435. this->Convert(buildTargetRuleName.c_str(),HOME_OUTPUT,MAKEFILE);
  1436. this->WriteConvenienceRule(ruleFileStream, targetFullPath.c_str(),
  1437. buildTargetRuleName.c_str());
  1438. // Write clean target
  1439. cleanFiles.push_back(cleanObjs);
  1440. cleanFiles.push_back(targetOutPath.c_str());
  1441. this->WriteTargetCleanRule(ruleFileName, target, cleanFiles, objects, external_objects);
  1442. // Write the driving make target.
  1443. this->WriteTargetRequiresRule(ruleFileStream, target, objects);
  1444. }
  1445. //----------------------------------------------------------------------------
  1446. void
  1447. cmLocalUnixMakefileGenerator3
  1448. ::WriteStaticLibraryRule(std::ostream& ruleFileStream,
  1449. const char* ruleFileName,
  1450. const cmTarget& target,
  1451. const std::vector<std::string>& objects,
  1452. const std::vector<std::string>& external_objects,
  1453. const std::vector<std::string>& provides_requires)
  1454. {
  1455. const char* linkLanguage =
  1456. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1457. std::string linkRuleVar = "CMAKE_";
  1458. linkRuleVar += linkLanguage;
  1459. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  1460. std::string extraFlags;
  1461. this->AppendFlags(extraFlags, target.GetProperty("STATIC_LIBRARY_FLAGS"));
  1462. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1463. objects, external_objects,
  1464. linkRuleVar.c_str(), extraFlags.c_str(),
  1465. provides_requires);
  1466. }
  1467. //----------------------------------------------------------------------------
  1468. void
  1469. cmLocalUnixMakefileGenerator3
  1470. ::WriteSharedLibraryRule(std::ostream& ruleFileStream,
  1471. const char* ruleFileName,
  1472. const cmTarget& target,
  1473. const std::vector<std::string>& objects,
  1474. const std::vector<std::string>& external_objects,
  1475. const std::vector<std::string>& provides_requires)
  1476. {
  1477. const char* linkLanguage =
  1478. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1479. std::string linkRuleVar = "CMAKE_";
  1480. linkRuleVar += linkLanguage;
  1481. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  1482. std::string extraFlags;
  1483. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1484. this->AddConfigVariableFlags(extraFlags, "CMAKE_SHARED_LINKER_FLAGS");
  1485. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1486. {
  1487. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1488. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1489. i != sources.end(); ++i)
  1490. {
  1491. if((*i)->GetSourceExtension() == "def")
  1492. {
  1493. extraFlags += " ";
  1494. extraFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1495. extraFlags +=
  1496. this->Convert((*i)->GetFullPath().c_str(),HOME_OUTPUT,MAKEFILE);
  1497. }
  1498. }
  1499. }
  1500. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1501. objects, external_objects,
  1502. linkRuleVar.c_str(), extraFlags.c_str(),
  1503. provides_requires);
  1504. }
  1505. //----------------------------------------------------------------------------
  1506. void
  1507. cmLocalUnixMakefileGenerator3
  1508. ::WriteModuleLibraryRule(std::ostream& ruleFileStream,
  1509. const char* ruleFileName,
  1510. const cmTarget& target,
  1511. const std::vector<std::string>& objects,
  1512. const std::vector<std::string>& external_objects,
  1513. const std::vector<std::string>& provides_requires)
  1514. {
  1515. const char* linkLanguage =
  1516. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1517. std::string linkRuleVar = "CMAKE_";
  1518. linkRuleVar += linkLanguage;
  1519. linkRuleVar += "_CREATE_SHARED_MODULE";
  1520. std::string extraFlags;
  1521. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1522. this->AddConfigVariableFlags(extraFlags, "CMAKE_MODULE_LINKER_FLAGS");
  1523. // TODO: .def files should be supported here also.
  1524. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1525. objects, external_objects,
  1526. linkRuleVar.c_str(), extraFlags.c_str(),
  1527. provides_requires);
  1528. }
  1529. //----------------------------------------------------------------------------
  1530. void
  1531. cmLocalUnixMakefileGenerator3
  1532. ::WriteLibraryRule(std::ostream& ruleFileStream,
  1533. const char* ruleFileName,
  1534. const cmTarget& target,
  1535. const std::vector<std::string>& objects,
  1536. const std::vector<std::string>& external_objects,
  1537. const char* linkRuleVar,
  1538. const char* extraFlags,
  1539. const std::vector<std::string>& provides_requires)
  1540. {
  1541. // Write the dependency generation rule.
  1542. this->WriteTargetDependRule(ruleFileStream, target, objects);
  1543. // TODO: Merge the methods that call this method to avoid
  1544. // code duplication.
  1545. std::vector<std::string> commands;
  1546. std::string relPath = this->GetHomeRelativeOutputPath();
  1547. std::string objTarget;
  1548. // Build list of dependencies.
  1549. std::vector<std::string> depends;
  1550. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1551. obj != objects.end(); ++obj)
  1552. {
  1553. objTarget = relPath;
  1554. objTarget += *obj;
  1555. depends.push_back(objTarget);
  1556. }
  1557. // Add dependencies on targets that must be built first.
  1558. this->AppendTargetDepends(depends, target);
  1559. // Add a dependency on the rule file itself.
  1560. objTarget = relPath;
  1561. objTarget += ruleFileName;
  1562. this->AppendRuleDepend(depends, objTarget.c_str());
  1563. std::vector<std::string> cleanFiles;
  1564. // from here up is the same for exe or lib
  1565. for(std::vector<std::string>::const_iterator obj = external_objects.begin();
  1566. obj != external_objects.end(); ++obj)
  1567. {
  1568. depends.push_back(*obj);
  1569. }
  1570. // Get the language to use for linking this library.
  1571. const char* linkLanguage =
  1572. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1573. // Make sure we have a link language.
  1574. if(!linkLanguage)
  1575. {
  1576. cmSystemTools::Error("Cannot determine link language for target \"",
  1577. target.GetName(), "\".");
  1578. return;
  1579. }
  1580. // Create set of linking flags.
  1581. std::string linkFlags;
  1582. this->AppendFlags(linkFlags, extraFlags);
  1583. // Construct the name of the library.
  1584. std::string targetName;
  1585. std::string targetNameSO;
  1586. std::string targetNameReal;
  1587. std::string targetNameBase;
  1588. target.GetLibraryNames(m_Makefile,
  1589. targetName, targetNameSO,
  1590. targetNameReal, targetNameBase);
  1591. // Construct the full path version of the names.
  1592. std::string outpath = m_LibraryOutputPath;
  1593. if(outpath.length() == 0)
  1594. {
  1595. outpath = m_Makefile->GetStartOutputDirectory();
  1596. outpath += "/";
  1597. }
  1598. std::string targetFullPath = outpath + targetName;
  1599. std::string targetFullPathSO = outpath + targetNameSO;
  1600. std::string targetFullPathReal = outpath + targetNameReal;
  1601. std::string targetFullPathBase = outpath + targetNameBase;
  1602. // Construct the output path version of the names for use in command
  1603. // arguments.
  1604. std::string targetOutPath =
  1605. this->Convert(targetFullPath.c_str(),HOME_OUTPUT,MAKEFILE);
  1606. std::string targetOutPathSO =
  1607. this->Convert(targetFullPathSO.c_str(),HOME_OUTPUT,MAKEFILE);
  1608. std::string targetOutPathReal =
  1609. this->Convert(targetFullPathReal.c_str(),HOME_OUTPUT,MAKEFILE);
  1610. std::string targetOutPathBase =
  1611. this->Convert(targetFullPathBase.c_str(),HOME_OUTPUT,MAKEFILE);
  1612. // Add the link message.
  1613. std::string buildEcho = "Linking ";
  1614. buildEcho += linkLanguage;
  1615. switch(target.GetType())
  1616. {
  1617. case cmTarget::STATIC_LIBRARY:
  1618. buildEcho += " static library "; break;
  1619. case cmTarget::SHARED_LIBRARY:
  1620. buildEcho += " shared library "; break;
  1621. case cmTarget::MODULE_LIBRARY:
  1622. buildEcho += " shared module "; break;
  1623. default:
  1624. buildEcho += " library "; break;
  1625. }
  1626. buildEcho += targetOutPath.c_str();
  1627. this->AppendEcho(commands, buildEcho.c_str());
  1628. // Construct a list of files associated with this library that may
  1629. // need to be cleaned.
  1630. {
  1631. std::string cleanStaticName;
  1632. std::string cleanSharedName;
  1633. std::string cleanSharedSOName;
  1634. std::string cleanSharedRealName;
  1635. target.GetLibraryCleanNames(m_Makefile,
  1636. cleanStaticName,
  1637. cleanSharedName,
  1638. cleanSharedSOName,
  1639. cleanSharedRealName);
  1640. std::string cleanFullStaticName = outpath + cleanStaticName;
  1641. std::string cleanFullSharedName = outpath + cleanSharedName;
  1642. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  1643. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  1644. cleanFiles.push_back(cleanFullStaticName);
  1645. if(cleanSharedRealName != cleanStaticName)
  1646. {
  1647. cleanFiles.push_back(cleanFullSharedRealName);
  1648. }
  1649. if(cleanSharedSOName != cleanStaticName &&
  1650. cleanSharedSOName != cleanSharedRealName)
  1651. {
  1652. cleanFiles.push_back(cleanFullSharedSOName);
  1653. }
  1654. if(cleanSharedName != cleanStaticName &&
  1655. cleanSharedName != cleanSharedSOName &&
  1656. cleanSharedName != cleanSharedRealName)
  1657. {
  1658. cleanFiles.push_back(cleanFullSharedName);
  1659. }
  1660. }
  1661. // Add a command to remove any existing files for this library.
  1662. this->AppendCleanCommand(commands, cleanFiles);
  1663. // Add the pre-build and pre-link rules.
  1664. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  1665. this->AppendCustomCommands(commands, target.GetPreLinkCommands());
  1666. // Construct the main link rule.
  1667. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar);
  1668. cmSystemTools::ExpandListArgument(linkRule, commands);
  1669. // Add a rule to create necessary symlinks for the library.
  1670. if(targetOutPath != targetOutPathReal)
  1671. {
  1672. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  1673. symlink += targetOutPathReal;
  1674. symlink += " ";
  1675. symlink += targetOutPathSO;
  1676. symlink += " ";
  1677. symlink += targetOutPath;
  1678. commands.push_back(symlink);
  1679. }
  1680. // Add the post-build rules.
  1681. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  1682. // Collect up flags to link in needed libraries.
  1683. cmOStringStream linklibs;
  1684. this->OutputLinkLibraries(linklibs, target.GetName(), target);
  1685. // Construct object file lists that may be needed to expand the
  1686. // rule.
  1687. std::string variableName;
  1688. std::string variableNameExternal;
  1689. this->WriteObjectsVariable(ruleFileStream, target, objects, external_objects,
  1690. variableName, variableNameExternal);
  1691. std::string buildObjs = "$(";
  1692. buildObjs += variableName;
  1693. buildObjs += ") $(";
  1694. buildObjs += variableNameExternal;
  1695. buildObjs += ")";
  1696. std::string cleanObjs = "$(";
  1697. cleanObjs += variableName;
  1698. cleanObjs += ")";
  1699. // Expand placeholders in the commands.
  1700. for(std::vector<std::string>::iterator i = commands.begin();
  1701. i != commands.end(); ++i)
  1702. {
  1703. this->ExpandRuleVariables(*i,
  1704. linkLanguage,
  1705. buildObjs.c_str(),
  1706. targetOutPathReal.c_str(),
  1707. linklibs.str().c_str(),
  1708. 0, 0, 0, buildObjs.c_str(),
  1709. targetOutPathBase.c_str(),
  1710. targetNameSO.c_str(),
  1711. linkFlags.c_str());
  1712. }
  1713. // from here down is the same for exe or lib
  1714. // Write the build rule.
  1715. this->WriteMakeRule(ruleFileStream, 0,
  1716. targetFullPath.c_str(), depends, commands);
  1717. // Write convenience targets.
  1718. std::string dir = m_Makefile->GetStartOutputDirectory();
  1719. dir += "/";
  1720. dir += this->GetTargetDirectory(target);
  1721. std::string buildTargetRuleName = dir;
  1722. buildTargetRuleName += "/build";
  1723. buildTargetRuleName =
  1724. this->Convert(buildTargetRuleName.c_str(),HOME_OUTPUT,MAKEFILE);
  1725. this->WriteConvenienceRule(ruleFileStream, targetFullPath.c_str(),
  1726. buildTargetRuleName.c_str());
  1727. // Write clean target
  1728. cleanFiles.push_back(cleanObjs);
  1729. cleanFiles.push_back(targetOutPath.c_str());
  1730. this->WriteTargetCleanRule(ruleFileName, target, cleanFiles, objects, external_objects);
  1731. // Write the driving make target.
  1732. this->WriteTargetRequiresRule(ruleFileStream, target, objects);
  1733. }
  1734. //----------------------------------------------------------------------------
  1735. void
  1736. cmLocalUnixMakefileGenerator3
  1737. ::WriteObjectsVariable(std::ostream& ruleFileStream,
  1738. const cmTarget& target,
  1739. const std::vector<std::string>& objects,
  1740. const std::vector<std::string>& external_objects,
  1741. std::string& variableName,
  1742. std::string& variableNameExternal)
  1743. {
  1744. // Write a make variable assignment that lists all objects for the
  1745. // target.
  1746. variableName = this->CreateMakeVariable(target.GetName(), "_OBJECTS");
  1747. ruleFileStream
  1748. << "# Object files for target " << target.GetName() << "\n"
  1749. << variableName.c_str() << " =";
  1750. std::string relPath = this->GetHomeRelativeOutputPath();
  1751. std::string object;
  1752. for(std::vector<std::string>::const_iterator i = objects.begin();
  1753. i != objects.end(); ++i)
  1754. {
  1755. object = relPath;
  1756. object += *i;
  1757. ruleFileStream
  1758. << " \\\n"
  1759. << this->ConvertToQuotedOutputPath(object.c_str());
  1760. }
  1761. ruleFileStream
  1762. << "\n";
  1763. // Write a make variable assignment that lists all external objects
  1764. // for the target.
  1765. variableNameExternal = this->CreateMakeVariable(target.GetName(),
  1766. "_EXTERNAL_OBJECTS");
  1767. ruleFileStream
  1768. << "\n"
  1769. << "# External object files for target " << target.GetName() << "\n"
  1770. << variableNameExternal.c_str() << " =";
  1771. for(std::vector<std::string>::const_iterator i = external_objects.begin();
  1772. i != external_objects.end(); ++i)
  1773. {
  1774. object = this->Convert(i->c_str(),HOME_OUTPUT);
  1775. ruleFileStream
  1776. << " \\\n"
  1777. << this->ConvertToQuotedOutputPath(object.c_str());
  1778. }
  1779. ruleFileStream
  1780. << "\n"
  1781. << "\n";
  1782. }
  1783. //----------------------------------------------------------------------------
  1784. void
  1785. cmLocalUnixMakefileGenerator3
  1786. ::WriteTargetDependRule(std::ostream& ruleFileStream,
  1787. const cmTarget& target,
  1788. const std::vector<std::string>& objects)
  1789. {
  1790. std::vector<std::string> depends;
  1791. std::vector<std::string> no_commands;
  1792. // Construct the name of the dependency generation target.
  1793. std::string depTarget = this->GetRelativeTargetDirectory(target);
  1794. depTarget += "/depend";
  1795. // This target drives dependency generation for all object files.
  1796. std::string relPath = this->GetHomeRelativeOutputPath();
  1797. std::string objTarget;
  1798. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1799. obj != objects.end(); ++obj)
  1800. {
  1801. objTarget = relPath;
  1802. objTarget += *obj;
  1803. objTarget += ".depend";
  1804. depends.push_back(objTarget);
  1805. }
  1806. // Write the rule.
  1807. this->WriteMakeRule(ruleFileStream, 0,
  1808. depTarget.c_str(), depends, no_commands);
  1809. }
  1810. //----------------------------------------------------------------------------
  1811. void
  1812. cmLocalUnixMakefileGenerator3
  1813. ::WriteTargetCleanRule(const char *ruleFileName,
  1814. const cmTarget& target,
  1815. const std::vector<std::string>& files,
  1816. const std::vector<std::string>& objects,
  1817. const std::vector<std::string>& external_objects)
  1818. {
  1819. std::string dir = cmSystemTools::GetFilenamePath(ruleFileName);
  1820. std::string cleanFileName = dir;
  1821. cleanFileName += "/clean.make";
  1822. // Open the rule file. This should be copy-if-different because the
  1823. // rules may depend on this file itself.
  1824. std::string ruleFileNameFull = this->ConvertToFullPath(cleanFileName);
  1825. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1826. ruleFileStream.SetCopyIfDifferent(true);
  1827. if(!ruleFileStream)
  1828. {
  1829. return;
  1830. }
  1831. this->WriteDisclaimer(ruleFileStream);
  1832. std::string variableName;
  1833. std::string variableNameExternal;
  1834. this->WriteObjectsVariable(ruleFileStream, target, objects, external_objects,
  1835. variableName, variableNameExternal);
  1836. std::vector<std::string> no_depends;
  1837. std::vector<std::string> commands;
  1838. // Construct the clean target name.
  1839. std::string cleanTarget = this->GetRelativeTargetDirectory(target);
  1840. cleanTarget += "/clean";
  1841. // Construct the clean command.
  1842. this->AppendCleanCommand(commands, files);
  1843. // Write the rule.
  1844. this->WriteMakeRule(ruleFileStream, 0,
  1845. cleanTarget.c_str(), no_depends, commands);
  1846. }
  1847. //----------------------------------------------------------------------------
  1848. void
  1849. cmLocalUnixMakefileGenerator3
  1850. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1851. {
  1852. // Write the given string to the stream with escaping to get it back
  1853. // into CMake through the lexical scanner.
  1854. os << "\"";
  1855. for(const char* c = s; *c; ++c)
  1856. {
  1857. if(*c == '\\')
  1858. {
  1859. os << "\\\\";
  1860. }
  1861. else if(*c == '"')
  1862. {
  1863. os << "\\\"";
  1864. }
  1865. else
  1866. {
  1867. os << *c;
  1868. }
  1869. }
  1870. os << "\"";
  1871. }
  1872. //----------------------------------------------------------------------------
  1873. std::string
  1874. cmLocalUnixMakefileGenerator3::GetTargetDirectory(const cmTarget& target)
  1875. {
  1876. std::string dir = target.GetName();
  1877. dir += ".dir";
  1878. return dir;
  1879. }
  1880. //----------------------------------------------------------------------------
  1881. std::string
  1882. cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(const cmTarget& target)
  1883. {
  1884. std::string dir = m_Makefile->GetStartOutputDirectory();
  1885. dir += "/";
  1886. dir += this->GetTargetDirectory(target);
  1887. dir = cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(), dir.c_str());
  1888. return this->Convert(dir.c_str(),NONE,MAKEFILE);
  1889. }
  1890. //----------------------------------------------------------------------------
  1891. std::string
  1892. cmLocalUnixMakefileGenerator3
  1893. ::GetSubdirTargetName(const char* pass, const char* subdir)
  1894. {
  1895. // Convert the subdirectory name to a relative path to keep it short.
  1896. std::string reldir = this->Convert(subdir,START_OUTPUT);
  1897. // Convert the subdirectory name to a valid make target name.
  1898. std::string s = pass;
  1899. s += "_";
  1900. s += reldir;
  1901. // Replace "../" with 3 underscores. This allows one .. at the beginning.
  1902. size_t pos = s.find("../");
  1903. if(pos != std::string::npos)
  1904. {
  1905. s.replace(pos, 3, "___");
  1906. }
  1907. // Replace "/" directory separators with a single underscore.
  1908. while((pos = s.find('/')) != std::string::npos)
  1909. {
  1910. s.replace(pos, 1, "_");
  1911. }
  1912. // Replace ":" drive specifier with a single underscore
  1913. while((pos = s.find(':')) != std::string::npos)
  1914. {
  1915. s.replace(pos, 1, "_");
  1916. }
  1917. return s;
  1918. }
  1919. //----------------------------------------------------------------------------
  1920. std::string
  1921. cmLocalUnixMakefileGenerator3
  1922. ::GetObjectFileName(const cmTarget& target,
  1923. const cmSourceFile& source)
  1924. {
  1925. // If the full path to the source file includes this directory,
  1926. // we want to use the relative path for the filename of the
  1927. // object file. Otherwise, we will use just the filename
  1928. // portion.
  1929. std::string objectName;
  1930. if((cmSystemTools::GetFilenamePath(
  1931. source.GetFullPath()).find(
  1932. m_Makefile->GetCurrentDirectory()) == 0)
  1933. || (cmSystemTools::GetFilenamePath(
  1934. source.GetFullPath()).find(
  1935. m_Makefile->GetStartOutputDirectory()) == 0))
  1936. {
  1937. objectName = source.GetSourceName();
  1938. }
  1939. else
  1940. {
  1941. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  1942. }
  1943. // Append the object file extension.
  1944. objectName +=
  1945. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1946. source.GetSourceExtension().c_str());
  1947. // Convert to a safe name.
  1948. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  1949. // Prepend the target directory.
  1950. std::string obj = this->GetTargetDirectory(target);
  1951. obj += "/";
  1952. obj += objectName;
  1953. return obj;
  1954. }
  1955. //----------------------------------------------------------------------------
  1956. const char*
  1957. cmLocalUnixMakefileGenerator3
  1958. ::GetSourceFileLanguage(const cmSourceFile& source)
  1959. {
  1960. // Identify the language of the source file.
  1961. return (m_GlobalGenerator
  1962. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  1963. }
  1964. //----------------------------------------------------------------------------
  1965. std::string
  1966. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1967. {
  1968. // Split the path into its components.
  1969. std::vector<std::string> components;
  1970. cmSystemTools::SplitPath(p, components);
  1971. // Return an empty path if there are no components.
  1972. if(components.empty())
  1973. {
  1974. return "\"\"";
  1975. }
  1976. // Choose a slash direction and fix root component.
  1977. const char* slash = "/";
  1978. #if defined(_WIN32) && !defined(__CYGWIN__)
  1979. if(!cmSystemTools::GetForceUnixPaths())
  1980. {
  1981. slash = "\\";
  1982. for(std::string::iterator i = components[0].begin();
  1983. i != components[0].end(); ++i)
  1984. {
  1985. if(*i == '/')
  1986. {
  1987. *i = '\\';
  1988. }
  1989. }
  1990. }
  1991. #endif
  1992. // Begin the quoted result with the root component.
  1993. std::string result = "\"";
  1994. result += components[0];
  1995. // Now add the rest of the components separated by the proper slash
  1996. // direction for this platform.
  1997. bool first = true;
  1998. for(unsigned int i=1; i < components.size(); ++i)
  1999. {
  2000. // Only the last component can be empty to avoid double slashes.
  2001. if(components[i].length() > 0 || (i == (components.size()-1)))
  2002. {
  2003. if(!first)
  2004. {
  2005. result += slash;
  2006. }
  2007. result += components[i];
  2008. first = false;
  2009. }
  2010. }
  2011. // Close the quoted result.
  2012. result += "\"";
  2013. return result;
  2014. }
  2015. //----------------------------------------------------------------------------
  2016. void
  2017. cmLocalUnixMakefileGenerator3
  2018. ::AppendTargetDepends(std::vector<std::string>& depends,
  2019. const cmTarget& target)
  2020. {
  2021. // Do not bother with dependencies for static libraries.
  2022. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  2023. {
  2024. return;
  2025. }
  2026. // Keep track of dependencies already listed.
  2027. std::set<cmStdString> emitted;
  2028. // A target should not depend on itself.
  2029. emitted.insert(target.GetName());
  2030. // Loop over all library dependencies.
  2031. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  2032. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  2033. lib != tlibs.end(); ++lib)
  2034. {
  2035. // Don't emit the same library twice for this target.
  2036. if(emitted.insert(lib->first).second)
  2037. {
  2038. // Add this dependency.
  2039. this->AppendAnyDepend(depends, lib->first.c_str());
  2040. }
  2041. }
  2042. // Loop over all utility dependencies.
  2043. const std::set<cmStdString>& tutils = target.GetUtilities();
  2044. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  2045. util != tutils.end(); ++util)
  2046. {
  2047. // Don't emit the same utility twice for this target.
  2048. if(emitted.insert(*util).second)
  2049. {
  2050. // Add this dependency.
  2051. this->AppendAnyDepend(depends, util->c_str());
  2052. }
  2053. }
  2054. }
  2055. //----------------------------------------------------------------------------
  2056. void
  2057. cmLocalUnixMakefileGenerator3
  2058. ::AppendAnyDepend(std::vector<std::string>& depends, const char* name,
  2059. bool assume_unknown_is_file)
  2060. {
  2061. // There are a few cases for the name of the target:
  2062. // - CMake target in this directory: depend on it.
  2063. // - CMake target in another directory: depend and add jump-and-build.
  2064. // - Full path to a file: depend on it.
  2065. // - Other format (like -lm): do nothing.
  2066. // If it is an executable or library target there will be a
  2067. // definition for it.
  2068. std::string dirVar = name;
  2069. dirVar += "_CMAKE_PATH";
  2070. const char* dir = m_Makefile->GetDefinition(dirVar.c_str());
  2071. if(dir && *dir)
  2072. {
  2073. // This is a CMake target somewhere in this project.
  2074. // Get the type of the library. If it does not have a type then
  2075. // it is an executable.
  2076. std::string typeVar = name;
  2077. typeVar += "_LIBRARY_TYPE";
  2078. const char* libType = m_Makefile->GetDefinition(typeVar.c_str());
  2079. // Get the output path for this target type.
  2080. std::string tgtOutputPath;
  2081. if(libType)
  2082. {
  2083. tgtOutputPath = m_LibraryOutputPath;
  2084. }
  2085. else
  2086. {
  2087. tgtOutputPath = m_ExecutableOutputPath;
  2088. }
  2089. // Get the path to the target.
  2090. std::string tgtPath;
  2091. if(tgtOutputPath.size())
  2092. {
  2093. tgtPath = tgtOutputPath;
  2094. }
  2095. else
  2096. {
  2097. tgtPath = dir;
  2098. tgtPath += "/";
  2099. }
  2100. // Add the name of the targets's file. This depends on the type
  2101. // of the target.
  2102. std::string prefix;
  2103. std::string suffix;
  2104. if(!libType)
  2105. {
  2106. suffix = cmSystemTools::GetExecutableExtension();
  2107. }
  2108. else if(strcmp(libType, "SHARED") == 0)
  2109. {
  2110. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  2111. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  2112. }
  2113. else if(strcmp(libType, "MODULE") == 0)
  2114. {
  2115. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  2116. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  2117. }
  2118. else if(strcmp(libType, "STATIC") == 0)
  2119. {
  2120. prefix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  2121. suffix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  2122. }
  2123. tgtPath += prefix;
  2124. tgtPath += name;
  2125. tgtPath += suffix;
  2126. // Add a dependency on the target.
  2127. depends.push_back(tgtPath.c_str());
  2128. }
  2129. else if(m_Makefile->GetTargets().find(name) !=
  2130. m_Makefile->GetTargets().end())
  2131. {
  2132. // This is a CMake target that is not an executable or library.
  2133. // It must be in this directory, so just depend on the name
  2134. // directly.
  2135. depends.push_back(name);
  2136. }
  2137. else if(cmSystemTools::FileIsFullPath(name))
  2138. {
  2139. // This is a path to a file. Just trust the listfile author that
  2140. // it will be present or there is a rule to build it.
  2141. depends.push_back(cmSystemTools::CollapseFullPath(name));
  2142. }
  2143. else if(assume_unknown_is_file)
  2144. {
  2145. // Just assume this is a file or make target that will be present.
  2146. depends.push_back(name);
  2147. }
  2148. }
  2149. //----------------------------------------------------------------------------
  2150. void
  2151. cmLocalUnixMakefileGenerator3
  2152. ::AppendRuleDepend(std::vector<std::string>& depends,
  2153. const char* ruleFileName)
  2154. {
  2155. // Add a dependency on the rule file itself unless an option to skip
  2156. // it is specifically enabled by the user or project.
  2157. const char* nodep = m_Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  2158. if(!nodep || cmSystemTools::IsOff(nodep))
  2159. {
  2160. depends.push_back(ruleFileName);
  2161. }
  2162. }
  2163. //----------------------------------------------------------------------------
  2164. void
  2165. cmLocalUnixMakefileGenerator3
  2166. ::AppendCustomDepends(std::vector<std::string>& depends,
  2167. const std::vector<cmCustomCommand>& ccs)
  2168. {
  2169. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  2170. i != ccs.end(); ++i)
  2171. {
  2172. this->AppendCustomDepend(depends, *i);
  2173. }
  2174. }
  2175. //----------------------------------------------------------------------------
  2176. void
  2177. cmLocalUnixMakefileGenerator3
  2178. ::AppendCustomDepend(std::vector<std::string>& depends,
  2179. const cmCustomCommand& cc)
  2180. {
  2181. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  2182. d != cc.GetDepends().end(); ++d)
  2183. {
  2184. // Add this dependency.
  2185. this->AppendAnyDepend(depends, d->c_str(), true);
  2186. }
  2187. }
  2188. //----------------------------------------------------------------------------
  2189. void
  2190. cmLocalUnixMakefileGenerator3
  2191. ::AppendCustomCommands(std::vector<std::string>& commands,
  2192. const std::vector<cmCustomCommand>& ccs)
  2193. {
  2194. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  2195. i != ccs.end(); ++i)
  2196. {
  2197. this->AppendCustomCommand(commands, *i);
  2198. }
  2199. }
  2200. //----------------------------------------------------------------------------
  2201. void
  2202. cmLocalUnixMakefileGenerator3
  2203. ::AppendCustomCommand(std::vector<std::string>& commands,
  2204. const cmCustomCommand& cc)
  2205. {
  2206. // TODO: Convert outputs/dependencies (arguments?) to relative paths.
  2207. // Add each command line to the set of commands.
  2208. for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
  2209. cl != cc.GetCommandLines().end(); ++cl)
  2210. {
  2211. // Build the command line in a single string.
  2212. const cmCustomCommandLine& commandLine = *cl;
  2213. std::string cmd = commandLine[0];
  2214. cmSystemTools::ReplaceString(cmd, "/./", "/");
  2215. cmd = this->Convert(cmd.c_str(),HOME_OUTPUT);
  2216. if(cmd.find("/") == cmd.npos &&
  2217. commandLine[0].find("/") != cmd.npos)
  2218. {
  2219. // Add a leading "./" for executables in the current directory.
  2220. cmd = "./" + cmd;
  2221. }
  2222. cmd = this->Convert(cmd.c_str(),NONE,SHELL);
  2223. for(unsigned int j=1; j < commandLine.size(); ++j)
  2224. {
  2225. cmd += " ";
  2226. cmd += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  2227. }
  2228. commands.push_back(cmd);
  2229. }
  2230. }
  2231. //----------------------------------------------------------------------------
  2232. void
  2233. cmLocalUnixMakefileGenerator3
  2234. ::AppendCleanCommand(std::vector<std::string>& commands,
  2235. const std::vector<std::string>& files)
  2236. {
  2237. if(!files.empty())
  2238. {
  2239. std::string remove = "$(CMAKE_COMMAND) -E remove -f";
  2240. for(std::vector<std::string>::const_iterator f = files.begin();
  2241. f != files.end(); ++f)
  2242. {
  2243. remove += " ";
  2244. remove += this->Convert(f->c_str(),START_OUTPUT,SHELL);
  2245. }
  2246. commands.push_back(remove);
  2247. }
  2248. }
  2249. //----------------------------------------------------------------------------
  2250. void
  2251. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  2252. const char* text)
  2253. {
  2254. // Echo one line at a time.
  2255. std::string line;
  2256. for(const char* c = text;; ++c)
  2257. {
  2258. if(*c == '\n' || *c == '\0')
  2259. {
  2260. // Avoid writing a blank last line on end-of-string.
  2261. if(*c != '\0' || !line.empty())
  2262. {
  2263. // Add a command to echo this line.
  2264. std::string cmd = "@echo ";
  2265. if(m_EchoNeedsQuote)
  2266. {
  2267. cmd += "\"";
  2268. }
  2269. cmd += line;
  2270. if(m_EchoNeedsQuote)
  2271. {
  2272. cmd += "\"";
  2273. }
  2274. commands.push_back(cmd);
  2275. }
  2276. // Reset the line to emtpy.
  2277. line = "";
  2278. // Terminate on end-of-string.
  2279. if(*c == '\0')
  2280. {
  2281. return;
  2282. }
  2283. }
  2284. else if(*c != '\r')
  2285. {
  2286. // Append this character to the current line.
  2287. line += *c;
  2288. }
  2289. }
  2290. }
  2291. //============================================================================
  2292. //----------------------------------------------------------------------------
  2293. bool
  2294. cmLocalUnixMakefileGenerator3::SamePath(const char* path1, const char* path2)
  2295. {
  2296. if (strcmp(path1, path2) == 0)
  2297. {
  2298. return true;
  2299. }
  2300. #if defined(_WIN32) || defined(__APPLE__)
  2301. return
  2302. (cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  2303. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2)));
  2304. #else
  2305. return false;
  2306. #endif
  2307. }
  2308. //----------------------------------------------------------------------------
  2309. // take a tgt path and convert it into a make target, it could be full, or relative
  2310. std::string
  2311. cmLocalUnixMakefileGenerator3
  2312. ::ConvertToMakeTarget(const char* tgt)
  2313. {
  2314. // Make targets should not have a leading './' for a file in the
  2315. // directory containing the makefile.
  2316. std::string ret = tgt;
  2317. if(ret.size() > 2 && (ret[0] == '.') &&
  2318. ( (ret[1] == '/') || ret[1] == '\\'))
  2319. {
  2320. std::string upath = ret;
  2321. cmSystemTools::ConvertToUnixSlashes(upath);
  2322. if(upath.find(2, '/') == upath.npos)
  2323. {
  2324. ret = ret.substr(2, ret.size()-2);
  2325. }
  2326. }
  2327. return ret;
  2328. }
  2329. //----------------------------------------------------------------------------
  2330. std::string&
  2331. cmLocalUnixMakefileGenerator3::CreateSafeUniqueObjectFileName(const char* sin)
  2332. {
  2333. if ( m_Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES") )
  2334. {
  2335. std::map<cmStdString,cmStdString>::iterator it = m_UniqueObjectNamesMap.find(sin);
  2336. if ( it == m_UniqueObjectNamesMap.end() )
  2337. {
  2338. std::string ssin = sin;
  2339. bool done;
  2340. int cc = 0;
  2341. char rpstr[100];
  2342. sprintf(rpstr, "_p_");
  2343. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2344. std::string sssin = sin;
  2345. do
  2346. {
  2347. done = true;
  2348. for ( it = m_UniqueObjectNamesMap.begin();
  2349. it != m_UniqueObjectNamesMap.end();
  2350. ++ it )
  2351. {
  2352. if ( it->second == ssin )
  2353. {
  2354. done = false;
  2355. }
  2356. }
  2357. if ( done )
  2358. {
  2359. break;
  2360. }
  2361. sssin = ssin;
  2362. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2363. sprintf(rpstr, "_p%d_", cc++);
  2364. }
  2365. while ( !done );
  2366. m_UniqueObjectNamesMap[sin] = ssin;
  2367. }
  2368. }
  2369. else
  2370. {
  2371. m_UniqueObjectNamesMap[sin] = sin;
  2372. }
  2373. return m_UniqueObjectNamesMap[sin];
  2374. }
  2375. //----------------------------------------------------------------------------
  2376. std::string
  2377. cmLocalUnixMakefileGenerator3
  2378. ::CreateMakeVariable(const char* sin, const char* s2in)
  2379. {
  2380. std::string s = sin;
  2381. std::string s2 = s2in;
  2382. std::string unmodified = s;
  2383. unmodified += s2;
  2384. // if there is no restriction on the length of make variables
  2385. // and there are no "." charactors in the string, then return the
  2386. // unmodified combination.
  2387. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  2388. {
  2389. return unmodified;
  2390. }
  2391. // see if the variable has been defined before and return
  2392. // the modified version of the variable
  2393. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2394. if(i != m_MakeVariableMap.end())
  2395. {
  2396. return i->second;
  2397. }
  2398. // start with the unmodified variable
  2399. std::string ret = unmodified;
  2400. // if this there is no value for m_MakefileVariableSize then
  2401. // the string must have bad characters in it
  2402. if(!m_MakefileVariableSize)
  2403. {
  2404. cmSystemTools::ReplaceString(ret, ".", "_");
  2405. int ni = 0;
  2406. char buffer[5];
  2407. // make sure the _ version is not already used, if
  2408. // it is used then add number to the end of the variable
  2409. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2410. {
  2411. ++ni;
  2412. sprintf(buffer, "%04d", ni);
  2413. ret = unmodified + buffer;
  2414. }
  2415. m_ShortMakeVariableMap[ret] = "1";
  2416. m_MakeVariableMap[unmodified] = ret;
  2417. return ret;
  2418. }
  2419. // if the string is greater the 32 chars it is an invalid vairable name
  2420. // for borland make
  2421. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2422. {
  2423. int keep = m_MakefileVariableSize - 8;
  2424. int size = keep + 3;
  2425. std::string str1 = s;
  2426. std::string str2 = s2;
  2427. // we must shorten the combined string by 4 charactors
  2428. // keep no more than 24 charactors from the second string
  2429. if(static_cast<int>(str2.size()) > keep)
  2430. {
  2431. str2 = str2.substr(0, keep);
  2432. }
  2433. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2434. {
  2435. str1 = str1.substr(0, size - str2.size());
  2436. }
  2437. char buffer[5];
  2438. int ni = 0;
  2439. sprintf(buffer, "%04d", ni);
  2440. ret = str1 + str2 + buffer;
  2441. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2442. {
  2443. ++ni;
  2444. sprintf(buffer, "%04d", ni);
  2445. ret = str1 + str2 + buffer;
  2446. }
  2447. if(ni == 1000)
  2448. {
  2449. cmSystemTools::Error("Borland makefile variable length too long");
  2450. return unmodified;
  2451. }
  2452. // once an unused variable is found
  2453. m_ShortMakeVariableMap[ret] = "1";
  2454. }
  2455. // always make an entry into the unmodified to variable map
  2456. m_MakeVariableMap[unmodified] = ret;
  2457. return ret;
  2458. }
  2459. //============================================================================
  2460. //----------------------------------------------------------------------------
  2461. cmDepends*
  2462. cmLocalUnixMakefileGenerator3::GetDependsChecker(const std::string& lang,
  2463. const char* dir,
  2464. const char* objFile,
  2465. bool verbose)
  2466. {
  2467. cmDepends *ret = 0;
  2468. if(lang == "C" || lang == "CXX" || lang == "RC")
  2469. {
  2470. ret = new cmDependsC();
  2471. }
  2472. #ifdef CMAKE_BUILD_WITH_CMAKE
  2473. else if(lang == "Fortran")
  2474. {
  2475. ret = new cmDependsFortran();
  2476. }
  2477. else if(lang == "Java")
  2478. {
  2479. ret = new cmDependsJava();
  2480. }
  2481. #endif
  2482. if (ret)
  2483. {
  2484. ret->SetTargetFile(dir, objFile, ".depend",".build.depend.make");
  2485. ret->SetVerbose(verbose);
  2486. }
  2487. return ret;
  2488. }
  2489. //----------------------------------------------------------------------------
  2490. bool
  2491. cmLocalUnixMakefileGenerator3
  2492. ::ScanDependencies(std::vector<std::string> const& args)
  2493. {
  2494. // Format of arguments is:
  2495. // $(CMAKE_COMMAND), cmake_depends, GeneratorName, <lang>, <obj>, <src>
  2496. // The caller has ensured that all required arguments exist.
  2497. // The language for which we are scanning dependencies.
  2498. std::string const& lang = args[3];
  2499. // The file to which to write dependencies.
  2500. const char* objFile = args[4].c_str();
  2501. // The source file at which to start the scan.
  2502. const char* srcFile = args[5].c_str();
  2503. // Read the directory information file.
  2504. cmake cm;
  2505. cmGlobalGenerator gg;
  2506. gg.SetCMakeInstance(&cm);
  2507. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  2508. lg->SetGlobalGenerator(&gg);
  2509. cmMakefile* mf = lg->GetMakefile();
  2510. bool haveDirectoryInfo = false;
  2511. if(mf->ReadListFile(0, "CMakeDirectoryInformation.cmake") &&
  2512. !cmSystemTools::GetErrorOccuredFlag())
  2513. {
  2514. haveDirectoryInfo = true;
  2515. }
  2516. // Test whether we need to force Unix paths.
  2517. if(haveDirectoryInfo)
  2518. {
  2519. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  2520. {
  2521. if(!cmSystemTools::IsOff(force))
  2522. {
  2523. cmSystemTools::SetForceUnixPaths(true);
  2524. }
  2525. }
  2526. }
  2527. // Get the set of include directories.
  2528. std::vector<std::string> includes;
  2529. if(haveDirectoryInfo)
  2530. {
  2531. std::string includePathVar = "CMAKE_";
  2532. includePathVar += lang;
  2533. includePathVar += "_INCLUDE_PATH";
  2534. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  2535. {
  2536. cmSystemTools::ExpandListArgument(includePath, includes);
  2537. }
  2538. }
  2539. // Get the include file regular expression.
  2540. std::string includeRegexScan = "^.*$";
  2541. std::string includeRegexComplain = "^$";
  2542. if(haveDirectoryInfo)
  2543. {
  2544. std::string scanRegexVar = "CMAKE_";
  2545. scanRegexVar += lang;
  2546. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  2547. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  2548. {
  2549. includeRegexScan = scanRegex;
  2550. }
  2551. std::string complainRegexVar = "CMAKE_";
  2552. complainRegexVar += lang;
  2553. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  2554. if(const char* complainRegex = mf->GetDefinition(complainRegexVar.c_str()))
  2555. {
  2556. includeRegexComplain = complainRegex;
  2557. }
  2558. }
  2559. // Dispatch the scan for each language.
  2560. if(lang == "C" || lang == "CXX" || lang == "RC")
  2561. {
  2562. // TODO: Handle RC (resource files) dependencies correctly.
  2563. cmDependsC scanner(srcFile, includes,
  2564. includeRegexScan.c_str(), includeRegexComplain.c_str());
  2565. scanner.SetTargetFile(".",objFile,".depend",".build.depend.make");
  2566. return scanner.Write();
  2567. }
  2568. #ifdef CMAKE_BUILD_WITH_CMAKE
  2569. else if(lang == "Fortran")
  2570. {
  2571. cmDependsFortran scanner(srcFile, includes);
  2572. scanner.SetTargetFile(".",objFile,".depend",".build.depend.make");
  2573. return scanner.Write();
  2574. }
  2575. else if(lang == "Java")
  2576. {
  2577. cmDependsJava scanner(srcFile);
  2578. scanner.SetTargetFile(".",objFile,".depend",".build.depend.make");
  2579. return scanner.Write();
  2580. }
  2581. #endif
  2582. return false;
  2583. }
  2584. //----------------------------------------------------------------------------
  2585. void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
  2586. {
  2587. // only write the locla Makefile if we are not at the top
  2588. if (!m_Parent)
  2589. {
  2590. return;
  2591. }
  2592. // generate the includes
  2593. std::string ruleFileName = "Makefile";
  2594. // Open the rule file. This should be copy-if-different because the
  2595. // rules may depend on this file itself.
  2596. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  2597. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  2598. ruleFileStream.SetCopyIfDifferent(true);
  2599. if(!ruleFileStream)
  2600. {
  2601. return;
  2602. }
  2603. this->WriteDisclaimer(ruleFileStream);
  2604. this->WriteMakeVariables(ruleFileStream);
  2605. std::vector<std::string> depends;
  2606. std::vector<std::string> commands;
  2607. // Write the empty all rule.
  2608. std::string dir = m_Makefile->GetStartOutputDirectory();
  2609. dir += "/directory";
  2610. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  2611. this->CreateJumpCommand(commands,dir);
  2612. this->WriteMakeRule(ruleFileStream, "The main all target", "all", depends, commands);
  2613. // recursively write our targets
  2614. this->WriteLocalMakefileTargets(ruleFileStream);
  2615. }
  2616. void cmLocalUnixMakefileGenerator3
  2617. ::WriteLocalMakefileTargets(std::ostream& ruleFileStream)
  2618. {
  2619. std::vector<std::string> depends;
  2620. std::vector<std::string> commands;
  2621. // for each target we just provide a rule to cd up to the top and do a make
  2622. // on the target
  2623. const cmTargets& targets = m_Makefile->GetTargets();
  2624. std::string localName;
  2625. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  2626. {
  2627. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  2628. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  2629. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2630. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  2631. {
  2632. // Add a rule to build the target by name.
  2633. localName = this->GetRelativeTargetDirectory(t->second);
  2634. commands.clear();
  2635. depends.clear();
  2636. this->CreateJumpCommand(commands,localName);
  2637. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  2638. localName.c_str(), depends, commands);
  2639. // Add a target with the canonical name (no prefix, suffix or path).
  2640. if(localName != t->second.GetName())
  2641. {
  2642. commands.clear();
  2643. depends.push_back(localName);
  2644. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  2645. t->second.GetName(), depends, commands);
  2646. }
  2647. }
  2648. }
  2649. }
  2650. void cmLocalUnixMakefileGenerator3::CreateJumpCommand(std::vector<std::string>& commands,
  2651. std::string& localName)
  2652. {
  2653. if(m_WindowsShell)
  2654. {
  2655. // On Windows we must perform each step separately and then change
  2656. // back because the shell keeps the working directory between
  2657. // commands.
  2658. std::string cmd = "cd ";
  2659. cmd += this->ConvertToOutputForExisting(m_Makefile->GetHomeOutputDirectory());
  2660. commands.push_back(cmd);
  2661. // Build the target for this pass.
  2662. commands.push_back(this->GetRecursiveMakeCall("Makefile",localName.c_str()));
  2663. // Change back to the starting directory. Any trailing slash must be
  2664. // removed to avoid problems with Borland Make.
  2665. std::string back =
  2666. cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(),
  2667. m_Makefile->GetStartOutputDirectory());
  2668. if(back.size() && back[back.size()-1] == '/')
  2669. {
  2670. back = back.substr(0, back.size()-1);
  2671. }
  2672. cmd = "cd ";
  2673. cmd += this->ConvertToOutputForExisting(back.c_str());
  2674. commands.push_back(cmd);
  2675. }
  2676. else
  2677. {
  2678. // On UNIX we must construct a single shell command to change
  2679. // directory and build because make resets the directory between
  2680. // each command.
  2681. std::string cmd = "cd ";
  2682. cmd += this->ConvertToOutputForExisting(m_Makefile->GetHomeOutputDirectory());
  2683. // Build the target for this pass.
  2684. cmd += " && ";
  2685. cmd += this->GetRecursiveMakeCall("Makefile",localName.c_str());
  2686. // Add the command as a single line.
  2687. commands.push_back(cmd);
  2688. }
  2689. }
  2690. //----------------------------------------------------------------------------
  2691. void cmLocalUnixMakefileGenerator3::CheckDependencies(cmMakefile* mf,
  2692. bool verbose)
  2693. {
  2694. // Get the list of languages that may have sources to check.
  2695. const char* langDef = mf->GetDefinition("CMAKE_DEPENDS_LANGUAGES");
  2696. if(!langDef)
  2697. {
  2698. return;
  2699. }
  2700. std::vector<std::string> languages;
  2701. cmSystemTools::ExpandListArgument(langDef, languages);
  2702. // For each language get the set of files to check.
  2703. for(std::vector<std::string>::iterator l = languages.begin();
  2704. l != languages.end(); ++l)
  2705. {
  2706. std::string depCheck = "CMAKE_DEPENDS_CHECK_";
  2707. depCheck += *l;
  2708. if(const char* fileDef = mf->GetDefinition(depCheck.c_str()))
  2709. {
  2710. // Check each file. The current working directory is already
  2711. // correct.
  2712. std::vector<std::string> files;
  2713. cmSystemTools::ExpandListArgument(fileDef, files);
  2714. for(std::vector<std::string>::iterator f = files.begin();
  2715. f != files.end(); ++f)
  2716. {
  2717. // Construct a checker for the given language.
  2718. std::auto_ptr<cmDepends>
  2719. checker(cmLocalUnixMakefileGenerator3
  2720. ::GetDependsChecker(*l, ".", f->c_str(), verbose));
  2721. if(checker.get())
  2722. {
  2723. checker->Check();
  2724. }
  2725. }
  2726. }
  2727. }
  2728. }
  2729. //----------------------------------------------------------------------------
  2730. std::string
  2731. cmLocalUnixMakefileGenerator3
  2732. ::GetRecursiveMakeCall(const char *Makefile, const char* tgt)
  2733. {
  2734. // Call make on the given file.
  2735. std::string cmd;
  2736. cmd += "$(MAKE) -f ";
  2737. cmd += Makefile;
  2738. cmd += " ";
  2739. // Passg down verbosity level.
  2740. if(this->GetMakeSilentFlag().size())
  2741. {
  2742. cmd += this->GetMakeSilentFlag();
  2743. cmd += " ";
  2744. }
  2745. // Most unix makes will pass the command line flags to make down to
  2746. // sub-invoked makes via an environment variable. However, some
  2747. // makes do not support that, so you have to pass the flags
  2748. // explicitly.
  2749. if(this->GetPassMakeflags())
  2750. {
  2751. cmd += "-$(MAKEFLAGS) ";
  2752. }
  2753. // Add the target.
  2754. if (tgt && tgt[0] != '\0')
  2755. {
  2756. std::string tgt2 = this->Convert(tgt,HOME_OUTPUT,MAKEFILE);
  2757. tgt2 = this->ConvertToMakeTarget(tgt2.c_str());
  2758. cmd += tgt2;
  2759. }
  2760. return cmd;
  2761. }