cmLocalUnixMakefileGenerator.cxx 88 KB

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