cmLocalUnixMakefileGenerator.cxx 94 KB

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