cmLocalUnixMakefileGenerator.cxx 86 KB

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