cmLocalUnixMakefileGenerator.cxx 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  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 "cmGlobalGenerator.h"
  14. #include "cmLocalUnixMakefileGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include <cmsys/RegularExpression.hxx>
  22. cmLocalUnixMakefileGenerator::cmLocalUnixMakefileGenerator()
  23. {
  24. m_WindowsShell = false;
  25. m_IncludeDirective = "include";
  26. m_MakefileVariableSize = 0;
  27. m_IgnoreLibPrefix = false;
  28. m_PassMakeflags = false;
  29. m_UseRelativePaths = false;
  30. }
  31. cmLocalUnixMakefileGenerator::~cmLocalUnixMakefileGenerator()
  32. {
  33. }
  34. void cmLocalUnixMakefileGenerator::Generate(bool fromTheTop)
  35. {
  36. m_UseRelativePaths = m_Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  37. // suppoirt override in output directories
  38. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  39. {
  40. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  41. if(m_LibraryOutputPath.size())
  42. {
  43. if(m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
  44. {
  45. m_LibraryOutputPath += "/";
  46. }
  47. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  48. {
  49. cmSystemTools::Error("Error failed create "
  50. "LIBRARY_OUTPUT_PATH directory:",
  51. m_LibraryOutputPath.c_str());
  52. }
  53. m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  54. }
  55. }
  56. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  57. {
  58. m_ExecutableOutputPath =
  59. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  60. if(m_ExecutableOutputPath.size())
  61. {
  62. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
  63. {
  64. m_ExecutableOutputPath += "/";
  65. }
  66. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  67. {
  68. cmSystemTools::Error("Error failed to create "
  69. "EXECUTABLE_OUTPUT_PATH directory:",
  70. m_ExecutableOutputPath.c_str());
  71. }
  72. m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
  73. }
  74. }
  75. if (!fromTheTop)
  76. {
  77. // Generate depends
  78. cmMakeDepend md;
  79. md.SetMakefile(m_Makefile);
  80. md.GenerateMakefileDependencies();
  81. this->ProcessDepends(md);
  82. }
  83. // output the makefile fragment
  84. std::string dest = m_Makefile->GetStartOutputDirectory();
  85. dest += "/Makefile";
  86. this->OutputMakefile(dest.c_str(), !fromTheTop);
  87. }
  88. void
  89. cmLocalUnixMakefileGenerator::AddDependenciesToSourceFile(cmDependInformation const *info,
  90. cmSourceFile *i,
  91. std::set<cmDependInformation const*> *visited)
  92. {
  93. // add info to the visited set
  94. visited->insert(info);
  95. // add this dependency and the recurse
  96. // now recurse with info's dependencies
  97. for(cmDependInformation::DependencySet::const_iterator d =
  98. info->m_DependencySet.begin();
  99. d != info->m_DependencySet.end(); ++d)
  100. {
  101. if (visited->find(*d) == visited->end())
  102. {
  103. if((*d)->m_FullPath != "")
  104. {
  105. i->GetDepends().push_back((*d)->m_FullPath.c_str());
  106. }
  107. this->AddDependenciesToSourceFile(*d,i,visited);
  108. }
  109. }
  110. }
  111. void cmLocalUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  112. {
  113. // Now create cmDependInformation objects for files in the directory
  114. cmTargets &tgts = m_Makefile->GetTargets();
  115. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  116. {
  117. std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  118. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  119. i != classes.end(); ++i)
  120. {
  121. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  122. {
  123. // get the depends
  124. const cmDependInformation *info =
  125. md.GetDependInformationForSourceFile(*(*i));
  126. // Delete any hints from the source file's dependencies.
  127. (*i)->GetDepends().erase((*i)->GetDepends().begin(), (*i)->GetDepends().end());
  128. // Now add the real dependencies for the file.
  129. if (info)
  130. {
  131. // create visited set
  132. std::set<cmDependInformation const*> visited;
  133. this->AddDependenciesToSourceFile(info,*i, &visited);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. // This is where CMakeTargets.make is generated
  140. void cmLocalUnixMakefileGenerator::OutputMakefile(const char* file,
  141. bool withDepends)
  142. {
  143. // Create sub directories fro aux source directories
  144. std::vector<std::string>& auxSourceDirs =
  145. m_Makefile->GetAuxSourceDirectories();
  146. if( auxSourceDirs.size() )
  147. {
  148. // For the case when this is running as a remote build
  149. // on unix, make the directory
  150. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  151. i != auxSourceDirs.end(); ++i)
  152. {
  153. if(i->c_str()[0] != '/')
  154. {
  155. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  156. if(dir.size() && dir[dir.size()-1] != '/')
  157. {
  158. dir += "/";
  159. }
  160. dir += *i;
  161. cmSystemTools::MakeDirectory(dir.c_str());
  162. }
  163. else
  164. {
  165. cmSystemTools::MakeDirectory(i->c_str());
  166. }
  167. }
  168. }
  169. // Create a stream that writes to a temporary file
  170. // then does a copy at the end. This is to allow users
  171. // to hit control-c during the make of the makefile
  172. cmGeneratedFileStream fout(file);
  173. if(!fout)
  174. {
  175. return;
  176. }
  177. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  178. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  179. << " Generator, CMake Version "
  180. << cmMakefile::GetMajorVersion() << "."
  181. << cmMakefile::GetMinorVersion() << "\n"
  182. << "# Generated from the following files:\n# "
  183. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  184. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  185. // sort the array
  186. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  187. // remove duplicates
  188. std::vector<std::string>::iterator new_end =
  189. std::unique(lfiles.begin(), lfiles.end());
  190. lfiles.erase(new_end, lfiles.end());
  191. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  192. i != lfiles.end(); ++i)
  193. {
  194. fout << "# " << i->c_str() << "\n";
  195. }
  196. fout << "\n\n";
  197. fout << "# disable some common implicit rules to speed things up\n";
  198. fout << ".SUFFIXES:\n";
  199. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  200. // create a make variable with all of the sources for this Makefile
  201. // for depend purposes.
  202. fout << "CMAKE_MAKEFILE_SOURCES = ";
  203. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  204. i != lfiles.end(); ++i)
  205. {
  206. fout << " " << this->ConvertToRelativeOutputPath(i->c_str());
  207. }
  208. // Add the cache to the list
  209. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  210. cacheFile += "/CMakeCache.txt";
  211. fout << " " << this->ConvertToRelativeOutputPath(cacheFile.c_str());
  212. fout << "\n\n\n";
  213. this->OutputMakeVariables(fout);
  214. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  215. checkCache += "/cmake.check_cache";
  216. checkCache = this->ConvertToRelativeOutputPath(checkCache.c_str());
  217. // most unix makes will pass the command line flags to make down
  218. // to sub invoked makes via an environment variable. However, some
  219. // makes do not support that, so you have to pass the flags explicitly
  220. const char* allRule = "$(MAKE) $(MAKESILENT) all";
  221. if(m_PassMakeflags)
  222. {
  223. allRule = "$(MAKE) $(MAKESILENT) -$(MAKEFLAGS) all";
  224. }
  225. // Set up the default target as the VERY first target, so that make with no arguments will run it
  226. this->
  227. OutputMakeRule(fout,
  228. "Default target executed when no arguments are given to make, first make sure cmake.depends exists, cmake.check_depends is up-to-date, check the sources, then build the all target",
  229. "default_target",
  230. checkCache.c_str(),
  231. "$(MAKE) $(MAKESILENT) cmake.depends",
  232. "$(MAKE) $(MAKESILENT) cmake.check_depends",
  233. "$(MAKE) $(MAKESILENT) -f cmake.check_depends",
  234. allRule);
  235. // Generation of SILENT target must be after default_target.
  236. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  237. {
  238. fout << "# Suppresses display of executed commands\n";
  239. fout << "$(VERBOSE).SILENT:\n\n";
  240. }
  241. this->OutputTargetRules(fout);
  242. this->OutputDependLibs(fout);
  243. this->OutputTargets(fout);
  244. this->OutputSubDirectoryRules(fout);
  245. std::string dependName = m_Makefile->GetStartOutputDirectory();
  246. dependName += "/cmake.depends";
  247. if(withDepends)
  248. {
  249. std::ofstream dependout(dependName.c_str());
  250. if(!dependout)
  251. {
  252. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  253. return;
  254. }
  255. dependout << "# .o dependencies in this directory." << std::endl;
  256. std::string checkDepend = m_Makefile->GetStartOutputDirectory();
  257. checkDepend += "/cmake.check_depends";
  258. std::ofstream checkdependout(checkDepend.c_str());
  259. if(!checkdependout)
  260. {
  261. cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
  262. return;
  263. }
  264. checkdependout << "# This file is used as a tag file, that all sources depend on. If a source changes, then the rule to rebuild this file will cause cmake.depends to be rebuilt." << std::endl;
  265. // if there were any depends output, then output the check depends
  266. // information inot checkdependout
  267. if(this->OutputObjectDepends(dependout))
  268. {
  269. this->OutputCheckDepends(checkdependout);
  270. }
  271. else
  272. {
  273. checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
  274. }
  275. }
  276. this->OutputCustomRules(fout);
  277. this->OutputMakeRules(fout);
  278. // only add the depend include if the depend file exists
  279. if(cmSystemTools::FileExists(dependName.c_str()))
  280. {
  281. fout << m_IncludeDirective << " cmake.depends\n";
  282. }
  283. }
  284. std::string cmLocalUnixMakefileGenerator::GetBaseTargetName(const char* n,
  285. const cmTarget& t)
  286. {
  287. std::string pathPrefix = "";
  288. #ifdef __APPLE__
  289. if ( t.GetPropertyAsBool("MACOSX_BUNDLE") )
  290. {
  291. pathPrefix = n;
  292. pathPrefix += ".app/Contents/MacOS/";
  293. }
  294. #endif
  295. const char* targetPrefix = t.GetProperty("PREFIX");
  296. const char* prefixVar = t.GetPrefixVariable();
  297. // if there is no prefix on the target use the cmake definition
  298. if(!targetPrefix && prefixVar)
  299. {
  300. // first check for a language specific suffix var
  301. const char* ll = t.GetLinkerLanguage(this->GetGlobalGenerator());
  302. if(ll)
  303. {
  304. std::string langPrefix = prefixVar + std::string("_") + ll;
  305. targetPrefix = m_Makefile->GetDefinition(langPrefix.c_str());
  306. }
  307. // if there not a language specific suffix then use the general one
  308. if(!targetPrefix)
  309. {
  310. targetPrefix = m_Makefile->GetSafeDefinition(prefixVar);
  311. }
  312. }
  313. std::string name = pathPrefix + (targetPrefix?targetPrefix:"");
  314. name += n;
  315. return name;
  316. }
  317. // Output the rules for any targets
  318. void cmLocalUnixMakefileGenerator::OutputEcho(std::ostream& fout,
  319. const char *msg)
  320. {
  321. std::string echostring = msg;
  322. // for unix we want to quote the output of echo
  323. // for nmake and borland, the echo should not be quoted
  324. if(strcmp(m_GlobalGenerator->GetName(), "Unix Makefiles") == 0)
  325. {
  326. cmSystemTools::ReplaceString(echostring, "\\\n", " ");
  327. cmSystemTools::ReplaceString(echostring, " \t", " ");
  328. cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\t@echo \"");
  329. fout << "\t@echo \"" << echostring.c_str() << "\"\n";
  330. }
  331. else
  332. {
  333. cmSystemTools::ReplaceString(echostring, "\n\t", "\n\t@echo ");
  334. fout << "\t@echo " << echostring.c_str() << "\n";
  335. }
  336. }
  337. // Output the rules for any targets
  338. void cmLocalUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  339. {
  340. const cmTargets &tgts = m_Makefile->GetTargets();
  341. // add the help target
  342. fout << "help:\n";
  343. this->OutputEcho(fout,"The following are some of the valid targets for this Makefile:");
  344. this->OutputEcho(fout,"... all (the default if no target is provided)");
  345. this->OutputEcho(fout,"... clean");
  346. this->OutputEcho(fout,"... depend");
  347. this->OutputEcho(fout,"... install");
  348. this->OutputEcho(fout,"... rebuild_cache");
  349. // libraries
  350. std::string path;
  351. for(cmTargets::const_iterator l = tgts.begin();
  352. l != tgts.end(); l++)
  353. {
  354. if((l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  355. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  356. (l->second.GetType() == cmTarget::MODULE_LIBRARY))
  357. {
  358. std::string path2 = m_LibraryOutputPath;
  359. path2 += this->GetFullTargetName(l->first.c_str(), l->second);
  360. path = "... ";
  361. path += this->ConvertToRelativeOutputPath(path2.c_str());
  362. this->OutputEcho(fout,path.c_str());
  363. }
  364. }
  365. // executables
  366. for(cmTargets::const_iterator l = tgts.begin();
  367. l != tgts.end(); l++)
  368. {
  369. if ( l->second.GetType() == cmTarget::EXECUTABLE )
  370. {
  371. path = "... ";
  372. path += l->first + cmSystemTools::GetExecutableExtension();
  373. this->OutputEcho(fout,path.c_str());
  374. }
  375. }
  376. // list utilities last
  377. for(cmTargets::const_iterator l = tgts.begin();
  378. l != tgts.end(); l++)
  379. {
  380. if (l->second.GetType() == cmTarget::UTILITY)
  381. {
  382. path = "... ";
  383. path += l->first;
  384. this->OutputEcho(fout,path.c_str());
  385. }
  386. }
  387. fout << "\n\n";
  388. // for each target add to the list of targets
  389. fout << "TARGETS = ";
  390. // list libraries first
  391. for(cmTargets::const_iterator l = tgts.begin();
  392. l != tgts.end(); l++)
  393. {
  394. if (l->second.IsInAll())
  395. {
  396. if((l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  397. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  398. (l->second.GetType() == cmTarget::MODULE_LIBRARY))
  399. {
  400. path = m_LibraryOutputPath;
  401. path += this->GetFullTargetName(l->first.c_str(), l->second);
  402. fout << " \\\n"
  403. << this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(path.c_str()).c_str());
  404. }
  405. }
  406. }
  407. // executables
  408. for(cmTargets::const_iterator l = tgts.begin();
  409. l != tgts.end(); l++)
  410. {
  411. if (l->second.GetType() == cmTarget::EXECUTABLE &&
  412. l->second.IsInAll())
  413. {
  414. path = m_ExecutableOutputPath;
  415. path += this->GetFullTargetName(l->first.c_str(), l->second);
  416. fout << " \\\n" << this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(path.c_str()).c_str());
  417. }
  418. }
  419. // list utilities last
  420. for(cmTargets::const_iterator l = tgts.begin();
  421. l != tgts.end(); l++)
  422. {
  423. if (l->second.GetType() == cmTarget::UTILITY &&
  424. l->second.IsInAll())
  425. {
  426. fout << " \\\n" << l->first.c_str();
  427. }
  428. }
  429. fout << "\n\n";
  430. // get the classes from the source lists then add them to the groups
  431. for(cmTargets::const_iterator l = tgts.begin();
  432. l != tgts.end(); l++)
  433. {
  434. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  435. if (classes.begin() != classes.end())
  436. {
  437. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS") << " = ";
  438. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  439. i != classes.end(); i++)
  440. {
  441. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  442. !(*i)->GetCustomCommand())
  443. {
  444. std::string outExt =
  445. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  446. (*i)->GetSourceExtension().c_str());
  447. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  448. {
  449. fout << "\\\n";
  450. std::string ofname = (*i)->GetSourceName() + outExt;
  451. ofname = this->CreateSafeUniqueObjectFileName(ofname.c_str());
  452. fout << this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(ofname.c_str()).c_str());
  453. }
  454. }
  455. }
  456. fout << "\n\n";
  457. fout << this->CreateMakeVariable(l->first.c_str(), "_EXTERNAL_OBJS") << " = ";
  458. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  459. i != classes.end(); i++)
  460. {
  461. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  462. !(*i)->GetCustomCommand())
  463. {
  464. std::string outExt =
  465. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  466. (*i)->GetSourceExtension().c_str());
  467. if(outExt.size() && (*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  468. {
  469. fout << "\\\n";
  470. fout << this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str()).c_str()) << " ";
  471. }
  472. }
  473. }
  474. fout << "\n\n";
  475. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS_QUOTED") << " = ";
  476. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  477. i != classes.end(); i++)
  478. {
  479. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  480. !(*i)->GetCustomCommand())
  481. {
  482. std::string outExt =
  483. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  484. (*i)->GetSourceExtension().c_str());
  485. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  486. {
  487. std::string ofname = (*i)->GetSourceName() + outExt;
  488. ofname = this->CreateSafeUniqueObjectFileName(ofname.c_str());
  489. fout << "\\\n\"" << this->ConvertToMakeTarget(ConvertToRelativeOutputPath(ofname.c_str()).c_str()) << "\" ";
  490. }
  491. }
  492. }
  493. fout << "\n\n";
  494. fout << this->CreateMakeVariable(l->first.c_str(), "_EXTERNAL_OBJS_QUOTED") << " = ";
  495. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  496. i != classes.end(); i++)
  497. {
  498. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  499. !(*i)->GetCustomCommand())
  500. {
  501. std::string outExt =
  502. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  503. (*i)->GetSourceExtension().c_str());
  504. if(outExt.size() && (*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  505. {
  506. fout << "\\\n\"" << this->ConvertToMakeTarget(ConvertToRelativeOutputPath((*i)->GetFullPath().c_str()).c_str()) << "\" ";
  507. }
  508. }
  509. }
  510. fout << "\n\n";
  511. }
  512. }
  513. // This list contains extra files created for a target. It includes
  514. // the extra names associated with a versioned shared library.
  515. fout << "TARGET_EXTRAS = ";
  516. for(cmTargets::const_iterator l = tgts.begin();
  517. l != tgts.end(); l++)
  518. {
  519. if (l->second.IsInAll())
  520. {
  521. if(l->second.GetType() == cmTarget::SHARED_LIBRARY ||
  522. l->second.GetType() == cmTarget::MODULE_LIBRARY)
  523. {
  524. std::string targetName;
  525. std::string targetNameSO;
  526. std::string targetNameReal;
  527. std::string targetNameBase;
  528. this->GetLibraryNames(l->first.c_str(), l->second,
  529. targetName, targetNameSO,
  530. targetNameReal, targetNameBase);
  531. if(targetNameSO != targetName)
  532. {
  533. path = m_LibraryOutputPath;
  534. path += targetNameSO;
  535. fout << " \\\n"
  536. << this->ConvertToRelativeOutputPath(path.c_str());
  537. }
  538. if(targetNameReal != targetName &&
  539. targetNameReal != targetNameSO)
  540. {
  541. path = m_LibraryOutputPath;
  542. path += targetNameReal;
  543. fout << " \\\n"
  544. << this->ConvertToRelativeOutputPath(path.c_str());
  545. }
  546. }
  547. }
  548. }
  549. fout << "\n\n";
  550. fout << "CLEAN_OBJECT_FILES = ";
  551. for(cmTargets::const_iterator l = tgts.begin();
  552. l != tgts.end(); l++)
  553. {
  554. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  555. if (classes.begin() != classes.end())
  556. {
  557. fout << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
  558. << ") ";
  559. }
  560. }
  561. fout << "\n\n";
  562. const char * additional_clean_files =
  563. m_Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES");
  564. if ( additional_clean_files && strlen(additional_clean_files) > 0 )
  565. {
  566. std::string arg = additional_clean_files;
  567. std::vector<std::string> args;
  568. cmSystemTools::ExpandListArgument(arg, args);
  569. fout << "ADDITIONAL_MAKE_CLEAN_FILES = ";
  570. for(std::vector<std::string>::iterator i = args.begin(); i != args.end(); ++i)
  571. {
  572. fout << this->ConvertToRelativeOutputPath(i->c_str()) << " ";
  573. }
  574. fout << "\n\n";
  575. }
  576. const char * qt_files = m_Makefile->GetDefinition("GENERATED_QT_FILES");
  577. if (qt_files != NULL &&
  578. strlen(m_Makefile->GetDefinition("GENERATED_QT_FILES"))>0)
  579. {
  580. fout << "GENERATED_QT_FILES = ";
  581. fout << qt_files;
  582. fout << "\n\n";
  583. }
  584. }
  585. std::string cmLocalUnixMakefileGenerator::CreatePreBuildRules(
  586. const cmTarget &target, const char* /* targetName */)
  587. {
  588. std::string customRuleCode = "";
  589. bool initNext = false;
  590. for (std::vector<cmCustomCommand>::const_iterator cr =
  591. target.GetPreBuildCommands().begin();
  592. cr != target.GetPreBuildCommands().end(); ++cr)
  593. {
  594. cmCustomCommand cc(*cr);
  595. cc.ExpandVariables(*m_Makefile);
  596. if(initNext)
  597. {
  598. customRuleCode += "\n\t";
  599. }
  600. else
  601. {
  602. initNext = true;
  603. }
  604. std::string command = this->ConvertToRelativeOutputPath(cc.GetCommand().c_str());
  605. customRuleCode += command + " " + cc.GetArguments();
  606. }
  607. return customRuleCode;
  608. }
  609. std::string cmLocalUnixMakefileGenerator::CreatePreLinkRules(
  610. const cmTarget &target, const char* /* targetName */)
  611. {
  612. std::string customRuleCode = "";
  613. bool initNext = false;
  614. for (std::vector<cmCustomCommand>::const_iterator cr =
  615. target.GetPreLinkCommands().begin();
  616. cr != target.GetPreLinkCommands().end(); ++cr)
  617. {
  618. cmCustomCommand cc(*cr);
  619. cc.ExpandVariables(*m_Makefile);
  620. if(initNext)
  621. {
  622. customRuleCode += "\n\t";
  623. }
  624. else
  625. {
  626. initNext = true;
  627. }
  628. std::string command = this->ConvertToRelativeOutputPath(cc.GetCommand().c_str());
  629. customRuleCode += command + " " + cc.GetArguments();
  630. }
  631. return customRuleCode;
  632. }
  633. std::string cmLocalUnixMakefileGenerator::CreatePostBuildRules(
  634. const cmTarget &target, const char* /* targetName */)
  635. {
  636. std::string customRuleCode = "";
  637. bool initNext = false;
  638. for (std::vector<cmCustomCommand>::const_iterator cr =
  639. target.GetPostBuildCommands().begin();
  640. cr != target.GetPostBuildCommands().end(); ++cr)
  641. {
  642. cmCustomCommand cc(*cr);
  643. cc.ExpandVariables(*m_Makefile);
  644. if(initNext)
  645. {
  646. customRuleCode += "\n\t";
  647. }
  648. else
  649. {
  650. initNext = true;
  651. }
  652. std::string command = this->ConvertToRelativeOutputPath(cc.GetCommand().c_str());
  653. customRuleCode += command + " " + cc.GetArguments();
  654. }
  655. return customRuleCode;
  656. }
  657. void cmLocalUnixMakefileGenerator::OutputLibraryRule(std::ostream& fout,
  658. const char* name,
  659. const cmTarget &t,
  660. const char* createVariable,
  661. const char* comment,
  662. const char* linkFlags
  663. )
  664. {
  665. std::string targetName;
  666. std::string targetNameSO;
  667. std::string targetNameReal;
  668. std::string targetNameBase;
  669. this->GetLibraryNames(name, t,
  670. targetName, targetNameSO,
  671. targetNameReal, targetNameBase);
  672. std::string outpath;
  673. std::string outdir;
  674. if(m_UseRelativePaths)
  675. {
  676. outdir = this->ConvertToRelativeOutputPath(m_LibraryOutputPath.c_str());
  677. }
  678. else
  679. {
  680. outdir = m_LibraryOutputPath;
  681. }
  682. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  683. {
  684. outpath = "\"`cd ";
  685. }
  686. outpath += outdir;
  687. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  688. {
  689. outpath += ";pwd`\"/";
  690. }
  691. if(outdir.size() == 0 && m_UseRelativePaths && !m_WindowsShell)
  692. {
  693. outpath = "\"`pwd`\"/";
  694. }
  695. // The full path versions of the names.
  696. std::string targetFullPath = outpath + targetName;
  697. std::string targetFullPathSO = outpath + targetNameSO;
  698. std::string targetFullPathReal = outpath + targetNameReal;
  699. std::string targetFullPathBase = outpath + targetNameBase;
  700. // If not using relative paths then the output path needs to be
  701. // converted here
  702. if(!m_UseRelativePaths)
  703. {
  704. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  705. targetFullPathSO = this->ConvertToRelativeOutputPath(targetFullPathSO.c_str());
  706. targetFullPathReal = this->ConvertToRelativeOutputPath(targetFullPathReal.c_str());
  707. targetFullPathBase = this->ConvertToRelativeOutputPath(targetFullPathBase.c_str());
  708. }
  709. // get the objects that are used to link this library
  710. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS")
  711. + ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS") + ") ";
  712. std::string objsQuoted = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED")
  713. + ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS_QUOTED") + ") ";
  714. // create a variable with the objects that this library depends on
  715. std::string depend =
  716. objs + " $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  717. std::vector<std::string> commands;
  718. std::string cmakecommand = this->ConvertToOutputForExisting(
  719. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  720. // Remove any existing files for this library.
  721. std::string remove = cmakecommand;
  722. remove += " -E remove -f ";
  723. remove += targetFullPathReal;
  724. if(targetFullPathSO != targetFullPathReal)
  725. {
  726. remove += " ";
  727. remove += targetFullPathSO;
  728. }
  729. if(targetFullPath != targetFullPathSO &&
  730. targetFullPath != targetFullPathReal)
  731. {
  732. remove += " ";
  733. remove += targetFullPath;
  734. }
  735. commands.push_back(remove);
  736. // collect custom commands for this target and add them to the list
  737. std::string customCommands = this->CreatePreBuildRules(t, name);
  738. if(customCommands.size() > 0)
  739. {
  740. commands.push_back(customCommands);
  741. }
  742. // collect custom commands for this target and add them to the list
  743. customCommands = this->CreatePreLinkRules(t, name);
  744. if(customCommands.size() > 0)
  745. {
  746. commands.push_back(customCommands);
  747. }
  748. // collect up the build rules
  749. std::vector<std::string> rules;
  750. rules.push_back(m_Makefile->GetRequiredDefinition(createVariable));
  751. // expand multi-command semi-colon separated lists
  752. // of commands into separate commands
  753. cmSystemTools::ExpandList(rules, commands);
  754. // Create necessary symlinks for library.
  755. if(targetFullPath != targetFullPathReal)
  756. {
  757. std::string symlink = cmakecommand;
  758. symlink += " -E cmake_symlink_library ";
  759. symlink += targetFullPathReal;
  760. symlink += " ";
  761. symlink += targetFullPathSO;
  762. symlink += " ";
  763. symlink += targetFullPath;
  764. commands.push_back(symlink);
  765. }
  766. // collect custom commands for this target and add them to the list
  767. customCommands = this->CreatePostBuildRules(t, name);
  768. if(customCommands.size() > 0)
  769. {
  770. commands.push_back(customCommands);
  771. }
  772. // collect up the link libraries
  773. cmOStringStream linklibs;
  774. this->OutputLinkLibraries(linklibs, name, t);
  775. for(std::vector<std::string>::iterator i = commands.begin();
  776. i != commands.end(); ++i)
  777. {
  778. this->ExpandRuleVariables(*i,
  779. t.GetLinkerLanguage(m_GlobalGenerator),
  780. objs.c_str(),
  781. targetFullPathReal.c_str(),
  782. linklibs.str().c_str(),
  783. 0, 0, 0, objsQuoted.c_str(),
  784. targetFullPathBase.c_str(),
  785. targetNameSO.c_str(),
  786. linkFlags);
  787. }
  788. targetFullPath = m_LibraryOutputPath + targetName;
  789. this->OutputMakeRule(fout, comment,
  790. targetFullPath.c_str(),
  791. depend.c_str(),
  792. commands);
  793. depend = targetFullPath;
  794. std::string tgt = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  795. tgt = this->ConvertToMakeTarget(tgt.c_str());
  796. cmSystemTools::ConvertToUnixSlashes(tgt);
  797. if(tgt.find('/', 0) != tgt.npos)
  798. {
  799. // we need a local target
  800. depend = this->ConvertToRelativeOutputPath(depend.c_str());
  801. std::string target = targetName;
  802. commands.resize(0);
  803. this->OutputMakeRule(fout,
  804. comment,
  805. target.c_str(),
  806. depend.c_str(),
  807. commands);
  808. }
  809. // Add a target with the canonical name (no prefix, suffix or path).
  810. this->OutputMakeRule(fout,
  811. comment,
  812. name,
  813. this->ConvertToRelativeOutputPath(tgt.c_str()).c_str(),
  814. 0);
  815. }
  816. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  817. const char* name,
  818. const cmTarget &t)
  819. {
  820. const char* linkLanguage = t.GetLinkerLanguage(this->GetGlobalGenerator());
  821. if(!linkLanguage)
  822. {
  823. cmSystemTools::Error("CMake can not determine linker language target:",
  824. t.GetName());
  825. return;
  826. }
  827. std::string createRule = "CMAKE_";
  828. createRule += linkLanguage;
  829. createRule += "_CREATE_SHARED_LIBRARY";
  830. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  831. buildType = cmSystemTools::UpperCase(buildType);
  832. std::string linkFlags = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  833. linkFlags += " ";
  834. if(buildType.size())
  835. {
  836. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  837. build += buildType;
  838. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  839. linkFlags += " ";
  840. }
  841. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  842. {
  843. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  844. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  845. i != sources.end(); ++i)
  846. {
  847. if((*i)->GetSourceExtension() == "def")
  848. {
  849. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  850. linkFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  851. linkFlags += " ";
  852. }
  853. }
  854. }
  855. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  856. if(targetLinkFlags)
  857. {
  858. linkFlags += targetLinkFlags;
  859. linkFlags += " ";
  860. }
  861. this->OutputLibraryRule(fout, name, t,
  862. createRule.c_str(),
  863. "shared library",
  864. linkFlags.c_str());
  865. }
  866. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  867. const char* name,
  868. const cmTarget &t)
  869. {
  870. const char* linkLanguage = t.GetLinkerLanguage(this->GetGlobalGenerator());
  871. if(!linkLanguage)
  872. {
  873. cmSystemTools::Error("CMake can not determine linker language for target:",
  874. t.GetName());
  875. return;
  876. }
  877. std::string createRule = "CMAKE_";
  878. createRule += linkLanguage;
  879. createRule += "_CREATE_SHARED_MODULE";
  880. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  881. buildType = cmSystemTools::UpperCase(buildType);
  882. std::string linkFlags = m_Makefile->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  883. linkFlags += " ";
  884. if(buildType.size())
  885. {
  886. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  887. build += buildType;
  888. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  889. linkFlags += " ";
  890. }
  891. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  892. if(targetLinkFlags)
  893. {
  894. linkFlags += targetLinkFlags;
  895. linkFlags += " ";
  896. }
  897. this->OutputLibraryRule(fout, name, t,
  898. createRule.c_str(),
  899. "shared module",
  900. linkFlags.c_str());
  901. }
  902. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  903. const char* name,
  904. const cmTarget &t)
  905. {
  906. const char* linkLanguage = t.GetLinkerLanguage(this->GetGlobalGenerator());
  907. if(!linkLanguage)
  908. {
  909. cmSystemTools::Error("CMake can not determine linker language for target:",
  910. t.GetName());
  911. return;
  912. }
  913. std::string createRule = "CMAKE_";
  914. createRule += linkLanguage;
  915. createRule += "_CREATE_STATIC_LIBRARY";
  916. std::string linkFlags;
  917. const char* targetLinkFlags = t.GetProperty("STATIC_LIBRARY_FLAGS");
  918. if(targetLinkFlags)
  919. {
  920. linkFlags += targetLinkFlags;
  921. linkFlags += " ";
  922. }
  923. this->OutputLibraryRule(fout, name, t,
  924. createRule.c_str(),
  925. "static library",
  926. linkFlags.c_str());
  927. }
  928. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  929. const char* name,
  930. const cmTarget &t)
  931. {
  932. std::string linkFlags;
  933. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  934. buildType = cmSystemTools::UpperCase(buildType);
  935. std::string flags;
  936. // Construct the full path to the executable that will be generated.
  937. std::string target = m_ExecutableOutputPath;
  938. if(target.length() == 0)
  939. {
  940. target = m_Makefile->GetCurrentOutputDirectory();
  941. if(target.size() && target[target.size()-1] != '/')
  942. {
  943. target += "/";
  944. }
  945. }
  946. #ifdef __APPLE__
  947. if ( t.GetPropertyAsBool("MACOSX_BUNDLE") )
  948. {
  949. // Make bundle directories
  950. target += name;
  951. target += ".app/Contents/MacOS/";
  952. }
  953. #endif
  954. target += name;
  955. target += cmSystemTools::GetExecutableExtension();
  956. target = this->ConvertToRelativeOutputPath(target.c_str());
  957. bool needsLocalTarget = false;
  958. if(m_UseRelativePaths)
  959. {
  960. cmSystemTools::ConvertToUnixSlashes(target);
  961. std::string tgt = this->ConvertToMakeTarget(target.c_str());
  962. if(tgt.find('/', 0) != tgt.npos)
  963. {
  964. needsLocalTarget = true;
  965. }
  966. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  967. }
  968. else
  969. {
  970. needsLocalTarget = true;
  971. }
  972. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") +
  973. ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS") + ") ";
  974. std::string depend = "$(";
  975. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  976. + ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS")
  977. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  978. std::vector<std::string> rules;
  979. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  980. linkFlags += " ";
  981. if(buildType.size())
  982. {
  983. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  984. build += buildType;
  985. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  986. linkFlags += " ";
  987. }
  988. const char* linkLanguage = t.GetLinkerLanguage(this->GetGlobalGenerator());
  989. if(!linkLanguage)
  990. {
  991. cmSystemTools::Error("CMake can not determine linker language for target:",
  992. t.GetName());
  993. return;
  994. }
  995. std::string langVar = "CMAKE_";
  996. langVar += linkLanguage;
  997. std::string ruleVar = langVar + "_LINK_EXECUTABLE";
  998. std::string flagsVar = langVar + "_FLAGS";
  999. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1000. sharedFlagsVar += langVar;
  1001. sharedFlagsVar += "_FLAGS";
  1002. rules.push_back(m_Makefile->GetRequiredDefinition(ruleVar.c_str()));
  1003. flags += m_Makefile->GetSafeDefinition(flagsVar.c_str());
  1004. flags += " ";
  1005. flags += m_Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1006. flags += " ";
  1007. cmOStringStream linklibs;
  1008. this->OutputLinkLibraries(linklibs, 0, t);
  1009. std::string comment = "executable";
  1010. std::vector<std::string> commands;
  1011. std::string customCommands = this->CreatePreBuildRules(t, name);
  1012. if(customCommands.size() > 0)
  1013. {
  1014. commands.push_back(customCommands.c_str());
  1015. }
  1016. customCommands = this->CreatePreLinkRules(t, name);
  1017. if(customCommands.size() > 0)
  1018. {
  1019. commands.push_back(customCommands.c_str());
  1020. }
  1021. cmSystemTools::ExpandList(rules, commands);
  1022. customCommands = this->CreatePostBuildRules(t, name);
  1023. if(customCommands.size() > 0)
  1024. {
  1025. commands.push_back(customCommands.c_str());
  1026. }
  1027. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1028. {
  1029. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  1030. + std::string("_FLAGS");
  1031. linkFlags += m_Makefile->GetSafeDefinition(sFlagVar.c_str());
  1032. linkFlags += " ";
  1033. }
  1034. if ( t.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1035. {
  1036. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1037. linkFlags += " ";
  1038. }
  1039. else
  1040. {
  1041. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1042. linkFlags += " ";
  1043. }
  1044. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1045. if(targetLinkFlags)
  1046. {
  1047. linkFlags += targetLinkFlags;
  1048. linkFlags += " ";
  1049. }
  1050. for(std::vector<std::string>::iterator i = commands.begin();
  1051. i != commands.end(); ++i)
  1052. {
  1053. this->ExpandRuleVariables(*i,
  1054. linkLanguage,
  1055. objs.c_str(),
  1056. target.c_str(),
  1057. linklibs.str().c_str(),
  1058. 0,
  1059. 0,
  1060. flags.c_str(),
  1061. 0,
  1062. 0,
  1063. 0,
  1064. linkFlags.c_str());
  1065. }
  1066. this->OutputMakeRule(fout,
  1067. comment.c_str(),
  1068. target.c_str(),
  1069. depend.c_str(),
  1070. commands);
  1071. // If there is no executable output path, add a rule with the
  1072. // relative path to the executable. This is necessary for
  1073. // try-compile to work in this case.
  1074. if(needsLocalTarget)
  1075. {
  1076. depend = target;
  1077. target = name;
  1078. target += cmSystemTools::GetExecutableExtension();
  1079. target = this->ConvertToRelativeOutputPath(target.c_str());
  1080. commands.resize(0);
  1081. this->OutputMakeRule(fout,
  1082. comment.c_str(),
  1083. target.c_str(),
  1084. depend.c_str(),
  1085. commands);
  1086. }
  1087. // Add a target with the canonical name (no prefix, suffix or path).
  1088. // Note that on some platforms the "local target" added above will
  1089. // actually be the canonical name and will have set "target"
  1090. // correctly. Do not duplicate this target.
  1091. if(target != name)
  1092. {
  1093. this->OutputMakeRule(fout, comment.c_str(), name, target.c_str(), 0);
  1094. }
  1095. }
  1096. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  1097. const char* name,
  1098. const cmTarget &t)
  1099. {
  1100. const char* cc = 0;
  1101. std::string customCommands = this->CreatePreBuildRules(t, name);
  1102. std::string customCommands2 = this->CreatePreLinkRules(t, name);
  1103. if(customCommands2.size() > 0)
  1104. {
  1105. customCommands += customCommands2;
  1106. }
  1107. customCommands2 = this->CreatePostBuildRules(t, name);
  1108. if(customCommands2.size() > 0)
  1109. {
  1110. customCommands += customCommands2;
  1111. }
  1112. if(customCommands.size() > 0)
  1113. {
  1114. cc = customCommands.c_str();
  1115. }
  1116. std::string comment = "Utility";
  1117. std::string depends;
  1118. depends = "$(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1119. std::string replaceVars;
  1120. const std::vector<cmCustomCommand> &ccs = t.GetPostBuildCommands();
  1121. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1122. i != ccs.end(); ++i)
  1123. {
  1124. const std::vector<std::string> & dep = i->GetDepends();
  1125. for(std::vector<std::string>::const_iterator d = dep.begin();
  1126. d != dep.end(); ++d)
  1127. {
  1128. depends += " \\\n";
  1129. replaceVars = *d;
  1130. m_Makefile->ExpandVariablesInString(replaceVars);
  1131. depends += this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(replaceVars.c_str()).c_str());
  1132. }
  1133. }
  1134. this->OutputMakeRule(fout, comment.c_str(), name,
  1135. depends.c_str(), cc);
  1136. }
  1137. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  1138. {
  1139. // for each target
  1140. const cmTargets &tgts = m_Makefile->GetTargets();
  1141. for(cmTargets::const_iterator l = tgts.begin();
  1142. l != tgts.end(); l++)
  1143. {
  1144. switch(l->second.GetType())
  1145. {
  1146. case cmTarget::STATIC_LIBRARY:
  1147. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  1148. break;
  1149. case cmTarget::SHARED_LIBRARY:
  1150. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  1151. break;
  1152. case cmTarget::MODULE_LIBRARY:
  1153. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  1154. break;
  1155. case cmTarget::EXECUTABLE:
  1156. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  1157. break;
  1158. case cmTarget::UTILITY:
  1159. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  1160. break;
  1161. // This is handled by the OutputCustomRules method
  1162. case cmTarget::INSTALL_FILES:
  1163. // This is handled by the OutputInstallRules method
  1164. case cmTarget::INSTALL_PROGRAMS:
  1165. // This is handled by the OutputInstallRules method
  1166. break;
  1167. }
  1168. }
  1169. }
  1170. // For each target that is an executable or shared library, generate
  1171. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1172. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1173. {
  1174. // Build a set of libraries that will be linked into any target in
  1175. // this directory.
  1176. std::set<cmStdString> used;
  1177. // for each target
  1178. const cmTargets &tgts = m_Makefile->GetTargets();
  1179. for(cmTargets::const_iterator l = tgts.begin();
  1180. l != tgts.end(); l++)
  1181. {
  1182. // Each dependency should only be emitted once per target.
  1183. std::set<cmStdString> emitted;
  1184. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1185. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1186. || (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  1187. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1188. || (l->second.GetType() == cmTarget::UTILITY))
  1189. {
  1190. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1191. // A library should not depend on itself!
  1192. emitted.insert(l->first);
  1193. // for static libraries do not depend on other libraries
  1194. if(l->second.GetType() != cmTarget::STATIC_LIBRARY)
  1195. {
  1196. // Now, look at all link libraries specific to this target.
  1197. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1198. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1199. lib != tlibs.end(); ++lib)
  1200. {
  1201. // Record that this library was used.
  1202. used.insert(lib->first);
  1203. // Don't emit the same library twice for this target.
  1204. if(emitted.insert(lib->first).second)
  1205. {
  1206. // Output this dependency.
  1207. this->OutputLibDepend(fout, lib->first.c_str());
  1208. }
  1209. }
  1210. }
  1211. // for all targets depend on utilities
  1212. // Now, look at all utilities specific to this target.
  1213. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1214. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1215. util != tutils.end(); ++util)
  1216. {
  1217. // Record that this utility was used.
  1218. used.insert(*util);
  1219. // Don't emit the same utility twice for this target.
  1220. if(emitted.insert(*util).second)
  1221. {
  1222. // Output this dependency.
  1223. std::string utilType = *util + "_LIBRARY_TYPE";
  1224. const char* libType = m_Makefile->GetDefinition(utilType.c_str());
  1225. if ( libType )
  1226. {
  1227. this->OutputLibDepend(fout, util->c_str());
  1228. }
  1229. else
  1230. {
  1231. this->OutputExeDepend(fout, util->c_str());
  1232. }
  1233. }
  1234. }
  1235. fout << "\n";
  1236. }
  1237. }
  1238. fout << "\n";
  1239. // Loop over the libraries used and make sure there is a rule to
  1240. // build them in this makefile. If the library is in another
  1241. // directory, add a rule to jump to that directory and make sure it
  1242. // exists.
  1243. for(std::set<cmStdString>::const_iterator lib = used.begin();
  1244. lib != used.end(); ++lib)
  1245. {
  1246. // loop over the list of directories that the libraries might
  1247. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1248. // be stored in the cache
  1249. std::string libPath = *lib + "_CMAKE_PATH";
  1250. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1251. // if cache and not the current directory add a rule, to
  1252. // jump into the directory and build for the first time
  1253. if(cacheValue && *cacheValue &&
  1254. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1255. {
  1256. // add the correct extension
  1257. std::string ltname = *lib+"_LIBRARY_TYPE";
  1258. const char* libType
  1259. = m_Makefile->GetDefinition(ltname.c_str());
  1260. // if it was a library..
  1261. if (libType)
  1262. {
  1263. std::string library;
  1264. std::string libpath = cacheValue;
  1265. if(libType && std::string(libType) == "SHARED")
  1266. {
  1267. library = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1268. library += *lib;
  1269. library += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1270. }
  1271. else if(libType && std::string(libType) == "MODULE")
  1272. {
  1273. library = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1274. library += *lib;
  1275. library += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1276. }
  1277. else if(libType && std::string(libType) == "STATIC")
  1278. {
  1279. library = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1280. library += *lib;
  1281. library += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1282. }
  1283. else
  1284. {
  1285. cmSystemTools::Error("Unknown library type!");
  1286. return;
  1287. }
  1288. if(m_LibraryOutputPath.size())
  1289. {
  1290. libpath = m_LibraryOutputPath;
  1291. }
  1292. else
  1293. {
  1294. libpath += "/";
  1295. }
  1296. libpath += library;
  1297. // put out a rule to build the library if it does not exist
  1298. this->OutputBuildTargetInDir(fout,
  1299. cacheValue,
  1300. library.c_str(),
  1301. libpath.c_str());
  1302. }
  1303. // something other than a library...
  1304. else
  1305. {
  1306. std::string exepath = cacheValue;
  1307. if(m_ExecutableOutputPath.size())
  1308. {
  1309. exepath = m_ExecutableOutputPath;
  1310. }
  1311. else
  1312. {
  1313. exepath += "/";
  1314. }
  1315. std::string fullName = lib->c_str();
  1316. fullName += cmSystemTools::GetExecutableExtension();
  1317. exepath += fullName;
  1318. this->OutputBuildTargetInDir(fout,
  1319. cacheValue,
  1320. fullName.c_str(),
  1321. exepath.c_str());
  1322. }
  1323. }
  1324. }
  1325. }
  1326. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1327. const char* path,
  1328. const char* library,
  1329. const char* fullpath)
  1330. {
  1331. std::string jumpBack;
  1332. if(m_UseRelativePaths)
  1333. {
  1334. jumpBack = cmSystemTools::RelativePath(cmSystemTools::GetProgramPath(path).c_str(),
  1335. m_Makefile->GetCurrentOutputDirectory());
  1336. }
  1337. else
  1338. {
  1339. jumpBack = m_Makefile->GetCurrentOutputDirectory();
  1340. }
  1341. jumpBack = this->ConvertToOutputForExisting(jumpBack.c_str());
  1342. std::string wpath = this->ConvertToOutputForExisting(path);
  1343. std::string wfullpath = this->ConvertToRelativeOutputPath(fullpath);
  1344. fout << wfullpath
  1345. << ":\n\tcd " << wpath << "\n"
  1346. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1347. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1348. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1349. << "\t$(MAKE) $(MAKESILENT) " << library
  1350. << "\n\tcd " << jumpBack << "\n";
  1351. }
  1352. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1353. const char* path,
  1354. const char* library,
  1355. const char* fullpath)
  1356. {
  1357. if(m_WindowsShell)
  1358. {
  1359. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath);
  1360. return;
  1361. }
  1362. fout << this->ConvertToOutputForExisting(fullpath)
  1363. << ":\n\tcd " << this->ConvertToOutputForExisting(path)
  1364. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1365. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1366. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1367. << "; $(MAKE) $(MAKESILENT) "
  1368. << library << "\n\n";
  1369. }
  1370. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1371. {
  1372. if (strcmp(path1, path2) == 0)
  1373. {
  1374. return true;
  1375. }
  1376. #if defined(_WIN32) || defined(__APPLE__)
  1377. return
  1378. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1379. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1380. #else
  1381. return false;
  1382. #endif
  1383. }
  1384. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1385. const char* name)
  1386. {
  1387. std::string libPath = name;
  1388. libPath += "_CMAKE_PATH";
  1389. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1390. if( cacheValue && *cacheValue )
  1391. {
  1392. // if there is a cache value, then this is a library that cmake
  1393. // knows how to build, so we can depend on it
  1394. std::string libpath;
  1395. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1396. {
  1397. // if the library is not in the current directory, then get the full
  1398. // path to it
  1399. if(m_LibraryOutputPath.size())
  1400. {
  1401. libpath = m_LibraryOutputPath;
  1402. }
  1403. else
  1404. {
  1405. libpath = cacheValue;
  1406. libpath += "/";
  1407. }
  1408. }
  1409. else
  1410. {
  1411. // library is in current Makefile so use lib as a prefix
  1412. libpath = m_LibraryOutputPath;
  1413. }
  1414. // add the correct extension
  1415. std::string ltname = name;
  1416. ltname += "_LIBRARY_TYPE";
  1417. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1418. if(libType && std::string(libType) == "SHARED")
  1419. {
  1420. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1421. libpath += name;
  1422. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1423. }
  1424. else if (libType && std::string(libType) == "MODULE")
  1425. {
  1426. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1427. libpath += name;
  1428. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1429. }
  1430. else if (libType && std::string(libType) == "STATIC")
  1431. {
  1432. libpath += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1433. libpath += name;
  1434. libpath += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1435. }
  1436. fout << this->ConvertToRelativeOutputPath(libpath.c_str()) << " ";
  1437. }
  1438. else
  1439. {
  1440. if(cmSystemTools::FileExists(name))
  1441. {
  1442. std::string nameStr = name;
  1443. // if it starts with / or \ or ?:/ or ?:\ then it must be a full path
  1444. if( (nameStr.size() && (nameStr[0] == '/' || nameStr[0] == '\\')) ||
  1445. ((nameStr.size() > 3) && (nameStr[1] == ':') && (nameStr[2] == '/' || nameStr[2] == '\\')))
  1446. {
  1447. fout << this->ConvertToRelativeOutputPath(name) << " ";
  1448. }
  1449. }
  1450. }
  1451. }
  1452. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1453. const char* name)
  1454. {
  1455. std::string exePath = name;
  1456. exePath += "_CMAKE_PATH";
  1457. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1458. if( cacheValue && *cacheValue )
  1459. {
  1460. // if there is a cache value, then this is a executable/utility that cmake
  1461. // knows how to build, so we can depend on it
  1462. std::string exepath;
  1463. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1464. {
  1465. // if the exe/utility is not in the current directory, then get the full
  1466. // path to it
  1467. if(m_ExecutableOutputPath.size())
  1468. {
  1469. exepath = m_ExecutableOutputPath;
  1470. }
  1471. else
  1472. {
  1473. exepath = cacheValue;
  1474. exepath += "/";
  1475. }
  1476. }
  1477. else
  1478. {
  1479. // library is in current Makefile
  1480. exepath = m_ExecutableOutputPath;
  1481. }
  1482. // add the library name
  1483. exepath += name;
  1484. // add the correct extension
  1485. exepath += cmSystemTools::GetExecutableExtension();
  1486. fout << this->ConvertToRelativeOutputPath(exepath.c_str()) << " ";
  1487. }
  1488. // if it isn't in the cache, it might still be a utility target
  1489. // so check for that
  1490. else
  1491. {
  1492. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1493. if (targets.find(name) != targets.end())
  1494. {
  1495. fout << name << " ";
  1496. }
  1497. }
  1498. }
  1499. // fix up names of directories so they can be used
  1500. // as targets in makefiles.
  1501. inline std::string FixDirectoryName(const char* dir)
  1502. {
  1503. std::string s = dir;
  1504. // replace ../ with 3 under bars
  1505. size_t pos = s.find("../");
  1506. if(pos != std::string::npos)
  1507. {
  1508. s.replace(pos, 3, "___");
  1509. }
  1510. // replace / directory separators with a single under bar
  1511. pos = s.find("/");
  1512. while(pos != std::string::npos)
  1513. {
  1514. s.replace(pos, 1, "_");
  1515. pos = s.find("/");
  1516. }
  1517. return s;
  1518. }
  1519. void cmLocalUnixMakefileGenerator::
  1520. BuildInSubDirectoryWindows(std::ostream& fout,
  1521. const char* directory,
  1522. const char* target1,
  1523. const char* target2,
  1524. bool silent)
  1525. {
  1526. std::string dir;
  1527. if(target1)
  1528. {
  1529. dir = this->ConvertToOutputForExisting(directory);
  1530. if(dir[0] == '\"')
  1531. {
  1532. dir = dir.substr(1, dir.size()-2);
  1533. }
  1534. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1535. << " "
  1536. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1537. if (!silent)
  1538. {
  1539. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1540. }
  1541. fout << "\tcd " << dir << "\n"
  1542. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1543. }
  1544. if(target2)
  1545. {
  1546. if (!silent)
  1547. {
  1548. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1549. }
  1550. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1551. }
  1552. std::string currentDir;
  1553. if(m_UseRelativePaths)
  1554. {
  1555. currentDir = dir;
  1556. cmSystemTools::ConvertToUnixSlashes(currentDir);
  1557. std::string cdback = "..";
  1558. unsigned int i = 0;
  1559. if(currentDir.size() > 2 && currentDir[0] == '.' && currentDir[1] == '/')
  1560. {
  1561. // start past ./ if it starts with ./
  1562. i = 2;
  1563. }
  1564. for(; i < currentDir.size(); ++i)
  1565. {
  1566. if(currentDir[i] == '/')
  1567. {
  1568. cdback += "/..";
  1569. }
  1570. }
  1571. fout << "\tcd " << this->ConvertToOutputForExisting(cdback.c_str()) << "\n\n";
  1572. }
  1573. else
  1574. {
  1575. currentDir = m_Makefile->GetCurrentOutputDirectory();
  1576. fout << "\tcd " << this->ConvertToOutputForExisting(currentDir.c_str()) << "\n\n";
  1577. }
  1578. }
  1579. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1580. const char* dir,
  1581. const char* target1,
  1582. const char* target2,
  1583. bool silent)
  1584. {
  1585. if(m_WindowsShell)
  1586. {
  1587. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1588. return;
  1589. }
  1590. std::string directory = this->ConvertToRelativeOutputPath(dir);
  1591. if(target1)
  1592. {
  1593. fout << "\t@if test ! -d " << directory
  1594. << "; then $(MAKE) rebuild_cache; fi\n";
  1595. if (!silent)
  1596. {
  1597. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1598. }
  1599. fout << "\t@cd " << directory
  1600. << "; $(MAKE) " << target1 << "\n";
  1601. }
  1602. if(target2)
  1603. {
  1604. if (!silent)
  1605. {
  1606. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1607. }
  1608. fout << "\t@cd " << directory
  1609. << "; $(MAKE) " << target2 << "\n";
  1610. }
  1611. fout << "\n";
  1612. }
  1613. void
  1614. cmLocalUnixMakefileGenerator::
  1615. OutputSubDirectoryVars(std::ostream& fout,
  1616. const char* var,
  1617. const char* target,
  1618. const char* target1,
  1619. const char* target2,
  1620. const char* depend,
  1621. const std::vector<std::pair<cmStdString, bool> >& SubDirectories,
  1622. bool silent, int order)
  1623. {
  1624. if(!depend)
  1625. {
  1626. depend = "";
  1627. }
  1628. if( SubDirectories.size() == 0)
  1629. {
  1630. return;
  1631. }
  1632. fout << "# Variable for making " << target << " in subdirectories.\n";
  1633. fout << var << " = ";
  1634. unsigned int ii;
  1635. // make sure all the pre-order subdirectories are fist
  1636. // other than that keep the same order that the user specified
  1637. std::vector<std::pair<cmStdString, bool> > orderedDirs;
  1638. // collect pre-order first
  1639. for(ii =0; ii < SubDirectories.size(); ii++)
  1640. {
  1641. if(m_Makefile->IsDirectoryPreOrder(SubDirectories[ii].first.c_str()))
  1642. {
  1643. orderedDirs.push_back(SubDirectories[ii]);
  1644. }
  1645. }
  1646. // now collect post order dirs
  1647. for(ii =0; ii < SubDirectories.size(); ii++)
  1648. {
  1649. if(!m_Makefile->IsDirectoryPreOrder(SubDirectories[ii].first.c_str()))
  1650. {
  1651. orderedDirs.push_back(SubDirectories[ii]);
  1652. }
  1653. }
  1654. for(ii =0; ii < orderedDirs.size(); ii++)
  1655. {
  1656. if(!orderedDirs[ii].second)
  1657. {
  1658. continue;
  1659. }
  1660. if(order == 1 && m_Makefile->IsDirectoryPreOrder(orderedDirs[ii].first.c_str()))
  1661. {
  1662. continue;
  1663. }
  1664. if(order == 2 && !m_Makefile->IsDirectoryPreOrder(orderedDirs[ii].first.c_str()))
  1665. {
  1666. continue;
  1667. }
  1668. fout << " \\\n";
  1669. std::string subdir = FixDirectoryName(orderedDirs[ii].first.c_str());
  1670. fout << target << "_" << subdir.c_str();
  1671. }
  1672. fout << " \n\n";
  1673. fout << "# Targets for making " << target << " in subdirectories.\n";
  1674. std::string last = "";
  1675. for(unsigned int cc =0; cc < orderedDirs.size(); cc++)
  1676. {
  1677. if(!orderedDirs[cc].second)
  1678. {
  1679. continue;
  1680. }
  1681. std::string subdir = FixDirectoryName(orderedDirs[cc].first.c_str());
  1682. if(order == 1 && m_Makefile->IsDirectoryPreOrder(orderedDirs[cc].first.c_str()))
  1683. {
  1684. last = subdir;
  1685. continue;
  1686. }
  1687. if(order == 2 && !m_Makefile->IsDirectoryPreOrder(orderedDirs[cc].first.c_str()))
  1688. {
  1689. last = subdir;
  1690. continue;
  1691. }
  1692. fout << target << "_" << subdir.c_str() << ": " << depend;
  1693. // Make each subdirectory depend on previous one. This forces
  1694. // parallel builds (make -j 2) to build in same order as a single
  1695. // threaded build to avoid dependency problems.
  1696. if(cc > 0)
  1697. {
  1698. fout << " " << target << "_" << last.c_str();
  1699. }
  1700. fout << "\n";
  1701. last = subdir;
  1702. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1703. dir += "/";
  1704. dir += orderedDirs[cc].first;
  1705. this->BuildInSubDirectory(fout, dir.c_str(),
  1706. target1, target2, silent);
  1707. }
  1708. fout << "\n\n";
  1709. }
  1710. // output rules for decending into sub directories
  1711. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1712. {
  1713. // Output Sub directory build rules
  1714. const std::vector<std::pair<cmStdString, bool> >& SubDirectories
  1715. = m_Makefile->GetSubDirectories();
  1716. if( SubDirectories.size() == 0)
  1717. {
  1718. return;
  1719. }
  1720. this->OutputSubDirectoryVars(fout,
  1721. "SUBDIR_BUILD",
  1722. "default_target",
  1723. "default_target",
  1724. 0, "$(TARGETS)",
  1725. SubDirectories,
  1726. false, 1);
  1727. this->OutputSubDirectoryVars(fout,
  1728. "SUBDIR_PREORDER_BUILD",
  1729. "default_target",
  1730. "default_target",
  1731. 0, 0,
  1732. SubDirectories,
  1733. false, 2);
  1734. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1735. "clean",
  1736. 0, 0,
  1737. SubDirectories);
  1738. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1739. "depend",
  1740. 0, 0,
  1741. SubDirectories);
  1742. }
  1743. // Output the depend information for all the classes
  1744. // in the makefile. These would have been generated
  1745. // by the class cmMakeDepend GenerateMakefile
  1746. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1747. {
  1748. bool ret = false;
  1749. // Iterate over every target.
  1750. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1751. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1752. target != targets.end(); ++target)
  1753. {
  1754. // Iterate over every source for this target.
  1755. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1756. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1757. source != sources.end(); ++source)
  1758. {
  1759. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1760. {
  1761. if(!(*source)->GetDepends().empty())
  1762. {
  1763. // Iterate through all the dependencies for this source.
  1764. for(std::vector<std::string>::const_iterator dep =
  1765. (*source)->GetDepends().begin();
  1766. dep != (*source)->GetDepends().end(); ++dep)
  1767. {
  1768. std::string s = (*source)->GetSourceName();
  1769. std::string outExt =
  1770. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1771. (*source)->GetSourceExtension().c_str());
  1772. s += outExt;
  1773. fout << this->ConvertToRelativeOutputPath(s.c_str()) << " : "
  1774. << this->ConvertToRelativeOutputPath(dep->c_str()) << "\n";
  1775. ret = true;
  1776. }
  1777. fout << "\n\n";
  1778. }
  1779. }
  1780. }
  1781. }
  1782. return ret;
  1783. }
  1784. // Output the depend information for all the classes
  1785. // in the makefile. These would have been generated
  1786. // by the class cmMakeDepend GenerateMakefile
  1787. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1788. {
  1789. std::set<cmStdString> emittedLowerPath;
  1790. std::set<cmStdString> emitted;
  1791. // Iterate over every target.
  1792. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1793. fout << "# Suppresses display of executed commands\n";
  1794. fout << ".SILENT:\n";
  1795. fout << "# disable some common implicit rules to speed things up\n";
  1796. fout << ".SUFFIXES:\n";
  1797. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1798. this->OutputMakeVariables(fout);
  1799. fout << "default:\n";
  1800. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1801. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1802. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1803. target != targets.end(); ++target)
  1804. {
  1805. // Iterate over every source for this target.
  1806. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1807. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1808. source != sources.end(); ++source)
  1809. {
  1810. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1811. {
  1812. if(!(*source)->GetDepends().empty())
  1813. {
  1814. for(std::vector<std::string>::const_iterator dep =
  1815. (*source)->GetDepends().begin();
  1816. dep != (*source)->GetDepends().end(); ++dep)
  1817. {
  1818. // do not call CollapseFullPath on dep here, because it already
  1819. // has been done because m_FullPath on cmDependInformation
  1820. // always is it called. If it is called here, network builds are
  1821. // very slow because of the number of stats
  1822. std::string dependfile = this->ConvertToRelativeOutputPath(dep->c_str());
  1823. // use the lower path function to create uniqe names
  1824. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1825. if(emittedLowerPath.insert(lowerpath).second)
  1826. {
  1827. emitted.insert(dependfile);
  1828. fout << "all: " << dependfile << "\n";
  1829. }
  1830. }
  1831. }
  1832. }
  1833. }
  1834. }
  1835. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1836. std::set<cmStdString>::iterator i;
  1837. for(i = emitted.begin(); i != emitted.end(); ++i)
  1838. {
  1839. fout << "cmake.depends: " << *i << "\n";
  1840. }
  1841. fout << "cmake.depends: \n"
  1842. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1843. fout << "\n\n";
  1844. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1845. for(std::set<cmStdString>::iterator it = emitted.begin();
  1846. it != emitted.end(); ++it)
  1847. {
  1848. fout << *it << ":\n"
  1849. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1850. }
  1851. }
  1852. // Output each custom rule in the following format:
  1853. // output: source depends...
  1854. // (tab) command...
  1855. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1856. {
  1857. // we cannot provide multiple rules for a single output
  1858. // so we will keep track of outputs to make sure we don't write
  1859. // two rules. First found wins
  1860. std::set<cmStdString> processedOutputs;
  1861. // first output all custom rules
  1862. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  1863. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1864. i != sources.end(); ++i)
  1865. {
  1866. if ((*i)->GetCustomCommand())
  1867. {
  1868. cmCustomCommand *c = (*i)->GetCustomCommand();
  1869. // escape spaces and convert to native slashes path for
  1870. // the command
  1871. std::string comment = c->GetComment();
  1872. std::string command = c->GetCommand();
  1873. cmSystemTools::ReplaceString(command, "/./", "/");
  1874. command = this->ConvertToRelativeOutputPath(command.c_str());
  1875. command += " ";
  1876. // now add the arguments
  1877. command += c->GetArguments();
  1878. std::vector<std::string> depends;
  1879. // Collect out all the dependencies for this rule.
  1880. for(std::vector<std::string>::const_iterator d =
  1881. c->GetDepends().begin();
  1882. d != c->GetDepends().end(); ++d)
  1883. {
  1884. std::string dep = *d;
  1885. m_Makefile->ExpandVariablesInString(dep);
  1886. // watch for target dependencies,
  1887. std::string libPath = dep + "_CMAKE_PATH";
  1888. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1889. if ( cacheValue && *cacheValue )
  1890. {
  1891. libPath = cacheValue;
  1892. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH") &&
  1893. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH")[0] != '\0')
  1894. {
  1895. libPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  1896. }
  1897. libPath += "/";
  1898. libPath += dep;
  1899. libPath += cmSystemTools::GetExecutableExtension();
  1900. dep = libPath;
  1901. }
  1902. cmSystemTools::ReplaceString(dep, "/./", "/");
  1903. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  1904. dep = this->ConvertToRelativeOutputPath(dep.c_str());
  1905. depends.push_back(dep.c_str());
  1906. }
  1907. // output rule
  1908. if (processedOutputs.find(c->GetOutput()) == processedOutputs.end())
  1909. {
  1910. this->OutputMakeRule(fout,
  1911. (comment.size()?comment.c_str():"Custom command"),
  1912. c->GetOutput().c_str(),
  1913. depends,
  1914. command.c_str());
  1915. processedOutputs.insert(c->GetOutput());
  1916. }
  1917. else
  1918. {
  1919. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  1920. c->GetOutput().c_str());
  1921. }
  1922. }
  1923. }
  1924. }
  1925. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1926. {
  1927. const char* variables =
  1928. "# the standard shell for make\n"
  1929. "SHELL = /bin/sh\n"
  1930. "\n";
  1931. if(!m_WindowsShell)
  1932. {
  1933. fout << variables;
  1934. }
  1935. else
  1936. {
  1937. fout <<
  1938. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  1939. "NULL=\n"
  1940. "!ELSE \n"
  1941. "NULL=nul\n"
  1942. "!ENDIF \n";
  1943. }
  1944. if(m_MakeSilentFlag.size())
  1945. {
  1946. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  1947. }
  1948. std::string cmakecommand = this->ConvertToOutputForExisting(
  1949. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  1950. fout << "CMAKE_COMMAND = "
  1951. << cmakecommand
  1952. << "\n";
  1953. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  1954. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1955. {
  1956. fout << "CMAKE_EDIT_COMMAND = "
  1957. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1958. << "\n";
  1959. }
  1960. fout << "CMAKE_CURRENT_SOURCE = " <<
  1961. this->ConvertToRelativeOutputPath(m_Makefile->GetStartDirectory())
  1962. << "\n";
  1963. fout << "CMAKE_CURRENT_BINARY = " <<
  1964. this->ConvertToRelativeOutputPath(m_Makefile->GetStartOutputDirectory())
  1965. << "\n";
  1966. fout << "CMAKE_SOURCE_DIR = " <<
  1967. this->ConvertToRelativeOutputPath(m_Makefile->GetHomeDirectory())
  1968. << "\n";
  1969. fout << "CMAKE_BINARY_DIR = " <<
  1970. this->ConvertToRelativeOutputPath(m_Makefile->GetHomeOutputDirectory())
  1971. << "\n";
  1972. fout << "\n\n";
  1973. }
  1974. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1975. {
  1976. this->OutputMakeRule(fout,
  1977. "default build rule",
  1978. "all",
  1979. "cmake.depends $(SUBDIR_PREORDER_BUILD) $(TARGETS) $(SUBDIR_BUILD)",
  1980. 0);
  1981. this->OutputMakeRule(fout,
  1982. "clean generated files",
  1983. "clean",
  1984. "$(SUBDIR_CLEAN)",
  1985. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  1986. " $(TARGETS) $(TARGET_EXTRAS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES) $(ADDITIONAL_MAKE_CLEAN_FILES)");
  1987. // collect up all the sources
  1988. std::vector<std::string> allsources;
  1989. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1990. for(std::map<cmStdString,cmTarget>::const_iterator target = targets.begin();
  1991. target != targets.end(); ++target)
  1992. {
  1993. // Iterate over every source for this target.
  1994. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1995. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1996. source != sources.end(); ++source)
  1997. {
  1998. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1999. {
  2000. allsources.push_back(this->ConvertToRelativeOutputPath((*source)->GetFullPath().c_str()));
  2001. }
  2002. }
  2003. }
  2004. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  2005. checkCache += "/cmake.check_cache";
  2006. checkCache = this->ConvertToRelativeOutputPath(checkCache.c_str());
  2007. std::vector<std::string> cmake_depends;
  2008. cmake_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2009. this->OutputMakeRule(fout,
  2010. "dependencies.",
  2011. "cmake.depends",
  2012. cmake_depends,
  2013. "$(CMAKE_COMMAND) "
  2014. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2015. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2016. this->OutputMakeRule(fout,
  2017. "dependencies",
  2018. "cmake.check_depends",
  2019. allsources,
  2020. "$(CMAKE_COMMAND) "
  2021. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2022. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2023. this->OutputMakeRule(fout,
  2024. "dependencies",
  2025. "depend",
  2026. "$(SUBDIR_DEPEND)",
  2027. "$(CMAKE_COMMAND) "
  2028. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2029. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2030. this->OutputMakeRule(fout,
  2031. "dependencies",
  2032. "dependlocal",
  2033. 0,
  2034. "$(CMAKE_COMMAND) "
  2035. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2036. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2037. this->OutputMakeRule(fout,
  2038. "CMakeCache.txt",
  2039. "rebuild_cache",
  2040. 0,
  2041. "$(CMAKE_COMMAND) "
  2042. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2043. std::vector<std::string> check_cache_depends;
  2044. std::string CMakeCache = m_Makefile->GetHomeOutputDirectory();
  2045. CMakeCache += "/CMakeCache.txt";
  2046. CMakeCache = this->ConvertToRelativeOutputPath(CMakeCache.c_str());
  2047. check_cache_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2048. this->OutputMakeRule(fout,
  2049. "CMakeCache.txt because out-of-date:",
  2050. checkCache.c_str(),
  2051. check_cache_depends,
  2052. "$(CMAKE_COMMAND) "
  2053. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2054. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  2055. // called edit_cache
  2056. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2057. {
  2058. this->OutputMakeRule(fout,
  2059. "edit CMakeCache.txt",
  2060. "edit_cache",
  2061. 0,
  2062. "$(CMAKE_EDIT_COMMAND) "
  2063. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2064. }
  2065. else
  2066. {
  2067. this->OutputMakeRule(fout,
  2068. "edit CMakeCache.txt",
  2069. "edit_cache",
  2070. 0,
  2071. "$(CMAKE_COMMAND) "
  2072. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  2073. }
  2074. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  2075. cacheFile += "/CMakeCache.txt";
  2076. cacheFile = this->ConvertToRelativeOutputPath(cacheFile.c_str());
  2077. this->OutputMakeRule(fout,
  2078. "CMakeCache.txt",
  2079. cacheFile.c_str(),
  2080. 0,
  2081. "$(CMAKE_COMMAND) "
  2082. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2083. this->OutputMakeRule(fout,
  2084. "Rule to keep make from removing Makefiles "
  2085. "if control-C is hit during a run of cmake.",
  2086. ".PRECIOUS",
  2087. "Makefile cmake.depends",
  2088. 0);
  2089. this->OutputSourceObjectBuildRules(fout);
  2090. // find ctest
  2091. std::string ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  2092. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2093. ctest += "/";
  2094. ctest += "ctest";
  2095. ctest += cmSystemTools::GetExecutableExtension();
  2096. if(!cmSystemTools::FileExists(ctest.c_str()))
  2097. {
  2098. ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  2099. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2100. ctest += "/Debug/";
  2101. ctest += "ctest";
  2102. ctest += cmSystemTools::GetExecutableExtension();
  2103. }
  2104. if(!cmSystemTools::FileExists(ctest.c_str()))
  2105. {
  2106. ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  2107. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2108. ctest += "/Release/";
  2109. ctest += "ctest";
  2110. ctest += cmSystemTools::GetExecutableExtension();
  2111. }
  2112. if (!cmSystemTools::FileExists(ctest.c_str()))
  2113. {
  2114. if ( !m_Makefile->GetDefinition("PROJECT_NAME") ||
  2115. strcmp(m_Makefile->GetDefinition("PROJECT_NAME"), "CMake") )
  2116. {
  2117. return;
  2118. }
  2119. ctest = m_Makefile->GetRequiredDefinition("EXECUTABLE_OUTPUT_PATH");
  2120. ctest += "/ctest";
  2121. }
  2122. if (m_Makefile->IsOn("CMAKE_TESTING_ENABLED"))
  2123. {
  2124. fout << "ARGS=\n";
  2125. std::string cmd = this->ConvertToOutputForExisting(ctest.c_str());
  2126. cmd += " $(ARGS)";
  2127. this->OutputMakeRule(fout,
  2128. "tests",
  2129. "test",
  2130. "",
  2131. cmd.c_str());
  2132. }
  2133. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  2134. {
  2135. // We are building CMake itself. We cannot use the original
  2136. // executable to install over itself.
  2137. std::string rule = m_Makefile->GetRequiredDefinition("EXECUTABLE_OUTPUT_PATH");
  2138. rule += "/cmake";
  2139. rule = cmSystemTools::ConvertToOutputPath(rule.c_str());
  2140. rule += " -P cmake_install.cmake";
  2141. this->OutputMakeRule(fout, "installation", "install", "", rule.c_str());
  2142. }
  2143. else
  2144. {
  2145. this->OutputMakeRule(fout, "installation", "install", "",
  2146. "$(CMAKE_COMMAND) -P cmake_install.cmake");
  2147. }
  2148. }
  2149. void
  2150. cmLocalUnixMakefileGenerator::
  2151. OutputBuildObjectFromSource(std::ostream& fout,
  2152. const char* shortName,
  2153. const cmSourceFile& source,
  2154. const char* extraCompileFlags,
  2155. bool shared)
  2156. {
  2157. // Header files shouldn't have build rules.
  2158. if(source.GetPropertyAsBool("HEADER_FILE_ONLY") ||
  2159. m_GlobalGenerator->IgnoreFile(source.GetSourceExtension().c_str()))
  2160. {
  2161. return;
  2162. }
  2163. std::string outputExt =
  2164. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  2165. source.GetSourceExtension().c_str());
  2166. std::string objectFile = std::string(shortName) + outputExt;
  2167. objectFile = this->CreateSafeUniqueObjectFileName(objectFile.c_str());
  2168. objectFile = this->ConvertToRelativeOutputPath(objectFile.c_str());
  2169. std::vector<std::string> rules;
  2170. std::string flags;
  2171. if(extraCompileFlags)
  2172. {
  2173. flags += extraCompileFlags;
  2174. }
  2175. std::string sourceFile =
  2176. this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  2177. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2178. buildType = cmSystemTools::UpperCase(buildType);
  2179. // find out what language the source file is
  2180. const char* lang =
  2181. m_GlobalGenerator->GetLanguageFromExtension(source.GetSourceExtension().c_str());
  2182. // for now if the lang is defined add the rules and flags for it
  2183. std::string comment = outputExt;
  2184. comment += " file";
  2185. if(lang)
  2186. {
  2187. comment += " from ";
  2188. comment += lang;
  2189. comment += ": ";
  2190. if(comment.size() < 18)
  2191. {
  2192. comment.resize(18, ' ');
  2193. }
  2194. }
  2195. if(lang)
  2196. {
  2197. std::string varString = "CMAKE_";
  2198. varString += lang;
  2199. varString += "_COMPILE_OBJECT";
  2200. rules.push_back(m_Makefile->GetRequiredDefinition(varString.c_str()));
  2201. varString = "CMAKE_";
  2202. varString += lang;
  2203. varString += "_FLAGS";
  2204. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  2205. flags += " ";
  2206. if(buildType.size())
  2207. {
  2208. varString += "_";
  2209. varString += buildType;
  2210. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  2211. flags += " ";
  2212. }
  2213. if(shared)
  2214. {
  2215. varString = "CMAKE_SHARED_LIBRARY_";
  2216. varString += lang;
  2217. varString += "_FLAGS";
  2218. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  2219. flags += " ";
  2220. }
  2221. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2222. {
  2223. varString = "CMAKE_SHARED_BUILD_";
  2224. varString += lang;
  2225. varString += "_FLAGS";
  2226. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  2227. flags += " ";
  2228. }
  2229. }
  2230. else
  2231. {
  2232. // if the language is not defined and should not be ignored,
  2233. // then produce an error
  2234. cmSystemTools::Error("Unexpected file type ",
  2235. sourceFile.c_str());
  2236. }
  2237. flags += this->GetIncludeFlags(lang);
  2238. // expand multi-command semi-colon separated lists
  2239. // of commands into separate commands
  2240. std::vector<std::string> commands;
  2241. cmSystemTools::ExpandList(rules, commands);
  2242. for(std::vector<std::string>::iterator i = commands.begin();
  2243. i != commands.end(); ++i)
  2244. {
  2245. this->ExpandRuleVariables(*i,
  2246. lang,
  2247. 0, // no objects
  2248. 0, // no target
  2249. 0, // no link libs
  2250. sourceFile.c_str(),
  2251. objectFile.c_str(),
  2252. flags.c_str());
  2253. }
  2254. std::vector<std::string> sourceAndDeps;
  2255. sourceAndDeps.push_back(sourceFile);
  2256. // Check for extra object-file dependencies.
  2257. std::vector<std::string> depends;
  2258. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  2259. if(additionalDeps)
  2260. {
  2261. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  2262. for(std::vector<std::string>::iterator i = depends.begin();
  2263. i != depends.end(); ++i)
  2264. {
  2265. sourceAndDeps.push_back(this->ConvertToRelativeOutputPath(i->c_str()));
  2266. }
  2267. }
  2268. this->OutputMakeRule(fout,
  2269. comment.c_str(),
  2270. objectFile.c_str(),
  2271. sourceAndDeps,
  2272. commands);
  2273. }
  2274. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  2275. {
  2276. fout << "# Rules to build source files :\n\n";
  2277. std::set<cmStdString> rules;
  2278. // Iterate over every target.
  2279. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2280. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2281. target != targets.end(); ++target)
  2282. {
  2283. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2284. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  2285. std::string exportsDef = "";
  2286. if(shared)
  2287. {
  2288. std::string export_symbol;
  2289. if (const char* custom_export_name = target->second.GetProperty("DEFINE_SYMBOL"))
  2290. {
  2291. export_symbol = custom_export_name;
  2292. }
  2293. else
  2294. {
  2295. std::string in = target->first + "_EXPORTS";
  2296. export_symbol = cmSystemTools::MakeCindentifier(in.c_str());
  2297. }
  2298. exportsDef = "-D"+ export_symbol;
  2299. }
  2300. // Iterate over every source for this target.
  2301. const std::vector<cmSourceFile*>& sources =
  2302. target->second.GetSourceFiles();
  2303. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2304. source != sources.end(); ++source)
  2305. {
  2306. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  2307. !(*source)->GetCustomCommand())
  2308. {
  2309. std::string shortName;
  2310. std::string sourceName;
  2311. // If the full path to the source file includes this
  2312. // directory, we want to use the relative path for the
  2313. // filename of the object file. Otherwise, we will use just
  2314. // the filename portion.
  2315. if((cmSystemTools::GetFilenamePath(
  2316. (*source)->GetFullPath()).find(
  2317. m_Makefile->GetCurrentDirectory()) == 0)
  2318. || (cmSystemTools::GetFilenamePath(
  2319. (*source)->GetFullPath()).find(
  2320. m_Makefile->GetCurrentOutputDirectory()) == 0))
  2321. {
  2322. sourceName = (*source)->GetSourceName()+"."+
  2323. (*source)->GetSourceExtension();
  2324. shortName = (*source)->GetSourceName();
  2325. // The path may be relative. See if a directory needs to be
  2326. // created for the output file. This is a ugly, and perhaps
  2327. // should be moved elsewhere.
  2328. std::string relPath =
  2329. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  2330. if(relPath != "")
  2331. {
  2332. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  2333. outPath += "/"+relPath;
  2334. cmSystemTools::MakeDirectory(outPath.c_str());
  2335. }
  2336. }
  2337. else
  2338. {
  2339. sourceName = (*source)->GetFullPath();
  2340. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  2341. }
  2342. std::string shortNameWithExt = shortName +
  2343. (*source)->GetSourceExtension();
  2344. // Only output a rule for each .o once.
  2345. std::string compileFlags = exportsDef;
  2346. compileFlags += " ";
  2347. if(rules.find(shortNameWithExt) == rules.end())
  2348. {
  2349. if((*source)->GetProperty("COMPILE_FLAGS"))
  2350. {
  2351. compileFlags += (*source)->GetProperty("COMPILE_FLAGS");
  2352. compileFlags += " ";
  2353. }
  2354. this->OutputBuildObjectFromSource(fout,
  2355. shortName.c_str(),
  2356. *(*source),
  2357. compileFlags.c_str(),
  2358. shared);
  2359. rules.insert(shortNameWithExt);
  2360. }
  2361. }
  2362. }
  2363. }
  2364. }
  2365. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2366. const char* comment,
  2367. const char* target,
  2368. const char* depends,
  2369. const char* command,
  2370. const char* command2,
  2371. const char* command3,
  2372. const char* command4)
  2373. {
  2374. std::vector<std::string> commands;
  2375. if(command)
  2376. {
  2377. commands.push_back(command);
  2378. }
  2379. if(command2)
  2380. {
  2381. commands.push_back(command2);
  2382. }
  2383. if(command3)
  2384. {
  2385. commands.push_back(command3);
  2386. }
  2387. if(command4)
  2388. {
  2389. commands.push_back(command4);
  2390. }
  2391. this->OutputMakeRule(fout, comment, target, depends, commands);
  2392. }
  2393. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2394. const char* comment,
  2395. const char* target,
  2396. const char* depends,
  2397. const std::vector<std::string>& commands)
  2398. {
  2399. std::vector<std::string> depend;
  2400. if(depends)
  2401. {
  2402. depend.push_back(depends);
  2403. }
  2404. this->OutputMakeRule(fout, comment, target, depend, commands);
  2405. }
  2406. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2407. const char* comment,
  2408. const char* target,
  2409. const std::vector<std::string>& depends,
  2410. const char* command)
  2411. {
  2412. std::vector<std::string> commands;
  2413. if(command)
  2414. {
  2415. commands.push_back(command);
  2416. }
  2417. this->OutputMakeRule(fout, comment, target, depends, commands);
  2418. }
  2419. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2420. const char* comment,
  2421. const char* target,
  2422. const std::vector<std::string>& depends,
  2423. const std::vector<std::string>& commands)
  2424. {
  2425. if(!target)
  2426. {
  2427. cmSystemTools::Error("no target for OutputMakeRule");
  2428. return;
  2429. }
  2430. std::string replace;
  2431. if(comment)
  2432. {
  2433. replace = comment;
  2434. m_Makefile->ExpandVariablesInString(replace);
  2435. fout << "#---------------------------------------------------------\n";
  2436. fout << "# " << replace;
  2437. fout << "\n#\n";
  2438. }
  2439. fout << "\n";
  2440. replace = target;
  2441. m_Makefile->ExpandVariablesInString(replace);
  2442. std::string tgt = this->ConvertToRelativeOutputPath(replace.c_str());
  2443. tgt = this->ConvertToMakeTarget(tgt.c_str());
  2444. const char* space = "";
  2445. if(tgt.size() == 1)
  2446. {
  2447. // Add a space before the ":" to avoid drive letter confusion on
  2448. // Windows.
  2449. space = " ";
  2450. }
  2451. if(depends.empty())
  2452. {
  2453. fout << tgt.c_str() << space << ":\n";
  2454. }
  2455. else
  2456. {
  2457. // Split dependencies into multiple rule lines. This allows for
  2458. // very long dependency lists.
  2459. for(std::vector<std::string>::const_iterator dep = depends.begin();
  2460. dep != depends.end(); ++dep)
  2461. {
  2462. replace = *dep;
  2463. m_Makefile->ExpandVariablesInString(replace);
  2464. replace = this->ConvertToMakeTarget(replace.c_str());
  2465. fout << tgt.c_str() << space << ": " << replace.c_str() << "\n";
  2466. }
  2467. }
  2468. int count = 0;
  2469. for (std::vector<std::string>::const_iterator i = commands.begin();
  2470. i != commands.end(); ++i)
  2471. {
  2472. replace = *i;
  2473. m_Makefile->ExpandVariablesInString(replace);
  2474. if(count == 0 && replace.find_first_not_of(" \t\n\r") != std::string::npos &&
  2475. replace[0] != '-' && replace.find("echo") != 0
  2476. && replace.find("$(MAKE)") != 0)
  2477. {
  2478. std::string echostring = "Building ";
  2479. echostring += comment;
  2480. echostring += " ";
  2481. echostring += target;
  2482. echostring += "...";
  2483. this->OutputEcho(fout,echostring.c_str());
  2484. }
  2485. fout << "\t" << replace.c_str() << "\n";
  2486. count++;
  2487. }
  2488. fout << "\n";
  2489. }
  2490. std::string cmLocalUnixMakefileGenerator::LowerCasePath(const char* path)
  2491. {
  2492. #ifdef _WIN32
  2493. return cmSystemTools::LowerCase(path);
  2494. #else
  2495. return std::string(path);
  2496. #endif
  2497. }
  2498. std::string&
  2499. cmLocalUnixMakefileGenerator::CreateSafeUniqueObjectFileName(const char* sin)
  2500. {
  2501. if ( m_Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES") )
  2502. {
  2503. std::map<cmStdString,cmStdString>::iterator it = m_UniqueObjectNamesMap.find(sin);
  2504. if ( it == m_UniqueObjectNamesMap.end() )
  2505. {
  2506. std::string ssin = sin;
  2507. bool done;
  2508. int cc = 0;
  2509. char rpstr[100];
  2510. sprintf(rpstr, "_p_");
  2511. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2512. std::string sssin = sin;
  2513. do
  2514. {
  2515. done = true;
  2516. for ( it = m_UniqueObjectNamesMap.begin();
  2517. it != m_UniqueObjectNamesMap.end();
  2518. ++ it )
  2519. {
  2520. if ( it->second == ssin )
  2521. {
  2522. done = false;
  2523. }
  2524. }
  2525. if ( done )
  2526. {
  2527. break;
  2528. }
  2529. sssin = ssin;
  2530. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2531. sprintf(rpstr, "_p%d_", cc++);
  2532. }
  2533. while ( !done );
  2534. m_UniqueObjectNamesMap[sin] = ssin;
  2535. }
  2536. }
  2537. else
  2538. {
  2539. m_UniqueObjectNamesMap[sin] = sin;
  2540. }
  2541. return m_UniqueObjectNamesMap[sin];
  2542. }
  2543. std::string
  2544. cmLocalUnixMakefileGenerator::CreateMakeVariable(const char* sin, const char* s2in)
  2545. {
  2546. std::string s = sin;
  2547. std::string s2 = s2in;
  2548. std::string unmodified = s;
  2549. unmodified += s2;
  2550. // if there is no restriction on the length of make variables
  2551. // and there are no "." charactors in the string, then return the
  2552. // unmodified combination.
  2553. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  2554. {
  2555. return unmodified;
  2556. }
  2557. // see if the variable has been defined before and return
  2558. // the modified version of the variable
  2559. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2560. if(i != m_MakeVariableMap.end())
  2561. {
  2562. return i->second;
  2563. }
  2564. // start with the unmodified variable
  2565. std::string ret = unmodified;
  2566. // if this there is no value for m_MakefileVariableSize then
  2567. // the string must have bad characters in it
  2568. if(!m_MakefileVariableSize)
  2569. {
  2570. cmSystemTools::ReplaceString(ret, ".", "_");
  2571. int ni = 0;
  2572. char buffer[5];
  2573. // make sure the _ version is not already used, if
  2574. // it is used then add number to the end of the variable
  2575. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2576. {
  2577. ++ni;
  2578. sprintf(buffer, "%04d", ni);
  2579. ret = unmodified + buffer;
  2580. }
  2581. m_ShortMakeVariableMap[ret] = "1";
  2582. m_MakeVariableMap[unmodified] = ret;
  2583. return ret;
  2584. }
  2585. // if the string is greater the 32 chars it is an invalid vairable name
  2586. // for borland make
  2587. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2588. {
  2589. int keep = m_MakefileVariableSize - 8;
  2590. int size = keep + 3;
  2591. std::string str1 = s;
  2592. std::string str2 = s2;
  2593. // we must shorten the combined string by 4 charactors
  2594. // keep no more than 24 charactors from the second string
  2595. if(static_cast<int>(str2.size()) > keep)
  2596. {
  2597. str2 = str2.substr(0, keep);
  2598. }
  2599. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2600. {
  2601. str1 = str1.substr(0, size - str2.size());
  2602. }
  2603. char buffer[5];
  2604. int ni = 0;
  2605. sprintf(buffer, "%04d", ni);
  2606. ret = str1 + str2 + buffer;
  2607. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2608. {
  2609. ++ni;
  2610. sprintf(buffer, "%04d", ni);
  2611. ret = str1 + str2 + buffer;
  2612. }
  2613. if(ni == 1000)
  2614. {
  2615. cmSystemTools::Error("Borland makefile variable length too long");
  2616. return unmodified;
  2617. }
  2618. // once an unused variable is found
  2619. m_ShortMakeVariableMap[ret] = "1";
  2620. }
  2621. // always make an entry into the unmodified to variable map
  2622. m_MakeVariableMap[unmodified] = ret;
  2623. return ret;
  2624. }
  2625. void cmLocalUnixMakefileGenerator::GetLibraryNames(const char* n,
  2626. const cmTarget& t,
  2627. std::string& name,
  2628. std::string& soName,
  2629. std::string& realName,
  2630. std::string& baseName)
  2631. {
  2632. // Check for library version properties.
  2633. const char* version = t.GetProperty("VERSION");
  2634. const char* soversion = t.GetProperty("SOVERSION");
  2635. if((t.GetType() != cmTarget::SHARED_LIBRARY &&
  2636. t.GetType() != cmTarget::MODULE_LIBRARY) ||
  2637. !m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  2638. {
  2639. // Versioning is supported only for shared libraries and modules,
  2640. // and then only when the platform supports an soname flag.
  2641. version = 0;
  2642. soversion = 0;
  2643. }
  2644. if(version && !soversion)
  2645. {
  2646. // The soversion must be set if the library version is set. Use
  2647. // the library version as the soversion.
  2648. soversion = version;
  2649. }
  2650. // The library name.
  2651. name = this->GetFullTargetName(n, t);
  2652. // The library's soname.
  2653. soName = name;
  2654. if(soversion)
  2655. {
  2656. soName += ".";
  2657. soName += soversion;
  2658. }
  2659. // The library's real name on disk.
  2660. realName = name;
  2661. if(version)
  2662. {
  2663. realName += ".";
  2664. realName += version;
  2665. }
  2666. else if(soversion)
  2667. {
  2668. realName += ".";
  2669. realName += soversion;
  2670. }
  2671. // The library name without extension.
  2672. baseName = this->GetBaseTargetName(n, t);
  2673. }
  2674. std::string cmLocalUnixMakefileGenerator::ConvertToMakeTarget(const char* tgt)
  2675. {
  2676. // Make targets should not have a leading './' for a file in the
  2677. // directory containing the makefile.
  2678. std::string ret = tgt;
  2679. if(ret.size() > 2 &&
  2680. (ret[0] == '.') &&
  2681. ( (ret[1] == '/') || ret[1] == '\\'))
  2682. {
  2683. std::string upath = ret;
  2684. cmSystemTools::ConvertToUnixSlashes(upath);
  2685. if(upath.find(2, '/') == upath.npos)
  2686. {
  2687. ret = ret.substr(2, ret.size()-2);
  2688. }
  2689. }
  2690. return ret;
  2691. }