cmUnixMakefileGenerator.cxx 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  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. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  450. for(std::vector<std::string>::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. if(!targetLibrary)
  531. {
  532. // For executables, add these a second time so order does not matter
  533. linkLibs += librariesLinked;
  534. }
  535. fout << linkLibs;
  536. if(outputRuntime && runtimeDirs.size()>0)
  537. {
  538. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  539. // a "-R a -R b -R c" type link line
  540. fout << runtimeFlag;
  541. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  542. fout << *itr;
  543. ++itr;
  544. for( ; itr != runtimeDirs.end(); ++itr )
  545. {
  546. if(runtimeConcatenate)
  547. {
  548. fout << runtimeSep << *itr;
  549. }
  550. else
  551. {
  552. fout << " " << runtimeFlag << *itr;
  553. }
  554. }
  555. fout << " ";
  556. }
  557. }
  558. std::string cmUnixMakefileGenerator::CreateTargetRules(const cmTarget &target,
  559. const char* targetName)
  560. {
  561. std::string customRuleCode = "";
  562. bool initNext = false;
  563. for (std::vector<cmCustomCommand>::const_iterator cr =
  564. target.GetCustomCommands().begin();
  565. cr != target.GetCustomCommands().end(); ++cr)
  566. {
  567. cmCustomCommand cc(*cr);
  568. cc.ExpandVariables(*m_Makefile);
  569. if (cc.GetSourceName() == targetName)
  570. {
  571. if(initNext)
  572. {
  573. customRuleCode += "\n\t";
  574. }
  575. else
  576. {
  577. initNext = true;
  578. }
  579. std::string command = this->ConvertToOutputPath(cc.GetCommand().c_str());
  580. customRuleCode += command + " " + cc.GetArguments();
  581. }
  582. }
  583. return customRuleCode;
  584. }
  585. void cmUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  586. const char* name,
  587. const cmTarget &t)
  588. {
  589. std::string target = m_LibraryOutputPath + "lib" + name + "$(SHLIB_SUFFIX)";
  590. std::string depend = "$(";
  591. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  592. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  593. std::string command = "$(RM) lib";
  594. command += name;
  595. command += "$(SHLIB_SUFFIX)";
  596. std::string command2;
  597. if(t.HasCxx())
  598. {
  599. command2 = "$(CMAKE_CXX_LINK_SHARED) $(CMAKE_CXX_SHLIB_LINK_FLAGS) "
  600. "$(CMAKE_CXX_SHLIB_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  601. }
  602. else
  603. {
  604. command2 = "$(CMAKE_C_LINK_SHARED) $(CMAKE_SHLIB_LINK_FLAGS) "
  605. "$(CMAKE_SHLIB_BUILD_FLAGS) -o \\\n";
  606. }
  607. command2 += "\t ";
  608. std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(SHLIB_SUFFIX)";
  609. libName = this->ConvertToOutputPath(libName.c_str());
  610. command2 += libName + " \\\n";
  611. command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  612. std::strstream linklibs;
  613. this->OutputLinkLibraries(linklibs, name, t);
  614. linklibs << std::ends;
  615. command2 += linklibs.str();
  616. delete [] linklibs.str();
  617. std::string customCommands = this->CreateTargetRules(t, name);
  618. const char* cc = 0;
  619. if(customCommands.size() > 0)
  620. {
  621. cc = customCommands.c_str();
  622. }
  623. this->OutputMakeRule(fout, "rules for a shared library",
  624. target.c_str(),
  625. depend.c_str(),
  626. command.c_str(),
  627. command2.c_str(),
  628. cc);
  629. }
  630. void cmUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  631. const char* name,
  632. const cmTarget &t)
  633. {
  634. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  635. std::string depend = "$(";
  636. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  637. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  638. std::string command = "$(RM) lib" + std::string(name) + "$(MODULE_SUFFIX)";
  639. std::string command2;
  640. if(t.HasCxx())
  641. {
  642. command2 = "$(CMAKE_CXX_LINK_SHARED) $(CMAKE_CXX_MODULE_LINK_FLAGS) "
  643. "$(CMAKE_CXX_MODULE_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  644. }
  645. else
  646. {
  647. command2 = "$(CMAKE_C_LINK_SHARED) $(CMAKE_SHLIB_LINK_FLAGS) "
  648. "$(CMAKE_SHLIB_BUILD_FLAGS) -o \\\n";
  649. }
  650. command2 += "\t ";
  651. std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  652. libName = this->ConvertToOutputPath(libName.c_str());
  653. command2 += libName + " \\\n";
  654. command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  655. std::strstream linklibs;
  656. this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
  657. linklibs << std::ends;
  658. command2 += linklibs.str();
  659. delete [] linklibs.str();
  660. std::string customCommands = this->CreateTargetRules(t, name);
  661. const char* cc = 0;
  662. if(customCommands.size() > 0)
  663. {
  664. cc = customCommands.c_str();
  665. }
  666. this->OutputMakeRule(fout, "rules for a shared module library",
  667. target.c_str(),
  668. depend.c_str(),
  669. command.c_str(),
  670. command2.c_str(),
  671. cc);
  672. }
  673. void cmUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  674. const char* name,
  675. const cmTarget &t)
  676. {
  677. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + ".a";
  678. target = this->ConvertToOutputPath(target.c_str());
  679. std::string depend = "$(";
  680. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  681. std::string command;
  682. if(t.HasCxx())
  683. {
  684. command = "$(CMAKE_CXX_AR) $(CMAKE_CXX_AR_ARGS) ";
  685. }
  686. else
  687. {
  688. command = "$(CMAKE_AR) $(CMAKE_AR_ARGS) ";
  689. }
  690. command += target;
  691. command += " $(";
  692. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  693. std::string command2 = "$(CMAKE_RANLIB) ";
  694. command2 += target;
  695. std::string comment = "rule to build static library: ";
  696. comment += name;
  697. std::string customCommands = this->CreateTargetRules(t, name);
  698. const char* cc = 0;
  699. if(customCommands.size() > 0)
  700. {
  701. cc = customCommands.c_str();
  702. }
  703. this->OutputMakeRule(fout,
  704. comment.c_str(),
  705. target.c_str(),
  706. depend.c_str(),
  707. command.c_str(),
  708. command2.c_str(),
  709. cc);
  710. }
  711. void cmUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  712. const char* name,
  713. const cmTarget &t)
  714. {
  715. std::string target = m_ExecutableOutputPath + name;
  716. std::string depend = "$(";
  717. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  718. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  719. std::string command;
  720. if(t.HasCxx())
  721. {
  722. command =
  723. "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_SHLIB_LINK_FLAGS) $(CMAKE_CXX_FLAGS) ";
  724. }
  725. else
  726. {
  727. command =
  728. "$(CMAKE_C_COMPILER) $(CMAKE_C_SHLIB_LINK_FLAGS) $(CMAKE_C_FLAGS) ";
  729. }
  730. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  731. std::strstream linklibs;
  732. this->OutputLinkLibraries(linklibs, 0, t);
  733. linklibs << std::ends;
  734. command += linklibs.str();
  735. std::string outputFile = m_ExecutableOutputPath + name;
  736. command += " -o " + this->ConvertToOutputPath(outputFile.c_str());
  737. std::string comment = "rule to build executable: ";
  738. comment += name;
  739. std::string customCommands = this->CreateTargetRules(t, name);
  740. const char* cc = 0;
  741. if(customCommands.size() > 0)
  742. {
  743. cc = customCommands.c_str();
  744. }
  745. this->OutputMakeRule(fout,
  746. comment.c_str(),
  747. target.c_str(),
  748. depend.c_str(),
  749. command.c_str(),
  750. cc);
  751. }
  752. void cmUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  753. const char* name,
  754. const cmTarget &t)
  755. {
  756. std::string customCommands = this->CreateTargetRules(t, name);
  757. const char* cc = 0;
  758. if(customCommands.size() > 0)
  759. {
  760. cc = customCommands.c_str();
  761. }
  762. std::string comment = "Rule to build Utility ";
  763. comment += name;
  764. std::string depends;
  765. const std::vector<cmCustomCommand> &ccs = t.GetCustomCommands();
  766. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  767. i != ccs.end(); ++i)
  768. {
  769. const std::vector<std::string> & dep = i->GetDepends();
  770. for(std::vector<std::string>::const_iterator d = dep.begin();
  771. d != dep.end(); ++d)
  772. {
  773. depends += "\\\n";
  774. depends += *d;
  775. }
  776. }
  777. this->OutputMakeRule(fout,
  778. comment.c_str(),
  779. name,
  780. depends.c_str(),
  781. cc);
  782. }
  783. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  784. {
  785. // for each target
  786. const cmTargets &tgts = m_Makefile->GetTargets();
  787. for(cmTargets::const_iterator l = tgts.begin();
  788. l != tgts.end(); l++)
  789. {
  790. switch(l->second.GetType())
  791. {
  792. case cmTarget::STATIC_LIBRARY:
  793. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  794. break;
  795. case cmTarget::SHARED_LIBRARY:
  796. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  797. break;
  798. case cmTarget::MODULE_LIBRARY:
  799. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  800. break;
  801. case cmTarget::EXECUTABLE:
  802. case cmTarget::WIN32_EXECUTABLE:
  803. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  804. break;
  805. case cmTarget::UTILITY:
  806. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  807. break;
  808. // This is handled by the OutputCustomRules method
  809. case cmTarget::INSTALL_FILES:
  810. // This is handled by the OutputInstallRules method
  811. case cmTarget::INSTALL_PROGRAMS:
  812. // This is handled by the OutputInstallRules method
  813. break;
  814. }
  815. }
  816. }
  817. // For each target that is an executable or shared library, generate
  818. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  819. void cmUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  820. {
  821. // Build a set of libraries that will be linked into any target in
  822. // this directory.
  823. std::set<std::string> used;
  824. // for each target
  825. const cmTargets &tgts = m_Makefile->GetTargets();
  826. for(cmTargets::const_iterator l = tgts.begin();
  827. l != tgts.end(); l++)
  828. {
  829. // Each dependency should only be emitted once per target.
  830. std::set<std::string> emitted;
  831. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  832. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  833. || (l->second.GetType() == cmTarget::EXECUTABLE)
  834. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  835. {
  836. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  837. // A library should not depend on itself!
  838. emitted.insert(l->first);
  839. // First look at all makefile level link libraries.
  840. const cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  841. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  842. lib != libs.end(); ++lib)
  843. {
  844. // Record that this library was used.
  845. used.insert(lib->first);
  846. // Don't emit the same library twice for this target.
  847. if(emitted.insert(lib->first).second)
  848. {
  849. // Output this dependency.
  850. this->OutputLibDepend(fout, lib->first.c_str());
  851. }
  852. }
  853. // Now, look at all link libraries specific to this target.
  854. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  855. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  856. lib != tlibs.end(); ++lib)
  857. {
  858. // Record that this library was used.
  859. used.insert(lib->first);
  860. // Don't emit the same library twice for this target.
  861. if(emitted.insert(lib->first).second)
  862. {
  863. // Output this dependency.
  864. this->OutputLibDepend(fout, lib->first.c_str());
  865. }
  866. }
  867. // Now, look at all utilities specific to this target.
  868. const std::set<std::string>& tutils = l->second.GetUtilities();
  869. for(std::set<std::string>::const_iterator util = tutils.begin();
  870. util != tutils.end(); ++util)
  871. {
  872. // Record that this utility was used.
  873. used.insert(*util);
  874. // Don't emit the same utility twice for this target.
  875. if(emitted.insert(*util).second)
  876. {
  877. // Output this dependency.
  878. this->OutputExeDepend(fout, util->c_str());
  879. }
  880. }
  881. fout << "\n";
  882. }
  883. }
  884. fout << "\n";
  885. // Loop over the libraries used and make sure there is a rule to
  886. // build them in this makefile. If the library is in another
  887. // directory, add a rule to jump to that directory and make sure it
  888. // exists.
  889. for(std::set<std::string>::const_iterator lib = used.begin();
  890. lib != used.end(); ++lib)
  891. {
  892. // loop over the list of directories that the libraries might
  893. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  894. // be stored in the cache
  895. const char* cacheValue = m_Makefile->GetDefinition(lib->c_str());
  896. // if cache and not the current directory add a rule, to
  897. // jump into the directory and build for the first time
  898. if(cacheValue &&
  899. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  900. {
  901. // add the correct extension
  902. std::string ltname = *lib+"_LIBRARY_TYPE";
  903. const char* libType
  904. = m_Makefile->GetDefinition(ltname.c_str());
  905. // if it was a library..
  906. if (libType)
  907. {
  908. std::string library = m_LibraryPrefix;
  909. library += *lib;
  910. std::string libpath = cacheValue;
  911. if(libType && std::string(libType) == "SHARED")
  912. {
  913. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  914. }
  915. else if(libType && std::string(libType) == "MODULE")
  916. {
  917. library += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  918. }
  919. else if(libType && std::string(libType) == "STATIC")
  920. {
  921. library += m_StaticLibraryExtension;
  922. }
  923. else
  924. {
  925. cmSystemTools::Error("Unknown library type!");
  926. return;
  927. }
  928. if(m_LibraryOutputPath.size())
  929. {
  930. libpath = m_LibraryOutputPath;
  931. }
  932. else
  933. {
  934. libpath += "/";
  935. }
  936. libpath += library;
  937. // put out a rule to build the library if it does not exist
  938. this->OutputBuildLibraryInDir(fout,
  939. cacheValue,
  940. library.c_str(),
  941. libpath.c_str());
  942. }
  943. // something other than a library...
  944. else
  945. {
  946. std::string exepath = cacheValue;
  947. if(m_ExecutableOutputPath.size())
  948. {
  949. exepath = m_ExecutableOutputPath;
  950. }
  951. else
  952. {
  953. exepath += "/";
  954. }
  955. exepath += *lib;
  956. this->OutputBuildExecutableInDir(fout,
  957. cacheValue,
  958. lib->c_str(),
  959. exepath.c_str());
  960. }
  961. }
  962. }
  963. }
  964. void cmUnixMakefileGenerator::OutputBuildLibraryInDir(std::ostream& fout,
  965. const char* path,
  966. const char* library,
  967. const char* fullpath)
  968. {
  969. const char* makeTarget = library;
  970. const char* libOutPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  971. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  972. {
  973. makeTarget = fullpath;
  974. }
  975. fout << this->ConvertToOutputPath(fullpath)
  976. << ":\n\tcd " << this->ConvertToOutputPath(path)
  977. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends"
  978. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends"
  979. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends"
  980. << "; $(MAKE) $(MAKESILENT) " << makeTarget << "\n\n";
  981. }
  982. void cmUnixMakefileGenerator::OutputBuildExecutableInDir(std::ostream& fout,
  983. const char* path,
  984. const char* library,
  985. const char* fullpath)
  986. {
  987. const char* makeTarget = library;
  988. const char* libOutPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  989. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  990. {
  991. makeTarget = fullpath;
  992. }
  993. fout << this->ConvertToOutputPath(fullpath)
  994. << ":\n\tcd " << this->ConvertToOutputPath(path)
  995. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends"
  996. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends"
  997. << "; $(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends"
  998. << "; $(MAKE) $(MAKESILENT) " << makeTarget << "\n\n";
  999. }
  1000. bool cmUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1001. {
  1002. return strcmp(path1, path2) == 0;
  1003. }
  1004. void cmUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1005. const char* name)
  1006. {
  1007. const char* cacheValue = m_Makefile->GetDefinition(name);
  1008. if(cacheValue )
  1009. {
  1010. // if there is a cache value, then this is a library that cmake
  1011. // knows how to build, so we can depend on it
  1012. std::string libpath;
  1013. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1014. {
  1015. // if the library is not in the current directory, then get the full
  1016. // path to it
  1017. if(m_LibraryOutputPath.size())
  1018. {
  1019. libpath = m_LibraryOutputPath;
  1020. libpath += m_LibraryPrefix;
  1021. }
  1022. else
  1023. {
  1024. libpath = cacheValue;
  1025. libpath += "/";
  1026. libpath += m_LibraryPrefix;
  1027. }
  1028. }
  1029. else
  1030. {
  1031. // library is in current Makefile so use lib as a prefix
  1032. libpath = m_LibraryOutputPath;
  1033. libpath += m_LibraryPrefix;
  1034. }
  1035. // add the library name
  1036. libpath += name;
  1037. // add the correct extension
  1038. std::string ltname = name;
  1039. ltname += "_LIBRARY_TYPE";
  1040. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1041. if(libType && std::string(libType) == "SHARED")
  1042. {
  1043. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1044. }
  1045. else if (libType && std::string(libType) == "MODULE")
  1046. {
  1047. libpath += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1048. }
  1049. else if (libType && std::string(libType) == "STATIC")
  1050. {
  1051. libpath += m_StaticLibraryExtension;
  1052. }
  1053. fout << this->ConvertToOutputPath(libpath.c_str()) << " ";
  1054. }
  1055. }
  1056. void cmUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1057. const char* name)
  1058. {
  1059. const char* cacheValue = m_Makefile->GetDefinition(name);
  1060. if(cacheValue )
  1061. {
  1062. // if there is a cache value, then this is a executable/utility that cmake
  1063. // knows how to build, so we can depend on it
  1064. std::string exepath;
  1065. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1066. {
  1067. // if the exe/utility is not in the current directory, then get the full
  1068. // path to it
  1069. if(m_ExecutableOutputPath.size())
  1070. {
  1071. exepath = m_ExecutableOutputPath;
  1072. }
  1073. else
  1074. {
  1075. exepath = cacheValue;
  1076. exepath += "/";
  1077. }
  1078. }
  1079. else
  1080. {
  1081. // library is in current Makefile
  1082. exepath = m_ExecutableOutputPath;
  1083. }
  1084. // add the library name
  1085. exepath += name;
  1086. // add the correct extension
  1087. if (m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX"))
  1088. {
  1089. std::string replaceVars =
  1090. m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
  1091. if (!strcmp(replaceVars.c_str(),"@CMAKE_EXECUTABLE_SUFFIX@"))
  1092. {
  1093. replaceVars = "";
  1094. }
  1095. exepath += replaceVars;
  1096. }
  1097. fout << this->ConvertToOutputPath(exepath.c_str()) << " ";
  1098. }
  1099. }
  1100. // fix up names of directories so they can be used
  1101. // as targets in makefiles.
  1102. inline std::string FixDirectoryName(const char* dir)
  1103. {
  1104. std::string s = dir;
  1105. // replace ../ with 3 under bars
  1106. size_t pos = s.find("../");
  1107. if(pos != std::string::npos)
  1108. {
  1109. s.replace(pos, 3, "___");
  1110. }
  1111. // replace / directory separators with a single under bar
  1112. pos = s.find("/");
  1113. while(pos != std::string::npos)
  1114. {
  1115. s.replace(pos, 1, "_");
  1116. pos = s.find("/");
  1117. }
  1118. return s;
  1119. }
  1120. void cmUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1121. const char* dir,
  1122. const char* target1,
  1123. const char* target2,
  1124. bool silent)
  1125. {
  1126. std::string directory = this->ConvertToOutputPath(dir);
  1127. if(target1)
  1128. {
  1129. fout << "\t@if test ! -d " << directory
  1130. << "; then $(MAKE) rebuild_cache; fi\n";
  1131. if (!silent)
  1132. {
  1133. fout << "\techo " << directory << ": building " << target1 << "\n";
  1134. }
  1135. fout << "\t@cd " << directory
  1136. << "; $(MAKE) -$(MAKEFLAGS) " << target1 << "\n";
  1137. }
  1138. if(target2)
  1139. {
  1140. if (!silent)
  1141. {
  1142. fout << "\techo " << directory << ": building " << target2 << "\n";
  1143. }
  1144. fout << "\t@cd " << directory
  1145. << "; $(MAKE) -$(MAKEFLAGS) " << target2 << "\n";
  1146. }
  1147. fout << "\n";
  1148. }
  1149. void
  1150. cmUnixMakefileGenerator::
  1151. OutputSubDirectoryVars(std::ostream& fout,
  1152. const char* var,
  1153. const char* target,
  1154. const char* target1,
  1155. const char* target2,
  1156. const char* depend,
  1157. const std::vector<std::string>& SubDirectories,
  1158. bool silent)
  1159. {
  1160. if(!depend)
  1161. {
  1162. depend = "";
  1163. }
  1164. if( SubDirectories.size() == 0)
  1165. {
  1166. return;
  1167. }
  1168. fout << "# Variable for making " << target << " in subdirectories.\n";
  1169. fout << var << " = \\\n";
  1170. unsigned int i;
  1171. for(i =0; i < SubDirectories.size(); i++)
  1172. {
  1173. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1174. fout << target << "_" << subdir.c_str();
  1175. if(i == SubDirectories.size()-1)
  1176. {
  1177. fout << " \n\n";
  1178. }
  1179. else
  1180. {
  1181. fout << " \\\n";
  1182. }
  1183. }
  1184. fout << "# Targets for making " << target << " in subdirectories.\n";
  1185. std::string last = "";
  1186. for(unsigned int i =0; i < SubDirectories.size(); i++)
  1187. {
  1188. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1189. fout << target << "_" << subdir.c_str() << ": " << depend;
  1190. // Make each subdirectory depend on previous one. This forces
  1191. // parallel builds (make -j 2) to build in same order as a single
  1192. // threaded build to avoid dependency problems.
  1193. if(i > 0)
  1194. {
  1195. fout << " " << target << "_" << last.c_str();
  1196. }
  1197. fout << "\n";
  1198. last = subdir;
  1199. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1200. dir += "/";
  1201. dir += SubDirectories[i];
  1202. this->BuildInSubDirectory(fout, dir.c_str(),
  1203. target1, target2, silent);
  1204. }
  1205. fout << "\n\n";
  1206. }
  1207. // output rules for decending into sub directories
  1208. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1209. {
  1210. // Output Sub directory build rules
  1211. const std::vector<std::string>& SubDirectories
  1212. = m_Makefile->GetSubDirectories();
  1213. if( SubDirectories.size() == 0)
  1214. {
  1215. return;
  1216. }
  1217. this->OutputSubDirectoryVars(fout,
  1218. "SUBDIR_BUILD",
  1219. "default_target",
  1220. "default_target",
  1221. 0, "$(TARGETS)",
  1222. SubDirectories,
  1223. false);
  1224. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1225. "clean",
  1226. 0, 0,
  1227. SubDirectories);
  1228. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1229. "depend",
  1230. 0, 0,
  1231. SubDirectories);
  1232. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1233. "install",
  1234. 0, 0,
  1235. SubDirectories);
  1236. }
  1237. // Output the depend information for all the classes
  1238. // in the makefile. These would have been generated
  1239. // by the class cmMakeDepend GenerateMakefile
  1240. bool cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1241. {
  1242. bool ret = false;
  1243. // Iterate over every target.
  1244. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1245. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1246. target != targets.end(); ++target)
  1247. {
  1248. // Iterate over every source for this target.
  1249. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1250. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1251. source != sources.end(); ++source)
  1252. {
  1253. if(!(*source)->IsAHeaderFileOnly())
  1254. {
  1255. if(!(*source)->GetDepends().empty())
  1256. {
  1257. fout << (*source)->GetSourceName() << m_ObjectFileExtension << " :";
  1258. // Iterate through all the dependencies for this source.
  1259. for(std::vector<std::string>::const_iterator dep =
  1260. (*source)->GetDepends().begin();
  1261. dep != (*source)->GetDepends().end(); ++dep)
  1262. {
  1263. fout << " \\\n"
  1264. << this->ConvertToOutputPath(dep->c_str());
  1265. ret = true;
  1266. }
  1267. fout << "\n\n";
  1268. }
  1269. }
  1270. }
  1271. }
  1272. return ret;
  1273. }
  1274. // Output the depend information for all the classes
  1275. // in the makefile. These would have been generated
  1276. // by the class cmMakeDepend GenerateMakefile
  1277. void cmUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1278. {
  1279. std::set<std::string> emitted;
  1280. // Iterate over every target.
  1281. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1282. fout << "# Suppresses display of executed commands\n";
  1283. fout << ".SILENT:\n";
  1284. fout << "# disable some common implicit rules to speed things up\n";
  1285. fout << ".SUFFIXES:\n";
  1286. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1287. this->OutputMakeVariables(fout);
  1288. fout << "default:\n";
  1289. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends all\n"
  1290. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1291. fout << "all: ";
  1292. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1293. target != targets.end(); ++target)
  1294. {
  1295. // Iterate over every source for this target.
  1296. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1297. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1298. source != sources.end(); ++source)
  1299. {
  1300. if(!(*source)->IsAHeaderFileOnly())
  1301. {
  1302. if(!(*source)->GetDepends().empty())
  1303. {
  1304. for(std::vector<std::string>::const_iterator dep =
  1305. (*source)->GetDepends().begin();
  1306. dep != (*source)->GetDepends().end(); ++dep)
  1307. {
  1308. std::string dependfile =
  1309. this->ConvertToOutputPath(cmSystemTools::CollapseFullPath(dep->c_str()).c_str());
  1310. if(emitted.insert(dependfile).second)
  1311. {
  1312. fout << " \\\n" << dependfile ;
  1313. }
  1314. }
  1315. }
  1316. }
  1317. }
  1318. }
  1319. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1320. fout << "cmake.depends: ";
  1321. std::set<std::string>::iterator i;
  1322. for(i = emitted.begin(); i != emitted.end(); ++i)
  1323. {
  1324. fout << " \\\n" << *i;
  1325. }
  1326. fout << "\n\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) dependlocal\n\n";
  1327. fout << "\n\n";
  1328. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1329. for(std::set<std::string>::iterator i = emitted.begin();
  1330. i != emitted.end(); ++i)
  1331. {
  1332. fout << *i << ":\n"
  1333. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) dependlocal\n\n";
  1334. }
  1335. }
  1336. // Output each custom rule in the following format:
  1337. // output: source depends...
  1338. // (tab) command...
  1339. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1340. {
  1341. // We may be modifying the source groups temporarily, so make a copy.
  1342. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1343. const cmTargets &tgts = m_Makefile->GetTargets();
  1344. for(cmTargets::const_iterator tgt = tgts.begin();
  1345. tgt != tgts.end(); ++tgt)
  1346. {
  1347. // add any custom rules to the source groups
  1348. for (std::vector<cmCustomCommand>::const_iterator cr =
  1349. tgt->second.GetCustomCommands().begin();
  1350. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1351. {
  1352. // if the source for the custom command is the same name
  1353. // as the target, then to not create a rule in the makefile for
  1354. // the custom command, as the command will be fired when the other target
  1355. // is built.
  1356. if ( cr->GetSourceName().compare(tgt->first) !=0)
  1357. {
  1358. cmSourceGroup& sourceGroup =
  1359. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1360. sourceGroups);
  1361. cmCustomCommand cc(*cr);
  1362. cc.ExpandVariables(*m_Makefile);
  1363. sourceGroup.AddCustomCommand(cc);
  1364. }
  1365. }
  1366. }
  1367. // Loop through every source group.
  1368. for(std::vector<cmSourceGroup>::const_iterator sg =
  1369. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1370. {
  1371. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1372. if(buildRules.empty())
  1373. { continue; }
  1374. std::string name = sg->GetName();
  1375. if(name != "")
  1376. {
  1377. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1378. }
  1379. // Loop through each source in the source group.
  1380. for(cmSourceGroup::BuildRules::const_iterator cc =
  1381. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1382. {
  1383. std::string source = cc->first;
  1384. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  1385. // Loop through every command generating code from the current source.
  1386. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1387. c != commands.end(); ++c)
  1388. {
  1389. // escape spaces and convert to native slashes path for
  1390. // the command
  1391. std::string command =
  1392. this->ConvertToOutputPath(c->second.m_Command.c_str());
  1393. command += " ";
  1394. // now add the arguments
  1395. command += c->second.m_Arguments;
  1396. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1397. // if the command has no outputs, then it is a utility command
  1398. // with no outputs
  1399. if(commandFiles.m_Outputs.size() == 0)
  1400. {
  1401. std::string depends;
  1402. // collect out all the dependencies for this rule.
  1403. for(std::set<std::string>::const_iterator d =
  1404. commandFiles.m_Depends.begin();
  1405. d != commandFiles.m_Depends.end(); ++d)
  1406. {
  1407. std::string dep = this->ConvertToOutputPath(d->c_str());
  1408. depends += " ";
  1409. depends += dep;
  1410. }
  1411. // output rule
  1412. this->OutputMakeRule(fout,
  1413. "Custom command",
  1414. source.c_str(),
  1415. depends.c_str(),
  1416. command.c_str());
  1417. }
  1418. // Write a rule for every output generated by this command.
  1419. for(std::set<std::string>::const_iterator output =
  1420. commandFiles.m_Outputs.begin();
  1421. output != commandFiles.m_Outputs.end(); ++output)
  1422. {
  1423. std::string src = this->ConvertToOutputPath(source.c_str());
  1424. std::string depends;
  1425. depends += src;
  1426. // Collect out all the dependencies for this rule.
  1427. for(std::set<std::string>::const_iterator d =
  1428. commandFiles.m_Depends.begin();
  1429. d != commandFiles.m_Depends.end(); ++d)
  1430. {
  1431. std::string dep = this->ConvertToOutputPath(d->c_str());
  1432. depends += " ";
  1433. depends += dep;
  1434. }
  1435. // output rule
  1436. this->OutputMakeRule(fout,
  1437. "Custom command",
  1438. output->c_str(),
  1439. depends.c_str(),
  1440. command.c_str());
  1441. }
  1442. }
  1443. }
  1444. if(name != "")
  1445. {
  1446. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1447. }
  1448. }
  1449. }
  1450. void cmUnixMakefileGenerator::GenerateCacheOnly()
  1451. {
  1452. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  1453. std::string dest = m_Makefile->GetStartOutputDirectory();
  1454. dest += "/Makefile";
  1455. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  1456. this->OutputMakefile(dest.c_str());
  1457. return;
  1458. }
  1459. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  1460. {
  1461. std::vector<cmMakefile*> makefiles;
  1462. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  1463. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  1464. i != makefiles.end(); ++i)
  1465. {
  1466. cmMakefile* mf = *i;
  1467. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  1468. {
  1469. mf->AddDefinition("RUN_CONFIGURE", true);
  1470. }
  1471. cmUnixMakefileGenerator* gen =
  1472. static_cast<cmUnixMakefileGenerator*>(mf->GetMakefileGenerator());
  1473. gen->SetCacheOnlyOn();
  1474. gen->SetRecurseOff();
  1475. mf->GenerateMakefile();
  1476. }
  1477. // CLEAN up the makefiles created
  1478. for(unsigned int i =0; i < makefiles.size(); ++i)
  1479. {
  1480. delete makefiles[i];
  1481. }
  1482. }
  1483. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1484. {
  1485. const char* variables =
  1486. "# the standard shell for make\n"
  1487. "SHELL = /bin/sh\n"
  1488. "\n"
  1489. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  1490. "CMAKE_AR = @CMAKE_AR@\n"
  1491. "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
  1492. "CMAKE_CXX_AR = @CMAKE_CXX_AR@\n"
  1493. "CMAKE_CXX_AR_ARGS = @CMAKE_CXX_AR_ARGS@\n"
  1494. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@\n"
  1495. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  1496. "CMAKE_C_LINK_SHARED = @CMAKE_C_LINK_SHARED@\n"
  1497. "CMAKE_CXX_LINK_SHARED = @CMAKE_CXX_LINK_SHARED@\n"
  1498. "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
  1499. "CMAKE_CXX_SHLIB_CFLAGS = @CMAKE_CXX_SHLIB_CFLAGS@\n"
  1500. "CMAKE_CXX_SHLIB_BUILD_FLAGS = @CMAKE_CXX_SHLIB_BUILD_FLAGS@\n"
  1501. "CMAKE_CXX_SHLIB_LINK_FLAGS = @CMAKE_CXX_SHLIB_LINK_FLAGS@\n"
  1502. "CMAKE_CXX_MODULE_BUILD_FLAGS = @CMAKE_CXX_MODULE_BUILD_FLAGS@\n"
  1503. "CMAKE_CXX_MODULE_LINK_FLAGS = @CMAKE_CXX_MODULE_LINK_FLAGS@\n"
  1504. "CMAKE_CXX_SHLIB_RUNTIME_FLAG = @CMAKE_CXX_SHLIB_RUNTIME_FLAG@\n"
  1505. "CMAKE_CXX_SHLIB_RUNTIME_SEP = @CMAKE_CXX_SHLIB_RUNTIME_SEP@\n"
  1506. "\n"
  1507. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  1508. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@\n"
  1509. "\n"
  1510. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  1511. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  1512. "CMAKE_C_SHLIB_LINK_FLAGS = @CMAKE_C_SHLIB_LINK_FLAGS@\n"
  1513. "CMAKE_MODULE_BUILD_FLAGS = @CMAKE_MODULE_BUILD_FLAGS@\n"
  1514. "CMAKE_MODULE_LINK_FLAGS = @CMAKE_MODULE_LINK_FLAGS@\n"
  1515. "CMAKE_C_SHLIB_RUNTIME_FLAG = @CMAKE_C_SHLIB_RUNTIME_FLAG@\n"
  1516. "CMAKE_SHLIB_RUNTIME_FLAG = @CMAKE_SHLIB_RUNTIME_FLAG@\n"
  1517. "CMAKE_SHLIB_RUNTIME_SEP = @CMAKE_SHLIB_RUNTIME_SEP@\n"
  1518. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  1519. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  1520. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  1521. "MODULE_SUFFIX = @CMAKE_MODULE_SUFFIX@\n"
  1522. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  1523. "RM = rm -f\n"
  1524. "\n";
  1525. std::string replaceVars = variables;
  1526. m_Makefile->ExpandVariablesInString(replaceVars);
  1527. fout << replaceVars.c_str();
  1528. fout << "CMAKE_COMMAND = "
  1529. << this->ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_COMMAND"))
  1530. << "\n";
  1531. fout << "CMAKE_CURRENT_SOURCE = " <<
  1532. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << "\n";
  1533. fout << "CMAKE_CURRENT_BINARY = " <<
  1534. this->ConvertToOutputPath(m_Makefile->GetStartOutputDirectory()) << "\n";
  1535. fout << "CMAKE_SOURCE_DIR = " <<
  1536. this->ConvertToOutputPath(m_Makefile->GetHomeDirectory()) << "\n";
  1537. fout << "CMAKE_BINARY_DIR = " <<
  1538. this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()) << "\n";
  1539. // Output Include paths
  1540. fout << "INCLUDE_FLAGS = ";
  1541. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1542. std::vector<std::string>::iterator i;
  1543. fout << "-I" <<
  1544. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  1545. for(i = includes.begin(); i != includes.end(); ++i)
  1546. {
  1547. std::string include = *i;
  1548. // Don't output a -I for the standard include path "/usr/include".
  1549. // This can cause problems with certain standard library
  1550. // implementations because the wrong headers may be found first.
  1551. if(include != "/usr/include")
  1552. {
  1553. fout << "-I" << this->ConvertToOutputPath(i->c_str()) << " ";
  1554. }
  1555. }
  1556. fout << m_Makefile->GetDefineFlags();
  1557. fout << "\n\n";
  1558. }
  1559. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1560. {
  1561. const char* root
  1562. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1563. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1564. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1565. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1566. const cmTargets &tgts = m_Makefile->GetTargets();
  1567. fout << "install: $(SUBDIR_INSTALL)\n";
  1568. fout << "\t@echo \"Installing ...\"\n";
  1569. const char* prefix
  1570. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1571. if (!prefix)
  1572. {
  1573. prefix = "/usr/local";
  1574. }
  1575. for(cmTargets::const_iterator l = tgts.begin();
  1576. l != tgts.end(); l++)
  1577. {
  1578. if (l->second.GetInstallPath() != "")
  1579. {
  1580. // first make the directories for each target
  1581. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1582. " ] ; then \\\n";
  1583. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1584. << l->second.GetInstallPath() << " \"; \\\n";
  1585. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1586. << "; \\\n";
  1587. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1588. << "; \\\n";
  1589. fout << "\t else true; \\\n";
  1590. fout << "\t fi\n";
  1591. // now install the target
  1592. switch (l->second.GetType())
  1593. {
  1594. case cmTarget::STATIC_LIBRARY:
  1595. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1596. << l->first;
  1597. fout << ".a";
  1598. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1599. break;
  1600. case cmTarget::SHARED_LIBRARY:
  1601. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1602. << l->first;
  1603. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1604. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1605. break;
  1606. case cmTarget::MODULE_LIBRARY:
  1607. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1608. << l->first;
  1609. fout << m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1610. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1611. break;
  1612. case cmTarget::WIN32_EXECUTABLE:
  1613. case cmTarget::EXECUTABLE:
  1614. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1615. << l->first
  1616. << cmSystemTools::GetExecutableExtension()
  1617. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1618. break;
  1619. case cmTarget::INSTALL_FILES:
  1620. {
  1621. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1622. std::vector<std::string>::const_iterator i;
  1623. for (i = sf.begin(); i != sf.end(); ++i)
  1624. {
  1625. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1626. fout << "\t@if [ -f " << *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 << *i
  1638. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1639. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1640. // avoid using install-sh to install install-sh
  1641. // does not work on windows....
  1642. if(*i == "install-sh")
  1643. {
  1644. fout << "\t cp ";
  1645. }
  1646. else
  1647. {
  1648. fout << "\t $(INSTALL_DATA) ";
  1649. }
  1650. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1651. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1652. fout << "\telse \\\n";
  1653. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1654. << " \"; \\\n";
  1655. fout << "\t fi\n";
  1656. }
  1657. }
  1658. break;
  1659. case cmTarget::INSTALL_PROGRAMS:
  1660. {
  1661. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1662. std::vector<std::string>::const_iterator i;
  1663. for (i = sf.begin(); i != sf.end(); ++i)
  1664. {
  1665. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1666. fout << "\t@if [ -f " << *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 << *i
  1678. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1679. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1680. // avoid using install-sh to install install-sh
  1681. // does not work on windows....
  1682. if(*i == "install-sh")
  1683. {
  1684. fout << "\t cp ";
  1685. }
  1686. else
  1687. {
  1688. fout << "\t $(INSTALL_PROGRAM) ";
  1689. }
  1690. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1691. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1692. fout << "\telse \\\n";
  1693. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1694. << " \"; \\\n";
  1695. fout << "\t fi\n";
  1696. }
  1697. }
  1698. break;
  1699. case cmTarget::UTILITY:
  1700. default:
  1701. break;
  1702. }
  1703. }
  1704. }
  1705. }
  1706. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1707. {
  1708. this->OutputMakeRule(fout,
  1709. "Default build rule",
  1710. "all",
  1711. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1712. 0);
  1713. this->OutputMakeRule(fout,
  1714. "remove generated files",
  1715. "clean",
  1716. "$(SUBDIR_CLEAN)",
  1717. "-@ $(RM) $(CLEAN_OBJECT_FILES) $(EXECUTABLES)"
  1718. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  1719. // collect up all the sources
  1720. std::string allsources;
  1721. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1722. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1723. target != targets.end(); ++target)
  1724. {
  1725. // Iterate over every source for this target.
  1726. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1727. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1728. source != sources.end(); ++source)
  1729. {
  1730. if(!(*source)->IsAHeaderFileOnly())
  1731. {
  1732. allsources += " \\\n";
  1733. allsources += this->ConvertToOutputPath((*source)->GetFullPath().c_str());
  1734. }
  1735. }
  1736. }
  1737. this->OutputMakeRule(fout,
  1738. "Rule to build the cmake.depends and Makefile as side effect, if a source cmakelist file is out of date.",
  1739. "cmake.depends",
  1740. "$(CMAKE_MAKEFILE_SOURCES)",
  1741. "$(CMAKE_COMMAND) "
  1742. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1743. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1744. );
  1745. this->OutputMakeRule(fout,
  1746. "Rule to build the cmake.check_depends and Makefile as side effect, if any source file has changed.",
  1747. "cmake.check_depends",
  1748. allsources.c_str(),
  1749. "$(CMAKE_COMMAND) "
  1750. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1751. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1752. );
  1753. this->OutputMakeRule(fout,
  1754. "Rule to force the build of cmake.depends",
  1755. "depend",
  1756. "$(SUBDIR_DEPEND)",
  1757. "$(CMAKE_COMMAND) "
  1758. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1759. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1760. this->OutputMakeRule(fout,
  1761. "Rule to force the build of cmake.depends "
  1762. "in the current directory only.",
  1763. "dependlocal",
  1764. 0,
  1765. "$(CMAKE_COMMAND) "
  1766. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1767. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1768. this->OutputMakeRule(fout,
  1769. "Rebuild CMakeCache.txt file",
  1770. "rebuild_cache",
  1771. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1772. "$(CMAKE_COMMAND) "
  1773. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1774. this->OutputMakeRule(fout,
  1775. "Create CMakeCache.txt file",
  1776. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1777. 0,
  1778. "$(CMAKE_COMMAND) "
  1779. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1780. this->OutputMakeRule(fout,
  1781. "Rule to keep make from removing Makefiles "
  1782. "if control-C is hit during a run of cmake.",
  1783. ".PRECIOUS",
  1784. "Makefile cmake.depends",
  1785. 0);
  1786. this->OutputSourceObjectBuildRules(fout);
  1787. // find ctest
  1788. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1789. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1790. ctest += "/";
  1791. ctest += "ctest";
  1792. ctest += cmSystemTools::GetExecutableExtension();
  1793. if(!cmSystemTools::FileExists(ctest.c_str()))
  1794. {
  1795. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1796. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1797. ctest += "/Debug/";
  1798. ctest += "ctest";
  1799. ctest += cmSystemTools::GetExecutableExtension();
  1800. }
  1801. if(!cmSystemTools::FileExists(ctest.c_str()))
  1802. {
  1803. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1804. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1805. ctest += "/Release/";
  1806. ctest += "ctest";
  1807. ctest += cmSystemTools::GetExecutableExtension();
  1808. }
  1809. if (cmSystemTools::FileExists(ctest.c_str()))
  1810. {
  1811. this->OutputMakeRule(fout,
  1812. "run any tests",
  1813. "test",
  1814. "",
  1815. this->ConvertToOutputPath(ctest.c_str()).c_str());
  1816. }
  1817. }
  1818. void
  1819. cmUnixMakefileGenerator::
  1820. OutputBuildObjectFromSource(std::ostream& fout,
  1821. const char* shortName,
  1822. const cmSourceFile& source,
  1823. const char* extraCompileFlags,
  1824. bool shared)
  1825. {
  1826. // Header files shouldn't have build rules.
  1827. if(source.IsAHeaderFileOnly())
  1828. {
  1829. return;
  1830. }
  1831. std::string comment = "Build ";
  1832. std::string objectFile = std::string(shortName) + m_ObjectFileExtension;
  1833. objectFile = this->ConvertToOutputPath(objectFile.c_str());
  1834. comment += objectFile + " From ";
  1835. comment += source.GetFullPath();
  1836. std::string compileCommand;
  1837. std::string ext = source.GetSourceExtension();
  1838. if(ext == "c" )
  1839. {
  1840. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  1841. compileCommand += extraCompileFlags;
  1842. if(shared)
  1843. {
  1844. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1845. }
  1846. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1847. compileCommand +=
  1848. this->ConvertToOutputPath(source.GetFullPath().c_str());
  1849. compileCommand += " -o ";
  1850. compileCommand += objectFile;
  1851. }
  1852. else
  1853. {
  1854. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  1855. compileCommand += extraCompileFlags;
  1856. if(shared)
  1857. {
  1858. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1859. }
  1860. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1861. compileCommand +=
  1862. this->ConvertToOutputPath(source.GetFullPath().c_str());
  1863. compileCommand += " -o ";
  1864. compileCommand += objectFile;
  1865. }
  1866. this->OutputMakeRule(fout,
  1867. comment.c_str(),
  1868. objectFile.c_str(),
  1869. this->ConvertToOutputPath(source.GetFullPath().
  1870. c_str()).c_str(),
  1871. compileCommand.c_str());
  1872. }
  1873. void cmUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  1874. {
  1875. fout << "# Rules to build " << m_ObjectFileExtension
  1876. << " files from their sources:\n";
  1877. std::set<std::string> rules;
  1878. // Iterate over every target.
  1879. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1880. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1881. target != targets.end(); ++target)
  1882. {
  1883. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  1884. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  1885. std::string exportsDef = "";
  1886. if(shared)
  1887. {
  1888. exportsDef = "-D"+target->first+"_EXPORTS ";
  1889. }
  1890. // Iterate over every source for this target.
  1891. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1892. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1893. source != sources.end(); ++source)
  1894. {
  1895. if(!(*source)->IsAHeaderFileOnly())
  1896. {
  1897. std::string shortName;
  1898. std::string sourceName;
  1899. // If the full path to the source file includes this
  1900. // directory, we want to use the relative path for the
  1901. // filename of the object file. Otherwise, we will use just
  1902. // the filename portion.
  1903. if((cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  1904. || (cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->
  1905. GetCurrentOutputDirectory()) == 0))
  1906. {
  1907. sourceName = (*source)->GetSourceName()+"."+(*source)->GetSourceExtension();
  1908. shortName = (*source)->GetSourceName();
  1909. // The path may be relative. See if a directory needs to be
  1910. // created for the output file. This is a ugly, and perhaps
  1911. // should be moved elsewhere.
  1912. std::string relPath =
  1913. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  1914. if(relPath != "")
  1915. {
  1916. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  1917. outPath += "/"+relPath;
  1918. cmSystemTools::MakeDirectory(outPath.c_str());
  1919. }
  1920. }
  1921. else
  1922. {
  1923. sourceName = (*source)->GetFullPath();
  1924. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  1925. }
  1926. std::string shortNameWithExt = shortName +
  1927. (*source)->GetSourceExtension();
  1928. // Only output a rule for each .o once.
  1929. if(rules.find(shortNameWithExt) == rules.end())
  1930. {
  1931. if((*source)->GetCompileFlags())
  1932. {
  1933. exportsDef += (*source)->GetCompileFlags();
  1934. exportsDef += " ";
  1935. }
  1936. this->OutputBuildObjectFromSource(fout,
  1937. shortName.c_str(),
  1938. *(*source),
  1939. exportsDef.c_str(),
  1940. shared);
  1941. rules.insert(shortNameWithExt);
  1942. }
  1943. }
  1944. }
  1945. }
  1946. }
  1947. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  1948. const char* comment,
  1949. const char* target,
  1950. const char* depends,
  1951. const char* command,
  1952. const char* command2,
  1953. const char* command3,
  1954. const char* command4)
  1955. {
  1956. if(!target)
  1957. {
  1958. cmSystemTools::Error("no target for OutputMakeRule");
  1959. return;
  1960. }
  1961. std::string replace;
  1962. if(comment)
  1963. {
  1964. replace = comment;
  1965. m_Makefile->ExpandVariablesInString(replace);
  1966. fout << "#---------------------------------------------------------\n";
  1967. fout << "# " << replace;
  1968. fout << "\n#\n";
  1969. }
  1970. fout << "\n";
  1971. replace = target;
  1972. m_Makefile->ExpandVariablesInString(replace);
  1973. fout << this->ConvertToOutputPath(replace.c_str()) << ": ";
  1974. if(depends)
  1975. {
  1976. replace = depends;
  1977. m_Makefile->ExpandVariablesInString(replace);
  1978. fout << replace.c_str();
  1979. }
  1980. fout << "\n";
  1981. const char* commands[] = { command, command2, command3, command4 };
  1982. for (int i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i)
  1983. {
  1984. if(commands[i])
  1985. {
  1986. replace = commands[i];
  1987. m_Makefile->ExpandVariablesInString(replace);
  1988. if(replace[0] != '-' && replace.find("echo") != 0
  1989. && replace.find("$(MAKE)") != 0)
  1990. {
  1991. std::string echostring = replace;
  1992. // for unix we want to quote the output of echo
  1993. // for nmake and borland, the echo should not be quoted
  1994. if(strcmp(this->GetName(), "Unix Makefiles") == 0)
  1995. {
  1996. cmSystemTools::ReplaceString(echostring, "\\\n", " ");
  1997. cmSystemTools::ReplaceString(echostring, " \t", " ");
  1998. cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\techo \"");
  1999. fout << "\techo \"" << echostring.c_str() << "\"\n";
  2000. }
  2001. else
  2002. {
  2003. cmSystemTools::ReplaceString(echostring, "\n\t", "\n\techo ");
  2004. fout << "\techo " << echostring.c_str() << "\n";
  2005. }
  2006. }
  2007. fout << "\t" << replace.c_str() << "\n";
  2008. }
  2009. }
  2010. fout << "\n";
  2011. }
  2012. void cmUnixMakefileGenerator::SetLocal (bool local)
  2013. {
  2014. if (local)
  2015. {
  2016. m_CacheOnly = false;
  2017. m_Recurse = false;
  2018. }
  2019. else
  2020. {
  2021. m_CacheOnly = true;
  2022. m_Recurse = true;
  2023. }
  2024. }
  2025. void cmUnixMakefileGenerator::EnableLanguage(const char* lang)
  2026. {
  2027. if (m_CacheOnly)
  2028. {
  2029. // see man putenv for explaination of this stupid code....
  2030. static char envCXX[5000];
  2031. static char envCC[5000];
  2032. if(m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"))
  2033. {
  2034. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  2035. m_Makefile->ExpandVariablesInString(env);
  2036. strncpy(envCXX, env.c_str(), 4999);
  2037. envCXX[4999] = 0;
  2038. putenv(envCXX);
  2039. }
  2040. if(m_Makefile->GetDefinition("CMAKE_C_COMPILER"))
  2041. {
  2042. std::string env = "CC=${CMAKE_C_COMPILER}";
  2043. m_Makefile->ExpandVariablesInString(env);
  2044. strncpy(envCC, env.c_str(), 4999);
  2045. envCC[4999] = 0;
  2046. putenv(envCC);
  2047. }
  2048. std::string output;
  2049. std::string root
  2050. = this->ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_ROOT"));
  2051. // if no lang specified use CXX
  2052. if(!lang )
  2053. {
  2054. lang = "CXX";
  2055. }
  2056. // if CXX or C, then enable C
  2057. if((!this->GetLanguageEnabled(lang) && lang[0] == 'C'))
  2058. {
  2059. std::string cmd = root;
  2060. cmd += "/Templates/cconfigure";
  2061. cmSystemTools::RunCommand(cmd.c_str(), output,
  2062. this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()).c_str());
  2063. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  2064. fpath += "/CCMakeSystemConfig.cmake";
  2065. m_Makefile->ReadListFile(NULL,fpath.c_str());
  2066. this->SetLanguageEnabled("C");
  2067. }
  2068. // if CXX
  2069. if(!this->GetLanguageEnabled(lang) || strcmp(lang, "CXX") == 0)
  2070. {
  2071. std::string cmd = root;
  2072. cmd += "/Templates/cxxconfigure";
  2073. cmSystemTools::RunCommand(cmd.c_str(), output,
  2074. this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()).c_str());
  2075. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  2076. fpath += "/CXXCMakeSystemConfig.cmake";
  2077. m_Makefile->ReadListFile(NULL,fpath.c_str());
  2078. this->SetLanguageEnabled("CXX");
  2079. }
  2080. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  2081. }
  2082. }