cmLocalUnixMakefileGenerator.cxx 107 KB

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