cmLocalUnixMakefileGenerator.cxx 105 KB

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