cmLocalUnixMakefileGenerator.cxx 92 KB

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