cmLocalUnixMakefileGenerator.cxx 108 KB

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