cmLocalUnixMakefileGenerator.cxx 105 KB

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