cmLocalUnixMakefileGenerator.cxx 98 KB

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