cmUnixMakefileGenerator.cxx 75 KB

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