cmLocalUnixMakefileGenerator.cxx 86 KB

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