cmLocalUnixMakefileGenerator3.cxx 100 KB

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