cmUnixMakefileGenerator.cxx 69 KB

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