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