cmLocalUnixMakefileGenerator.cxx 99 KB

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