cmLocalUnixMakefileGenerator.cxx 95 KB

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