cmLocalUnixMakefileGenerator.cxx 81 KB

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