cmLocalUnixMakefileGenerator.cxx 91 KB

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