cmLocalUnixMakefileGenerator.cxx 96 KB

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