cmLocalUnixMakefileGenerator.cxx 96 KB

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