cmLocalUnixMakefileGenerator.cxx 91 KB

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