cmUnixMakefileGenerator.cxx 74 KB

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