cmLocalUnixMakefileGenerator.cxx 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225
  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. std::string outpath;
  1012. std::string outdir = this->ConvertToRelativeOutputPath(m_LibraryOutputPath.c_str());
  1013. if(!m_WindowsShell && outdir.size())
  1014. {
  1015. outpath = "\"`cd ";
  1016. }
  1017. outpath += outdir;
  1018. if(!m_WindowsShell && outdir.size())
  1019. {
  1020. outpath += ";pwd`\"/";
  1021. }
  1022. if(outdir.size() == 0 && !m_WindowsShell)
  1023. {
  1024. outpath = "\"`pwd`\"/";
  1025. }
  1026. // The full path versions of the names.
  1027. std::string targetFullPath = outpath + targetName;
  1028. std::string targetFullPathSO = outpath + targetNameSO;
  1029. std::string targetFullPathReal = outpath + targetNameReal;
  1030. std::string targetFullPathBase = outpath + targetNameBase;
  1031. // get the objects that are used to link this library
  1032. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  1033. std::string objsQuoted = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ") ";
  1034. // create a variable with the objects that this library depends on
  1035. std::string depend =
  1036. objs + " $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1037. std::vector<std::string> commands;
  1038. std::string cmakecommand = this->ConvertToOutputForExisting(
  1039. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  1040. // Remove any existing files for this library.
  1041. std::string remove = cmakecommand;
  1042. remove += " -E remove -f ";
  1043. remove += targetFullPathReal;
  1044. if(targetFullPathSO != targetFullPathReal)
  1045. {
  1046. remove += " ";
  1047. remove += targetFullPathSO;
  1048. }
  1049. if(targetFullPath != targetFullPathSO &&
  1050. targetFullPath != targetFullPathReal)
  1051. {
  1052. remove += " ";
  1053. remove += targetFullPath;
  1054. }
  1055. commands.push_back(remove);
  1056. // collect custom commands for this target and add them to the list
  1057. std::string customCommands = this->CreatePreBuildRules(t, name);
  1058. if(customCommands.size() > 0)
  1059. {
  1060. commands.push_back(customCommands);
  1061. }
  1062. // collect custom commands for this target and add them to the list
  1063. customCommands = this->CreatePreLinkRules(t, name);
  1064. if(customCommands.size() > 0)
  1065. {
  1066. commands.push_back(customCommands);
  1067. }
  1068. // collect up the build rules
  1069. std::vector<std::string> rules;
  1070. rules.push_back(m_Makefile->GetDefinition(createVariable));
  1071. // expand multi-command semi-colon separated lists
  1072. // of commands into separate commands
  1073. cmSystemTools::ExpandList(rules, commands);
  1074. // Create necessary symlinks for library.
  1075. if(targetFullPath != targetFullPathReal)
  1076. {
  1077. std::string symlink = cmakecommand;
  1078. symlink += " -E cmake_symlink_library ";
  1079. symlink += targetFullPathReal;
  1080. symlink += " ";
  1081. symlink += targetFullPathSO;
  1082. symlink += " ";
  1083. symlink += targetFullPath;
  1084. commands.push_back(symlink);
  1085. }
  1086. // collect custom commands for this target and add them to the list
  1087. customCommands = this->CreatePostBuildRules(t, name);
  1088. if(customCommands.size() > 0)
  1089. {
  1090. commands.push_back(customCommands);
  1091. }
  1092. // collect up the link libraries
  1093. cmOStringStream linklibs;
  1094. this->OutputLinkLibraries(linklibs, name, t);
  1095. for(std::vector<std::string>::iterator i = commands.begin();
  1096. i != commands.end(); ++i)
  1097. {
  1098. this->ExpandRuleVariables(*i,
  1099. objs.c_str(),
  1100. targetFullPathReal.c_str(),
  1101. linklibs.str().c_str(),
  1102. 0, 0, 0, objsQuoted.c_str(),
  1103. targetFullPathBase.c_str(),
  1104. targetNameSO.c_str(),
  1105. linkFlags);
  1106. }
  1107. targetFullPath = m_LibraryOutputPath + targetName;
  1108. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  1109. this->OutputMakeRule(fout, comment,
  1110. targetFullPath.c_str(),
  1111. depend.c_str(),
  1112. commands);
  1113. }
  1114. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  1115. const char* name,
  1116. const cmTarget &t)
  1117. {
  1118. const char* createRule;
  1119. if(t.HasCxx())
  1120. {
  1121. createRule = "CMAKE_CXX_CREATE_SHARED_LIBRARY";
  1122. }
  1123. else
  1124. {
  1125. createRule = "CMAKE_C_CREATE_SHARED_LIBRARY";
  1126. }
  1127. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1128. buildType = cmSystemTools::UpperCase(buildType);
  1129. std::string linkFlags = this->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1130. linkFlags += " ";
  1131. if(buildType.size())
  1132. {
  1133. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  1134. build += buildType;
  1135. linkFlags += this->GetSafeDefinition(build.c_str());
  1136. linkFlags += " ";
  1137. }
  1138. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1139. {
  1140. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  1141. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1142. i != sources.end(); ++i)
  1143. {
  1144. if((*i)->GetSourceExtension() == "def")
  1145. {
  1146. linkFlags += this->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1147. linkFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  1148. linkFlags += " ";
  1149. }
  1150. }
  1151. }
  1152. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1153. if(targetLinkFlags)
  1154. {
  1155. linkFlags += targetLinkFlags;
  1156. linkFlags += " ";
  1157. }
  1158. this->OutputLibraryRule(fout, name, t,
  1159. createRule,
  1160. "shared library",
  1161. linkFlags.c_str());
  1162. }
  1163. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  1164. const char* name,
  1165. const cmTarget &t)
  1166. {
  1167. const char* createRule;
  1168. if(t.HasCxx())
  1169. {
  1170. createRule = "CMAKE_CXX_CREATE_SHARED_MODULE";
  1171. }
  1172. else
  1173. {
  1174. createRule = "CMAKE_C_CREATE_SHARED_MODULE";
  1175. }
  1176. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1177. buildType = cmSystemTools::UpperCase(buildType);
  1178. std::string linkFlags = this->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1179. linkFlags += " ";
  1180. if(buildType.size())
  1181. {
  1182. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  1183. build += buildType;
  1184. linkFlags += this->GetSafeDefinition(build.c_str());
  1185. linkFlags += " ";
  1186. }
  1187. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1188. if(targetLinkFlags)
  1189. {
  1190. linkFlags += targetLinkFlags;
  1191. linkFlags += " ";
  1192. }
  1193. this->OutputLibraryRule(fout, name, t,
  1194. createRule,
  1195. "shared module",
  1196. linkFlags.c_str());
  1197. }
  1198. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  1199. const char* name,
  1200. const cmTarget &t)
  1201. {
  1202. const char* createRule;
  1203. if(t.HasCxx())
  1204. {
  1205. createRule = "CMAKE_CXX_CREATE_STATIC_LIBRARY";
  1206. }
  1207. else
  1208. {
  1209. createRule = "CMAKE_C_CREATE_STATIC_LIBRARY";
  1210. }
  1211. std::string linkFlags;
  1212. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1213. if(targetLinkFlags)
  1214. {
  1215. linkFlags += targetLinkFlags;
  1216. linkFlags += " ";
  1217. }
  1218. this->OutputLibraryRule(fout, name, t,
  1219. createRule,
  1220. "static library",
  1221. linkFlags.c_str());
  1222. }
  1223. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  1224. const char* name,
  1225. const cmTarget &t)
  1226. {
  1227. std::string linkFlags;
  1228. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1229. buildType = cmSystemTools::UpperCase(buildType);
  1230. std::string flags;
  1231. // Construct the full path to the executable that will be generated.
  1232. std::string target = m_ExecutableOutputPath;
  1233. if(target.length() == 0)
  1234. {
  1235. target = m_Makefile->GetCurrentOutputDirectory();
  1236. if(target.size() && target[target.size()-1] != '/')
  1237. {
  1238. target += "/";
  1239. }
  1240. }
  1241. target += name;
  1242. target += cmSystemTools::GetExecutableExtension();
  1243. target = this->ConvertToRelativeOutputPath(target.c_str());
  1244. cmSystemTools::ConvertToUnixSlashes(target);
  1245. bool needsLocalTarget = false;
  1246. unsigned int startPos = 2;
  1247. if(m_Makefile->GetDefinition("BORLAND"))
  1248. {
  1249. // the borland makefiles treat .\target and target as different
  1250. // targets. All other makes treat them the same
  1251. startPos = 0;
  1252. }
  1253. if(target.find('/', startPos) != target.npos)
  1254. {
  1255. needsLocalTarget = true;
  1256. }
  1257. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  1258. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  1259. std::string depend = "$(";
  1260. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  1261. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1262. std::vector<std::string> rules;
  1263. linkFlags += this->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1264. linkFlags += " ";
  1265. if(buildType.size())
  1266. {
  1267. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1268. build += buildType;
  1269. linkFlags += this->GetSafeDefinition(build.c_str());
  1270. linkFlags += " ";
  1271. }
  1272. if(t.HasCxx())
  1273. {
  1274. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_LINK_EXECUTABLE"));
  1275. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  1276. flags += " ";
  1277. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  1278. flags += " ";
  1279. }
  1280. else
  1281. {
  1282. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_LINK_EXECUTABLE"));
  1283. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  1284. flags += " ";
  1285. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  1286. flags += " ";
  1287. }
  1288. cmOStringStream linklibs;
  1289. this->OutputLinkLibraries(linklibs, 0, t);
  1290. std::string comment = "executable";
  1291. std::vector<std::string> commands;
  1292. std::string customCommands = this->CreatePreBuildRules(t, name);
  1293. if(customCommands.size() > 0)
  1294. {
  1295. commands.push_back(customCommands.c_str());
  1296. }
  1297. customCommands = this->CreatePreLinkRules(t, name);
  1298. if(customCommands.size() > 0)
  1299. {
  1300. commands.push_back(customCommands.c_str());
  1301. }
  1302. cmSystemTools::ExpandList(rules, commands);
  1303. customCommands = this->CreatePostBuildRules(t, name);
  1304. if(customCommands.size() > 0)
  1305. {
  1306. commands.push_back(customCommands.c_str());
  1307. }
  1308. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1309. {
  1310. linkFlags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  1311. linkFlags += " ";
  1312. }
  1313. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  1314. {
  1315. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1316. linkFlags += " ";
  1317. }
  1318. else
  1319. {
  1320. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1321. linkFlags += " ";
  1322. }
  1323. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1324. if(targetLinkFlags)
  1325. {
  1326. linkFlags += targetLinkFlags;
  1327. linkFlags += " ";
  1328. }
  1329. for(std::vector<std::string>::iterator i = commands.begin();
  1330. i != commands.end(); ++i)
  1331. {
  1332. this->ExpandRuleVariables(*i,
  1333. objs.c_str(),
  1334. target.c_str(),
  1335. linklibs.str().c_str(),
  1336. 0,
  1337. 0,
  1338. flags.c_str(),
  1339. 0,
  1340. 0,
  1341. 0,
  1342. linkFlags.c_str());
  1343. }
  1344. this->OutputMakeRule(fout,
  1345. comment.c_str(),
  1346. target.c_str(),
  1347. depend.c_str(),
  1348. commands);
  1349. // If there is no executable output path, add a rule with the
  1350. // relative path to the executable. This is necessary for
  1351. // try-compile to work in this case.
  1352. if(needsLocalTarget)
  1353. {
  1354. depend = target;
  1355. target = name;
  1356. target += cmSystemTools::GetExecutableExtension();
  1357. target = this->ConvertToRelativeOutputPath(target.c_str());
  1358. commands.resize(0);
  1359. this->OutputMakeRule(fout,
  1360. comment.c_str(),
  1361. target.c_str(),
  1362. depend.c_str(),
  1363. commands);
  1364. }
  1365. }
  1366. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  1367. const char* name,
  1368. const cmTarget &t)
  1369. {
  1370. const char* cc = 0;
  1371. std::string customCommands = this->CreatePreBuildRules(t, name);
  1372. std::string customCommands2 = this->CreatePreLinkRules(t, name);
  1373. if(customCommands2.size() > 0)
  1374. {
  1375. customCommands += customCommands2;
  1376. }
  1377. customCommands2 = this->CreatePostBuildRules(t, name);
  1378. if(customCommands2.size() > 0)
  1379. {
  1380. customCommands += customCommands2;
  1381. }
  1382. if(customCommands.size() > 0)
  1383. {
  1384. cc = customCommands.c_str();
  1385. }
  1386. std::string comment = "Utility";
  1387. std::string depends;
  1388. depends = "$(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1389. std::string replaceVars;
  1390. const std::vector<cmCustomCommand> &ccs = t.GetPostBuildCommands();
  1391. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1392. i != ccs.end(); ++i)
  1393. {
  1394. const std::vector<std::string> & dep = i->GetDepends();
  1395. for(std::vector<std::string>::const_iterator d = dep.begin();
  1396. d != dep.end(); ++d)
  1397. {
  1398. depends += " \\\n";
  1399. replaceVars = *d;
  1400. m_Makefile->ExpandVariablesInString(replaceVars);
  1401. depends += this->ConvertToRelativeOutputPath(replaceVars.c_str());
  1402. }
  1403. }
  1404. this->OutputMakeRule(fout, comment.c_str(), name,
  1405. depends.c_str(), cc);
  1406. }
  1407. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  1408. {
  1409. // for each target
  1410. const cmTargets &tgts = m_Makefile->GetTargets();
  1411. for(cmTargets::const_iterator l = tgts.begin();
  1412. l != tgts.end(); l++)
  1413. {
  1414. switch(l->second.GetType())
  1415. {
  1416. case cmTarget::STATIC_LIBRARY:
  1417. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  1418. break;
  1419. case cmTarget::SHARED_LIBRARY:
  1420. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  1421. break;
  1422. case cmTarget::MODULE_LIBRARY:
  1423. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  1424. break;
  1425. case cmTarget::EXECUTABLE:
  1426. case cmTarget::WIN32_EXECUTABLE:
  1427. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  1428. break;
  1429. case cmTarget::UTILITY:
  1430. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  1431. break;
  1432. // This is handled by the OutputCustomRules method
  1433. case cmTarget::INSTALL_FILES:
  1434. // This is handled by the OutputInstallRules method
  1435. case cmTarget::INSTALL_PROGRAMS:
  1436. // This is handled by the OutputInstallRules method
  1437. break;
  1438. }
  1439. }
  1440. }
  1441. // For each target that is an executable or shared library, generate
  1442. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1443. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1444. {
  1445. // Build a set of libraries that will be linked into any target in
  1446. // this directory.
  1447. std::set<std::string> used;
  1448. // for each target
  1449. const cmTargets &tgts = m_Makefile->GetTargets();
  1450. for(cmTargets::const_iterator l = tgts.begin();
  1451. l != tgts.end(); l++)
  1452. {
  1453. // Each dependency should only be emitted once per target.
  1454. std::set<std::string> emitted;
  1455. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1456. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1457. || (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  1458. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1459. || (l->second.GetType() == cmTarget::UTILITY)
  1460. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  1461. {
  1462. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1463. // A library should not depend on itself!
  1464. emitted.insert(l->first);
  1465. // for static libraries do not depend on other libraries
  1466. if(l->second.GetType() != cmTarget::STATIC_LIBRARY)
  1467. {
  1468. // Now, look at all link libraries specific to this target.
  1469. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1470. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1471. lib != tlibs.end(); ++lib)
  1472. {
  1473. // Record that this library was used.
  1474. used.insert(lib->first);
  1475. // Don't emit the same library twice for this target.
  1476. if(emitted.insert(lib->first).second)
  1477. {
  1478. // Output this dependency.
  1479. this->OutputLibDepend(fout, lib->first.c_str());
  1480. }
  1481. }
  1482. }
  1483. // for all targets depend on utilities
  1484. // Now, look at all utilities specific to this target.
  1485. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1486. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1487. util != tutils.end(); ++util)
  1488. {
  1489. // Record that this utility was used.
  1490. used.insert(*util);
  1491. // Don't emit the same utility twice for this target.
  1492. if(emitted.insert(*util).second)
  1493. {
  1494. // Output this dependency.
  1495. this->OutputExeDepend(fout, util->c_str());
  1496. }
  1497. }
  1498. fout << "\n";
  1499. }
  1500. }
  1501. fout << "\n";
  1502. // Loop over the libraries used and make sure there is a rule to
  1503. // build them in this makefile. If the library is in another
  1504. // directory, add a rule to jump to that directory and make sure it
  1505. // exists.
  1506. for(std::set<std::string>::const_iterator lib = used.begin();
  1507. lib != used.end(); ++lib)
  1508. {
  1509. // loop over the list of directories that the libraries might
  1510. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1511. // be stored in the cache
  1512. std::string libPath = *lib + "_CMAKE_PATH";
  1513. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1514. // if cache and not the current directory add a rule, to
  1515. // jump into the directory and build for the first time
  1516. if(cacheValue && *cacheValue &&
  1517. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1518. {
  1519. // add the correct extension
  1520. std::string ltname = *lib+"_LIBRARY_TYPE";
  1521. const char* libType
  1522. = m_Makefile->GetDefinition(ltname.c_str());
  1523. // if it was a library..
  1524. if (libType)
  1525. {
  1526. std::string library;
  1527. std::string libpath = cacheValue;
  1528. if(libType && std::string(libType) == "SHARED")
  1529. {
  1530. library = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1531. library += *lib;
  1532. library += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1533. }
  1534. else if(libType && std::string(libType) == "MODULE")
  1535. {
  1536. library = this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1537. library += *lib;
  1538. library += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1539. }
  1540. else if(libType && std::string(libType) == "STATIC")
  1541. {
  1542. library = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1543. library += *lib;
  1544. library += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1545. }
  1546. else
  1547. {
  1548. cmSystemTools::Error("Unknown library type!");
  1549. return;
  1550. }
  1551. if(m_LibraryOutputPath.size())
  1552. {
  1553. libpath = m_LibraryOutputPath;
  1554. }
  1555. else
  1556. {
  1557. libpath += "/";
  1558. }
  1559. libpath += library;
  1560. // put out a rule to build the library if it does not exist
  1561. this->OutputBuildTargetInDir(fout,
  1562. cacheValue,
  1563. library.c_str(),
  1564. libpath.c_str(),
  1565. m_Makefile->
  1566. GetDefinition("LIBRARY_OUTPUT_PATH")
  1567. );
  1568. }
  1569. // something other than a library...
  1570. else
  1571. {
  1572. std::string exepath = cacheValue;
  1573. if(m_ExecutableOutputPath.size())
  1574. {
  1575. exepath = m_ExecutableOutputPath;
  1576. }
  1577. else
  1578. {
  1579. exepath += "/";
  1580. }
  1581. std::string fullName = lib->c_str();
  1582. fullName += cmSystemTools::GetExecutableExtension();
  1583. exepath += fullName;
  1584. this->OutputBuildTargetInDir(fout,
  1585. cacheValue,
  1586. fullName.c_str(),
  1587. exepath.c_str(),
  1588. m_Makefile->
  1589. GetDefinition("EXECUTABLE_OUTPUT_PATH")
  1590. );
  1591. }
  1592. }
  1593. }
  1594. }
  1595. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1596. const char* path,
  1597. const char* library,
  1598. const char* fullpath,
  1599. const char* libOutPath)
  1600. {
  1601. const char* makeTarget = library;
  1602. std::string jumpBack =
  1603. cmSystemTools::RelativePath(cmSystemTools::GetProgramPath(fullpath).c_str(),
  1604. m_Makefile->GetCurrentOutputDirectory());
  1605. jumpBack = this->ConvertToOutputForExisting(jumpBack.c_str());
  1606. std::string wpath = this->ConvertToOutputForExisting(path);
  1607. std::string wfullpath = this->ConvertToOutputForExisting(fullpath);
  1608. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  1609. {
  1610. makeTarget = wfullpath.c_str();
  1611. }
  1612. fout << wfullpath
  1613. << ":\n\tcd " << wpath << "\n"
  1614. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1615. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1616. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1617. << "\t$(MAKE) $(MAKESILENT) " << makeTarget
  1618. << "\n\tcd " << jumpBack << "\n";
  1619. }
  1620. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1621. const char* path,
  1622. const char* library,
  1623. const char* fullpath,
  1624. const char* outputPath)
  1625. {
  1626. if(m_WindowsShell)
  1627. {
  1628. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath, outputPath);
  1629. return;
  1630. }
  1631. const char* makeTarget = library;
  1632. if(outputPath && strcmp( outputPath, "" ) != 0)
  1633. {
  1634. makeTarget = fullpath;
  1635. }
  1636. fout << this->ConvertToOutputForExisting(fullpath)
  1637. << ":\n\tcd " << this->ConvertToOutputForExisting(path)
  1638. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1639. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1640. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1641. << "; $(MAKE) $(MAKESILENT) "
  1642. << this->ConvertToRelativeOutputPath(makeTarget) << "\n\n";
  1643. }
  1644. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1645. {
  1646. if (strcmp(path1, path2) == 0)
  1647. {
  1648. return true;
  1649. }
  1650. #if defined(_WIN32) || defined(__APPLE__)
  1651. return
  1652. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1653. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1654. #else
  1655. return false;
  1656. #endif
  1657. }
  1658. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1659. const char* name)
  1660. {
  1661. std::string libPath = name;
  1662. libPath += "_CMAKE_PATH";
  1663. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1664. if( cacheValue && *cacheValue )
  1665. {
  1666. // if there is a cache value, then this is a library that cmake
  1667. // knows how to build, so we can depend on it
  1668. std::string libpath;
  1669. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1670. {
  1671. // if the library is not in the current directory, then get the full
  1672. // path to it
  1673. if(m_LibraryOutputPath.size())
  1674. {
  1675. libpath = m_LibraryOutputPath;
  1676. }
  1677. else
  1678. {
  1679. libpath = cacheValue;
  1680. libpath += "/";
  1681. }
  1682. }
  1683. else
  1684. {
  1685. // library is in current Makefile so use lib as a prefix
  1686. libpath = m_LibraryOutputPath;
  1687. }
  1688. // add the correct extension
  1689. std::string ltname = name;
  1690. ltname += "_LIBRARY_TYPE";
  1691. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1692. if(libType && std::string(libType) == "SHARED")
  1693. {
  1694. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1695. libpath += name;
  1696. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1697. }
  1698. else if (libType && std::string(libType) == "MODULE")
  1699. {
  1700. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1701. libpath += name;
  1702. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1703. }
  1704. else if (libType && std::string(libType) == "STATIC")
  1705. {
  1706. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1707. libpath += name;
  1708. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1709. }
  1710. fout << this->ConvertToRelativeOutputPath(libpath.c_str()) << " ";
  1711. }
  1712. }
  1713. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1714. const char* name)
  1715. {
  1716. std::string exePath = name;
  1717. exePath += "_CMAKE_PATH";
  1718. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1719. if( cacheValue && *cacheValue )
  1720. {
  1721. // if there is a cache value, then this is a executable/utility that cmake
  1722. // knows how to build, so we can depend on it
  1723. std::string exepath;
  1724. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1725. {
  1726. // if the exe/utility is not in the current directory, then get the full
  1727. // path to it
  1728. if(m_ExecutableOutputPath.size())
  1729. {
  1730. exepath = m_ExecutableOutputPath;
  1731. }
  1732. else
  1733. {
  1734. exepath = cacheValue;
  1735. exepath += "/";
  1736. }
  1737. }
  1738. else
  1739. {
  1740. // library is in current Makefile
  1741. exepath = m_ExecutableOutputPath;
  1742. }
  1743. // add the library name
  1744. exepath += name;
  1745. // add the correct extension
  1746. exepath += cmSystemTools::GetExecutableExtension();
  1747. fout << this->ConvertToRelativeOutputPath(exepath.c_str()) << " ";
  1748. }
  1749. // if it isn't in the cache, it might still be a utility target
  1750. // so check for that
  1751. else
  1752. {
  1753. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1754. if (targets.find(name) != targets.end())
  1755. {
  1756. fout << name << " ";
  1757. }
  1758. }
  1759. }
  1760. // fix up names of directories so they can be used
  1761. // as targets in makefiles.
  1762. inline std::string FixDirectoryName(const char* dir)
  1763. {
  1764. std::string s = dir;
  1765. // replace ../ with 3 under bars
  1766. size_t pos = s.find("../");
  1767. if(pos != std::string::npos)
  1768. {
  1769. s.replace(pos, 3, "___");
  1770. }
  1771. // replace / directory separators with a single under bar
  1772. pos = s.find("/");
  1773. while(pos != std::string::npos)
  1774. {
  1775. s.replace(pos, 1, "_");
  1776. pos = s.find("/");
  1777. }
  1778. return s;
  1779. }
  1780. void cmLocalUnixMakefileGenerator::
  1781. BuildInSubDirectoryWindows(std::ostream& fout,
  1782. const char* directory,
  1783. const char* target1,
  1784. const char* target2,
  1785. bool silent)
  1786. {
  1787. std::string dir;
  1788. if(target1)
  1789. {
  1790. dir = this->ConvertToOutputForExisting(directory);
  1791. if(dir[0] == '\"')
  1792. {
  1793. dir = dir.substr(1, dir.size()-2);
  1794. }
  1795. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1796. << " "
  1797. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1798. if (!silent)
  1799. {
  1800. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1801. }
  1802. fout << "\tcd " << dir << "\n"
  1803. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1804. }
  1805. if(target2)
  1806. {
  1807. if (!silent)
  1808. {
  1809. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1810. }
  1811. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1812. }
  1813. std::string currentDir = dir;
  1814. cmSystemTools::ConvertToUnixSlashes(currentDir);
  1815. std::string cdback = "..";
  1816. unsigned int i = 0;
  1817. if(currentDir.size() > 2 && currentDir[0] == '.' && currentDir[1] == '/')
  1818. {
  1819. // start past ./ if it starts with ./
  1820. i = 2;
  1821. }
  1822. for(; i < currentDir.size(); ++i)
  1823. {
  1824. if(currentDir[i] == '/')
  1825. {
  1826. cdback += "/..";
  1827. }
  1828. }
  1829. fout << "\tcd " << this->ConvertToOutputForExisting(cdback.c_str()) << "\n\n";
  1830. }
  1831. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1832. const char* dir,
  1833. const char* target1,
  1834. const char* target2,
  1835. bool silent)
  1836. {
  1837. if(m_WindowsShell)
  1838. {
  1839. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1840. return;
  1841. }
  1842. std::string directory = this->ConvertToRelativeOutputPath(dir);
  1843. if(target1)
  1844. {
  1845. fout << "\t@if test ! -d " << directory
  1846. << "; then $(MAKE) rebuild_cache; fi\n";
  1847. if (!silent)
  1848. {
  1849. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1850. }
  1851. fout << "\t@cd " << directory
  1852. << "; $(MAKE) " << target1 << "\n";
  1853. }
  1854. if(target2)
  1855. {
  1856. if (!silent)
  1857. {
  1858. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1859. }
  1860. fout << "\t@cd " << directory
  1861. << "; $(MAKE) " << target2 << "\n";
  1862. }
  1863. fout << "\n";
  1864. }
  1865. void
  1866. cmLocalUnixMakefileGenerator::
  1867. OutputSubDirectoryVars(std::ostream& fout,
  1868. const char* var,
  1869. const char* target,
  1870. const char* target1,
  1871. const char* target2,
  1872. const char* depend,
  1873. const std::vector<std::string>& SubDirectories,
  1874. bool silent)
  1875. {
  1876. if(!depend)
  1877. {
  1878. depend = "";
  1879. }
  1880. if( SubDirectories.size() == 0)
  1881. {
  1882. return;
  1883. }
  1884. fout << "# Variable for making " << target << " in subdirectories.\n";
  1885. fout << var << " = \\\n";
  1886. unsigned int ii;
  1887. for(ii =0; ii < SubDirectories.size(); ii++)
  1888. {
  1889. std::string subdir = FixDirectoryName(SubDirectories[ii].c_str());
  1890. fout << target << "_" << subdir.c_str();
  1891. if(ii == SubDirectories.size()-1)
  1892. {
  1893. fout << " \n\n";
  1894. }
  1895. else
  1896. {
  1897. fout << " \\\n";
  1898. }
  1899. }
  1900. fout << "# Targets for making " << target << " in subdirectories.\n";
  1901. std::string last = "";
  1902. for(unsigned int cc =0; cc < SubDirectories.size(); cc++)
  1903. {
  1904. std::string subdir = FixDirectoryName(SubDirectories[cc].c_str());
  1905. fout << target << "_" << subdir.c_str() << ": " << depend;
  1906. // Make each subdirectory depend on previous one. This forces
  1907. // parallel builds (make -j 2) to build in same order as a single
  1908. // threaded build to avoid dependency problems.
  1909. if(cc > 0)
  1910. {
  1911. fout << " " << target << "_" << last.c_str();
  1912. }
  1913. fout << "\n";
  1914. last = subdir;
  1915. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1916. dir += "/";
  1917. dir += SubDirectories[cc];
  1918. this->BuildInSubDirectory(fout, dir.c_str(),
  1919. target1, target2, silent);
  1920. }
  1921. fout << "\n\n";
  1922. }
  1923. // output rules for decending into sub directories
  1924. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1925. {
  1926. // Output Sub directory build rules
  1927. const std::vector<std::string>& SubDirectories
  1928. = m_Makefile->GetSubDirectories();
  1929. if( SubDirectories.size() == 0)
  1930. {
  1931. return;
  1932. }
  1933. this->OutputSubDirectoryVars(fout,
  1934. "SUBDIR_BUILD",
  1935. "default_target",
  1936. "default_target",
  1937. 0, "$(TARGETS)",
  1938. SubDirectories,
  1939. false);
  1940. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1941. "clean",
  1942. 0, 0,
  1943. SubDirectories);
  1944. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1945. "depend",
  1946. 0, 0,
  1947. SubDirectories);
  1948. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1949. "install",
  1950. 0, 0,
  1951. SubDirectories);
  1952. }
  1953. // Output the depend information for all the classes
  1954. // in the makefile. These would have been generated
  1955. // by the class cmMakeDepend GenerateMakefile
  1956. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1957. {
  1958. bool ret = false;
  1959. // Iterate over every target.
  1960. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1961. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1962. target != targets.end(); ++target)
  1963. {
  1964. // Iterate over every source for this target.
  1965. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1966. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1967. source != sources.end(); ++source)
  1968. {
  1969. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1970. {
  1971. if(!(*source)->GetDepends().empty())
  1972. {
  1973. // Iterate through all the dependencies for this source.
  1974. for(std::vector<std::string>::const_iterator dep =
  1975. (*source)->GetDepends().begin();
  1976. dep != (*source)->GetDepends().end(); ++dep)
  1977. {
  1978. std::string s = (*source)->GetSourceName();
  1979. s += this->GetOutputExtension((*source)->GetSourceExtension().c_str());
  1980. fout << this->ConvertToRelativeOutputPath(s.c_str()) << " : "
  1981. << this->ConvertToRelativeOutputPath(dep->c_str()) << "\n";
  1982. ret = true;
  1983. }
  1984. fout << "\n\n";
  1985. }
  1986. }
  1987. }
  1988. }
  1989. return ret;
  1990. }
  1991. // Output the depend information for all the classes
  1992. // in the makefile. These would have been generated
  1993. // by the class cmMakeDepend GenerateMakefile
  1994. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1995. {
  1996. std::set<std::string> emittedLowerPath;
  1997. std::set<std::string> emitted;
  1998. // Iterate over every target.
  1999. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2000. fout << "# Suppresses display of executed commands\n";
  2001. fout << ".SILENT:\n";
  2002. fout << "# disable some common implicit rules to speed things up\n";
  2003. fout << ".SUFFIXES:\n";
  2004. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  2005. this->OutputMakeVariables(fout);
  2006. fout << "default:\n";
  2007. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  2008. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  2009. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2010. target != targets.end(); ++target)
  2011. {
  2012. // Iterate over every source for this target.
  2013. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2014. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2015. source != sources.end(); ++source)
  2016. {
  2017. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2018. {
  2019. if(!(*source)->GetDepends().empty())
  2020. {
  2021. for(std::vector<std::string>::const_iterator dep =
  2022. (*source)->GetDepends().begin();
  2023. dep != (*source)->GetDepends().end(); ++dep)
  2024. {
  2025. // do not call CollapseFullPath on dep here, because it already
  2026. // has been done because m_FullPath on cmDependInformation
  2027. // always is it called. If it is called here, network builds are
  2028. // very slow because of the number of stats
  2029. std::string dependfile = this->ConvertToRelativeOutputPath(dep->c_str());
  2030. // use the lower path function to create uniqe names
  2031. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  2032. if(emittedLowerPath.insert(lowerpath).second)
  2033. {
  2034. emitted.insert(dependfile);
  2035. fout << "all: " << dependfile << "\n";
  2036. }
  2037. }
  2038. }
  2039. }
  2040. }
  2041. }
  2042. fout << "\n\n# if any of these files changes run make dependlocal\n";
  2043. std::set<std::string>::iterator i;
  2044. for(i = emitted.begin(); i != emitted.end(); ++i)
  2045. {
  2046. fout << "cmake.depends: " << *i << "\n";
  2047. }
  2048. fout << "cmake.depends: \n"
  2049. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  2050. fout << "\n\n";
  2051. fout << "# if a .h file is removed then run make dependlocal\n\n";
  2052. for(std::set<std::string>::iterator it = emitted.begin();
  2053. it != emitted.end(); ++it)
  2054. {
  2055. fout << *it << ":\n"
  2056. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  2057. }
  2058. }
  2059. // Output each custom rule in the following format:
  2060. // output: source depends...
  2061. // (tab) command...
  2062. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  2063. {
  2064. // we cannot provide multiple rules for a single output
  2065. // so we will keep track of outputs to make sure we don't write
  2066. // two rules. First found wins
  2067. std::set<std::string> processedOutputs;
  2068. // first output all custom rules
  2069. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  2070. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  2071. i != sources.end(); ++i)
  2072. {
  2073. if ((*i)->GetCustomCommand())
  2074. {
  2075. cmCustomCommand *c = (*i)->GetCustomCommand();
  2076. // escape spaces and convert to native slashes path for
  2077. // the command
  2078. std::string comment = c->GetComment();
  2079. std::string command = c->GetCommand();
  2080. cmSystemTools::ReplaceString(command, "/./", "/");
  2081. command = this->ConvertToRelativeOutputPath(command.c_str());
  2082. command += " ";
  2083. // now add the arguments
  2084. command += c->GetArguments();
  2085. std::string depends;
  2086. // Collect out all the dependencies for this rule.
  2087. for(std::vector<std::string>::const_iterator d =
  2088. c->GetDepends().begin();
  2089. d != c->GetDepends().end(); ++d)
  2090. {
  2091. std::string dep = *d;
  2092. m_Makefile->ExpandVariablesInString(dep);
  2093. // watch for target dependencies,
  2094. std::string libPath = dep + "_CMAKE_PATH";
  2095. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  2096. if ( cacheValue && *cacheValue )
  2097. {
  2098. libPath = cacheValue;
  2099. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH") &&
  2100. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH")[0] != '\0')
  2101. {
  2102. libPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  2103. }
  2104. libPath += "/";
  2105. libPath += dep;
  2106. libPath += cmSystemTools::GetExecutableExtension();
  2107. dep = libPath;
  2108. }
  2109. cmSystemTools::ReplaceString(dep, "/./", "/");
  2110. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  2111. dep = this->ConvertToRelativeOutputPath(dep.c_str());
  2112. depends += " ";
  2113. depends += dep;
  2114. }
  2115. // output rule
  2116. if (processedOutputs.find(c->GetOutput()) == processedOutputs.end())
  2117. {
  2118. this->OutputMakeRule(fout,
  2119. (comment.size()?comment.c_str():"Custom command"),
  2120. c->GetOutput().c_str(),
  2121. depends.c_str(),
  2122. command.c_str());
  2123. processedOutputs.insert(c->GetOutput());
  2124. }
  2125. else
  2126. {
  2127. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  2128. c->GetOutput().c_str());
  2129. }
  2130. }
  2131. }
  2132. }
  2133. std::string
  2134. cmLocalUnixMakefileGenerator::ConvertToOutputForExisting(const char* p)
  2135. {
  2136. std::string ret = this->ConvertToRelativeOutputPath(p);
  2137. // if there are spaces in the path, then get the short path version
  2138. // if there is one
  2139. if(ret.find(' ') != std::string::npos)
  2140. {
  2141. if(cmSystemTools::FileExists(p))
  2142. {
  2143. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  2144. {
  2145. ret = this->ConvertToRelativeOutputPath(p);
  2146. }
  2147. }
  2148. }
  2149. return ret;
  2150. }
  2151. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  2152. {
  2153. const char* variables =
  2154. "# the standard shell for make\n"
  2155. "SHELL = /bin/sh\n"
  2156. "\n";
  2157. if(!m_WindowsShell)
  2158. {
  2159. fout << variables;
  2160. }
  2161. else
  2162. {
  2163. fout <<
  2164. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  2165. "NULL=\n"
  2166. "!ELSE \n"
  2167. "NULL=nul\n"
  2168. "!ENDIF \n";
  2169. }
  2170. if(m_MakeSilentFlag.size())
  2171. {
  2172. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  2173. }
  2174. std::string cmakecommand = this->ConvertToOutputForExisting(
  2175. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  2176. fout << "CMAKE_COMMAND = "
  2177. << cmakecommand
  2178. << "\n";
  2179. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  2180. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2181. {
  2182. fout << "CMAKE_EDIT_COMMAND = "
  2183. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2184. << "\n";
  2185. }
  2186. fout << "CMAKE_CURRENT_SOURCE = " <<
  2187. this->ConvertToRelativeOutputPath(m_Makefile->GetStartDirectory())
  2188. << "\n";
  2189. fout << "CMAKE_CURRENT_BINARY = " <<
  2190. this->ConvertToRelativeOutputPath(m_Makefile->GetStartOutputDirectory())
  2191. << "\n";
  2192. fout << "CMAKE_SOURCE_DIR = " <<
  2193. this->ConvertToRelativeOutputPath(m_Makefile->GetHomeDirectory())
  2194. << "\n";
  2195. fout << "CMAKE_BINARY_DIR = " <<
  2196. this->ConvertToRelativeOutputPath(m_Makefile->GetHomeOutputDirectory())
  2197. << "\n";
  2198. // Output Include paths
  2199. fout << "INCLUDE_FLAGS = ";
  2200. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  2201. std::vector<std::string>::iterator i;
  2202. fout << "-I" <<
  2203. this->ConvertToOutputForExisting(m_Makefile->GetStartDirectory()) << " ";
  2204. for(i = includes.begin(); i != includes.end(); ++i)
  2205. {
  2206. std::string include = *i;
  2207. // Don't output a -I for the standard include path "/usr/include".
  2208. // This can cause problems with certain standard library
  2209. // implementations because the wrong headers may be found first.
  2210. if(include != "/usr/include")
  2211. {
  2212. fout << "-I" << this->ConvertToOutputForExisting(i->c_str()) << " ";
  2213. }
  2214. }
  2215. fout << m_Makefile->GetDefineFlags();
  2216. fout << "\n\n";
  2217. }
  2218. void cmLocalUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  2219. {
  2220. // Install is not currently supported on windows
  2221. if(m_WindowsShell)
  2222. {
  2223. return;
  2224. }
  2225. std::string cmakecommand = this->ConvertToOutputForExisting(
  2226. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  2227. const char* root
  2228. = m_Makefile->GetDefinition("CMAKE_ROOT");
  2229. fout << "INSTALL = \"" << root << "/Templates/install-sh\" -c\n";
  2230. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  2231. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  2232. const cmTargets &tgts = m_Makefile->GetTargets();
  2233. fout << "install: $(SUBDIR_INSTALL)\n";
  2234. fout << "\t@echo \"Installing ...\"\n";
  2235. const char* prefix
  2236. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  2237. if (!prefix)
  2238. {
  2239. prefix = "/usr/local";
  2240. }
  2241. for(cmTargets::const_iterator l = tgts.begin();
  2242. l != tgts.end(); l++)
  2243. {
  2244. if (l->second.GetInstallPath() != "")
  2245. {
  2246. // first make the directories for each target
  2247. fout << "\t@if [ ! -d \"$(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  2248. "\" ] ; then \\\n";
  2249. fout << "\t echo \"Making directory \\\"$(DESTDIR)" << prefix
  2250. << l->second.GetInstallPath() << "\\\" \"; \\\n";
  2251. fout << "\t mkdir -p \"$(DESTDIR)" << prefix << l->second.GetInstallPath()
  2252. << "\"; \\\n";
  2253. fout << "\t chmod 755 \"$(DESTDIR)" << prefix << l->second.GetInstallPath()
  2254. << "\"; \\\n";
  2255. fout << "\t else true; \\\n";
  2256. fout << "\t fi\n";
  2257. std::string fname;
  2258. // now install the target
  2259. switch (l->second.GetType())
  2260. {
  2261. case cmTarget::STATIC_LIBRARY:
  2262. case cmTarget::SHARED_LIBRARY:
  2263. case cmTarget::MODULE_LIBRARY:
  2264. {
  2265. std::string targetName;
  2266. std::string targetNameSO;
  2267. std::string targetNameReal;
  2268. std::string targetNameBase;
  2269. this->GetLibraryNames(l->first.c_str(), l->second,
  2270. targetName, targetNameSO,
  2271. targetNameReal, targetNameBase);
  2272. std::string installName = "$(DESTDIR)";
  2273. installName += prefix;
  2274. installName += l->second.GetInstallPath();
  2275. installName += "/";
  2276. std::string installNameSO = installName;
  2277. std::string installNameReal = installName;
  2278. installName += targetName;
  2279. installNameSO += targetNameSO;
  2280. installNameReal += targetNameReal;
  2281. fname = m_LibraryOutputPath;
  2282. fname += targetNameReal;
  2283. fout << "\t$(INSTALL_DATA) " << this->ConvertToRelativeOutputPath(fname.c_str())
  2284. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2285. fout << "\t" << cmakecommand << " -E cmake_symlink_library \""
  2286. << installNameReal << "\" \"" << installNameSO << "\" \"" << installName
  2287. << "\"\n";
  2288. }; break;
  2289. case cmTarget::WIN32_EXECUTABLE:
  2290. case cmTarget::EXECUTABLE:
  2291. fname = m_ExecutableOutputPath;
  2292. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  2293. fout << "\t$(INSTALL_PROGRAM) " << this->ConvertToRelativeOutputPath(fname.c_str())
  2294. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2295. break;
  2296. case cmTarget::INSTALL_FILES:
  2297. {
  2298. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  2299. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  2300. sourcePath += "/";
  2301. binaryPath += "/";
  2302. const std::vector<std::string> &sf = l->second.GetSourceLists();
  2303. std::vector<std::string>::const_iterator i;
  2304. for (i = sf.begin(); i != sf.end(); ++i)
  2305. {
  2306. std::string f = *i;
  2307. if(f.substr(0, sourcePath.length()) == sourcePath)
  2308. {
  2309. f = f.substr(sourcePath.length());
  2310. }
  2311. else if(f.substr(0, binaryPath.length()) == binaryPath)
  2312. {
  2313. f = f.substr(binaryPath.length());
  2314. }
  2315. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  2316. // avoid using install-sh to install install-sh
  2317. // does not work on windows....
  2318. if(*i == "install-sh")
  2319. {
  2320. fout << "\tcp ";
  2321. }
  2322. else
  2323. {
  2324. fout << "\t$(INSTALL_DATA) ";
  2325. }
  2326. fout << this->ConvertToRelativeOutputPath(i->c_str())
  2327. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2328. }
  2329. }
  2330. break;
  2331. case cmTarget::INSTALL_PROGRAMS:
  2332. {
  2333. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  2334. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  2335. sourcePath += "/";
  2336. binaryPath += "/";
  2337. const std::vector<std::string> &sf = l->second.GetSourceLists();
  2338. std::vector<std::string>::const_iterator i;
  2339. for (i = sf.begin(); i != sf.end(); ++i)
  2340. {
  2341. std::string f = *i;
  2342. if(f.substr(0, sourcePath.length()) == sourcePath)
  2343. {
  2344. f = f.substr(sourcePath.length());
  2345. }
  2346. else if(f.substr(0, binaryPath.length()) == binaryPath)
  2347. {
  2348. f = f.substr(binaryPath.length());
  2349. }
  2350. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  2351. // avoid using install-sh to install install-sh
  2352. // does not work on windows....
  2353. if(*i == "install-sh")
  2354. {
  2355. fout << "\t @cp ";
  2356. }
  2357. else
  2358. {
  2359. fout << "\t @$(INSTALL_PROGRAM) ";
  2360. }
  2361. fout << this->ConvertToRelativeOutputPath(i->c_str())
  2362. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2363. }
  2364. }
  2365. break;
  2366. case cmTarget::UTILITY:
  2367. default:
  2368. break;
  2369. }
  2370. }
  2371. }
  2372. }
  2373. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  2374. {
  2375. this->OutputMakeRule(fout,
  2376. "default build rule",
  2377. "all",
  2378. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  2379. 0);
  2380. this->OutputMakeRule(fout,
  2381. "clean generated files",
  2382. "clean",
  2383. "$(SUBDIR_CLEAN)",
  2384. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  2385. " $(TARGETS) $(TARGET_EXTRAS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  2386. // collect up all the sources
  2387. std::vector<std::string> allsources;
  2388. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2389. for(std::map<cmStdString,cmTarget>::const_iterator target = targets.begin();
  2390. target != targets.end(); ++target)
  2391. {
  2392. // Iterate over every source for this target.
  2393. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2394. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2395. source != sources.end(); ++source)
  2396. {
  2397. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2398. {
  2399. allsources.push_back(this->ConvertToRelativeOutputPath((*source)->GetFullPath().c_str()));
  2400. }
  2401. }
  2402. }
  2403. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  2404. checkCache += "/cmake.check_cache";
  2405. checkCache = this->ConvertToRelativeOutputPath(checkCache.c_str());
  2406. std::vector<std::string> cmake_depends;
  2407. cmake_depends.push_back(checkCache);
  2408. cmake_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2409. this->OutputMakeRule(fout,
  2410. "dependencies.",
  2411. "cmake.depends",
  2412. cmake_depends,
  2413. "$(CMAKE_COMMAND) "
  2414. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2415. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2416. this->OutputMakeRule(fout,
  2417. "dependencies",
  2418. "cmake.check_depends",
  2419. allsources,
  2420. "$(CMAKE_COMMAND) "
  2421. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2422. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2423. this->OutputMakeRule(fout,
  2424. "dependencies",
  2425. "depend",
  2426. "$(SUBDIR_DEPEND)",
  2427. "$(CMAKE_COMMAND) "
  2428. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2429. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2430. this->OutputMakeRule(fout,
  2431. "dependencies",
  2432. "dependlocal",
  2433. 0,
  2434. "$(CMAKE_COMMAND) "
  2435. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2436. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2437. this->OutputMakeRule(fout,
  2438. "CMakeCache.txt",
  2439. "rebuild_cache",
  2440. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2441. "$(CMAKE_COMMAND) "
  2442. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2443. std::vector<std::string> check_cache_depends;
  2444. std::string CMakeCache = m_Makefile->GetHomeOutputDirectory();
  2445. CMakeCache += "/CMakeCache.txt";
  2446. CMakeCache = this->ConvertToRelativeOutputPath(CMakeCache.c_str());
  2447. check_cache_depends.push_back(CMakeCache);
  2448. check_cache_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2449. this->OutputMakeRule(fout,
  2450. "CMakeCache.txt because out-of-date:",
  2451. checkCache.c_str(),
  2452. check_cache_depends,
  2453. "$(CMAKE_COMMAND) "
  2454. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2455. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  2456. // called edit_cache
  2457. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2458. {
  2459. this->OutputMakeRule(fout,
  2460. "edit CMakeCache.txt",
  2461. "edit_cache",
  2462. 0,
  2463. "$(CMAKE_EDIT_COMMAND) "
  2464. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2465. }
  2466. else
  2467. {
  2468. this->OutputMakeRule(fout,
  2469. "edit CMakeCache.txt",
  2470. "edit_cache",
  2471. 0,
  2472. "$(CMAKE_COMMAND) "
  2473. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  2474. }
  2475. this->OutputMakeRule(fout,
  2476. "CMakeCache.txt",
  2477. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2478. 0,
  2479. "$(CMAKE_COMMAND) "
  2480. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2481. this->OutputMakeRule(fout,
  2482. "Rule to keep make from removing Makefiles "
  2483. "if control-C is hit during a run of cmake.",
  2484. ".PRECIOUS",
  2485. "Makefile cmake.depends",
  2486. 0);
  2487. this->OutputSourceObjectBuildRules(fout);
  2488. // find ctest
  2489. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2490. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2491. ctest += "/";
  2492. ctest += "ctest";
  2493. ctest += cmSystemTools::GetExecutableExtension();
  2494. if(!cmSystemTools::FileExists(ctest.c_str()))
  2495. {
  2496. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2497. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2498. ctest += "/Debug/";
  2499. ctest += "ctest";
  2500. ctest += cmSystemTools::GetExecutableExtension();
  2501. }
  2502. if(!cmSystemTools::FileExists(ctest.c_str()))
  2503. {
  2504. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2505. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2506. ctest += "/Release/";
  2507. ctest += "ctest";
  2508. ctest += cmSystemTools::GetExecutableExtension();
  2509. }
  2510. if (!cmSystemTools::FileExists(ctest.c_str()))
  2511. {
  2512. if ( !m_Makefile->GetDefinition("PROJECT_NAME") ||
  2513. strcmp(m_Makefile->GetDefinition("PROJECT_NAME"), "CMake") )
  2514. {
  2515. return;
  2516. }
  2517. ctest = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  2518. ctest += "/ctest";
  2519. }
  2520. if (m_Makefile->IsOn("CMAKE_TESTING_ENABLED"))
  2521. {
  2522. fout << "ARGS=\n";
  2523. std::string cmd = this->ConvertToOutputForExisting(ctest.c_str());
  2524. cmd += " $(ARGS)";
  2525. this->OutputMakeRule(fout,
  2526. "tests",
  2527. "test",
  2528. "",
  2529. cmd.c_str());
  2530. }
  2531. }
  2532. void
  2533. cmLocalUnixMakefileGenerator::
  2534. OutputBuildObjectFromSource(std::ostream& fout,
  2535. const char* shortName,
  2536. const cmSourceFile& source,
  2537. const char* extraCompileFlags,
  2538. bool shared)
  2539. {
  2540. // Header files shouldn't have build rules.
  2541. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  2542. {
  2543. return;
  2544. }
  2545. std::string comment = "object file";
  2546. std::string objectFile = std::string(shortName) +
  2547. this->GetOutputExtension(source.GetSourceExtension().c_str());
  2548. objectFile = this->ConvertToRelativeOutputPath(objectFile.c_str());
  2549. cmSystemTools::FileFormat format =
  2550. cmSystemTools::GetFileFormat(source.GetSourceExtension().c_str());
  2551. std::vector<std::string> rules;
  2552. std::string flags;
  2553. if(extraCompileFlags)
  2554. {
  2555. flags += extraCompileFlags;
  2556. }
  2557. std::string sourceFile =
  2558. this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  2559. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2560. buildType = cmSystemTools::UpperCase(buildType);
  2561. switch(format)
  2562. {
  2563. case cmSystemTools::C_FILE_FORMAT:
  2564. {
  2565. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_COMPILE_OBJECT"));
  2566. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  2567. flags += " ";
  2568. if(buildType.size())
  2569. {
  2570. std::string build = "CMAKE_C_FLAGS_";
  2571. build += buildType;
  2572. flags += this->GetSafeDefinition(build.c_str());
  2573. flags += " ";
  2574. }
  2575. if(shared)
  2576. {
  2577. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  2578. flags += " ";
  2579. }
  2580. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2581. {
  2582. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_C_FLAGS");
  2583. flags += " ";
  2584. }
  2585. break;
  2586. }
  2587. case cmSystemTools::CXX_FILE_FORMAT:
  2588. {
  2589. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_COMPILE_OBJECT"));
  2590. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  2591. flags += " ";
  2592. if(buildType.size())
  2593. {
  2594. std::string build = "CMAKE_CXX_FLAGS_";
  2595. build += buildType;
  2596. flags += this->GetSafeDefinition(build.c_str());
  2597. flags += " ";
  2598. }
  2599. if(shared)
  2600. {
  2601. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  2602. flags += " ";
  2603. }
  2604. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2605. {
  2606. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  2607. flags += " ";
  2608. }
  2609. break;
  2610. }
  2611. case cmSystemTools::HEADER_FILE_FORMAT:
  2612. return;
  2613. case cmSystemTools::DEFINITION_FILE_FORMAT:
  2614. return;
  2615. case cmSystemTools::RESOURCE_FILE_FORMAT:
  2616. {
  2617. flags = " $(INCLUDE_FLAGS) ";
  2618. // use rc rule here if it is defined
  2619. const char* rule = m_Makefile->GetDefinition("CMAKE_COMPILE_RESOURCE");
  2620. if(rule)
  2621. {
  2622. rules.push_back(rule);
  2623. }
  2624. }
  2625. break;
  2626. case cmSystemTools::NO_FILE_FORMAT:
  2627. case cmSystemTools::JAVA_FILE_FORMAT:
  2628. case cmSystemTools::STATIC_LIBRARY_FILE_FORMAT:
  2629. case cmSystemTools::SHARED_LIBRARY_FILE_FORMAT:
  2630. case cmSystemTools::MODULE_FILE_FORMAT:
  2631. case cmSystemTools::OBJECT_FILE_FORMAT:
  2632. case cmSystemTools::UNKNOWN_FILE_FORMAT:
  2633. cmSystemTools::Error("Unexpected file type ",
  2634. sourceFile.c_str());
  2635. break;
  2636. }
  2637. flags += "$(INCLUDE_FLAGS) ";
  2638. // expand multi-command semi-colon separated lists
  2639. // of commands into separate commands
  2640. std::vector<std::string> commands;
  2641. cmSystemTools::ExpandList(rules, commands);
  2642. for(std::vector<std::string>::iterator i = commands.begin();
  2643. i != commands.end(); ++i)
  2644. {
  2645. this->ExpandRuleVariables(*i,
  2646. 0, // no objects
  2647. 0, // no target
  2648. 0, // no link libs
  2649. sourceFile.c_str(),
  2650. objectFile.c_str(),
  2651. flags.c_str());
  2652. }
  2653. std::vector<std::string> sourceAndDeps;
  2654. sourceAndDeps.push_back(sourceFile);
  2655. // Check for extra object-file dependencies.
  2656. std::vector<std::string> depends;
  2657. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  2658. if(additionalDeps)
  2659. {
  2660. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  2661. for(std::vector<std::string>::iterator i = depends.begin();
  2662. i != depends.end(); ++i)
  2663. {
  2664. sourceAndDeps.push_back(this->ConvertToRelativeOutputPath(i->c_str()));
  2665. }
  2666. }
  2667. this->OutputMakeRule(fout,
  2668. comment.c_str(),
  2669. objectFile.c_str(),
  2670. sourceAndDeps,
  2671. commands);
  2672. }
  2673. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  2674. {
  2675. fout << "# Rules to build " << this->GetOutputExtension("")
  2676. << " files from their sources:\n";
  2677. std::set<std::string> rules;
  2678. // Iterate over every target.
  2679. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2680. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2681. target != targets.end(); ++target)
  2682. {
  2683. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2684. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  2685. std::string exportsDef = "";
  2686. if(shared)
  2687. {
  2688. std::string export_symbol;
  2689. if (const char* custom_export_name = target->second.GetProperty("DEFINE_SYMBOL"))
  2690. {
  2691. export_symbol = custom_export_name;
  2692. }
  2693. else
  2694. {
  2695. std::string in = target->first + "_EXPORTS";
  2696. export_symbol = cmSystemTools::MakeCindentifier(in.c_str());
  2697. }
  2698. exportsDef = "-D"+ export_symbol;
  2699. }
  2700. // Iterate over every source for this target.
  2701. const std::vector<cmSourceFile*>& sources =
  2702. target->second.GetSourceFiles();
  2703. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2704. source != sources.end(); ++source)
  2705. {
  2706. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  2707. !(*source)->GetCustomCommand())
  2708. {
  2709. std::string shortName;
  2710. std::string sourceName;
  2711. // If the full path to the source file includes this
  2712. // directory, we want to use the relative path for the
  2713. // filename of the object file. Otherwise, we will use just
  2714. // the filename portion.
  2715. if((cmSystemTools::GetFilenamePath(
  2716. (*source)->GetFullPath()).find(
  2717. m_Makefile->GetCurrentDirectory()) == 0)
  2718. || (cmSystemTools::GetFilenamePath(
  2719. (*source)->GetFullPath()).find(
  2720. m_Makefile->GetCurrentOutputDirectory()) == 0))
  2721. {
  2722. sourceName = (*source)->GetSourceName()+"."+
  2723. (*source)->GetSourceExtension();
  2724. shortName = (*source)->GetSourceName();
  2725. // The path may be relative. See if a directory needs to be
  2726. // created for the output file. This is a ugly, and perhaps
  2727. // should be moved elsewhere.
  2728. std::string relPath =
  2729. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  2730. if(relPath != "")
  2731. {
  2732. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  2733. outPath += "/"+relPath;
  2734. cmSystemTools::MakeDirectory(outPath.c_str());
  2735. }
  2736. }
  2737. else
  2738. {
  2739. sourceName = (*source)->GetFullPath();
  2740. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  2741. }
  2742. std::string shortNameWithExt = shortName +
  2743. (*source)->GetSourceExtension();
  2744. // Only output a rule for each .o once.
  2745. std::string compileFlags = exportsDef;
  2746. compileFlags += " ";
  2747. if(rules.find(shortNameWithExt) == rules.end())
  2748. {
  2749. if((*source)->GetProperty("COMPILE_FLAGS"))
  2750. {
  2751. compileFlags += (*source)->GetProperty("COMPILE_FLAGS");
  2752. compileFlags += " ";
  2753. }
  2754. this->OutputBuildObjectFromSource(fout,
  2755. shortName.c_str(),
  2756. *(*source),
  2757. compileFlags.c_str(),
  2758. shared);
  2759. rules.insert(shortNameWithExt);
  2760. }
  2761. }
  2762. }
  2763. }
  2764. }
  2765. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2766. const char* comment,
  2767. const char* target,
  2768. const char* depends,
  2769. const char* command,
  2770. const char* command2,
  2771. const char* command3,
  2772. const char* command4)
  2773. {
  2774. std::vector<std::string> commands;
  2775. if(command)
  2776. {
  2777. commands.push_back(command);
  2778. }
  2779. if(command2)
  2780. {
  2781. commands.push_back(command2);
  2782. }
  2783. if(command3)
  2784. {
  2785. commands.push_back(command3);
  2786. }
  2787. if(command4)
  2788. {
  2789. commands.push_back(command4);
  2790. }
  2791. this->OutputMakeRule(fout, comment, target, depends, commands);
  2792. }
  2793. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2794. const char* comment,
  2795. const char* target,
  2796. const char* depends,
  2797. const std::vector<std::string>& commands)
  2798. {
  2799. std::vector<std::string> depend;
  2800. if(depends)
  2801. {
  2802. depend.push_back(depends);
  2803. }
  2804. this->OutputMakeRule(fout, comment, target, depend, commands);
  2805. }
  2806. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2807. const char* comment,
  2808. const char* target,
  2809. const std::vector<std::string>& depends,
  2810. const char* command)
  2811. {
  2812. std::vector<std::string> commands;
  2813. if(command)
  2814. {
  2815. commands.push_back(command);
  2816. }
  2817. this->OutputMakeRule(fout, comment, target, depends, commands);
  2818. }
  2819. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2820. const char* comment,
  2821. const char* target,
  2822. const std::vector<std::string>& depends,
  2823. const std::vector<std::string>& commands)
  2824. {
  2825. if(!target)
  2826. {
  2827. cmSystemTools::Error("no target for OutputMakeRule");
  2828. return;
  2829. }
  2830. std::string replace;
  2831. if(comment)
  2832. {
  2833. replace = comment;
  2834. m_Makefile->ExpandVariablesInString(replace);
  2835. fout << "#---------------------------------------------------------\n";
  2836. fout << "# " << replace;
  2837. fout << "\n#\n";
  2838. }
  2839. fout << "\n";
  2840. replace = target;
  2841. m_Makefile->ExpandVariablesInString(replace);
  2842. std::string tgt = this->ConvertToRelativeOutputPath(replace.c_str());
  2843. if(depends.empty())
  2844. {
  2845. fout << tgt.c_str() << ":\n";
  2846. }
  2847. else
  2848. {
  2849. // Split dependencies into multiple rule lines. This allows for
  2850. // very long dependency lists.
  2851. for(std::vector<std::string>::const_iterator dep = depends.begin();
  2852. dep != depends.end(); ++dep)
  2853. {
  2854. replace = *dep;
  2855. m_Makefile->ExpandVariablesInString(replace);
  2856. fout << tgt.c_str() << ": " << replace.c_str() << "\n";
  2857. }
  2858. }
  2859. int count = 0;
  2860. for (std::vector<std::string>::const_iterator i = commands.begin();
  2861. i != commands.end(); ++i)
  2862. {
  2863. replace = *i;
  2864. m_Makefile->ExpandVariablesInString(replace);
  2865. if(count == 0 && replace.find_first_not_of(" \t\n\r") != std::string::npos &&
  2866. replace[0] != '-' && replace.find("echo") != 0
  2867. && replace.find("$(MAKE)") != 0)
  2868. {
  2869. std::string echostring = "Building ";
  2870. echostring += comment;
  2871. echostring += " ";
  2872. echostring += target;
  2873. echostring += "...";
  2874. this->OutputEcho(fout,echostring.c_str());
  2875. }
  2876. fout << "\t" << replace.c_str() << "\n";
  2877. count++;
  2878. }
  2879. fout << "\n";
  2880. }
  2881. const char* cmLocalUnixMakefileGenerator::GetSafeDefinition(const char* def)
  2882. {
  2883. const char* ret = m_Makefile->GetDefinition(def);
  2884. if(!ret)
  2885. {
  2886. return "";
  2887. }
  2888. return ret;
  2889. }
  2890. std::string cmLocalUnixMakefileGenerator::LowerCasePath(const char* path)
  2891. {
  2892. #ifdef _WIN32
  2893. return cmSystemTools::LowerCase(path);
  2894. #else
  2895. return std::string(path);
  2896. #endif
  2897. }
  2898. std::string
  2899. cmLocalUnixMakefileGenerator::CreateMakeVariable(const char* sin, const char* s2in)
  2900. {
  2901. std::string s = sin;
  2902. std::string s2 = s2in;
  2903. std::string unmodified = s;
  2904. unmodified += s2;
  2905. // if there is no restriction on the length of make variables
  2906. // and there are no "." charactors in the string, then return the
  2907. // unmodified combination.
  2908. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  2909. {
  2910. return unmodified;
  2911. }
  2912. // see if the variable has been defined before and return
  2913. // the modified version of the variable
  2914. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2915. if(i != m_MakeVariableMap.end())
  2916. {
  2917. return i->second;
  2918. }
  2919. // start with the unmodified variable
  2920. std::string ret = unmodified;
  2921. // if this there is no value for m_MakefileVariableSize then
  2922. // the string must have bad characters in it
  2923. if(!m_MakefileVariableSize)
  2924. {
  2925. cmSystemTools::ReplaceString(ret, ".", "_");
  2926. int ni = 0;
  2927. char buffer[5];
  2928. // make sure the _ version is not already used, if
  2929. // it is used then add number to the end of the variable
  2930. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2931. {
  2932. ++ni;
  2933. sprintf(buffer, "%04d", ni);
  2934. ret = unmodified + buffer;
  2935. }
  2936. m_ShortMakeVariableMap[ret] = "1";
  2937. m_MakeVariableMap[unmodified] = ret;
  2938. return ret;
  2939. }
  2940. // if the string is greater the 32 chars it is an invalid vairable name
  2941. // for borland make
  2942. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2943. {
  2944. int keep = m_MakefileVariableSize - 8;
  2945. int size = keep + 3;
  2946. std::string str1 = s;
  2947. std::string str2 = s2;
  2948. // we must shorten the combined string by 4 charactors
  2949. // keep no more than 24 charactors from the second string
  2950. if(static_cast<int>(str2.size()) > keep)
  2951. {
  2952. str2 = str2.substr(0, keep);
  2953. }
  2954. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2955. {
  2956. str1 = str1.substr(0, size - str2.size());
  2957. }
  2958. char buffer[5];
  2959. int ni = 0;
  2960. sprintf(buffer, "%04d", ni);
  2961. ret = str1 + str2 + buffer;
  2962. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2963. {
  2964. ++ni;
  2965. sprintf(buffer, "%04d", ni);
  2966. ret = str1 + str2 + buffer;
  2967. }
  2968. if(ni == 1000)
  2969. {
  2970. cmSystemTools::Error("Borland makefile variable length too long");
  2971. return unmodified;
  2972. }
  2973. // once an unused variable is found
  2974. m_ShortMakeVariableMap[ret] = "1";
  2975. }
  2976. // always make an entry into the unmodified to variable map
  2977. m_MakeVariableMap[unmodified] = ret;
  2978. return ret;
  2979. }
  2980. void cmLocalUnixMakefileGenerator::GetLibraryNames(const char* n,
  2981. const cmTarget& t,
  2982. std::string& name,
  2983. std::string& soName,
  2984. std::string& realName,
  2985. std::string& baseName)
  2986. {
  2987. // Check for library version properties.
  2988. const char* version = t.GetProperty("VERSION");
  2989. const char* soversion = t.GetProperty("SOVERSION");
  2990. if((t.GetType() != cmTarget::SHARED_LIBRARY &&
  2991. t.GetType() != cmTarget::MODULE_LIBRARY) ||
  2992. !m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  2993. {
  2994. // Versioning is supported only for shared libraries and modules,
  2995. // and then only when the platform supports an soname flag.
  2996. version = 0;
  2997. soversion = 0;
  2998. }
  2999. if(version && !soversion)
  3000. {
  3001. // The soversion must be set if the library version is set. Use
  3002. // the library version as the soversion.
  3003. soversion = version;
  3004. }
  3005. // The library name.
  3006. name = this->GetFullTargetName(n, t);
  3007. // The library's soname.
  3008. soName = name;
  3009. if(soversion)
  3010. {
  3011. soName += ".";
  3012. soName += soversion;
  3013. }
  3014. // The library's real name on disk.
  3015. realName = name;
  3016. if(version)
  3017. {
  3018. realName += ".";
  3019. realName += version;
  3020. }
  3021. else if(soversion)
  3022. {
  3023. realName += ".";
  3024. realName += soversion;
  3025. }
  3026. // The library name without extension.
  3027. baseName = this->GetBaseTargetName(n, t);
  3028. }